Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions topic/src/main/java/tech/ydb/topic/read/impl/BatchMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class BatchMeta {
private final Map<String, String> 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();
Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions topic/src/main/java/tech/ydb/topic/read/impl/MessageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -181,6 +189,7 @@ public void decode(CodecRegistry registry) {
return;
}

long compressionStarted = System.nanoTime();
try {
Codec codec = registry.getCodec(codecCode);
if (codec == null) {
Expand All @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions topic/src/main/java/tech/ydb/topic/read/impl/ReaderMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,6 +22,8 @@ final class ReaderMetrics {
private final LongCounter receivedMessages;
private final LongCounter receivedBytes;
private final LongCounter sessionErrors;
private final DoubleHistogram compressionDuration;
private final DoubleHistogram processingDuration;
private final Attr[] commonAttributes;
private final boolean enabled;
private final Meter meter;
Expand All @@ -39,6 +43,10 @@ final class ReaderMetrics {
"The protocol bytes_size received in read responses.");
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);
}

Expand Down Expand Up @@ -85,6 +93,27 @@ void reportSessionError(Status status, boolean retry) {
sessionErrors.add(1, attributes);
}

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));
Expand Down
29 changes: 29 additions & 0 deletions topic/src/main/java/tech/ydb/topic/write/impl/WriterMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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)};
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion topic/src/main/java/tech/ydb/topic/write/impl/WriterQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,17 @@ private CompletableFuture<WriteAck> 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();
}

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();
Expand All @@ -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());
Expand All @@ -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);
}
Expand Down
Loading