From c47340f2f1a9eae52279e0c70885ebe2675fcd03 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sun, 3 Jan 2016 12:39:25 -0500 Subject: [PATCH] Refactor master node change predicate for reuse This commit migrates a ClusterStateObserver.ChangePredicate for detecting a master node change into a separate class for reuse elsewhere. --- .../master/TransportMasterNodeAction.java | 22 ++-------- .../cluster/MasterNodeChangePredicate.java | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 core/src/main/java/org/elasticsearch/cluster/MasterNodeChangePredicate.java diff --git a/core/src/main/java/org/elasticsearch/action/support/master/TransportMasterNodeAction.java b/core/src/main/java/org/elasticsearch/action/support/master/TransportMasterNodeAction.java index a06b11d9d58..f3fe2d30bb6 100644 --- a/core/src/main/java/org/elasticsearch/action/support/master/TransportMasterNodeAction.java +++ b/core/src/main/java/org/elasticsearch/action/support/master/TransportMasterNodeAction.java @@ -26,10 +26,10 @@ import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.action.support.ThreadedActionListener; -import org.elasticsearch.cluster.ClusterChangedEvent; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.MasterNodeChangePredicate; import org.elasticsearch.cluster.NotMasterException; import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -50,20 +50,6 @@ import java.util.function.Supplier; * A base class for operations that needs to be performed on the master node. */ public abstract class TransportMasterNodeAction extends HandledTransportAction { - private static final ClusterStateObserver.ChangePredicate masterNodeChangedPredicate = new ClusterStateObserver.ChangePredicate() { - @Override - public boolean apply(ClusterState previousState, ClusterState.ClusterStateStatus previousStatus, - ClusterState newState, ClusterState.ClusterStateStatus newStatus) { - // The condition !newState.nodes().masterNodeId().equals(previousState.nodes().masterNodeId()) is not sufficient as the same master node might get reelected after a disruption. - return newState.nodes().masterNodeId() != null && newState != previousState; - } - - @Override - public boolean apply(ClusterChangedEvent event) { - return event.nodesDelta().masterNodeChanged(); - } - }; - protected final TransportService transportService; protected final ClusterService clusterService; @@ -148,7 +134,7 @@ public abstract class TransportMasterNodeAction(listener) { @Override @@ -179,7 +165,7 @@ public abstract class TransportMasterNodeAction