Skip to content
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

[fix] [ml] fix key_shared mode delivery are order out order after a consumers reconnection #23803

Open
wants to merge 4 commits into
base: master
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 @@ -97,7 +97,8 @@ public class PersistentDispatcherMultipleConsumers extends AbstractPersistentDis
protected volatile boolean havePendingRead = false;
protected volatile boolean havePendingReplayRead = false;
protected volatile Position minReplayedPosition = null;
protected boolean shouldRewindBeforeReadingOrReplaying = false;
protected boolean shouldRewindBeforeReading = false;
protected boolean shouldSkipNextReplaying = false;
protected final String name;
private boolean sendInProgress = false;
protected static final AtomicIntegerFieldUpdater<PersistentDispatcherMultipleConsumers>
Expand Down Expand Up @@ -185,12 +186,15 @@ public synchronized CompletableFuture<Void> addConsumer(Consumer consumer) {
return CompletableFuture.completedFuture(null);
}
if (consumerList.isEmpty()) {
if (havePendingRead || havePendingReplayRead) {
if (havePendingReplayRead) {
shouldSkipNextReplaying = true;
}
if (havePendingRead) {
// There is a pending read from previous run. We must wait for it to complete and then rewind
shouldRewindBeforeReadingOrReplaying = true;
shouldRewindBeforeReading = true;
} else {
cursor.rewind();
shouldRewindBeforeReadingOrReplaying = false;
shouldRewindBeforeReading = false;
}
redeliveryMessages.clear();
delayedDeliveryTracker.ifPresent(tracker -> {
Expand Down Expand Up @@ -720,6 +724,10 @@ public final synchronized void readEntriesComplete(List<Entry> entries, Object c
havePendingRead = false;
} else {
havePendingReplayRead = false;
if (shouldSkipNextReplaying && readType == ReadType.Replay) {
shouldSkipNextReplaying = false;
return;
}
}

if (readBatchSize < serviceConfig.getDispatcherMaxReadBatchSize()) {
Expand All @@ -733,11 +741,11 @@ public final synchronized void readEntriesComplete(List<Entry> entries, Object c

readFailureBackoff.reduceToHalf();

if (shouldRewindBeforeReadingOrReplaying && readType == ReadType.Normal) {
if (shouldRewindBeforeReading && readType == ReadType.Normal) {
// All consumers got disconnected before the completion of the read operation
entries.forEach(Entry::release);
cursor.rewind();
shouldRewindBeforeReadingOrReplaying = false;
shouldRewindBeforeReading = false;
readMoreEntriesAsync();
return;
}
Expand Down Expand Up @@ -1061,8 +1069,10 @@ public synchronized void readEntriesFailed(ManagedLedgerException exception, Obj
}
}

if (shouldRewindBeforeReadingOrReplaying) {
shouldRewindBeforeReadingOrReplaying = false;
if (shouldSkipNextReplaying && readType == ReadType.Replay) {
shouldSkipNextReplaying = false;
} else if (shouldRewindBeforeReading && readType == ReadType.Normal) {
shouldRewindBeforeReading = false;
cursor.rewind();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public class PersistentDispatcherMultipleConsumersClassic extends AbstractPersis
protected volatile boolean havePendingRead = false;
protected volatile boolean havePendingReplayRead = false;
protected volatile Position minReplayedPosition = null;
protected boolean shouldRewindBeforeReadingOrReplaying = false;
protected boolean shouldRewindBeforeReading = false;
protected boolean shouldSkipNextReplaying = false;
protected final String name;
private boolean sendInProgress = false;
protected static final AtomicIntegerFieldUpdater<PersistentDispatcherMultipleConsumersClassic>
Expand Down Expand Up @@ -173,12 +174,15 @@ public synchronized CompletableFuture<Void> addConsumer(Consumer consumer) {
return CompletableFuture.completedFuture(null);
}
if (consumerList.isEmpty()) {
if (havePendingRead || havePendingReplayRead) {
if (havePendingReplayRead) {
shouldSkipNextReplaying = true;
}
if (havePendingRead) {
// There is a pending read from previous run. We must wait for it to complete and then rewind
shouldRewindBeforeReadingOrReplaying = true;
shouldRewindBeforeReading = true;
} else {
cursor.rewind();
shouldRewindBeforeReadingOrReplaying = false;
shouldRewindBeforeReading = false;
}
redeliveryMessages.clear();
delayedDeliveryTracker.ifPresent(tracker -> {
Expand Down Expand Up @@ -633,6 +637,11 @@ public final synchronized void readEntriesComplete(List<Entry> entries, Object c
havePendingRead = false;
} else {
havePendingReplayRead = false;
if (shouldSkipNextReplaying && readType == ReadType.Replay) {
shouldSkipNextReplaying = false;
readMoreEntriesAsync();
return;
}
}

if (readBatchSize < serviceConfig.getDispatcherMaxReadBatchSize()) {
Expand All @@ -646,11 +655,11 @@ public final synchronized void readEntriesComplete(List<Entry> entries, Object c

readFailureBackoff.reduceToHalf();

if (shouldRewindBeforeReadingOrReplaying && readType == ReadType.Normal) {
if (shouldRewindBeforeReading && readType == ReadType.Normal) {
// All consumers got disconnected before the completion of the read operation
entries.forEach(Entry::release);
cursor.rewind();
shouldRewindBeforeReadingOrReplaying = false;
shouldRewindBeforeReading = false;
readMoreEntries();
return;
}
Expand Down Expand Up @@ -931,8 +940,10 @@ public synchronized void readEntriesFailed(ManagedLedgerException exception, Obj
}
}

if (shouldRewindBeforeReadingOrReplaying) {
shouldRewindBeforeReadingOrReplaying = false;
if (shouldSkipNextReplaying && readType == ReadType.Replay) {
shouldSkipNextReplaying = false;
} else if (shouldRewindBeforeReading && readType == ReadType.Normal) {
shouldRewindBeforeReading = false;
cursor.rewind();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public synchronized CompletableFuture<Void> addConsumer(Consumer consumer) {
// If there is a delayed "cursor.rewind" after the pending read, the consumers that will be
// added before the "cursor.rewind" will have a same "recent joined position", which is the
// same as "mark deleted position +1", so we can skip this adding.
&& !shouldRewindBeforeReadingOrReplaying) {
&& !shouldRewindBeforeReading) {
recentlyJoinedConsumers.put(consumer, readPositionWhenJoining);
sortRecentlyJoinedConsumersIfNeeded();
}
Expand Down
Loading
Loading