-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add support for CLAIM arg in XREADGROUP #4344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1377,7 +1377,13 @@ public StreamEntry build(Object data) { | |
| while (hashIterator.hasNext()) { | ||
| map.put(SafeEncoder.encode(hashIterator.next()), SafeEncoder.encode(hashIterator.next())); | ||
| } | ||
| return new StreamEntry(entryID, map); | ||
| Long idle = null; | ||
| Long times = null; | ||
| if (objectList.size() >= 4) { | ||
| idle = LONG.build(objectList.get(2)); | ||
| times = LONG.build(objectList.get(3)); | ||
| } | ||
| return new StreamEntry(entryID, map, idle, times); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1418,7 +1424,19 @@ public List<StreamEntry> build(Object data) { | |
| while (hashIterator.hasNext()) { | ||
| map.put(SafeEncoder.encode(hashIterator.next()), SafeEncoder.encode(hashIterator.next())); | ||
| } | ||
| responses.add(new StreamEntry(entryID, map)); | ||
| Long idle = null; | ||
| Long times = null; | ||
| if (res.size() >= 4) { | ||
| Object idleObj = res.get(2); | ||
| Object timesObj = res.get(3); | ||
| idle = (idleObj instanceof Long) ? (Long) idleObj : Long.valueOf(STRING.build(idleObj)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless ternary - type is byteArray and it should be parsed to String via builder |
||
| times = (timesObj instanceof Long) ? (Long) timesObj : Long.valueOf(STRING.build(timesObj)); | ||
| if (idle != null && times != null && idle == 0L && times == 0L) { | ||
| idle = null; | ||
| times = null; | ||
| } | ||
| } | ||
| responses.add(new StreamEntry(entryID, map, idle, times)); | ||
| } | ||
|
|
||
| return responses; | ||
|
|
@@ -1969,7 +1987,19 @@ public List<StreamEntryBinary> build(Object data) { | |
| while (hashIterator.hasNext()) { | ||
| map.put(BINARY.build(hashIterator.next()), BINARY.build(hashIterator.next())); | ||
| } | ||
| responses.add(new StreamEntryBinary(entryID, map)); | ||
| Long idle = null; | ||
| Long times = null; | ||
| if (res.size() >= 4) { | ||
| Object idleObj = res.get(2); | ||
| Object timesObj = res.get(3); | ||
| idle = (idleObj instanceof Long) ? (Long) idleObj : Long.valueOf(STRING.build(idleObj)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| times = (timesObj instanceof Long) ? (Long) timesObj : Long.valueOf(STRING.build(timesObj)); | ||
| if (idle != null && times != null && idle == 0L && times == 0L) { | ||
| idle = null; | ||
| times = null; | ||
| } | ||
| } | ||
| responses.add(new StreamEntryBinary(entryID, map, idle, times)); | ||
| } | ||
|
|
||
| return responses; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| package redis.clients.jedis.commands.commandobjects; | ||
|
|
||
|
|
||
| import io.redis.test.annotations.SinceRedisVersion; | ||
|
|
||
| import static io.redis.test.utils.RedisVersion.V8_4_RC1_STRING; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.empty; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
@@ -1102,4 +1106,121 @@ public void testXReadGroupAsMap() { | |
| assertThat(xreadGroupConsumer2.get(streamKey).get(0).getID(), equalTo(secondMessageId)); | ||
| assertThat(xreadGroupConsumer2.get(streamKey).get(0).getFields(), equalTo(messageBody)); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| @SinceRedisVersion(V8_4_RC1_STRING) | ||
| public void xreadGroupWithClaimReturnsPendingThenNewEntries_commandObjects() throws InterruptedException { | ||
| String key = "co-claim-stream"; | ||
| String group = "co-claim-group"; | ||
| String consumer = "c1"; | ||
|
|
||
| exec(commandObjects.del(key)); | ||
| exec(commandObjects.xgroupCreate(key, group, StreamEntryID.XGROUP_LAST_ENTRY, true)); | ||
|
|
||
| // Make 3 entries pending | ||
| exec(commandObjects.xadd(key, new StreamEntryID("1-0"), Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("2-0"), Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("3-0"), Collections.singletonMap("f", "v"))); | ||
|
|
||
| Map<String, StreamEntryID> stream = Collections.singletonMap(key, StreamEntryID.XREADGROUP_UNDELIVERED_ENTRY); | ||
| exec(commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(3), stream)); | ||
|
|
||
| Thread.sleep(60); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lessen time waiting or think of a way to modify tests. |
||
|
|
||
| // Add two fresh entries | ||
| exec(commandObjects.xadd(key, new StreamEntryID("4-0"), Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("5-0"), Collections.singletonMap("f", "v"))); | ||
|
|
||
| List<Map.Entry<String, List<StreamEntry>>> messages = exec( | ||
| commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(5).claim(50), stream)); | ||
|
|
||
| assertThat(messages, hasSize(1)); | ||
| List<StreamEntry> entries = messages.get(0).getValue(); | ||
| org.junit.jupiter.api.Assertions.assertEquals(5, entries.size()); | ||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getIdleTime()); | ||
| org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getDeliveredTimes()); | ||
| } | ||
| for (int i = 3; i < 5; i++) { | ||
| org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getIdleTime()); | ||
| org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getDeliveredTimes()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @SinceRedisVersion(V8_4_RC1_STRING) | ||
| public void xreadGroupWithClaimNoEligiblePendingReturnsOnlyNewEntries_commandObjects() { | ||
| String key = "co-claim-noeligible"; | ||
| String group = "co-claim-group"; | ||
| String consumer = "c1"; | ||
|
|
||
| exec(commandObjects.del(key)); | ||
| exec(commandObjects.xgroupCreate(key, group, StreamEntryID.XGROUP_LAST_ENTRY, true)); | ||
|
|
||
| // Put 2 entries in PEL | ||
| exec(commandObjects.xadd(key, new StreamEntryID("1-0"), Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("2-0"), Collections.singletonMap("f", "v"))); | ||
| Map<String, StreamEntryID> stream = Collections.singletonMap(key, StreamEntryID.XREADGROUP_UNDELIVERED_ENTRY); | ||
| exec(commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(2), stream)); | ||
|
|
||
| // Add two new entries that should be returned | ||
| exec(commandObjects.xadd(key, new StreamEntryID("3-0"), Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("4-0"), Collections.singletonMap("f", "v"))); | ||
|
|
||
| List<Map.Entry<String, List<StreamEntry>>> messages = exec( | ||
| commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(4).claim(500), stream)); | ||
|
|
||
| assertThat(messages, hasSize(1)); | ||
| List<StreamEntry> entries = messages.get(0).getValue(); | ||
| org.junit.jupiter.api.Assertions.assertEquals(2, entries.size()); | ||
| for (StreamEntry e : entries) { | ||
| org.junit.jupiter.api.Assertions.assertNull(e.getIdleTime()); | ||
| org.junit.jupiter.api.Assertions.assertNull(e.getDeliveredTimes()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @SinceRedisVersion(V8_4_RC1_STRING) | ||
| public void xreadGroupWithClaimAndNoAckDoesNotAddNewEntriesToPEL_commandObjects() throws InterruptedException { | ||
| String key = "co-claim-noack"; | ||
| String group = "co-claim-group"; | ||
| String consumer = "c1"; | ||
|
|
||
| exec(commandObjects.del(key)); | ||
| exec(commandObjects.xgroupCreate(key, group, StreamEntryID.XGROUP_LAST_ENTRY, true)); | ||
|
|
||
| // Make 3 entries pending | ||
| exec(commandObjects.xadd(key, new StreamEntryID("1-0"), java.util.Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("2-0"), java.util.Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("3-0"), java.util.Collections.singletonMap("f", "v"))); | ||
| java.util.Map<String, StreamEntryID> stream = java.util.Collections.singletonMap(key, StreamEntryID.XREADGROUP_UNDELIVERED_ENTRY); | ||
| exec(commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(3), stream)); | ||
|
|
||
| // Wait then add fresh entries | ||
| Thread.sleep(60); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("4-0"), java.util.Collections.singletonMap("f", "v"))); | ||
| exec(commandObjects.xadd(key, new StreamEntryID("5-0"), java.util.Collections.singletonMap("f", "v"))); | ||
|
|
||
| // Read with CLAIM and NOACK | ||
| java.util.List<java.util.Map.Entry<String, java.util.List<StreamEntry>>> messages = exec( | ||
| commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(5).claim(50).noAck(), stream)); | ||
|
|
||
| assertThat(messages, hasSize(1)); | ||
| java.util.List<StreamEntry> entries = messages.get(0).getValue(); | ||
| org.junit.jupiter.api.Assertions.assertEquals(5, entries.size()); | ||
| for (int i = 0; i < 3; i++) { | ||
| org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getIdleTime()); | ||
| org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getDeliveredTimes()); | ||
| } | ||
| for (int i = 3; i < 5; i++) { | ||
| org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getIdleTime()); | ||
| org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getDeliveredTimes()); | ||
| } | ||
|
|
||
| Long acked = exec(commandObjects.xack(key, group, new StreamEntryID("4-0"), new StreamEntryID("5-0"))); | ||
| org.junit.jupiter.api.Assertions.assertEquals(0L, acked); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly unused code