Skip to content

Commit 0a6312a

Browse files
authored
Collapse REST resize handlers (#30229)
The REST resize handlers for shrink/split operations are effectively the same code with a minor difference. This commit collapse these handlers into a single base class.
1 parent 9c8e015 commit 0a6312a

File tree

3 files changed

+52
-69
lines changed

3 files changed

+52
-69
lines changed

server/src/main/java/org/elasticsearch/action/ActionModule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
import org.elasticsearch.rest.action.admin.cluster.RestRestoreSnapshotAction;
242242
import org.elasticsearch.rest.action.admin.cluster.RestSnapshotsStatusAction;
243243
import org.elasticsearch.rest.action.admin.cluster.RestVerifyRepositoryAction;
244+
import org.elasticsearch.rest.action.admin.indices.RestResizeHandler;
244245
import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction;
245246
import org.elasticsearch.rest.action.admin.indices.RestClearIndicesCacheAction;
246247
import org.elasticsearch.rest.action.admin.indices.RestCloseIndexAction;
@@ -270,8 +271,6 @@
270271
import org.elasticsearch.rest.action.admin.indices.RestRecoveryAction;
271272
import org.elasticsearch.rest.action.admin.indices.RestRefreshAction;
272273
import org.elasticsearch.rest.action.admin.indices.RestRolloverIndexAction;
273-
import org.elasticsearch.rest.action.admin.indices.RestShrinkIndexAction;
274-
import org.elasticsearch.rest.action.admin.indices.RestSplitIndexAction;
275274
import org.elasticsearch.rest.action.admin.indices.RestSyncedFlushAction;
276275
import org.elasticsearch.rest.action.admin.indices.RestUpdateSettingsAction;
277276
import org.elasticsearch.rest.action.admin.indices.RestUpgradeAction;
@@ -569,8 +568,8 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
569568
registerHandler.accept(new RestIndexPutAliasAction(settings, restController));
570569
registerHandler.accept(new RestIndicesAliasesAction(settings, restController));
571570
registerHandler.accept(new RestCreateIndexAction(settings, restController));
572-
registerHandler.accept(new RestShrinkIndexAction(settings, restController));
573-
registerHandler.accept(new RestSplitIndexAction(settings, restController));
571+
registerHandler.accept(new RestResizeHandler.RestShrinkIndexAction(settings, restController));
572+
registerHandler.accept(new RestResizeHandler.RestSplitIndexAction(settings, restController));
574573
registerHandler.accept(new RestRolloverIndexAction(settings, restController));
575574
registerHandler.accept(new RestDeleteIndexAction(settings, restController));
576575
registerHandler.accept(new RestCloseIndexAction(settings, restController));

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestShrinkIndexAction.java renamed to server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestResizeHandler.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,66 @@
3131

3232
import java.io.IOException;
3333

34-
public class RestShrinkIndexAction extends BaseRestHandler {
35-
public RestShrinkIndexAction(Settings settings, RestController controller) {
34+
public abstract class RestResizeHandler extends BaseRestHandler {
35+
36+
RestResizeHandler(final Settings settings) {
3637
super(settings);
37-
controller.registerHandler(RestRequest.Method.PUT, "/{index}/_shrink/{target}", this);
38-
controller.registerHandler(RestRequest.Method.POST, "/{index}/_shrink/{target}", this);
3938
}
4039

4140
@Override
42-
public String getName() {
43-
return "shrink_index_action";
44-
}
41+
public abstract String getName();
42+
43+
abstract ResizeType getResizeType();
4544

4645
@Override
47-
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
46+
public final RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
4847
final ResizeRequest resizeRequest = new ResizeRequest(request.param("target"), request.param("index"));
49-
resizeRequest.setResizeType(ResizeType.SHRINK);
48+
resizeRequest.setResizeType(getResizeType());
5049
request.applyContentParser(resizeRequest::fromXContent);
5150
resizeRequest.timeout(request.paramAsTime("timeout", resizeRequest.timeout()));
5251
resizeRequest.masterNodeTimeout(request.paramAsTime("master_timeout", resizeRequest.masterNodeTimeout()));
5352
resizeRequest.setWaitForActiveShards(ActiveShardCount.parseString(request.param("wait_for_active_shards")));
5453
return channel -> client.admin().indices().resizeIndex(resizeRequest, new RestToXContentListener<>(channel));
5554
}
55+
56+
public static class RestShrinkIndexAction extends RestResizeHandler {
57+
58+
public RestShrinkIndexAction(final Settings settings, final RestController controller) {
59+
super(settings);
60+
controller.registerHandler(RestRequest.Method.PUT, "/{index}/_shrink/{target}", this);
61+
controller.registerHandler(RestRequest.Method.POST, "/{index}/_shrink/{target}", this);
62+
}
63+
64+
@Override
65+
public String getName() {
66+
return "shrink_index_action";
67+
}
68+
69+
@Override
70+
protected ResizeType getResizeType() {
71+
return ResizeType.SHRINK;
72+
}
73+
74+
}
75+
76+
public static class RestSplitIndexAction extends RestResizeHandler {
77+
78+
public RestSplitIndexAction(final Settings settings, final RestController controller) {
79+
super(settings);
80+
controller.registerHandler(RestRequest.Method.PUT, "/{index}/_split/{target}", this);
81+
controller.registerHandler(RestRequest.Method.POST, "/{index}/_split/{target}", this);
82+
}
83+
84+
@Override
85+
public String getName() {
86+
return "split_index_action";
87+
}
88+
89+
@Override
90+
protected ResizeType getResizeType() {
91+
return ResizeType.SPLIT;
92+
}
93+
94+
}
95+
5696
}

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestSplitIndexAction.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)