Skip to content

Make StreamListener read batch messages at once #3138

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -17,10 +17,13 @@

import org.springframework.data.redis.connection.stream.Record;

import java.util.List;

/**
* Listener interface to receive delivery of {@link Record messages}.
*
* @author Mark Paluch
* @author Yongha Kwon
* @param <K> Stream key and Stream field type.
* @param <V> Stream value type.
* @since 2.2
Expand All @@ -33,5 +36,5 @@ public interface StreamListener<K, V extends Record<K, ?>> {
*
* @param message never {@literal null}.
*/
void onMessage(V message);
void onMessage(List<V> message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work as this is a breaking change.

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.redis.stream;

import java.time.Duration;
import java.util.List;
import java.util.OptionalInt;
import java.util.concurrent.Executor;
import java.util.function.Predicate;
Expand Down Expand Up @@ -82,10 +83,10 @@
* <p>
* {@link StreamMessageListenerContainer} requires a {@link Executor} to fork long-running polling tasks on a different
* {@link Thread}. This thread is used as event loop to poll for stream messages and invoke the
* {@link StreamListener#onMessage(Record) listener callback}.
* {@link StreamListener#onMessage(List)} listener callback}.
* <p>
* {@link StreamMessageListenerContainer} tasks propagate errors during stream reads and
* {@link StreamListener#onMessage(Record) listener notification} to a configurable {@link ErrorHandler}. Errors stop a
* {@link StreamListener#onMessage(List)} listener notification} to a configurable {@link ErrorHandler}. Errors stop a
* {@link Subscription} by default. Configuring a {@link Predicate} for a {@link StreamReadRequest} allows conditional
* subscription cancelling or continuing on all errors.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.redis.stream;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -147,14 +148,14 @@ private List<ByteRecord> readRecords() {
}

private void deserializeAndEmitRecords(List<ByteRecord> records) {
List<V> messages = new ArrayList<>();

for (ByteRecord raw : records) {

try {

pollState.updateReadOffset(raw.getId().getValue());
V record = convertRecord(raw);
listener.onMessage(record);
messages.add(record);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Batching introduces an error category of message loss if one of the messages causes an exception. In such a case, the read offset has been updated but the message was never delivered to a listener.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I will revise the PR based on your suggestions.
I'll try implementing a separate class called StreamBatchListener to handle batch processing.

} catch (RuntimeException ex) {

if (cancelSubscriptionOnError.test(ex)) {
Expand All @@ -166,8 +167,11 @@ private void deserializeAndEmitRecords(List<ByteRecord> records) {
}

errorHandler.handleError(ex);
return;
}
}

listener.onMessage(messages);
}

private V convertRecord(ByteRecord record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void shouldReceiveMapMessages() throws InterruptedException {
BlockingQueue<MapRecord<String, String, String>> queue = new LinkedBlockingQueue<>();

container.start();
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::add);
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand Down Expand Up @@ -119,7 +119,7 @@ void shouldReceiveSimpleObjectHashRecords() throws InterruptedException {
BlockingQueue<ObjectRecord<String, String>> queue = new LinkedBlockingQueue<>();

container.start();
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::add);
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand All @@ -143,7 +143,7 @@ void shouldReceiveObjectHashRecords() throws InterruptedException {
BlockingQueue<ObjectRecord<String, LoginEvent>> queue = new LinkedBlockingQueue<>();

container.start();
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::add);
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand All @@ -168,7 +168,7 @@ void shouldReceiveMessagesInConsumerGroup() throws InterruptedException {

container.start();
Subscription subscription = container.receive(Consumer.from("my-group", "my-consumer"),
StreamOffset.create("my-stream", ReadOffset.lastConsumed()), queue::add);
StreamOffset.create("my-stream", ReadOffset.lastConsumed()), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand All @@ -194,7 +194,7 @@ void shouldReceiveAndAckMessagesInConsumerGroup() throws InterruptedException {

container.start();
Subscription subscription = container.receiveAutoAck(Consumer.from("my-group", "my-consumer"),
StreamOffset.create("my-stream", ReadOffset.lastConsumed()), queue::add);
StreamOffset.create("my-stream", ReadOffset.lastConsumed()), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand Down Expand Up @@ -316,7 +316,7 @@ void deserializationShouldContinueStreamRead() throws InterruptedException {
redisTemplate.opsForStream().add("my-stream", Collections.singletonMap("payload", "3"));

container.start();
Subscription subscription = container.register(readRequest, records::add);
Subscription subscription = container.register(readRequest, records::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand Down Expand Up @@ -347,7 +347,7 @@ void cancelledStreamShouldNotReceiveMessages() throws InterruptedException {
BlockingQueue<MapRecord<String, String, String>> queue = new LinkedBlockingQueue<>();

container.start();
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::add);
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);
cancelAwait(subscription);
Expand All @@ -365,7 +365,7 @@ void containerRestartShouldRestartSubscription() throws InterruptedException {
BlockingQueue<MapRecord<String, String, String>> queue = new LinkedBlockingQueue<>();

container.start();
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::add);
Subscription subscription = container.receive(StreamOffset.create("my-stream", ReadOffset.from("0-0")), queue::addAll);

subscription.await(DEFAULT_TIMEOUT);

Expand Down