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

Adding the ability to register a PeerFinderListener to Coordinator #88626

Merged
merged 3 commits into from
Jul 21, 2022
Merged
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
5 changes: 5 additions & 0 deletions docs/changelog/88626.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 88626
summary: Adding the ability to register a `PeerFinderListener` to Coordinator
area: Distributed
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING;
import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;

public class ClusterBootstrapService {
public class ClusterBootstrapService implements Coordinator.PeerFinderListener {

public static final Setting<List<String>> INITIAL_MASTER_NODES_SETTING = Setting.listSetting(
"cluster.initial_master_nodes",
Expand Down Expand Up @@ -147,7 +147,8 @@ void logBootstrapState(Metadata metadata) {
}
}

void onFoundPeersUpdated() {
@Override
public void onFoundPeersUpdated() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public void onFoundPeersUpdated() {
@Override
public void onFoundPeersUpdated() {

final Set<DiscoveryNode> nodes = getDiscoveredNodes();
if (bootstrappingPermitted.get()
&& transportService.getLocalNode().isMasterNode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -174,6 +175,7 @@ public class Coordinator extends AbstractLifecycleComponent implements ClusterSt
private JoinHelper.JoinAccumulator joinAccumulator;
private Optional<CoordinatorPublication> currentPublication = Optional.empty();
private final NodeHealthService nodeHealthService;
private final List<PeerFinderListener> peerFinderListeners;

/**
* @param nodeName The name of the node, used to name the {@link java.util.concurrent.ExecutorService} of the {@link SeedHostsResolver}.
Expand Down Expand Up @@ -295,6 +297,8 @@ public Coordinator(
joinHelper::logLastFailedJoinAttempt
);
this.nodeHealthService = nodeHealthService;
this.peerFinderListeners = new CopyOnWriteArrayList<>();
this.peerFinderListeners.add(clusterBootstrapService);
}

/**
Expand Down Expand Up @@ -1515,6 +1519,10 @@ boolean hasIdleJoinValidationService() {
return joinValidationService.isIdle();
}

public void addPeerFinderListener(PeerFinderListener peerFinderListener) {
this.peerFinderListeners.add(peerFinderListener);
}

public enum Mode {
CANDIDATE,
LEADER,
Expand Down Expand Up @@ -1570,8 +1578,7 @@ protected void onFoundPeersUpdated() {
}
}
}

clusterBootstrapService.onFoundPeersUpdated();
peerFinderListeners.forEach(PeerFinderListener::onFoundPeersUpdated);
}
}

Expand Down Expand Up @@ -1937,4 +1944,8 @@ protected void sendApplyCommit(
);
}
}

public interface PeerFinderListener {
void onFoundPeersUpdated();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,23 @@ public void testImproveConfigurationPerformsVotingConfigExclusionStateCheck() {
}
}

public void testPeerFinderListener() throws Exception {
try (Cluster cluster = new Cluster(3, true, Settings.EMPTY)) {
cluster.runRandomly();
cluster.stabilise();
ClusterNode leader = cluster.getAnyLeader();
ClusterNode nodeWithListener = cluster.getAnyNodeExcept(leader);
AtomicBoolean listenerCalled = new AtomicBoolean(false);
nodeWithListener.coordinator.addPeerFinderListener(() -> listenerCalled.set(true));
assertFalse(listenerCalled.get());
leader.disconnect();
cluster.runFor(DEFAULT_STABILISATION_TIME, "Letting disconnect take effect");
cluster.stabilise();
assertTrue(cluster.clusterNodes.contains(nodeWithListener));
assertBusy(() -> assertTrue(listenerCalled.get()));
}
}

private ClusterState buildNewClusterStateWithVotingConfigExclusion(
ClusterState currentState,
Set<CoordinationMetadata.VotingConfigExclusion> newVotingConfigExclusion
Expand Down