-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[CCR] Change AutofollowCoordinator to use wait_for_metadata_version #36264
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
Changes from 2 commits
c2bdb4e
dc03711
6210008
ba05631
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import org.elasticsearch.ExceptionsHelper; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; | ||
| import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; | ||
| import org.elasticsearch.client.Client; | ||
| import org.elasticsearch.cluster.ClusterChangedEvent; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
|
|
@@ -23,13 +24,11 @@ | |
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.collect.CopyOnWriteHashMap; | ||
| import org.elasticsearch.common.collect.Tuple; | ||
| import org.elasticsearch.common.unit.TimeValue; | ||
| import org.elasticsearch.common.util.concurrent.AtomicArray; | ||
| import org.elasticsearch.common.util.concurrent.CountDown; | ||
| import org.elasticsearch.index.Index; | ||
| import org.elasticsearch.index.IndexSettings; | ||
| import org.elasticsearch.license.LicenseUtils; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.xpack.ccr.CcrLicenseChecker; | ||
| import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; | ||
| import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata.AutoFollowPattern; | ||
|
|
@@ -62,7 +61,6 @@ public class AutoFollowCoordinator implements ClusterStateListener { | |
| private static final int MAX_AUTO_FOLLOW_ERRORS = 256; | ||
|
|
||
| private final Client client; | ||
| private final ThreadPool threadPool; | ||
| private final ClusterService clusterService; | ||
| private final CcrLicenseChecker ccrLicenseChecker; | ||
|
|
||
|
|
@@ -76,11 +74,9 @@ public class AutoFollowCoordinator implements ClusterStateListener { | |
|
|
||
| public AutoFollowCoordinator( | ||
| Client client, | ||
| ThreadPool threadPool, | ||
| ClusterService clusterService, | ||
| CcrLicenseChecker ccrLicenseChecker) { | ||
| this.client = client; | ||
| this.threadPool = threadPool; | ||
| this.clusterService = clusterService; | ||
| this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker, "ccrLicenseChecker"); | ||
| clusterService.addListener(this); | ||
|
|
@@ -146,22 +142,24 @@ void updateAutoFollowers(ClusterState followerClusterState) { | |
|
|
||
| Map<String, AutoFollower> newAutoFollowers = new HashMap<>(newRemoteClusters.size()); | ||
| for (String remoteCluster : newRemoteClusters) { | ||
| AutoFollower autoFollower = new AutoFollower(remoteCluster, threadPool, this::updateStats, clusterService::state) { | ||
| AutoFollower autoFollower = new AutoFollower(remoteCluster, this::updateStats, clusterService::state) { | ||
|
|
||
| @Override | ||
| void getLeaderClusterState(final String remoteCluster, | ||
| final BiConsumer<ClusterState, Exception> handler) { | ||
| void getRemoteClusterState(final String remoteCluster, | ||
| final long metadataVersion, | ||
| final BiConsumer<ClusterStateResponse, Exception> handler) { | ||
| final ClusterStateRequest request = new ClusterStateRequest(); | ||
| request.clear(); | ||
| request.metaData(true); | ||
| request.routingTable(true); | ||
| request.waitForMetaDataVersion(metadataVersion); | ||
| // TODO: set non-compliant status on auto-follow coordination that can be viewed via a stats API | ||
| ccrLicenseChecker.checkRemoteClusterLicenseAndFetchClusterState( | ||
| client, | ||
| remoteCluster, | ||
| request, | ||
| e -> handler.accept(null, e), | ||
| leaderClusterState -> handler.accept(leaderClusterState, null)); | ||
| remoteClusterStateRsp -> handler.accept(remoteClusterStateRsp, null)); | ||
|
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -235,19 +233,17 @@ public void clusterChanged(ClusterChangedEvent event) { | |
| abstract static class AutoFollower { | ||
|
|
||
| private final String remoteCluster; | ||
| private final ThreadPool threadPool; | ||
| private final Consumer<List<AutoFollowResult>> statsUpdater; | ||
| private final Supplier<ClusterState> followerClusterStateSupplier; | ||
|
|
||
| private volatile long metadataVersion = 0; | ||
| private volatile CountDown autoFollowPatternsCountDown; | ||
| private volatile AtomicArray<AutoFollowResult> autoFollowResults; | ||
|
|
||
| AutoFollower(final String remoteCluster, | ||
| final ThreadPool threadPool, | ||
| final Consumer<List<AutoFollowResult>> statsUpdater, | ||
| final Supplier<ClusterState> followerClusterStateSupplier) { | ||
| this.remoteCluster = remoteCluster; | ||
| this.threadPool = threadPool; | ||
| this.statsUpdater = statsUpdater; | ||
| this.followerClusterStateSupplier = followerClusterStateSupplier; | ||
| } | ||
|
|
@@ -272,10 +268,15 @@ void autoFollowIndices() { | |
| this.autoFollowPatternsCountDown = new CountDown(patterns.size()); | ||
| this.autoFollowResults = new AtomicArray<>(patterns.size()); | ||
|
|
||
| getLeaderClusterState(remoteCluster, (leaderClusterState, e) -> { | ||
| if (leaderClusterState != null) { | ||
| getRemoteClusterState(remoteCluster, metadataVersion + 1, (leaderClusterStateRsp, e) -> { | ||
|
||
| if (leaderClusterStateRsp != null) { | ||
| assert e == null; | ||
| if (leaderClusterStateRsp.isWaitForTimedOut()) { | ||
| autoFollowIndices(); | ||
| return; | ||
| } | ||
|
|
||
| ClusterState leaderClusterState = leaderClusterStateRsp.getState(); | ||
| int i = 0; | ||
| for (String autoFollowPatternName : patterns) { | ||
| final int slot = i; | ||
|
|
@@ -392,8 +393,7 @@ private void finalise(int slot, AutoFollowResult result) { | |
| autoFollowResults.set(slot, result); | ||
| if (autoFollowPatternsCountDown.countDown()) { | ||
| statsUpdater.accept(autoFollowResults.asList()); | ||
| // TODO: Remove scheduling here with using cluster state API's waitForMetadataVersion: | ||
| threadPool.schedule(TimeValue.timeValueMillis(2500), ThreadPool.Names.GENERIC, this::autoFollowIndices); | ||
| autoFollowIndices(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -461,13 +461,15 @@ static Function<ClusterState, ClusterState> recordLeaderIndexAsFollowFunction(St | |
| } | ||
|
|
||
| /** | ||
| * Fetch the cluster state from the leader with the specified cluster alias | ||
| * Fetch a remote cluster state from with the specified cluster alias | ||
| * @param remoteCluster the name of the leader cluster | ||
| * @param metadataVersion the last seen metadata version | ||
| * @param handler the callback to invoke | ||
| */ | ||
| abstract void getLeaderClusterState( | ||
| abstract void getRemoteClusterState( | ||
| String remoteCluster, | ||
| BiConsumer<ClusterState, Exception> handler | ||
| long metadataVersion, | ||
| BiConsumer<ClusterStateResponse, Exception> handler | ||
| ); | ||
|
|
||
| abstract void createAndFollow( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess “
RspisResponse? Sorry to be a nit, can we spell it out?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my attempt to keep line length at bay. I will change it.