-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add ClusterStateSupplier interface + standard implementation #115931
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
Merged
+69
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7247413
Add ClusterStateSupplier interface + standard implementation
ldematte a645d43
Spotless
ldematte 531ce98
Make ClusterStateSupplier a Supplier<Optional<ClusterState>>; improve…
ldematte a6f987c
Merge branch 'main' into cluster-state-supplier
elasticmachine 1fd80f2
Addressed PR comments
ldematte fdb435d
Merge branch 'cluster-state-supplier' of github.com:ldematte/elastics…
ldematte d18d795
Merge remote-tracking branch 'upstream/main' into cluster-state-supplier
ldematte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
server/src/main/java/org/elasticsearch/cluster/ClusterStateSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.cluster; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Utility to access {@link ClusterState} only when it is "ready", with a fallback if it's not. The definition of "ready" is left to the | ||
| * class implementations. | ||
| */ | ||
| public interface ClusterStateSupplier extends Supplier<Optional<ClusterState>> { | ||
| default <T> T withCurrentClusterState(Function<ClusterState, T> clusterStateFunction, T fallbackIfNotReady) { | ||
| var x = get(); | ||
| return x.map(clusterStateFunction).orElse(fallbackIfNotReady); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
server/src/main/java/org/elasticsearch/cluster/SafeClusterStateSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.cluster; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.elasticsearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK; | ||
|
|
||
| /** | ||
| * Utility to access {@link ClusterState} only when it is "ready", where "ready" means that we received a first clusterChanged event | ||
| * with no global block of type {@code STATE_NOT_RECOVERED_BLOCK} | ||
| * This guarantees that: | ||
| * - the initial cluster state has been set (see | ||
| * {@link org.elasticsearch.cluster.service.ClusterApplierService#setInitialState(ClusterState)}); | ||
| * - the initial recovery process has completed. | ||
| */ | ||
| public class SafeClusterStateSupplier implements ClusterStateSupplier, ClusterStateListener { | ||
| private volatile ClusterState currentClusterState; | ||
|
|
||
| @Override | ||
| public void clusterChanged(ClusterChangedEvent event) { | ||
| // In this default implementation, "ready" is really "is cluster state available", which after the initial recovery it should be. | ||
| // If you need a different condition, feel free to add a different implementation of ClusterStateSupplier | ||
| if (isInitialized() || event.state().blocks().hasGlobalBlock(STATE_NOT_RECOVERED_BLOCK) == false) { | ||
| currentClusterState = event.state(); | ||
| } | ||
| } | ||
|
|
||
| private boolean isInitialized() { | ||
| return currentClusterState != null; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<ClusterState> get() { | ||
| return Optional.ofNullable(currentClusterState); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm not sure I quite grasp this if condition. Perhaps a comment would help?
It looks like once we've received the first event, we start ignoring
STATE_NOT_RECOVERED_BLOCK. Why is that?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.
That's the purpose. We just care about the initial recovery; after that, we consider the cluster state as available.
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.
Ok. But can
STATE_NOT_RECOVERED_BLOCKhappen again later, after initialization?isInitializedcall.Uh oh!
There was an error while loading. Please reload this page.
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.
Yes it can, but we care only if the initial recovery is happened yet. I'll add a comment about that.