Skip to content

Commit e5ae4a5

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

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class ConsumersCoordinator {
5656

5757
static final int MAX_SUBSCRIPTIONS_PER_CLIENT = 256;
5858

59-
private static final OffsetSpecification DEFAULT_OFFSET_SPECIFICATION =
60-
OffsetSpecification.next();
59+
static final OffsetSpecification DEFAULT_OFFSET_SPECIFICATION = OffsetSpecification.next();
6160

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

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

+26-11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class StreamConsumer implements Consumer {
6060
private volatile Status status;
6161
private volatile long lastRequestedStoredOffset = 0;
6262
private volatile ConsumerUpdateListener.Status sacStatus;
63+
private final OffsetSpecification initialOffsetSpecification;
6364

6465
StreamConsumer(
6566
String stream,
@@ -79,6 +80,10 @@ class StreamConsumer implements Consumer {
7980
this.name = name;
8081
this.stream = stream;
8182
this.environment = environment;
83+
this.initialOffsetSpecification =
84+
offsetSpecification == null
85+
? ConsumersCoordinator.DEFAULT_OFFSET_SPECIFICATION
86+
: offsetSpecification;
8287

8388
AtomicReference<MessageHandler> decoratedMessageHandler = new AtomicReference<>();
8489
LongSupplier trackingFlushCallback;
@@ -102,7 +107,7 @@ class StreamConsumer implements Consumer {
102107
}
103108

104109
this.trackingCallback = trackingConsumerRegistration.trackingCallback();
105-
trackingFlushCallback = () -> trackingConsumerRegistration.flush();
110+
trackingFlushCallback = trackingConsumerRegistration::flush;
106111
} else {
107112
trackingClosingCallback = () -> {};
108113
this.trackingCallback = Utils.NO_OP_LONG_CONSUMER;
@@ -132,11 +137,23 @@ class StreamConsumer implements Consumer {
132137
|| context.previousStatus() == ConsumerUpdateListener.Status.PASSIVE)
133138
&& context.status() == ConsumerUpdateListener.Status.ACTIVE) {
134139
LOGGER.debug("Looking up offset (stream {})", this.stream);
135-
StreamConsumer consumer = (StreamConsumer) context.consumer();
136-
long offset = consumer.storedOffset();
137-
LOGGER.debug(
138-
"Stored offset is {}, returning the value + 1 to the server", offset);
139-
return OffsetSpecification.offset(offset + 1);
140+
Consumer consumer = context.consumer();
141+
try {
142+
long offset = consumer.storedOffset();
143+
LOGGER.debug(
144+
"Stored offset is {}, returning the value + 1 to the server", offset);
145+
result = OffsetSpecification.offset(offset + 1);
146+
} catch (StreamException e) {
147+
if (e.getCode() == Constants.RESPONSE_CODE_NO_OFFSET) {
148+
LOGGER.debug(
149+
"No stored offset, using initial offset specification: {}",
150+
this.initialOffsetSpecification);
151+
result = initialOffsetSpecification;
152+
} else {
153+
throw e;
154+
}
155+
}
156+
return result;
140157
} else if (context.previousStatus() == ConsumerUpdateListener.Status.ACTIVE
141158
&& context.status() == ConsumerUpdateListener.Status.PASSIVE) {
142159
LOGGER.debug(
@@ -262,9 +279,7 @@ void waitForOffsetToBeStored(long expectedStoredOffset) {
262279
"Offset {} stored (consumer {}, stream {})", expectedStoredOffset, this.id, this.stream);
263280
} catch (InterruptedException e) {
264281
Thread.currentThread().interrupt();
265-
} catch (ExecutionException e) {
266-
LOGGER.warn("Error while checking offset has been stored", e);
267-
} catch (TimeoutException e) {
282+
} catch (ExecutionException | TimeoutException e) {
268283
LOGGER.warn("Error while checking offset has been stored", e);
269284
}
270285
}
@@ -449,8 +464,8 @@ public long storedOffset() {
449464
} else {
450465
throw new StreamException(
451466
String.format(
452-
"QueryOffset for consumer %s on stream %s returned an error",
453-
this.name, this.stream),
467+
"QueryOffset for consumer %s on stream %s returned an error (%s)",
468+
this.name, this.stream, Utils.formatConstant(response.getResponseCode())),
454469
response.getResponseCode());
455470
}
456471

0 commit comments

Comments
 (0)