-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added generic cluster state update ack mechanism
Added new AckedClusterStateUpdateTask interface that can be used to submit cluster state update tasks and allows actions to be notified back when a set of (configurable) nodes have acknowledged the cluster state update. Supports a configurable timeout, so that we wait for acknowledgement for a limited amount of time (will be provided in the request as it curently happens, default 10s). Internally, a low level AckListener is created (InternalClusterService) and passed to the publish method, so that it can be notified whenever each node responds to the publish request. Once all the expected nodes have responded or the timeoeout has expired, the AckListener notifies the action which will return adding the proper acknowledged flag to the response. Ideally, this new mechanism will gradually replace the existing ones based on custom endpoints and notifications (per api). Closes #3786
- Loading branch information
Showing
10 changed files
with
389 additions
and
38 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/org/elasticsearch/cluster/AckedClusterStateUpdateTask.java
This file contains 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,55 @@ | ||
/* | ||
* Licensed to ElasticSearch and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. ElasticSearch licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.elasticsearch.cluster; | ||
|
||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
|
||
/** | ||
* An extension interface to {@link ClusterStateUpdateTask} that allows to be notified when | ||
* all the nodes have acknowledged a cluster state update request | ||
*/ | ||
public interface AckedClusterStateUpdateTask extends TimeoutClusterStateUpdateTask { | ||
|
||
/** | ||
* Called to determine which nodes the acknowledgement is expected from | ||
* @param discoveryNode a node | ||
* @return true if the node is expected to send ack back, false otherwise | ||
*/ | ||
boolean mustAck(DiscoveryNode discoveryNode); | ||
|
||
/** | ||
* Called once all the nodes have acknowledged the cluster state update request. Must be | ||
* very lightweight execution, since it gets executed on the cluster service thread. | ||
* @param t optional error that might have been thrown | ||
*/ | ||
void onAllNodesAcked(@Nullable Throwable t); | ||
|
||
/** | ||
* Called once the acknowledgement timeout defined by | ||
* {@link AckedClusterStateUpdateTask#ackTimeout()} has expired | ||
*/ | ||
void onAckTimeout(); | ||
|
||
/** | ||
* Acknowledgement timeout, maximum time interval to wait for acknowledgements | ||
*/ | ||
TimeValue ackTimeout(); | ||
} |
This file contains 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
71 changes: 71 additions & 0 deletions
71
src/main/java/org/elasticsearch/discovery/AckClusterStatePublishResponseHandler.java
This file contains 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,71 @@ | ||
/* | ||
* Licensed to ElasticSearch and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. ElasticSearch licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.elasticsearch.discovery; | ||
|
||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.logging.ESLogger; | ||
import org.elasticsearch.common.logging.ESLoggerFactory; | ||
|
||
/** | ||
* Allows to wait for all nodes to reply to the publish of a new cluster state | ||
* and notifies the {@link org.elasticsearch.discovery.Discovery.AckListener} | ||
* so that the cluster state update can be acknowledged | ||
*/ | ||
public class AckClusterStatePublishResponseHandler extends BlockingClusterStatePublishResponseHandler { | ||
|
||
private static final ESLogger logger = ESLoggerFactory.getLogger(AckClusterStatePublishResponseHandler.class.getName()); | ||
|
||
private final Discovery.AckListener ackListener; | ||
|
||
/** | ||
* Creates a new AckClusterStatePublishResponseHandler | ||
* @param nonMasterNodes number of nodes that are supposed to reply to a cluster state publish from master | ||
* @param ackListener the {@link org.elasticsearch.discovery.Discovery.AckListener} to notify for each response | ||
* gotten from non master nodes | ||
*/ | ||
public AckClusterStatePublishResponseHandler(int nonMasterNodes, Discovery.AckListener ackListener) { | ||
//Don't count the master as acknowledged, because it's not done yet | ||
//otherwise we might end up with all the nodes but the master holding the latest cluster state | ||
super(nonMasterNodes); | ||
this.ackListener = ackListener; | ||
} | ||
|
||
@Override | ||
public void onResponse(DiscoveryNode node) { | ||
super.onResponse(node); | ||
onNodeAck(ackListener, node, null); | ||
} | ||
|
||
@Override | ||
public void onFailure(DiscoveryNode node, Throwable t) { | ||
try { | ||
super.onFailure(node, t); | ||
} finally { | ||
onNodeAck(ackListener, node, t); | ||
} | ||
} | ||
|
||
private void onNodeAck(final Discovery.AckListener ackListener, DiscoveryNode node, Throwable t) { | ||
try { | ||
ackListener.onNodeAck(node, t); | ||
} catch (Throwable t1) { | ||
logger.debug("error while processing ack for node [{}]", t1, node); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/elasticsearch/discovery/BlockingClusterStatePublishResponseHandler.java
This file contains 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,60 @@ | ||
/* | ||
* Licensed to ElasticSearch and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. ElasticSearch licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.elasticsearch.discovery; | ||
|
||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
|
||
/** | ||
* Default implementation of {@link ClusterStatePublishResponseHandler}, allows to await a reply | ||
* to a cluster state publish from all non master nodes, up to a timeout | ||
*/ | ||
public class BlockingClusterStatePublishResponseHandler implements ClusterStatePublishResponseHandler { | ||
|
||
private final CountDownLatch latch; | ||
|
||
/** | ||
* Creates a new BlockingClusterStatePublishResponseHandler | ||
* @param nonMasterNodes number of nodes that are supposed to reply to a cluster state publish from master | ||
*/ | ||
public BlockingClusterStatePublishResponseHandler(int nonMasterNodes) { | ||
//Don't count the master, as it's the one that does the publish | ||
//the master won't call onResponse either | ||
this.latch = new CountDownLatch(nonMasterNodes); | ||
} | ||
|
||
@Override | ||
public void onResponse(DiscoveryNode node) { | ||
latch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onFailure(DiscoveryNode node, Throwable t) { | ||
latch.countDown(); | ||
} | ||
|
||
@Override | ||
public boolean awaitAllNodes(TimeValue timeout) throws InterruptedException { | ||
return latch.await(timeout.millis(), TimeUnit.MILLISECONDS); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/elasticsearch/discovery/ClusterStatePublishResponseHandler.java
This file contains 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,49 @@ | ||
/* | ||
* Licensed to ElasticSearch and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. ElasticSearch licenses this | ||
* file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package org.elasticsearch.discovery; | ||
|
||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
|
||
/** | ||
* Handles responses obtained when publishing a new cluster state from master to all non master nodes. | ||
* Allows to await a reply from all non master nodes, up to a timeout | ||
*/ | ||
public interface ClusterStatePublishResponseHandler { | ||
|
||
/** | ||
* Called for each response obtained from non master nodes | ||
* @param node the node that replied to the publish event | ||
*/ | ||
void onResponse(DiscoveryNode node); | ||
|
||
/** | ||
* Called for each failure obtained from non master nodes | ||
* @param node the node that replied to the publish event | ||
*/ | ||
void onFailure(DiscoveryNode node, Throwable t); | ||
|
||
/** | ||
* Allows to wait for all non master nodes to reply to the publish event up to a timeout | ||
* @param timeout the timeout | ||
* @return true if the timeout expired or not, false otherwise | ||
* @throws InterruptedException | ||
*/ | ||
boolean awaitAllNodes(TimeValue timeout) throws InterruptedException; | ||
} |
Oops, something went wrong.