Skip to content

Commit

Permalink
WIP rest scaffolding for rename index API
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Dec 4, 2024
1 parent 28eda97 commit 3d41133
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -742,6 +744,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> 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);
Expand Down Expand Up @@ -1001,6 +1004,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster, Predicate<
registerHandler.accept(new RestCatComponentTemplateAction());
registerHandler.accept(new RestAnalyzeIndexDiskUsageAction());
registerHandler.accept(new RestFieldUsageStatsAction());
registerHandler.accept(new RestRenameIndexAction());

// Desired nodes
registerHandler.accept(new RestGetDesiredNodesAction());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RenameIndexRequest> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<RenameIndexRequest, AcknowledgedResponse> {
public static final String NAME = "indices:admin/rename";
public static final ActionType<AcknowledgedResponse> 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<AcknowledgedResponse> listener)
throws Exception {

}

@Override
protected ClusterBlockException checkBlock(RenameIndexRequest request, ClusterState state) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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<Route> 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));
}
}

0 comments on commit 3d41133

Please sign in to comment.