Skip to content
Open
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 @@ -15,6 +15,7 @@
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.metadata.ReservedStateErrorMetadata;
import org.elasticsearch.cluster.metadata.ReservedStateHandlerMetadata;
Expand All @@ -39,6 +40,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.stream.Stream;

import static org.elasticsearch.cluster.metadata.ReservedStateMetadata.EMPTY_VERSION;
Expand Down Expand Up @@ -194,22 +196,31 @@ private Tuple<CountDownLatch, AtomicLong> setupClusterStateListener(String node,
ClusterService clusterService = internalCluster().clusterService(node);
CountDownLatch savedClusterState = new CountDownLatch(1);
AtomicLong metadataVersion = new AtomicLong(-1);
clusterService.addListener(new ClusterStateListener() {
@Override
public void clusterChanged(ClusterChangedEvent event) {
ReservedStateMetadata reservedState = event.state().metadata().reservedStateMetadata().get(FileSettingsService.NAMESPACE);
Function<ClusterState, Boolean> clusterStateProcessor = clusterState -> {
synchronized (savedClusterState) {
ReservedStateMetadata reservedState = clusterState.metadata().reservedStateMetadata().get(FileSettingsService.NAMESPACE);
if (reservedState != null && reservedState.version() == fileSettingsVersion) {
clusterService.removeListener(this);
metadataVersion.set(event.state().metadata().version());
metadataVersion.set(clusterState.metadata().version());
Copy link
Contributor

Choose a reason for hiding this comment

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

Do I understand this correctly? If the reserved state version is the one we're expecting, we infer that the metadata version must also be the one we're expecting, on the assumption that the latest metadata update was the one that updated the reserved state. This seems ok in these particular tests where there are very few metadata updates, even though it wouldn't be correct in general.

Am I close?

Did you manage to induce this logic path with sleeps in the tests that call this method?

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't assert anything about the metadata version, this captures the metadata version once we see the file settings version we expect.

And yes I reproduced with a sleep just before creating the state listener.

savedClusterState.countDown();
logger.info(
"done waiting for file settings [version: {}, metadata version: {}]",
event.state().version(),
event.state().metadata().version()
clusterState.version(),
clusterState.metadata().version()
);
return true;
}
return false;
}
};
clusterService.addListener(new ClusterStateListener() {
@Override
public void clusterChanged(ClusterChangedEvent event) {
if (clusterStateProcessor.apply(event.state())) {
clusterService.removeListener(this);
}
}
});
clusterStateProcessor.apply(clusterService.state());

return new Tuple<>(savedClusterState, metadataVersion);
}
Expand Down