diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/BatchMeta.java b/topic/src/main/java/tech/ydb/topic/read/impl/BatchMeta.java index ecc8a3a11..0a37315d4 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/BatchMeta.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/BatchMeta.java @@ -14,6 +14,7 @@ public class BatchMeta { private final Map writeSessionMeta; private final int codec; private final Instant writtenAt; + private final long receivedAt = System.nanoTime(); public BatchMeta(YdbTopic.StreamReadMessage.ReadResponse.Batch batch) { this.producerId = batch.getProducerId(); @@ -22,6 +23,10 @@ public BatchMeta(YdbTopic.StreamReadMessage.ReadResponse.Batch batch) { this.writtenAt = ProtobufUtils.protoToInstant(batch.getWrittenAt()); } + long getReceivedAt() { + return receivedAt; + } + public String getProducerId() { return producerId; } diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/MessageImpl.java b/topic/src/main/java/tech/ydb/topic/read/impl/MessageImpl.java index 0fb94e9d6..d0b827092 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/MessageImpl.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/MessageImpl.java @@ -48,6 +48,10 @@ public MessageImpl(PartitionSession session, MessageCommitter committer, BatchMe public abstract boolean isReady(); + long getReceivedAt() { + return batchMeta.getReceivedAt(); + } + @Override public long getOffset() { return offset; diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java index b01a84fbe..f3720622f 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadConfig.java @@ -1,6 +1,7 @@ package tech.ydb.topic.read.impl; import java.util.concurrent.Executor; +import java.util.function.LongSupplier; import tech.ydb.topic.description.CodecRegistry; import tech.ydb.topic.settings.ReaderSettings; @@ -18,9 +19,16 @@ public class ReadConfig { private final long maxMemoryUsageBytes; private final int maxBatchSize; private final ReaderMetrics metrics; + private final LongSupplier readyMessages; + private final LongSupplier readyOldestTimestamp; public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor processor, Executor decompressor, ReaderSettings settings) { + this(codecRegistry, control, processor, decompressor, settings, () -> 0, () -> 0); + } + + public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor processor, Executor decompressor, + ReaderSettings settings, LongSupplier readyMessages, LongSupplier readyOldestTimestamp) { this.codecRegistry = codecRegistry; this.control = control; this.processor = processor; @@ -29,6 +37,8 @@ public ReadConfig(CodecRegistry codecRegistry, Executor control, Executor proces this.maxMemoryUsageBytes = settings.getMaxMemoryUsageBytes(); this.maxBatchSize = settings.getMaxBatchSize(); this.metrics = new ReaderMetrics(settings.getMeter(), settings.getConsumerName(), settings.getReaderName()); + this.readyMessages = readyMessages; + this.readyOldestTimestamp = readyOldestTimestamp; } public CodecRegistry getCodecRegistry() { @@ -59,6 +69,14 @@ public int getMaxBatchSize() { return maxBatchSize; } + public long getReadyMessages() { + return readyMessages.getAsLong(); + } + + public long getReadyOldestTimestamp() { + return readyOldestTimestamp.getAsLong(); + } + ReaderMetrics getMetrics() { return metrics; } diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartition.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartition.java index 88909462c..242d5244e 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartition.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartition.java @@ -75,7 +75,8 @@ private enum State { this.controlExecutor = config.getControlExecutor(); this.committer = new ReadPartitionCommitter(traceID, session, partition, lastCommittedOffset); - this.decoder = new ReadPartitionDecoder(traceID, sessionDecoder, partition, committer, this::sendDataToReaders); + this.decoder = new ReadPartitionDecoder(traceID, sessionDecoder, partition, committer, + this::sendDataToReaders, metrics); this.queue = new ReadPartitionQueue(traceID, decoder, config.getMaxBatchSize(), lastCommittedOffset); logger.info("[{}] started for {}", traceID, partition); @@ -90,6 +91,14 @@ public PartitionSession getPartition() { return partition; } + public long getCommitOffsetLag() { + return committer.getCommitOffsetLag(); + } + + void updateCommitTarget(long offset) { + committer.updateCommitTarget(offset); + } + @Override public void confirmRangeProcessed(OffsetsRange range) { decoder.releaseRange(range); @@ -104,11 +113,20 @@ public void confirmPartitionEnded(List childs) { sendDataToReaders(); } - public void confirmCommittedOffset(long committedOffset) { - committer.updateCommittedOffset(committedOffset); + long getOldestReceivedTimestamp() { + return queue.getOldestReceivedTimestamp(); + } + + long getLocalBufferMessages() { + return queue.getLocalBufferMessages(); + } + + public long confirmCommittedOffset(long committedOffset) { + long acknowledged = committer.updateCommittedOffset(committedOffset); committer.completePendingCommits(); commitOffsetAck.set(new CommitOffsetAcknowledgementEventImpl(partition, committedOffset)); sendDataToReaders(); + return acknowledged; } public void unpause() { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionCommitter.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionCommitter.java index 7993ce524..9f5a6afa9 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionCommitter.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionCommitter.java @@ -32,24 +32,36 @@ class ReadPartitionCommitter implements MessageCommitter { private final ReentrantLock commitFuturesLock = new ReentrantLock(); private final AtomicLong lastCommittedOffset; + private final AtomicLong commitTargetOffset; ReadPartitionCommitter(String traceID, ReadSession session, PartitionSession partition, long lastCommittedOffset) { this.traceID = traceID; this.session = session; this.partition = partition; this.lastCommittedOffset = new AtomicLong(lastCommittedOffset); + this.commitTargetOffset = new AtomicLong(lastCommittedOffset); } private RuntimeException partitionIsClosedException() { return new RuntimeException("" + partition + " is already stopped"); } - public void updateCommittedOffset(long offset) { - long old = lastCommittedOffset.get(); - if (old != lastCommittedOffset.accumulateAndGet(offset, Math::max)) { + public long getCommitOffsetLag() { + return Math.max(0, commitTargetOffset.get() - lastCommittedOffset.get()); + } + + void updateCommitTarget(long offset) { + commitTargetOffset.accumulateAndGet(offset, Math::max); + } + + public long updateCommittedOffset(long offset) { + long old = lastCommittedOffset.getAndAccumulate(offset, Math::max); + if (offset > old) { logger.debug("[{}] Updated last committed offset: {}. Previous committed offset: {} " + "(diff is {} message(s)).", traceID, offset, old, offset - old); + return offset - old; } + return 0; } public void completePendingCommits() { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionDecoder.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionDecoder.java index b7234a42b..42c044622 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionDecoder.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionDecoder.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import tech.ydb.core.metrics.Meter; import tech.ydb.proto.topic.YdbTopic; import tech.ydb.topic.description.Codec; import tech.ydb.topic.description.CodecRegistry; @@ -31,6 +32,7 @@ class ReadPartitionDecoder { private final PartitionSession partition; private final MessageCommitter committer; private final MessageDecoder decoder; + private final ReaderMetrics metrics; private final Runnable readyHandler; private final AtomicLong allocatedTotal = new AtomicLong(0); @@ -39,8 +41,14 @@ class ReadPartitionDecoder { ReadPartitionDecoder(String traceId, MessageDecoder decoder, PartitionSession partition, MessageCommitter committer, Runnable readyHandler) { + this(traceId, decoder, partition, committer, readyHandler, new ReaderMetrics(Meter.NOOP, null, null)); + } + + ReadPartitionDecoder(String traceId, MessageDecoder decoder, PartitionSession partition, + MessageCommitter committer, Runnable readyHandler, ReaderMetrics metrics) { this.traceID = traceId; this.decoder = decoder; + this.metrics = metrics; this.partition = partition; this.committer = committer; this.readyHandler = readyHandler; @@ -181,6 +189,7 @@ public void decode(CodecRegistry registry) { return; } + long compressionStarted = System.nanoTime(); try { Codec codec = registry.getCodec(codecCode); if (codec == null) { @@ -201,6 +210,8 @@ public void decode(CodecRegistry registry) { problem = new IOException("Cannot decode message", ex); } } finally { + metrics.reportDecoded(System.nanoTime() - compressionStarted, + System.nanoTime() - getReceivedAt(), codecCode, partition.getPath(), problem); if (problem == null) { origin = null; } else { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionQueue.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionQueue.java index 50ca6b33d..f2854da05 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionQueue.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadPartitionQueue.java @@ -36,6 +36,15 @@ void updateLastReadOffset(long offset) { lastReadOffset = offset; } + long getLocalBufferMessages() { + return readingQueue.size(); + } + + long getOldestReceivedTimestamp() { + MessageImpl first = readingQueue.peek(); + return first == null ? 0 : first.getReceivedAt(); + } + void addBatches(List batchList) { for (YdbTopic.StreamReadMessage.ReadResponse.Batch batch : batchList) { if (batch.getMessageDataCount() == 0) { diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java index a8d48ad51..ff5be7e87 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java @@ -128,6 +128,10 @@ public boolean commitOffsets(PartitionSession session, List ranges .build()) .build(); + for (OffsetsRange range : rangesToCommit) { + partition.updateCommitTarget(range.getEnd()); + config.getMetrics().reportCommitQueued(range.getEnd() - range.getStart(), session.getPath()); + } send(FromClient.newBuilder().setCommitOffsetRequest(req).build()); return true; } @@ -189,7 +193,30 @@ public void removeLocks(PartitionSession ps, List childs) { public void onInit(YdbTopic.StreamReadMessage.InitResponse response) { bufferManager.init(response.getSessionId()); - config.getMetrics().register(partitions::size, bufferManager::getCreditBalanceBytes); + config.getMetrics().register(partitions::size, bufferManager::getCreditBalanceBytes, + this::getCommitOffsetLagMax, this::getLocalBufferMessages, this::getOldestReceivedTimestamp); + } + + private long getCommitOffsetLagMax() { + return partitions.values().stream().mapToLong(ReadPartition::getCommitOffsetLag).max().orElse(0); + } + + private long getLocalBufferMessages() { + return config.getReadyMessages() + partitions.values().stream() + .filter(ReadPartition::isActive).mapToLong(ReadPartition::getLocalBufferMessages).sum(); + } + + private long getOldestReceivedTimestamp() { + long oldestTimestamp = config.getReadyOldestTimestamp(); + for (ReadPartition partition : partitions.values()) { + if (partition.isActive()) { + long timestamp = partition.getOldestReceivedTimestamp(); + if (timestamp != 0 && (oldestTimestamp == 0 || timestamp < oldestTimestamp)) { + oldestTimestamp = timestamp; + } + } + } + return oldestTimestamp; } public void onStartPartition(YdbTopic.StreamReadMessage.StartPartitionSessionRequest req) { @@ -304,7 +331,8 @@ public void onCommitOffset(YdbTopic.StreamReadMessage.CommitOffsetResponse respo continue; } - partition.confirmCommittedOffset(offset.getCommittedOffset()); + long acknowledged = partition.confirmCommittedOffset(offset.getCommittedOffset()); + config.getMetrics().reportCommitAcknowledged(acknowledged, partition.getPartition().getPath()); } } diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java b/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java index 3ccd4d520..97f639eb4 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java @@ -6,9 +6,11 @@ import tech.ydb.core.Status; import tech.ydb.core.StatusCode; import tech.ydb.core.metrics.Attr; +import tech.ydb.core.metrics.DoubleHistogram; import tech.ydb.core.metrics.LongCounter; import tech.ydb.core.metrics.Meter; import tech.ydb.core.metrics.MetricRegistration; +import tech.ydb.proto.topic.YdbTopic; /** * Topic reader metrics. @@ -19,12 +21,19 @@ final class ReaderMetrics { private final LongCounter deliveredMessages; private final LongCounter receivedMessages; private final LongCounter receivedBytes; + private final LongCounter commitQueued; + private final LongCounter commitAcknowledged; private final LongCounter sessionErrors; + private final DoubleHistogram compressionDuration; + private final DoubleHistogram processingDuration; private final Attr[] commonAttributes; private final boolean enabled; private final Meter meter; private MetricRegistration partitionsGauge = MetricRegistration.NOOP; private MetricRegistration creditGauge = MetricRegistration.NOOP; + private MetricRegistration commitLagGauge = MetricRegistration.NOOP; + private MetricRegistration bufferMessagesGauge = MetricRegistration.NOOP; + private MetricRegistration ageGauge = MetricRegistration.NOOP; ReaderMetrics(Meter meter, String consumer, String readerName) { this.meter = meter; @@ -37,12 +46,21 @@ final class ReaderMetrics { "The number of messages accepted by the SDK for an active partition session."); this.receivedBytes = meter.createCounter("ydb.topic.reader.received.bytes", "By", "The protocol bytes_size received in read responses."); + this.commitQueued = meter.createCounter("ydb.topic.reader.commit.queued", MESSAGE_UNIT, + "The number of messages in commit ranges accepted by the SDK."); + this.commitAcknowledged = meter.createCounter("ydb.topic.reader.commit.acknowledged", MESSAGE_UNIT, + "The number of messages acknowledged by advances of the committed offset."); this.sessionErrors = meter.createCounter("ydb.topic.reader.session.errors", "{error}", "The number of reader stream session errors by retry decision."); + this.compressionDuration = meter.createHistogram("ydb.topic.reader.compression.duration", "s", + "The time spent decompressing a message, excluding queueing."); + this.processingDuration = meter.createHistogram("ydb.topic.reader.message.processing.duration", "s", + "The time from receipt until a message is ready for delivery."); this.commonAttributes = createCommonAttributes(consumer, readerName); } - void register(LongSupplier partitionCount, LongSupplier bufferBudget) { + void register(LongSupplier partitionCount, LongSupplier bufferBudget, LongSupplier commitLag, + LongSupplier bufferMessages, LongSupplier oldestTimestamp) { partitionsGauge = meter.registerLongGauge( "ydb.topic.reader.partition_session.count", "{session}", "The number of partition sessions currently in the reader session processing lifecycle.", @@ -50,13 +68,32 @@ void register(LongSupplier partitionCount, LongSupplier bufferBudget) { creditGauge = meter.registerLongGauge("ydb.topic.reader.credit_balance_bytes", "By", "The protocol credit granted to the server and not yet consumed by read responses.", m -> m.record(bufferBudget.getAsLong(), commonAttributes)); + commitLagGauge = meter.registerLongGauge("ydb.topic.reader.commit_offset.lag.max", MESSAGE_UNIT, + "The maximum gap between requested and acknowledged commit offsets.", + m -> m.record(commitLag.getAsLong(), commonAttributes)); + bufferMessagesGauge = meter.registerLongGauge("ydb.topic.reader.local_buffer.messages", MESSAGE_UNIT, + "The number of messages currently buffered by the reader.", + m -> m.record(bufferMessages.getAsLong(), commonAttributes)); + ageGauge = meter.registerDoubleGauge("ydb.topic.reader.local_buffer.message_age.max", "s", + "The age of the oldest batch retained in the reader's local buffer.", + m -> { + long timestamp = oldestTimestamp.getAsLong(); + m.record(timestamp == 0 ? 0 : (System.nanoTime() - timestamp) / 1_000_000_000d, + commonAttributes); + }); } void unregister() { partitionsGauge.close(); creditGauge.close(); + commitLagGauge.close(); + bufferMessagesGauge.close(); + ageGauge.close(); partitionsGauge = MetricRegistration.NOOP; creditGauge = MetricRegistration.NOOP; + commitLagGauge = MetricRegistration.NOOP; + bufferMessagesGauge = MetricRegistration.NOOP; + ageGauge = MetricRegistration.NOOP; } void reportDelivered(long messages, String topic) { @@ -85,6 +122,35 @@ void reportSessionError(Status status, boolean retry) { sessionErrors.add(1, attributes); } + void reportCommitQueued(long messages, String topic) { + report(commitQueued, messages, topic); + } + + void reportCommitAcknowledged(long messages, String topic) { + report(commitAcknowledged, messages, topic); + } + + void reportDecoded(long compressionNanos, long processingNanos, int codec, String topic, Throwable error) { + recordDuration(compressionDuration, compressionNanos, codec, topic, error); + recordDuration(processingDuration, processingNanos, codec, topic, error); + } + + private void recordDuration(DoubleHistogram histogram, long durationNanos, int codec, String topic, + Throwable error) { + if (enabled) { + Attr[] attributes = Arrays.copyOf(commonAttributes, commonAttributes.length + (error == null ? 3 : 4)); + attributes[commonAttributes.length] = Attr.of("topic", topic); + YdbTopic.Codec codecType = YdbTopic.Codec.forNumber(codec); + attributes[commonAttributes.length + 1] = Attr.of("codec", codecType == null ? Integer.toString(codec) + : codecType.name().substring("CODEC_".length()).toLowerCase(java.util.Locale.ROOT)); + attributes[commonAttributes.length + 2] = Attr.of("outcome", error == null ? "ok" : "error"); + if (error != null) { + attributes[commonAttributes.length + 3] = Attr.of("error.type", error.getClass().getName()); + } + histogram.record(durationNanos / 1_000_000_000d, attributes); + } + } + private void report(LongCounter counter, long messages, String topic) { if (enabled && messages > 0) { record(counter, messages, Attr.of("topic", topic)); diff --git a/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java b/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java index 0efd13af4..2d68515e1 100644 --- a/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java +++ b/topic/src/main/java/tech/ydb/topic/read/impl/SyncReaderImpl.java @@ -65,7 +65,8 @@ public SyncReaderImpl(TopicRpc topicRpc, ReaderSettings settings, @Nonnull Codec this.debugId = DebugTools.createDebugId(settings.getLogPrefix()); this.decompressor = new LazyExecutor("reader[" + debugId + "]-decoder", settings.getDecompressionExecutor()); - this.config = new ReadConfig(codecRegistry, Runnable::run, Runnable::run, decompressor, settings); + this.config = new ReadConfig(codecRegistry, Runnable::run, Runnable::run, decompressor, settings, + queue::size, this::getOldestReceivedTimestamp); this.impl = new Impl(topicRpc, debugId, settings, config); String readerName = settings.getReaderName(); @@ -83,6 +84,11 @@ public String getSessionId() { return sessionId; } + private long getOldestReceivedTimestamp() { + return queue.stream().filter(MessageWrapper::isActive) + .mapToLong(message -> ((MessageImpl) message.msg).getReceivedAt()).min().orElse(0); + } + @Override public void init() { impl.start(); diff --git a/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java b/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java index 29cfbc8a1..755795868 100644 --- a/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java +++ b/topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java @@ -10,6 +10,7 @@ import tech.ydb.core.metrics.LongCounter; import tech.ydb.core.metrics.Meter; import tech.ydb.core.metrics.MetricRegistration; +import tech.ydb.proto.topic.YdbTopic; /** * Topic writer metrics. @@ -23,6 +24,8 @@ final class WriterMetrics { private final LongCounter sessionErrors; private final DoubleHistogram messageAckDuration; private final DoubleHistogram bufferWaitDuration; + private final DoubleHistogram compressionDuration; + private final DoubleHistogram processingDuration; private final Attr[] commonAttributes; private final Meter meter; private final boolean enabled; @@ -45,6 +48,10 @@ final class WriterMetrics { "Time from first sending a message to the server to its acknowledgement."); this.bufferWaitDuration = meter.createHistogram("ydb.topic.writer.buffer.wait.duration", "s", "Time waiting for buffer capacity before a message is accepted."); + this.compressionDuration = meter.createHistogram("ydb.topic.writer.compression.duration", "s", + "The time spent compressing a message, excluding queueing."); + this.processingDuration = meter.createHistogram("ydb.topic.writer.message.processing.duration", "s", + "The time from buffer acceptance until a message is ready for sending."); this.commonAttributes = writerName == null ? new Attr[0] : new Attr[]{Attr.of("topic", topic), Attr.of("writer.name", writerName)}; @@ -95,6 +102,28 @@ void reportWritten() { writtenMessages.add(1, commonAttributes); } + void reportCompression(long durationNanos, int codec, Throwable error) { + recordDuration(compressionDuration, durationNanos, codec, error); + } + + void reportProcessing(long durationNanos, int codec, Throwable error) { + recordDuration(processingDuration, durationNanos, codec, error); + } + + private void recordDuration(DoubleHistogram histogram, long durationNanos, int codec, Throwable error) { + if (enabled) { + Attr[] attributes = Arrays.copyOf(commonAttributes, commonAttributes.length + (error == null ? 2 : 3)); + YdbTopic.Codec codecType = YdbTopic.Codec.forNumber(codec); + attributes[commonAttributes.length] = Attr.of("codec", codecType == null ? Integer.toString(codec) + : codecType.name().substring("CODEC_".length()).toLowerCase(java.util.Locale.ROOT)); + attributes[commonAttributes.length + 1] = Attr.of("outcome", error == null ? "ok" : "error"); + if (error != null) { + attributes[commonAttributes.length + 2] = Attr.of("error.type", error.getClass().getName()); + } + histogram.record(durationNanos / 1_000_000_000d, attributes); + } + } + void reportSessionError(Status status, boolean retry) { StatusCode code = status.getCode(); Attr[] attributes = Arrays.copyOf(commonAttributes, commonAttributes.length + 3); diff --git a/topic/src/main/java/tech/ydb/topic/write/impl/WriterQueue.java b/topic/src/main/java/tech/ydb/topic/write/impl/WriterQueue.java index 307b37795..1daf22d17 100644 --- a/topic/src/main/java/tech/ydb/topic/write/impl/WriterQueue.java +++ b/topic/src/main/java/tech/ydb/topic/write/impl/WriterQueue.java @@ -254,7 +254,9 @@ private CompletableFuture accept(Message message, YdbTransaction tx, l if (codec.getId() == Codec.RAW) { // fast track without compression - msg.completeWithData(UnsafeByteOperations.unsafeWrap(message.getData()), reservedSizeBytes); + ByteString data = UnsafeByteOperations.unsafeWrap(message.getData()); + metrics.reportProcessing(System.nanoTime() - msg.getAcceptedTimestamp(), codec.getId(), null); + msg.completeWithData(data, reservedSizeBytes); readyNotify.run(); return msg.getAckFuture(); } @@ -262,6 +264,7 @@ private CompletableFuture accept(Message message, YdbTransaction tx, l try { compressionExecutor.execute(() -> encode(message.getData(), reservedSizeBytes, msg)); } catch (Throwable ex) { + metrics.reportProcessing(System.nanoTime() - msg.getAcceptedTimestamp(), codec.getId(), ex); logger.warn("[{}] Message wasn't sent because of processing error", debugId, ex); msg.completeWithProblem(ex); readyNotify.run(); @@ -276,10 +279,12 @@ private void encode(byte[] data, long reservedSize, EnqueuedMessage msg) { } logger.trace("[{}] Started encoding message", debugId); + long compressionStarted = System.nanoTime(); try (ByteString.Output encoded = ByteString.newOutput()) { try (OutputStream os = codec.encode(encoded)) { os.write(data, 0, data.length); } + long compressionDuration = System.nanoTime() - compressionStarted; if (logger.isTraceEnabled()) { logger.trace("[{}] Message compressed from {} to {} bytes", debugId, data.length, encoded.size()); @@ -301,8 +306,12 @@ private void encode(byte[] data, long reservedSize, EnqueuedMessage msg) { } bufferSizeBytes = updatedBufferSizeBytes; + metrics.reportCompression(compressionDuration, codec.getId(), null); + metrics.reportProcessing(System.nanoTime() - msg.getAcceptedTimestamp(), codec.getId(), null); msg.completeWithData(encodedData, bufferSizeBytes); } catch (Throwable ex) { + metrics.reportCompression(System.nanoTime() - compressionStarted, codec.getId(), ex); + metrics.reportProcessing(System.nanoTime() - msg.getAcceptedTimestamp(), codec.getId(), ex); logger.warn("[{}] Message wasn't sent because of encoding error", debugId, ex); msg.completeWithProblem(ex); }