|
@@ -19,6 +19,8 @@ import org.elasticsearch.action.admin.indices.alias.Alias;
|
|
|
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
|
import org.elasticsearch.action.support.master.MasterNodeRequest;
|
|
|
import org.elasticsearch.cluster.ClusterState;
|
|
|
+import org.elasticsearch.cluster.ClusterStateTaskConfig;
|
|
|
+import org.elasticsearch.cluster.ClusterStateTaskExecutor;
|
|
|
import org.elasticsearch.cluster.ClusterStateUpdateTask;
|
|
|
import org.elasticsearch.cluster.service.ClusterService;
|
|
|
import org.elasticsearch.common.Priority;
|
|
@@ -121,6 +123,53 @@ public class MetadataIndexTemplateService {
|
|
|
private final SystemIndices systemIndices;
|
|
|
private final Set<IndexSettingProvider> indexSettingProviders;
|
|
|
|
|
|
+ /**
|
|
|
+ * This is the cluster state task executor for all template-based actions.
|
|
|
+ */
|
|
|
+ private static final ClusterStateTaskExecutor<TemplateClusterStateUpdateTask> TEMPLATE_TASK_EXECUTOR = (currentState, taskContexts) -> {
|
|
|
+ ClusterState state = currentState;
|
|
|
+ for (final var taskContext : taskContexts) {
|
|
|
+ try {
|
|
|
+ final var task = taskContext.getTask();
|
|
|
+ state = task.execute(state);
|
|
|
+ taskContext.success(new ClusterStateTaskExecutor.LegacyClusterTaskResultActionListener(task, currentState));
|
|
|
+ } catch (Exception e) {
|
|
|
+ taskContext.onFailure(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return state;
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * A specialized cluster state update task that always takes a listener handling an
|
|
|
+ * AcknowledgedResponse, as all template actions have simple acknowledged yes/no responses.
|
|
|
+ */
|
|
|
+ private abstract static class TemplateClusterStateUpdateTask extends ClusterStateUpdateTask {
|
|
|
+ private final ActionListener<AcknowledgedResponse> listener;
|
|
|
+
|
|
|
+ TemplateClusterStateUpdateTask(Priority priority, TimeValue timeout, ActionListener<AcknowledgedResponse> listener) {
|
|
|
+ super(priority, timeout);
|
|
|
+ this.listener = listener;
|
|
|
+ }
|
|
|
+
|
|
|
+ public abstract ClusterState doExecute(ClusterState currentState) throws Exception;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ClusterState execute(ClusterState currentState) throws Exception {
|
|
|
+ return doExecute(currentState);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ listener.onFailure(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
+ listener.onResponse(AcknowledgedResponse.TRUE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Inject
|
|
|
public MetadataIndexTemplateService(
|
|
|
ClusterService clusterService,
|
|
@@ -145,18 +194,12 @@ public class MetadataIndexTemplateService {
|
|
|
clusterService.submitUnbatchedStateUpdateTask(source, task);
|
|
|
}
|
|
|
|
|
|
- public void removeTemplates(final RemoveRequest request, final RemoveListener listener) {
|
|
|
- submitUnbatchedTask(
|
|
|
+ public void removeTemplates(final RemoveRequest request, final ActionListener<AcknowledgedResponse> listener) {
|
|
|
+ clusterService.submitStateUpdateTask(
|
|
|
"remove-index-template [" + request.name + "]",
|
|
|
- new ClusterStateUpdateTask(Priority.URGENT, request.masterTimeout) {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- listener.onFailure(e);
|
|
|
- }
|
|
|
-
|
|
|
+ new TemplateClusterStateUpdateTask(Priority.URGENT, request.masterTimeout, listener) {
|
|
|
@Override
|
|
|
- public ClusterState execute(ClusterState currentState) {
|
|
|
+ public ClusterState doExecute(ClusterState currentState) {
|
|
|
Set<String> templateNames = new HashSet<>();
|
|
|
for (Map.Entry<String, IndexTemplateMetadata> cursor : currentState.metadata().templates().entrySet()) {
|
|
|
String templateName = cursor.getKey();
|
|
@@ -179,12 +222,9 @@ public class MetadataIndexTemplateService {
|
|
|
}
|
|
|
return ClusterState.builder(currentState).metadata(metadata).build();
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
- listener.onResponse(AcknowledgedResponse.TRUE);
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ ClusterStateTaskConfig.build(Priority.URGENT, request.masterTimeout),
|
|
|
+ TEMPLATE_TASK_EXECUTOR
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -200,25 +240,16 @@ public class MetadataIndexTemplateService {
|
|
|
final ComponentTemplate template,
|
|
|
final ActionListener<AcknowledgedResponse> listener
|
|
|
) {
|
|
|
- submitUnbatchedTask(
|
|
|
+ clusterService.submitStateUpdateTask(
|
|
|
"create-component-template [" + name + "], cause [" + cause + "]",
|
|
|
- new ClusterStateUpdateTask(Priority.URGENT, masterTimeout) {
|
|
|
-
|
|
|
+ new TemplateClusterStateUpdateTask(Priority.URGENT, masterTimeout, listener) {
|
|
|
@Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- listener.onFailure(e);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ClusterState execute(ClusterState currentState) throws Exception {
|
|
|
+ public ClusterState doExecute(ClusterState currentState) throws Exception {
|
|
|
return addComponentTemplate(currentState, create, name, template);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
- listener.onResponse(AcknowledgedResponse.TRUE);
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ ClusterStateTaskConfig.build(Priority.URGENT, masterTimeout),
|
|
|
+ TEMPLATE_TASK_EXECUTOR
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -371,25 +402,16 @@ public class MetadataIndexTemplateService {
|
|
|
final ActionListener<AcknowledgedResponse> listener
|
|
|
) {
|
|
|
validateNotInUse(state.metadata(), names);
|
|
|
- submitUnbatchedTask(
|
|
|
+ clusterService.submitStateUpdateTask(
|
|
|
"remove-component-template [" + String.join(",", names) + "]",
|
|
|
- new ClusterStateUpdateTask(Priority.URGENT, masterTimeout) {
|
|
|
-
|
|
|
+ new TemplateClusterStateUpdateTask(Priority.URGENT, masterTimeout, listener) {
|
|
|
@Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- listener.onFailure(e);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ClusterState execute(ClusterState currentState) {
|
|
|
+ public ClusterState doExecute(ClusterState currentState) {
|
|
|
return innerRemoveComponentTemplate(currentState, names);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
- listener.onResponse(AcknowledgedResponse.TRUE);
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ ClusterStateTaskConfig.build(Priority.URGENT, masterTimeout),
|
|
|
+ TEMPLATE_TASK_EXECUTOR
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -487,25 +509,16 @@ public class MetadataIndexTemplateService {
|
|
|
final ActionListener<AcknowledgedResponse> listener
|
|
|
) {
|
|
|
validateV2TemplateRequest(clusterService.state().metadata(), name, template);
|
|
|
- submitUnbatchedTask(
|
|
|
+ clusterService.submitStateUpdateTask(
|
|
|
"create-index-template-v2 [" + name + "], cause [" + cause + "]",
|
|
|
- new ClusterStateUpdateTask(Priority.URGENT, masterTimeout) {
|
|
|
-
|
|
|
+ new TemplateClusterStateUpdateTask(Priority.URGENT, masterTimeout, listener) {
|
|
|
@Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- listener.onFailure(e);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ClusterState execute(ClusterState currentState) throws Exception {
|
|
|
+ public ClusterState doExecute(ClusterState currentState) throws Exception {
|
|
|
return addIndexTemplateV2(currentState, create, name, template);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
- listener.onResponse(AcknowledgedResponse.TRUE);
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ ClusterStateTaskConfig.build(Priority.URGENT, masterTimeout),
|
|
|
+ TEMPLATE_TASK_EXECUTOR
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -880,25 +893,16 @@ public class MetadataIndexTemplateService {
|
|
|
final TimeValue masterTimeout,
|
|
|
final ActionListener<AcknowledgedResponse> listener
|
|
|
) {
|
|
|
- submitUnbatchedTask(
|
|
|
+ clusterService.submitStateUpdateTask(
|
|
|
"remove-index-template-v2 [" + String.join(",", names) + "]",
|
|
|
- new ClusterStateUpdateTask(Priority.URGENT, masterTimeout) {
|
|
|
-
|
|
|
+ new TemplateClusterStateUpdateTask(Priority.URGENT, masterTimeout, listener) {
|
|
|
@Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- listener.onFailure(e);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ClusterState execute(ClusterState currentState) {
|
|
|
+ public ClusterState doExecute(ClusterState currentState) {
|
|
|
return innerRemoveIndexTemplateV2(currentState, names);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
- listener.onResponse(AcknowledgedResponse.TRUE);
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ ClusterStateTaskConfig.build(Priority.URGENT, masterTimeout),
|
|
|
+ TEMPLATE_TASK_EXECUTOR
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -990,7 +994,7 @@ public class MetadataIndexTemplateService {
|
|
|
.collect(Collectors.toSet());
|
|
|
}
|
|
|
|
|
|
- public void putTemplate(final PutRequest request, final PutListener listener) {
|
|
|
+ public void putTemplate(final PutRequest request, final ActionListener<AcknowledgedResponse> listener) {
|
|
|
Settings.Builder updatedSettingsBuilder = Settings.builder();
|
|
|
updatedSettingsBuilder.put(request.settings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX);
|
|
|
request.settings(updatedSettingsBuilder.build());
|
|
@@ -1013,26 +1017,17 @@ public class MetadataIndexTemplateService {
|
|
|
|
|
|
final IndexTemplateMetadata.Builder templateBuilder = IndexTemplateMetadata.builder(request.name);
|
|
|
|
|
|
- submitUnbatchedTask(
|
|
|
+ clusterService.submitStateUpdateTask(
|
|
|
"create-index-template [" + request.name + "], cause [" + request.cause + "]",
|
|
|
- new ClusterStateUpdateTask(Priority.URGENT, request.masterTimeout) {
|
|
|
-
|
|
|
+ new TemplateClusterStateUpdateTask(Priority.URGENT, request.masterTimeout, listener) {
|
|
|
@Override
|
|
|
- public void onFailure(Exception e) {
|
|
|
- listener.onFailure(e);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ClusterState execute(ClusterState currentState) throws Exception {
|
|
|
+ public ClusterState doExecute(ClusterState currentState) throws Exception {
|
|
|
validateTemplate(request.settings, request.mappings, indicesService);
|
|
|
return innerPutTemplate(currentState, request, templateBuilder);
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
|
|
|
- listener.onResponse(new PutResponse(true));
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ ClusterStateTaskConfig.build(Priority.URGENT, request.masterTimeout),
|
|
|
+ TEMPLATE_TASK_EXECUTOR
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -1480,7 +1475,7 @@ public class MetadataIndexTemplateService {
|
|
|
try {
|
|
|
MapperService mapperService = tempIndexService.mapperService();
|
|
|
for (CompressedXContent mapping : mappings) {
|
|
|
- mapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.INDEX_TEMPLATE);
|
|
|
+ mapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MapperService.MergeReason.INDEX_TEMPLATE);
|
|
|
}
|
|
|
|
|
|
if (template.getDataStreamTemplate() != null) {
|
|
@@ -1550,12 +1545,7 @@ public class MetadataIndexTemplateService {
|
|
|
name,
|
|
|
maybeTemplate.map(Template::settings).orElse(Settings.EMPTY),
|
|
|
indexPatterns,
|
|
|
- maybeTemplate.map(Template::aliases)
|
|
|
- .orElse(Collections.emptyMap())
|
|
|
- .values()
|
|
|
- .stream()
|
|
|
- .map(MetadataIndexTemplateService::toAlias)
|
|
|
- .toList()
|
|
|
+ maybeTemplate.map(Template::aliases).orElse(emptyMap()).values().stream().map(MetadataIndexTemplateService::toAlias).toList()
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -1655,13 +1645,6 @@ public class MetadataIndexTemplateService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public interface PutListener {
|
|
|
-
|
|
|
- void onResponse(PutResponse response);
|
|
|
-
|
|
|
- void onFailure(Exception e);
|
|
|
- }
|
|
|
-
|
|
|
public static class PutRequest {
|
|
|
final String name;
|
|
|
final String cause;
|
|
@@ -1721,18 +1704,6 @@ public class MetadataIndexTemplateService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class PutResponse {
|
|
|
- private final boolean acknowledged;
|
|
|
-
|
|
|
- public PutResponse(boolean acknowledged) {
|
|
|
- this.acknowledged = acknowledged;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean acknowledged() {
|
|
|
- return acknowledged;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public static class RemoveRequest {
|
|
|
final String name;
|
|
|
TimeValue masterTimeout = MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT;
|
|
@@ -1746,11 +1717,4 @@ public class MetadataIndexTemplateService {
|
|
|
return this;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public interface RemoveListener {
|
|
|
-
|
|
|
- void onResponse(AcknowledgedResponse response);
|
|
|
-
|
|
|
- void onFailure(Exception e);
|
|
|
- }
|
|
|
}
|