Skip to content

Commit c42c086

Browse files
authored
Merge pull request #1311 from nats-io/kv-ttl-more
KV LimitMarker add missing getter, additional docs and tests
2 parents 488612c + e354a05 commit c42c086

File tree

8 files changed

+60
-38
lines changed

8 files changed

+60
-38
lines changed

src/main/java/io/nats/client/MessageTtl.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,29 @@
1515

1616
import io.nats.client.support.Validator;
1717

18+
/**
19+
* Class to make setting a per message ttl easier.
20+
*/
1821
public class MessageTtl {
19-
private final String messageTtl;
22+
private final String ttlString;
2023

21-
private MessageTtl(String messageTtl) {
22-
this.messageTtl = messageTtl;
24+
private MessageTtl(String ttlString) {
25+
this.ttlString = ttlString;
2326
}
2427

25-
public String getMessageTtl() {
26-
return messageTtl;
28+
public String getTtlString() {
29+
return ttlString;
2730
}
2831

2932
@Override
3033
public String toString() {
31-
return "MessageTtl{'" + messageTtl + "'}";
34+
return "MessageTtl{'" + ttlString + "'}";
3235
}
3336

3437
/**
3538
* Sets the TTL for this specific message to be published
3639
* @param msgTtlSeconds the ttl in seconds
37-
* @return The Builder
40+
* @return The MessageTtl instance
3841
*/
3942
public static MessageTtl seconds(int msgTtlSeconds) {
4043
if (msgTtlSeconds < 1) {
@@ -47,18 +50,18 @@ public static MessageTtl seconds(int msgTtlSeconds) {
4750
* Sets the TTL for this specific message to be published. Use at your own risk.
4851
* The current specification can be found here @see <a href="https://github.com/nats-io/nats-architecture-and-design/blob/main/adr/ADR-43.md#per-message-ttl">JetStream Per-Message TTL</a>
4952
* @param messageTtlCustom the ttl in seconds
50-
* @return The Builder
53+
* @return The MessageTtl instance
5154
*/
5255
public static MessageTtl custom(String messageTtlCustom) {
5356
if (Validator.nullOrEmpty(messageTtlCustom)) {
54-
throw new IllegalArgumentException("messageTtlCustom required.");
57+
throw new IllegalArgumentException("Custom value required.");
5558
}
5659
return new MessageTtl(messageTtlCustom);
5760
}
5861

5962
/**
6063
* Sets the TTL for this specific message to be published and never be expired
61-
* @return The Builder
64+
* @return The MessageTtl instance
6265
*/
6366
public static MessageTtl never() {
6467
return new MessageTtl("never");

src/main/java/io/nats/client/PublishOptions.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public String getMessageId() {
144144
* @return the message ttl string
145145
*/
146146
public String getMessageTtl() {
147-
return messageTtl == null ? null : messageTtl.getMessageTtl();
147+
return messageTtl == null ? null : messageTtl.getTtlString();
148148
}
149149

150150
/**
@@ -268,7 +268,8 @@ public Builder messageId(String msgId) {
268268
}
269269

270270
/**
271-
* Sets the TTL for this specific message to be published
271+
* Sets the TTL for this specific message to be published.
272+
* Less than 1 has the effect of clearing the message ttl
272273
* @param msgTtlSeconds the ttl in seconds
273274
* @return The Builder
274275
*/
@@ -280,11 +281,12 @@ public Builder messageTtlSeconds(int msgTtlSeconds) {
280281
/**
281282
* Sets the TTL for this specific message to be published. Use at your own risk.
282283
* The current specification can be found here @see <a href="https://github.com/nats-io/nats-architecture-and-design/blob/main/adr/ADR-43.md#per-message-ttl">JetStream Per-Message TTL</a>
283-
* @param messageTtlCustom the ttl in seconds
284+
* Null or empty has the effect of clearing the message ttl
285+
* @param msgTtlCustom the custom ttl string
284286
* @return The Builder
285287
*/
286-
public Builder messageTtlCustom(String messageTtlCustom) {
287-
this.messageTtl = nullOrEmpty(messageTtlCustom) ? null : MessageTtl.custom(messageTtlCustom);
288+
public Builder messageTtlCustom(String msgTtlCustom) {
289+
this.messageTtl = nullOrEmpty(msgTtlCustom) ? null : MessageTtl.custom(msgTtlCustom);
288290
return this;
289291
}
290292

@@ -299,6 +301,7 @@ public Builder messageTtlNever() {
299301

300302
/**
301303
* Sets the TTL for this specific message to be published
304+
* @param messageTtl the message ttl instance
302305
* @return The Builder
303306
*/
304307
public Builder messageTtl(MessageTtl messageTtl) {

src/main/java/io/nats/client/api/KeyValueConfiguration.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public List<Source> getSources() {
8181
return sc.getSources();
8282
}
8383

84+
/**
85+
* The limit marker ttl if set
86+
* @return the duration
87+
*/
88+
public Duration getLimitMarkerTtl() {
89+
return sc.getSubjectDeleteMarkerTtl();
90+
}
91+
8492
@Override
8593
public String toString() {
8694
return "KeyValueConfiguration" + toJson();
@@ -95,6 +103,7 @@ public JsonValue toJsonValue() {
95103
mb.put("republish", getRepublish());
96104
mb.put("mirror", getMirror());
97105
mb.put("sources", getSources());
106+
mb.put("limitMarkerTtl", getLimitMarkerTtl());
98107
mb.jv.mapOrder.add("metaData");
99108
return mb.toJsonValue();
100109
}
@@ -381,16 +390,18 @@ public Builder addSources(Collection<Source> sources) {
381390

382391
/**
383392
* The limit marker TTL duration. Server accepts 1 second or more.
393+
* Null or empty has the effect of clearing the limit marker ttl
384394
* @param limitMarkerTtl the TTL duration
385395
* @return The Builder
386396
*/
387397
public Builder limitMarker(Duration limitMarkerTtl) {
388-
this.limitMarkerTtl = validateDurationNotRequiredGtOrEqSeconds(1, limitMarkerTtl, null);
398+
this.limitMarkerTtl = validateDurationNotRequiredGtOrEqSeconds(1, limitMarkerTtl, null, "Limit Marker Ttl");
389399
return this;
390400
}
391401

392402
/**
393-
* The limit marker TTL duration. Server accepts 1 second or more.
403+
* The limit marker TTL duration in milliseconds. Server accepts 1 second or more.
404+
* 0 or less has the effect of clearing the limit marker ttl
394405
* @param limitMarkerTtlMillis the TTL duration
395406
* @return The Builder
396407
*/
@@ -399,7 +410,7 @@ public Builder limitMarker(long limitMarkerTtlMillis) {
399410
this.limitMarkerTtl = null;
400411
}
401412
else {
402-
this.limitMarkerTtl = validateDurationGtOrEqSeconds(1, limitMarkerTtlMillis);
413+
this.limitMarkerTtl = validateDurationGtOrEqSeconds(1, limitMarkerTtlMillis, "Limit Marker Ttl");
403414
}
404415
return this;
405416
}

src/main/java/io/nats/client/api/StreamConfiguration.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1080,23 +1080,25 @@ public Builder allowMessageTtl(boolean allowMessageTtl) {
10801080
}
10811081

10821082
/**
1083-
* The time delete marker TTL duration. Server accepts 1 second or more.
1083+
* Set the subject delete marker TTL duration. Server accepts 1 second or more.
1084+
* null has the effect of clearing the subject delete marker TTL
10841085
* @param subjectDeleteMarkerTtl the TTL duration
10851086
* @return The Builder
10861087
*/
10871088
public Builder subjectDeleteMarkerTtl(Duration subjectDeleteMarkerTtl) {
1088-
this.subjectDeleteMarkerTtl = validateDurationNotRequiredGtOrEqSeconds(1, subjectDeleteMarkerTtl, null);
1089+
this.subjectDeleteMarkerTtl = validateDurationNotRequiredGtOrEqSeconds(1, subjectDeleteMarkerTtl, null, "Subject Delete Marker Ttl");
10891090
return this;
10901091
}
10911092

10921093
/**
1093-
* The time delete marker TTL duration in milliseconds. Server accepts 1 second or more.
1094-
* @param subjectDeleteMarkerTtlMillis the TTL duration
1094+
* Set the subject delete marker TTL duration in milliseconds. Server accepts 1 second or more.
1095+
* 0 or less has the effect of clearing the subject delete marker TTL
1096+
* @param subjectDeleteMarkerTtlMillis the TTL duration in milliseconds
10951097
* @return The Builder
10961098
*/
10971099
public Builder subjectDeleteMarkerTtl(long subjectDeleteMarkerTtlMillis) {
10981100
this.subjectDeleteMarkerTtl = subjectDeleteMarkerTtlMillis <= 0 ? null
1099-
: validateDurationGtOrEqSeconds(1, subjectDeleteMarkerTtlMillis);
1101+
: validateDurationGtOrEqSeconds(1, subjectDeleteMarkerTtlMillis, "Subject Delete Marker Ttl");
11001102
return this;
11011103
}
11021104

src/main/java/io/nats/client/support/Validator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,13 @@ public static Duration validateDurationNotRequiredGtOrEqZero(long millis) {
363363
return Duration.ofMillis(millis);
364364
}
365365

366-
public static Duration validateDurationNotRequiredGtOrEqSeconds(long minSeconds, Duration d, Duration ifNull) {
367-
return d == null ? ifNull : validateDurationGtOrEqSeconds(minSeconds, d.toMillis());
366+
public static Duration validateDurationNotRequiredGtOrEqSeconds(long minSeconds, Duration d, Duration ifNull, String label) {
367+
return d == null ? ifNull : validateDurationGtOrEqSeconds(minSeconds, d.toMillis(), label);
368368
}
369369

370-
public static Duration validateDurationGtOrEqSeconds(long minSeconds, long millis) {
370+
public static Duration validateDurationGtOrEqSeconds(long minSeconds, long millis, String label) {
371371
if (millis < (minSeconds * 1000)) {
372-
throw new IllegalArgumentException("Must be greater than or equal to " + minSeconds + " second(s).");
372+
throw new IllegalArgumentException(label + " must be greater than or equal to " + minSeconds + " second(s).");
373373
}
374374
return Duration.ofMillis(millis);
375375
}

src/test/java/io/nats/client/api/KeyValueConfigurationTests.java

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void testConstruction() {
4242
.placement(p)
4343
.republish(r)
4444
.compression(true)
45+
.limitMarker(8888)
4546
.build();
4647
validate(bc);
4748

@@ -63,6 +64,7 @@ private void validate(KeyValueConfiguration kvc) {
6364
assertEquals(44, kvc.getMaxHistoryPerKey());
6465
assertEquals(555, kvc.getMaxBucketSize());
6566
assertEquals(666, kvc.getMaxValueSize());
67+
assertEquals(666, kvc.getMaximumValueSize());
6668
assertEquals(Duration.ofMillis(777), kvc.getTtl());
6769
assertEquals(StorageType.Memory, kvc.getStorageType());
6870
assertEquals(2, kvc.getReplicas());
@@ -74,6 +76,7 @@ private void validate(KeyValueConfiguration kvc) {
7476
assertEquals("dest", kvc.getRepublish().getDestination());
7577
assertTrue(kvc.getRepublish().isHeadersOnly());
7678
assertTrue(kvc.isCompressed());
79+
assertEquals(8888, kvc.getLimitMarkerTtl().toMillis());
7780

7881
assertTrue(kvc.toString().contains("bucketName"));
7982
}

src/test/java/io/nats/client/impl/KeyValueTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1784,16 +1784,16 @@ public void testConsumeKeys() throws Exception {
17841784

17851785
@Test
17861786
public void testLimitMarker() throws Exception {
1787-
jsServer.run(TestBase::atLeast2_10, nc -> {
1787+
jsServer.run(TestBase::atLeast2_11, nc -> {
17881788
KeyValueManagement kvm = nc.keyValueManagement();
17891789
String bucket = bucket();
17901790
KeyValueConfiguration config = KeyValueConfiguration.builder()
17911791
.name(bucket)
17921792
.storageType(StorageType.Memory)
17931793
.limitMarker(1000)
17941794
.build();
1795-
KeyValueStatus kvs = kvm.create(config);
1796-
assertEquals(1000, kvs.getLimitMarkerTtl().toMillis());
1795+
KeyValueStatus status = kvm.create(config);
1796+
assertEquals(1000, status.getLimitMarkerTtl().toMillis());
17971797

17981798
String key = key();
17991799
KeyValue kv = nc.keyValue(bucket);
@@ -1802,7 +1802,7 @@ public void testLimitMarker() throws Exception {
18021802
KeyValueEntry kve = kv.get(key);
18031803
assertNotNull(kve);
18041804

1805-
Thread.sleep(2000); // a good amount of time to make sure a CI server works
1805+
sleep(2000); // a good amount of time to make sure a CI server works
18061806

18071807
kve = kv.get(key);
18081808
assertNull(kve);
@@ -1812,8 +1812,8 @@ public void testLimitMarker() throws Exception {
18121812
.storageType(StorageType.Memory)
18131813
.limitMarker(Duration.ofSeconds(2)) // coverage of duration api vs ms api
18141814
.build();
1815-
kvs = kvm.create(config);
1816-
assertEquals(2000, kvs.getLimitMarkerTtl().toMillis());
1815+
status = kvm.create(config);
1816+
assertEquals(2000, status.getLimitMarkerTtl().toMillis());
18171817

18181818
assertThrows(IllegalArgumentException.class, () -> KeyValueConfiguration.builder()
18191819
.name(bucket)

src/test/java/io/nats/client/support/ValidatorTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ public void testValidateDurationNotRequiredGtOrEqZero() {
242242
@Test
243243
public void testValidateDurationGtOrEqSeconds() {
244244
Duration ifNull = Duration.ofMillis(999);
245-
assertEquals(ifNull, validateDurationNotRequiredGtOrEqSeconds(1, null, ifNull));
246-
assertEquals(Duration.ofSeconds(1), validateDurationNotRequiredGtOrEqSeconds(1, Duration.ofSeconds(1), ifNull));
247-
assertThrows(IllegalArgumentException.class, () -> validateDurationNotRequiredGtOrEqSeconds(1, Duration.ofMillis(999), ifNull));
245+
assertEquals(ifNull, validateDurationNotRequiredGtOrEqSeconds(1, null, ifNull, ""));
246+
assertEquals(Duration.ofSeconds(1), validateDurationNotRequiredGtOrEqSeconds(1, Duration.ofSeconds(1), ifNull, ""));
247+
assertThrows(IllegalArgumentException.class, () -> validateDurationNotRequiredGtOrEqSeconds(1, Duration.ofMillis(999), ifNull, ""));
248248

249-
assertEquals(Duration.ofSeconds(1), validateDurationGtOrEqSeconds(1, 1000));
250-
assertThrows(IllegalArgumentException.class, () -> validateDurationGtOrEqSeconds(1, 999));
249+
assertEquals(Duration.ofSeconds(1), validateDurationGtOrEqSeconds(1, 1000, ""));
250+
assertThrows(IllegalArgumentException.class, () -> validateDurationGtOrEqSeconds(1, 999, ""));
251251
}
252252

253253
@Test

0 commit comments

Comments
 (0)