Skip to content

Commit

Permalink
fix: notify new SnapshotReplicationListeners about ongoing replication
Browse files Browse the repository at this point in the history
We are using `SnapshotReplicationListener`s to transition to inactive
when a snapshot replication starts. Snapshot replication can start
before any listeners have registered, which means that the listener will
only be notified about snapshot replication finishing, triggering a
transition to follower without first transitioning to inactive.

Here we are keeping track of ongoing snapshot replication to immediately
notify listeners when they are registering.

(cherry picked from commit 3d1f6c5)
  • Loading branch information
lenaschoenburg authored and github-actions[bot] committed Feb 28, 2022
1 parent b4fad15 commit 7174005
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public class RaftContext implements AutoCloseable, HealthMonitorable {
private PersistedSnapshot currentSnapshot;

private boolean ongoingTransition = false;
private boolean ongoingSnapshotReplication = false;

@SuppressWarnings("java:S3077") // allow volatile here, health is immutable
private volatile HealthReport health = HealthReport.healthy(this);
Expand Down Expand Up @@ -469,6 +470,11 @@ public long setCommitIndex(final long commitIndex) {
public void addSnapshotReplicationListener(
final SnapshotReplicationListener snapshotReplicationListener) {
snapshotReplicationListeners.add(snapshotReplicationListener);
if (ongoingSnapshotReplication) {
// Notify listener immediately if it registered during an ongoing replication.
// This is to prevent missing necessary state transitions.
snapshotReplicationListener.onSnapshotReplicationStarted();
}
}

/**
Expand All @@ -482,11 +488,13 @@ public void removeSnapshotReplicationListener(
}

public void notifySnapshotReplicationStarted() {
ongoingSnapshotReplication = true;
snapshotReplicationListeners.forEach(SnapshotReplicationListener::onSnapshotReplicationStarted);
}

public void notifySnapshotReplicationCompleted() {
snapshotReplicationListeners.forEach(l -> l.onSnapshotReplicationCompleted(term));
ongoingSnapshotReplication = false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -59,4 +60,16 @@ public void shouldNotifySnapshotReplicationListener() throws Throwable {
verify(snapshotReplicationListener, timeout(1_000).times(1))
.onSnapshotReplicationCompleted(follower.getTerm());
}

@Test
public void shouldNotifyOnRegisteringListener() {
// given
final var snapshotReplicationListener = mock(SnapshotReplicationListener.class);
final var follower = raftRule.getFollower().orElseThrow();
// then
follower.getContext().notifySnapshotReplicationStarted();
verify(snapshotReplicationListener, never()).onSnapshotReplicationStarted();
follower.getContext().addSnapshotReplicationListener(snapshotReplicationListener);
verify(snapshotReplicationListener).onSnapshotReplicationStarted();
}
}

0 comments on commit 7174005

Please sign in to comment.