From 3d41133375b4f7d532f8217f6467bc437f473b86 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Wed, 4 Dec 2024 14:41:30 -0700 Subject: [PATCH] WIP rest scaffolding for rename index API --- .../elasticsearch/action/ActionModule.java | 4 ++ .../index/rename/RenameIndexRequest.java | 55 ++++++++++++++++ .../rename/TransportRenameIndexAction.java | 65 +++++++++++++++++++ .../admin/indices/RestRenameIndexAction.java | 47 ++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 server/src/main/java/org/elasticsearch/index/rename/RenameIndexRequest.java create mode 100644 server/src/main/java/org/elasticsearch/index/rename/TransportRenameIndexAction.java create mode 100644 server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRenameIndexAction.java diff --git a/server/src/main/java/org/elasticsearch/action/ActionModule.java b/server/src/main/java/org/elasticsearch/action/ActionModule.java index 98d6284fd91d2..e414132e81413 100644 --- a/server/src/main/java/org/elasticsearch/action/ActionModule.java +++ b/server/src/main/java/org/elasticsearch/action/ActionModule.java @@ -235,6 +235,7 @@ import org.elasticsearch.health.stats.HealthApiStatsAction; import org.elasticsearch.health.stats.HealthApiStatsTransportAction; import org.elasticsearch.http.HttpPreRequest; +import org.elasticsearch.index.rename.TransportRenameIndexAction; import org.elasticsearch.index.seqno.GlobalCheckpointSyncAction; import org.elasticsearch.index.seqno.RetentionLeaseActions; import org.elasticsearch.indices.SystemIndices; @@ -347,6 +348,7 @@ import org.elasticsearch.rest.action.admin.indices.RestRecoveryAction; import org.elasticsearch.rest.action.admin.indices.RestRefreshAction; import org.elasticsearch.rest.action.admin.indices.RestReloadAnalyzersAction; +import org.elasticsearch.rest.action.admin.indices.RestRenameIndexAction; import org.elasticsearch.rest.action.admin.indices.RestResizeHandler; import org.elasticsearch.rest.action.admin.indices.RestResolveClusterAction; import org.elasticsearch.rest.action.admin.indices.RestResolveIndexAction; @@ -742,6 +744,7 @@ public void reg actions.register(FieldUsageStatsAction.INSTANCE, TransportFieldUsageAction.class); actions.register(MasterHistoryAction.INSTANCE, MasterHistoryAction.TransportAction.class); actions.register(CoordinationDiagnosticsAction.INSTANCE, CoordinationDiagnosticsAction.TransportAction.class); + actions.register(TransportRenameIndexAction.TYPE, TransportRenameIndexAction.class); // Indexed scripts actions.register(TransportPutStoredScriptAction.TYPE, TransportPutStoredScriptAction.class); @@ -1001,6 +1004,7 @@ public void initRestHandlers(Supplier nodesInCluster, Predicate< registerHandler.accept(new RestCatComponentTemplateAction()); registerHandler.accept(new RestAnalyzeIndexDiskUsageAction()); registerHandler.accept(new RestFieldUsageStatsAction()); + registerHandler.accept(new RestRenameIndexAction()); // Desired nodes registerHandler.accept(new RestGetDesiredNodesAction()); diff --git a/server/src/main/java/org/elasticsearch/index/rename/RenameIndexRequest.java b/server/src/main/java/org/elasticsearch/index/rename/RenameIndexRequest.java new file mode 100644 index 0000000000000..6daa105dfae75 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/index/rename/RenameIndexRequest.java @@ -0,0 +1,55 @@ +/* + * 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.index.rename; + +import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.support.master.MasterNodeRequest; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.TimeValue; + +import java.io.IOException; + +public class RenameIndexRequest extends MasterNodeRequest { + private String index; + private String newIndex; + + public RenameIndexRequest(TimeValue masterNodeTimeout, String index, String newIndex) { + super(masterNodeTimeout); + this.index = index; + this.newIndex = newIndex; + } + + public RenameIndexRequest(StreamInput in) throws IOException { + super(in); + this.index = in.readString(); + this.newIndex = in.readString(); + } + + @Override + public ActionRequestValidationException validate() { + // TODO: validate + return null; + } + + public String newIndex() { + return newIndex; + } + + public String index() { + return index; + } + + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(this.index); + out.writeString(this.newIndex); + } +} diff --git a/server/src/main/java/org/elasticsearch/index/rename/TransportRenameIndexAction.java b/server/src/main/java/org/elasticsearch/index/rename/TransportRenameIndexAction.java new file mode 100644 index 0000000000000..acb6fb3db96d7 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/index/rename/TransportRenameIndexAction.java @@ -0,0 +1,65 @@ +/* + * 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.index.rename; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.ActionType; +import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.support.master.TransportMasterNodeAction; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.block.ClusterBlockException; +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.util.concurrent.EsExecutors; +import org.elasticsearch.injection.guice.Inject; +import org.elasticsearch.tasks.Task; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; + +public class TransportRenameIndexAction extends TransportMasterNodeAction { + public static final String NAME = "indices:admin/rename"; + public static final ActionType TYPE = new ActionType<>(NAME); + private static final Logger logger = LogManager.getLogger(TransportRenameIndexAction.class); + + @Inject + protected TransportRenameIndexAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + NAME, + transportService, + clusterService, + threadPool, + actionFilters, + RenameIndexRequest::new, + indexNameExpressionResolver, + AcknowledgedResponse::readFrom, + EsExecutors.DIRECT_EXECUTOR_SERVICE + ); + } + + @Override + protected void masterOperation(Task task, RenameIndexRequest request, ClusterState state, ActionListener listener) + throws Exception { + + } + + @Override + protected ClusterBlockException checkBlock(RenameIndexRequest request, ClusterState state) { + return null; + } +} diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRenameIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRenameIndexAction.java new file mode 100644 index 0000000000000..f320c22c19f15 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRenameIndexAction.java @@ -0,0 +1,47 @@ +/* + * 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.rest.action.admin.indices; + +import org.elasticsearch.client.internal.node.NodeClient; +import org.elasticsearch.index.rename.RenameIndexRequest; +import org.elasticsearch.index.rename.TransportRenameIndexAction; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout; + +public class RestRenameIndexAction extends BaseRestHandler { + @Override + public String getName() { + return "rename_index_action"; + } + + @Override + public List routes() { + return List.of( + new Route(RestRequest.Method.POST, "/_rename/{index}"), + new Route(RestRequest.Method.POST, "/_rename/{index}/{newIndex}") + ); + } + + @Override + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + RenameIndexRequest renameRequest = new RenameIndexRequest( + getMasterNodeTimeout(request), + request.param("index"), + request.param("newIndex", "bar") + ); + return channel -> client.execute(TransportRenameIndexAction.TYPE, renameRequest, new RestToXContentListener<>(channel)); + } +}