Skip to content
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
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);
}
}
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();
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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_BLOCK happen again later, after initialization?

  • If so, probably this code could use a comment explaining why we want to keep accepting new cluster states in that case but not during initialization.
  • If not, then we can get rid of the isInitialized call.

Copy link
Contributor Author

@ldematte ldematte Nov 4, 2024

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.

}
}

private boolean isInitialized() {
return currentClusterState != null;
}

@Override
public Optional<ClusterState> get() {
return Optional.ofNullable(currentClusterState);
}
}