Skip to content

Commit eb36d6b

Browse files
committed
Handle no offset response in default consumer update handler
References #46, rabbitmq/rabbitmq-server#3753
1 parent 262bbdf commit eb36d6b

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/main/java/com/rabbitmq/stream/impl/ConsumersCoordinator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ class ConsumersCoordinator {
5858

5959
static final int MAX_SUBSCRIPTIONS_PER_CLIENT = 256;
6060

61-
private static final OffsetSpecification DEFAULT_OFFSET_SPECIFICATION =
62-
OffsetSpecification.next();
61+
static final OffsetSpecification DEFAULT_OFFSET_SPECIFICATION = OffsetSpecification.next();
6362

6463
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumersCoordinator.class);
6564
private final Random random = new Random();

src/main/java/com/rabbitmq/stream/impl/StreamConsumer.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class StreamConsumer implements Consumer {
6464
private volatile long lastRequestedStoredOffset = 0;
6565
private final AtomicBoolean nothingStoredYet = new AtomicBoolean(true);
6666
private volatile ConsumerUpdateListener.Status sacStatus;
67+
private final OffsetSpecification initialOffsetSpecification;
6768

6869
StreamConsumer(
6970
String stream,
@@ -83,6 +84,10 @@ class StreamConsumer implements Consumer {
8384
this.name = name;
8485
this.stream = stream;
8586
this.environment = environment;
87+
this.initialOffsetSpecification =
88+
offsetSpecification == null
89+
? ConsumersCoordinator.DEFAULT_OFFSET_SPECIFICATION
90+
: offsetSpecification;
8691

8792
AtomicReference<MessageHandler> decoratedMessageHandler = new AtomicReference<>();
8893
LongSupplier trackingFlushCallback;
@@ -106,7 +111,7 @@ class StreamConsumer implements Consumer {
106111
}
107112

108113
this.trackingCallback = trackingConsumerRegistration.trackingCallback();
109-
trackingFlushCallback = () -> trackingConsumerRegistration.flush();
114+
trackingFlushCallback = trackingConsumerRegistration::flush;
110115
} else {
111116
trackingClosingCallback = () -> {};
112117
this.trackingCallback = Utils.NO_OP_LONG_CONSUMER;
@@ -136,11 +141,23 @@ class StreamConsumer implements Consumer {
136141
|| context.previousStatus() == ConsumerUpdateListener.Status.PASSIVE)
137142
&& context.status() == ConsumerUpdateListener.Status.ACTIVE) {
138143
LOGGER.debug("Looking up offset (stream {})", this.stream);
139-
StreamConsumer consumer = (StreamConsumer) context.consumer();
140-
long offset = consumer.storedOffset();
141-
LOGGER.debug(
142-
"Stored offset is {}, returning the value + 1 to the server", offset);
143-
return OffsetSpecification.offset(offset + 1);
144+
Consumer consumer = context.consumer();
145+
try {
146+
long offset = consumer.storedOffset();
147+
LOGGER.debug(
148+
"Stored offset is {}, returning the value + 1 to the server", offset);
149+
result = OffsetSpecification.offset(offset + 1);
150+
} catch (StreamException e) {
151+
if (e.getCode() == Constants.RESPONSE_CODE_NO_OFFSET) {
152+
LOGGER.debug(
153+
"No stored offset, using initial offset specification: {}",
154+
this.initialOffsetSpecification);
155+
result = initialOffsetSpecification;
156+
} else {
157+
throw e;
158+
}
159+
}
160+
return result;
144161
} else if (context.previousStatus() == ConsumerUpdateListener.Status.ACTIVE
145162
&& context.status() == ConsumerUpdateListener.Status.PASSIVE) {
146163
LOGGER.debug(
@@ -274,9 +291,7 @@ void waitForOffsetToBeStored(long expectedStoredOffset) {
274291
"Offset {} stored (consumer {}, stream {})", expectedStoredOffset, this.id, this.stream);
275292
} catch (InterruptedException e) {
276293
Thread.currentThread().interrupt();
277-
} catch (ExecutionException e) {
278-
LOGGER.warn("Error while checking offset has been stored", e);
279-
} catch (TimeoutException e) {
294+
} catch (ExecutionException | TimeoutException e) {
280295
LOGGER.warn("Error while checking offset has been stored", e);
281296
}
282297
}
@@ -466,8 +481,8 @@ public long storedOffset() {
466481
} else {
467482
throw new StreamException(
468483
String.format(
469-
"QueryOffset for consumer %s on stream %s returned an error",
470-
this.name, this.stream),
484+
"QueryOffset for consumer %s on stream %s returned an error (%s)",
485+
this.name, this.stream, Utils.formatConstant(response.getResponseCode())),
471486
response.getResponseCode());
472487
}
473488

0 commit comments

Comments
 (0)