Skip to content

Commit 52dc136

Browse files
committed
Added option includeStats to DescribeTopicSettings
1 parent b68e226 commit 52dc136

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

topic/src/main/java/tech/ydb/topic/description/TopicStats.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import javax.annotation.Nullable;
88

9+
import tech.ydb.core.utils.ProtobufUtils;
10+
import tech.ydb.proto.topic.YdbTopic;
11+
912
/**
1013
* @author Nikolay Perfilov
1114
*/
@@ -16,6 +19,18 @@ public class TopicStats {
1619
private final Duration maxWriteTimeLag;
1720
private final MultipleWindowsStat bytesWritten;
1821

22+
public TopicStats(YdbTopic.DescribeTopicResult.TopicStats stats) {
23+
this.storeSizeBytes = stats.getStoreSizeBytes();
24+
this.minLastWriteTime = ProtobufUtils.protoToInstant(stats.getMinLastWriteTime());
25+
this.maxWriteTimeLag = ProtobufUtils.protoToDuration(stats.getMaxWriteTimeLag());
26+
this.bytesWritten = new MultipleWindowsStat(
27+
stats.getBytesWritten().getPerMinute(),
28+
stats.getBytesWritten().getPerHour(),
29+
stats.getBytesWritten().getPerDay()
30+
);
31+
}
32+
33+
@Deprecated
1934
private TopicStats(Builder builder) {
2035
this.storeSizeBytes = builder.storeSizeBytes;
2136
this.minLastWriteTime = builder.minLastWriteTime;
@@ -40,13 +55,15 @@ public MultipleWindowsStat getBytesWritten() {
4055
return bytesWritten;
4156
}
4257

58+
@Deprecated
4359
public static TopicDescription.Builder newBuilder() {
4460
return new TopicDescription.Builder();
4561
}
4662

4763
/**
4864
* BUILDER
4965
*/
66+
@Deprecated
5067
public static class Builder {
5168
private long storeSizeBytes;
5269
private Instant minLastWriteTime;

topic/src/main/java/tech/ydb/topic/impl/TopicClientImpl.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import tech.ydb.topic.description.PartitionStats;
3232
import tech.ydb.topic.description.SupportedCodecs;
3333
import tech.ydb.topic.description.TopicDescription;
34+
import tech.ydb.topic.description.TopicStats;
3435
import tech.ydb.topic.read.AsyncReader;
3536
import tech.ydb.topic.read.SyncReader;
3637
import tech.ydb.topic.read.impl.AsyncReaderImpl;
@@ -284,11 +285,13 @@ public CompletableFuture<Status> dropTopic(String path, DropTopicSettings settin
284285
public CompletableFuture<Result<TopicDescription>> describeTopic(String path, DescribeTopicSettings settings) {
285286
YdbTopic.DescribeTopicRequest request = YdbTopic.DescribeTopicRequest.newBuilder()
286287
.setOperationParams(Operation.buildParams(settings))
288+
.setIncludeStats(settings.isIncludeStats())
287289
.setPath(path)
288290
.build();
291+
289292
final GrpcRequestSettings grpcRequestSettings = makeGrpcRequestSettings(settings);
290293
return topicRpc.describeTopic(request, grpcRequestSettings)
291-
.thenApply(result -> result.map(this::mapDescribeTopic));
294+
.thenApply(result -> result.map(desc -> mapDescribeTopic(desc, settings.isIncludeStats())));
292295
}
293296

294297
@Override
@@ -308,7 +311,7 @@ public CompletableFuture<Result<ConsumerDescription>> describeConsumer(
308311
}
309312

310313
@SuppressWarnings("deprecation")
311-
private TopicDescription mapDescribeTopic(YdbTopic.DescribeTopicResult result) {
314+
private TopicDescription mapDescribeTopic(YdbTopic.DescribeTopicResult result, boolean includeStats) {
312315
if (logger.isTraceEnabled()) {
313316
logger.trace("Received topic describe response:\n{}", result);
314317
}
@@ -351,6 +354,10 @@ private TopicDescription mapDescribeTopic(YdbTopic.DescribeTopicResult result) {
351354
.setParentPartitionIds(partition.getParentPartitionIdsList())
352355
.setPartitionStats(new PartitionStats(partition.getPartitionStats()));
353356

357+
if (includeStats) {
358+
partitionBuilder.setPartitionStats(new PartitionStats(partition.getPartitionStats()));
359+
}
360+
354361
partitions.add(partitionBuilder.build());
355362
}
356363
description.setPartitions(partitions);
@@ -364,6 +371,10 @@ private TopicDescription mapDescribeTopic(YdbTopic.DescribeTopicResult result) {
364371
description.setConsumers(result.getConsumersList().stream()
365372
.map(Consumer::new).collect(Collectors.toList()));
366373

374+
if (includeStats) {
375+
description.setTopicStats(new TopicStats(result.getTopicStats()));
376+
}
377+
367378
return description.build();
368379
}
369380

topic/src/main/java/tech/ydb/topic/settings/DescribeTopicSettings.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@
66
* @author Nikolay Perfilov
77
*/
88
public class DescribeTopicSettings extends OperationSettings {
9-
/* TODO: renew api and add stats
10-
private boolean includeStats = false;
9+
private final boolean includeStats;
1110

12-
public DescribeTopicSettings(boolean includeStats) {
13-
this.includeStats = includeStats;
14-
}
15-
16-
public void setIncludeStats(boolean includeStats) {
17-
this.includeStats = includeStats;
11+
private DescribeTopicSettings(Builder builder) {
12+
super(builder);
13+
this.includeStats = builder.includeStats;
1814
}
1915

20-
public boolean getIncludeStats() {
16+
public boolean isIncludeStats() {
2117
return includeStats;
22-
}*/
23-
24-
private DescribeTopicSettings(Builder builder) {
25-
super(builder);
2618
}
2719

2820
public static Builder newBuilder() {
2921
return new Builder();
3022
}
3123

3224
public static class Builder extends OperationBuilder<Builder> {
25+
private boolean includeStats = false;
26+
27+
public Builder withIncludeStats(boolean includeStats) {
28+
this.includeStats = includeStats;
29+
return this;
30+
}
31+
3332
@Override
3433
public DescribeTopicSettings build() {
3534
return new DescribeTopicSettings(this);

topic/src/test/java/tech/ydb/topic/impl/YdbTopicsIntegrationTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import tech.ydb.test.junit4.GrpcTransportRule;
2323
import tech.ydb.topic.TopicClient;
2424
import tech.ydb.topic.description.Consumer;
25+
import tech.ydb.topic.description.PartitionInfo;
2526
import tech.ydb.topic.description.TopicDescription;
2627
import tech.ydb.topic.read.AsyncReader;
2728
import tech.ydb.topic.read.DeferredCommitter;
@@ -34,6 +35,7 @@
3435
import tech.ydb.topic.settings.AutoPartitioningStrategy;
3536
import tech.ydb.topic.settings.AutoPartitioningWriteStrategySettings;
3637
import tech.ydb.topic.settings.CreateTopicSettings;
38+
import tech.ydb.topic.settings.DescribeTopicSettings;
3739
import tech.ydb.topic.settings.PartitioningSettings;
3840
import tech.ydb.topic.settings.ReadEventHandlersSettings;
3941
import tech.ydb.topic.settings.ReaderSettings;
@@ -299,4 +301,32 @@ public void step08_createTopicWithAutoPartitioning() {
299301

300302
Assert.assertEquals(expectedPartitioningSettings, description.getPartitioningSettings());
301303
}
304+
305+
@Test
306+
public void step09_describeTopicStats() {
307+
DescribeTopicSettings on = DescribeTopicSettings.newBuilder().withIncludeStats(true).build();
308+
DescribeTopicSettings off = DescribeTopicSettings.newBuilder().withIncludeStats(false).build();
309+
310+
TopicDescription withStats = client.describeTopic(TEST_TOPIC, on).join().getValue();
311+
TopicDescription withoutStats = client.describeTopic(TEST_TOPIC, off).join().getValue();
312+
313+
Assert.assertNull(withoutStats.getTopicStats());
314+
Assert.assertNotNull(withStats.getTopicStats());
315+
316+
for (Consumer consumer: withoutStats.getConsumers()) {
317+
// TODO: fix it, must be null
318+
Assert.assertNotNull(consumer.getStats());
319+
}
320+
for (Consumer consumer: withStats.getConsumers()) {
321+
Assert.assertNotNull(consumer.getStats());
322+
}
323+
324+
for (PartitionInfo partition: withoutStats.getPartitions()) {
325+
// TODO: fix it, must be null
326+
Assert.assertNotNull(partition.getPartitionStats());
327+
}
328+
for (PartitionInfo partition: withStats.getPartitions()) {
329+
Assert.assertNotNull(partition.getPartitionStats());
330+
}
331+
}
302332
}

0 commit comments

Comments
 (0)