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
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void onStartPartition(YdbTopic.StreamReadMessage.StartPartitionSessionReq
return;
}

config.getMetrics().addPartition(psid, committed);
config.getMetrics().addPartition(psid, committed, ps.getPath());
partition.start(committed, offsets);
TopicPartition lockKey = new TopicPartition(ps);
if (locks.computeIfPresent(lockKey, (key, lock) -> lock.bind(partition)) != null) {
Expand Down
25 changes: 17 additions & 8 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,6 +6,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongSupplier;
import java.util.stream.Collectors;

import tech.ydb.core.Status;
import tech.ydb.core.StatusCode;
Expand Down Expand Up @@ -78,8 +79,8 @@ void removeSource(Source source) {
sources.remove(source);
}

void addPartition(long id, long committedOffset) {
partitions.put(id, new CommitOffsets(committedOffset));
void addPartition(long id, long committedOffset, String topicPath) {
partitions.put(id, new CommitOffsets(committedOffset, topicPath));
}

void removePartition(long id) {
Expand All @@ -93,9 +94,10 @@ void updateCommittedOffset(long id, long offset) {
}
}

private long getCommitOffsetLagMax() {
return partitions.values().stream()
.mapToLong(p -> Math.max(0, p.requested.get() - p.committed.get())).max().orElse(0);
private Attr[] topicAttributes(String topic) {
Attr[] attributes = Arrays.copyOf(commonAttributes, commonAttributes.length + 1);
attributes[commonAttributes.length] = Attr.of("topic", topic);
return attributes;
}

private long getLocalBufferMessages() {
Expand All @@ -117,13 +119,18 @@ void register(LongSupplier bufferBudget) {
partitionsGauge = meter.registerLongGauge(
"ydb.topic.reader.partition_session.count", "{session}",
"The number of partition sessions currently in the reader session processing lifecycle.",
m -> m.record(partitions.size(), commonAttributes));
m -> partitions.values().stream().collect(Collectors.groupingBy(
offsets -> offsets.topicPath, Collectors.counting()))
.forEach((topic, count) -> m.record(count, topicAttributes(topic))));
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(getCommitOffsetLagMax(), commonAttributes));
m -> partitions.values().stream().collect(Collectors.toMap(
offsets -> offsets.topicPath,
offsets -> Math.max(0, offsets.requested.get() - offsets.committed.get()), Math::max))
.forEach((topic, lag) -> m.record(lag, topicAttributes(topic))));
bufferMessagesGauge = meter.registerLongGauge("ydb.topic.reader.local_buffer.messages", MESSAGE_UNIT,
"The number of messages currently buffered by the reader.",
m -> m.record(getLocalBufferMessages(), commonAttributes));
Expand Down Expand Up @@ -240,10 +247,12 @@ private static Attr[] createCommonAttributes(String consumer, String readerName)
}

private static class CommitOffsets {
private final String topicPath;
private final AtomicLong requested;
private final AtomicLong committed;

CommitOffsets(long offset) {
CommitOffsets(long offset, String topicPath) {
this.topicPath = topicPath;
this.requested = new AtomicLong(offset);
this.committed = new AtomicLong(offset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ public void readAndCommitMetrics() throws Exception {
reader.init();
stream.assertLastMessage().isInitRequest("consumer", "/topic", "/other-topic").hasReaderName("reader");
stream.responseInit("read-session");
meter.assertLongGaugeSeries(PREFIX + "partition_session.count", 0);
meter.assertLongGaugeSeries(PREFIX + "commit_offset.lag.max", 0);
meter.assertLongGauge(PREFIX + "local_buffer.messages", 0, COMMON);
meter.assertDoubleGauge(PREFIX + "local_buffer.message_age.max", 0, COMMON);
stream.responseStartPartition("/topic", 42, 0);
stream.responseStartPartition("/other-topic", 43, 0);
meter.assertLongGauge(PREFIX + "partition_session.count", 2, COMMON);
stream.responseStartPartition("/topic", 44, 0);
meter.assertLongGauge(PREFIX + "partition_session.count", 2, TOPIC);
meter.assertLongGauge(PREFIX + "partition_session.count", 1, OTHER);
meter.assertLongGauge(PREFIX + "credit_balance_bytes", 100, COMMON);
stream.responseData(12).partition(1, 0).batch(Codec.GZIP, MSG1, MSG2).and().send();
meter.assertCounter(PREFIX + "received.messages", 2, TOPIC);
Expand All @@ -61,13 +67,17 @@ public void readAndCommitMetrics() throws Exception {
meter.assertCounter(PREFIX + "received.messages", 2, TOPIC);
meter.assertCounter(PREFIX + "received.messages", 1, OTHER);
meter.assertCounter(PREFIX + "received.bytes", 20, COMMON);
meter.assertCounterSeries(PREFIX + "received.bytes", 1);
meter.assertLongGauge(PREFIX + "credit_balance_bytes", 80, COMMON);
meter.assertCounter(PREFIX + "delivered.messages", 0, TOPIC);
meter.assertCounter(PREFIX + "delivered.messages", 0, OTHER);
meter.assertLongGauge(PREFIX + "local_buffer.messages", 3, COMMON);
meter.assertPositiveDoubleGauge(PREFIX + "local_buffer.message_age.max", COMMON);
Message first = reader.receive(0, TimeUnit.SECONDS);
meter.assertLongGauge(PREFIX + "local_buffer.messages", 2, COMMON);
Message second = reader.receive(0, TimeUnit.SECONDS);
meter.assertLongGauge(PREFIX + "local_buffer.messages", 1, COMMON);
meter.assertPositiveDoubleGauge(PREFIX + "local_buffer.message_age.max", COMMON);
Message third = reader.receive(0, TimeUnit.SECONDS);
Assert.assertNotNull(first);
Assert.assertNotNull(second);
Expand All @@ -81,18 +91,28 @@ public void readAndCommitMetrics() throws Exception {
meter.assertDoubleGauge(PREFIX + "local_buffer.message_age.max", 0, COMMON);
// The final 8 released bytes stay below the 10-byte credit-request threshold.
meter.assertLongGauge(PREFIX + "credit_balance_bytes", 92, COMMON);
first.getCommitter().commitRanges(Arrays.asList(
first.getRangeToCommit(), second.getRangeToCommit()));
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 2, TOPIC);
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, OTHER);
CompletableFuture<Void> committed = CompletableFuture.allOf(
first.commit(), second.commit(), third.commit());
Assert.assertFalse(committed.isDone());
meter.assertCounter(PREFIX + "commit.queued", 2, TOPIC);
meter.assertCounter(PREFIX + "commit.queued", 4, TOPIC);
meter.assertCounter(PREFIX + "commit.queued", 1, OTHER);
meter.assertNoCounterSamples(PREFIX + "commit.acknowledged");
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 2, COMMON);
stream.responseCommitAck().partition(1, 2).partition(2, 1).send();
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 2, TOPIC);
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 1, OTHER);
stream.responseCommitAck().partition(1, 2).send();
Assert.assertFalse(committed.isDone());
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, TOPIC);
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 1, OTHER);
stream.responseCommitAck().partition(2, 1).send();
committed.get(1, TimeUnit.SECONDS);
meter.assertCounter(PREFIX + "commit.acknowledged", 2, TOPIC);
meter.assertCounter(PREFIX + "commit.acknowledged", 1, OTHER);
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, COMMON);
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, TOPIC);
meter.assertLongGauge(PREFIX + "commit_offset.lag.max", 0, OTHER);
for (String name : Arrays.asList("compression.duration", "message.processing.duration")) {
meter.assertHistogram(PREFIX + name, "s", 2, attributes("consumer", "consumer",
"reader.name", "reader", "topic", "/topic", "codec", "gzip", "outcome", "ok"));
Expand All @@ -101,8 +121,12 @@ public void readAndCommitMetrics() throws Exception {
}
meter.assertUnit(PREFIX + "received.bytes", "By");
stream.responseStopPartition(1, true);
meter.assertLongGauge(PREFIX + "partition_session.count", 1, TOPIC);
meter.assertLongGauge(PREFIX + "partition_session.count", 1, OTHER);
stream.responseStopPartition(2, true);
meter.assertLongGauge(PREFIX + "partition_session.count", 0, COMMON);
stream.responseStopPartition(3, true);
meter.assertLongGaugeSeries(PREFIX + "partition_session.count", 0);
meter.assertLongGaugeSeries(PREFIX + "commit_offset.lag.max", 0);
} finally {
reader.shutdown();
}
Expand Down Expand Up @@ -132,7 +156,7 @@ public void readWithoutConsumerMetrics() throws Exception {
Assert.assertNotNull(message);
Assert.assertArrayEquals(MSG1, message.getData());
meter.assertCounter(PREFIX + "delivered.messages", 1, topic);
meter.assertLongGauge(PREFIX + "partition_session.count", 1, common);
meter.assertLongGauge(PREFIX + "partition_session.count", 1, topic);
meter.assertLongGauge(PREFIX + "local_buffer.messages", 0, common);
for (String name : Arrays.asList("compression.duration", "message.processing.duration")) {
meter.assertHistogram(PREFIX + name, "s", 1, attributes("reader.name", "without-consumer",
Expand Down
11 changes: 11 additions & 0 deletions topic/src/test/java/tech/ydb/topic/utils/InMemoryMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public void assertLongGauge(String name, long expected, Map<String, String> attr
Assert.assertEquals(name + " " + attributes, expected, values.get(attributes).longValue());
}

public void assertLongGaugeSeries(String name, int expected) {
List<Consumer<LongMeasurement>> callbacks = longGauges.get(name);
Assert.assertNotNull(name, callbacks);
Map<Map<String, String>, Long> values = new HashMap<>();
for (Consumer<LongMeasurement> callback : callbacks) {
callback.accept((value, attrs) -> Assert.assertNull(name + " duplicate series",
values.put(key(attrs), value)));
}
Assert.assertEquals(name, expected, values.size());
}

public void assertDoubleGauge(String name, double expected, Map<String, String> attributes) {
Assert.assertEquals(name + " " + attributes, expected, doubleGaugeValue(name, attributes), 0);
}
Expand Down