|
@@ -22,18 +22,20 @@ package org.elasticsearch.cluster.metadata;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import org.elasticsearch.ElasticSearchIllegalArgumentException;
|
|
|
-import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
|
|
|
+import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest;
|
|
|
+import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
|
|
|
import org.elasticsearch.cluster.ClusterService;
|
|
|
import org.elasticsearch.cluster.ClusterState;
|
|
|
-import org.elasticsearch.cluster.TimeoutClusterStateUpdateTask;
|
|
|
-import org.elasticsearch.cluster.action.index.NodeAliasesUpdatedAction;
|
|
|
+import org.elasticsearch.cluster.ack.ClusterStateUpdateListener;
|
|
|
+import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
|
|
|
+import org.elasticsearch.cluster.node.DiscoveryNode;
|
|
|
+import org.elasticsearch.common.Nullable;
|
|
|
import org.elasticsearch.common.Priority;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.component.AbstractComponent;
|
|
|
import org.elasticsearch.common.inject.Inject;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
-import org.elasticsearch.common.util.concurrent.CountDown;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.index.Index;
|
|
@@ -48,7 +50,7 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ * Service responsible for submitting add and remove aliases requests
|
|
|
*/
|
|
|
public class MetaDataIndexAliasesService extends AbstractComponent {
|
|
|
|
|
@@ -56,22 +58,39 @@ public class MetaDataIndexAliasesService extends AbstractComponent {
|
|
|
|
|
|
private final IndicesService indicesService;
|
|
|
|
|
|
- private final NodeAliasesUpdatedAction aliasOperationPerformedAction;
|
|
|
-
|
|
|
@Inject
|
|
|
- public MetaDataIndexAliasesService(Settings settings, ClusterService clusterService, IndicesService indicesService, NodeAliasesUpdatedAction aliasOperationPerformedAction) {
|
|
|
+ public MetaDataIndexAliasesService(Settings settings, ClusterService clusterService, IndicesService indicesService) {
|
|
|
super(settings);
|
|
|
this.clusterService = clusterService;
|
|
|
this.indicesService = indicesService;
|
|
|
- this.aliasOperationPerformedAction = aliasOperationPerformedAction;
|
|
|
}
|
|
|
|
|
|
- public void indicesAliases(final Request request, final Listener listener) {
|
|
|
- clusterService.submitStateUpdateTask("index-aliases", Priority.URGENT, new TimeoutClusterStateUpdateTask() {
|
|
|
+ public void indicesAliases(final IndicesAliasesClusterStateUpdateRequest request, final ClusterStateUpdateListener listener) {
|
|
|
+ clusterService.submitStateUpdateTask("index-aliases", Priority.URGENT, new AckedClusterStateUpdateTask() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean mustAck(DiscoveryNode discoveryNode) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAllNodesAcked(@Nullable Throwable t) {
|
|
|
+ listener.onResponse(new ClusterStateUpdateResponse(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAckTimeout() {
|
|
|
+ listener.onResponse(new ClusterStateUpdateResponse(false));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TimeValue ackTimeout() {
|
|
|
+ return request.ackTimeout();
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public TimeValue timeout() {
|
|
|
- return request.masterTimeout;
|
|
|
+ return request.masterNodeTimeout();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -84,7 +103,7 @@ public class MetaDataIndexAliasesService extends AbstractComponent {
|
|
|
List<String> indicesToClose = Lists.newArrayList();
|
|
|
Map<String, IndexService> indices = Maps.newHashMap();
|
|
|
try {
|
|
|
- for (AliasAction aliasAction : request.actions) {
|
|
|
+ for (AliasAction aliasAction : request.actions()) {
|
|
|
if (!Strings.hasText(aliasAction.alias()) || !Strings.hasText(aliasAction.index())) {
|
|
|
throw new ElasticSearchIllegalArgumentException("Index name and alias name are required");
|
|
|
}
|
|
@@ -101,7 +120,7 @@ public class MetaDataIndexAliasesService extends AbstractComponent {
|
|
|
|
|
|
boolean changed = false;
|
|
|
MetaData.Builder builder = MetaData.builder(currentState.metaData());
|
|
|
- for (AliasAction aliasAction : request.actions) {
|
|
|
+ for (AliasAction aliasAction : request.actions()) {
|
|
|
IndexMetaData indexMetaData = builder.get(aliasAction.index());
|
|
|
if (indexMetaData == null) {
|
|
|
throw new IndexMissingException(new Index(aliasAction.index()));
|
|
@@ -175,20 +194,11 @@ public class MetaDataIndexAliasesService extends AbstractComponent {
|
|
|
ClusterState updatedState = ClusterState.builder(currentState).metaData(builder).build();
|
|
|
// even though changes happened, they resulted in 0 actual changes to metadata
|
|
|
// i.e. remove and add the same alias to the same index
|
|
|
- if (updatedState.metaData().aliases().equals(currentState.metaData().aliases())) {
|
|
|
- return currentState;
|
|
|
+ if (!updatedState.metaData().aliases().equals(currentState.metaData().aliases())) {
|
|
|
+ return updatedState;
|
|
|
}
|
|
|
- // wait for responses from other nodes if needed
|
|
|
- int responseCount = updatedState.nodes().size();
|
|
|
- long version = updatedState.version() + 1;
|
|
|
- logger.trace("waiting for [{}] notifications with version [{}]", responseCount, version);
|
|
|
- aliasOperationPerformedAction.add(new CountDownListener(responseCount, listener, version), request.timeout);
|
|
|
-
|
|
|
- return updatedState;
|
|
|
- } else {
|
|
|
- // Nothing to do
|
|
|
- return currentState;
|
|
|
}
|
|
|
+ return currentState;
|
|
|
} finally {
|
|
|
for (String index : indicesToClose) {
|
|
|
indicesService.removeIndex(index, "created for alias processing");
|
|
@@ -198,81 +208,8 @@ public class MetaDataIndexAliasesService extends AbstractComponent {
|
|
|
|
|
|
@Override
|
|
|
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
|
|
|
- if (oldState == newState) {
|
|
|
- // we didn't do anything, callback
|
|
|
- listener.onResponse(new Response(true));
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- public static interface Listener {
|
|
|
|
|
|
- void onResponse(Response response);
|
|
|
-
|
|
|
- void onFailure(Throwable t);
|
|
|
- }
|
|
|
-
|
|
|
- public static class Request {
|
|
|
-
|
|
|
- final AliasAction[] actions;
|
|
|
-
|
|
|
- final TimeValue timeout;
|
|
|
- TimeValue masterTimeout = MasterNodeOperationRequest.DEFAULT_MASTER_NODE_TIMEOUT;
|
|
|
-
|
|
|
- public Request(AliasAction[] actions, TimeValue timeout) {
|
|
|
- this.actions = actions;
|
|
|
- this.timeout = timeout;
|
|
|
- }
|
|
|
-
|
|
|
- public Request masterTimeout(TimeValue masterTimeout) {
|
|
|
- this.masterTimeout = masterTimeout;
|
|
|
- return this;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static class Response {
|
|
|
- private final boolean acknowledged;
|
|
|
-
|
|
|
- public Response(boolean acknowledged) {
|
|
|
- this.acknowledged = acknowledged;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean acknowledged() {
|
|
|
- return acknowledged;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private class CountDownListener implements NodeAliasesUpdatedAction.Listener {
|
|
|
-
|
|
|
- private final CountDown countDown;
|
|
|
- private final Listener listener;
|
|
|
- private final long version;
|
|
|
-
|
|
|
- public CountDownListener(int countDown, Listener listener, long version) {
|
|
|
- this.countDown = new CountDown(countDown);
|
|
|
- this.listener = listener;
|
|
|
- this.version = version;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onAliasesUpdated(NodeAliasesUpdatedAction.NodeAliasesUpdatedResponse response) {
|
|
|
- if (version <= response.version()) {
|
|
|
- logger.trace("Received NodeAliasesUpdatedResponse with version [{}] from [{}]", response.version(), response.nodeId());
|
|
|
- if (countDown.countDown()) {
|
|
|
- aliasOperationPerformedAction.remove(this);
|
|
|
- logger.trace("NodeAliasUpdated was acknowledged by all expected nodes, returning");
|
|
|
- listener.onResponse(new Response(true));
|
|
|
- }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onTimeout() {
|
|
|
- if (countDown.fastForward()) {
|
|
|
- aliasOperationPerformedAction.remove(this);
|
|
|
- listener.onResponse(new Response(false));
|
|
|
- }
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
}
|