Skip to content

Commit 772ce1f

Browse files
authored
Add deprecation warning for Force Merge API (#44903)
This commit adds a deprecation warning in 7.x for the Force Merge API when both only_expunge_deletes and max_num_segments are set in a request. Relates #44761
1 parent 210593d commit 772ce1f

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,10 @@ public void testForceMergeIndex() throws Exception {
13131313
request.onlyExpungeDeletes(true); // <1>
13141314
// end::force-merge-request-only-expunge-deletes
13151315

1316+
// set only expunge deletes back to its default value
1317+
// as it is mutually exclusive with max. num. segments
1318+
request.onlyExpungeDeletes(ForceMergeRequest.Defaults.ONLY_EXPUNGE_DELETES);
1319+
13161320
// tag::force-merge-request-flush
13171321
request.flush(true); // <1>
13181322
// end::force-merge-request-flush

rest-api-spec/src/main/resources/rest-api-spec/test/indices.forcemerge/10_basic.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,24 @@
88
indices.forcemerge:
99
index: testing
1010
max_num_segments: 1
11+
12+
---
13+
"Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set":
14+
- skip:
15+
version: " - 7.3.99"
16+
reason: "deprecation warning about only_expunge_deletes and max_num_segments added in 7.4"
17+
features: "warnings"
18+
19+
- do:
20+
indices.create:
21+
index: test
22+
23+
- do:
24+
warnings:
25+
- 'setting only_expunge_deletes and max_num_segments at the same time is deprecated and will be rejected in a future version'
26+
indices.forcemerge:
27+
index: test
28+
max_num_segments: 10
29+
only_expunge_deletes: true
30+
31+

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
package org.elasticsearch.rest.action.admin.indices;
2121

22+
import org.apache.logging.log4j.LogManager;
2223
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
2324
import org.elasticsearch.action.support.IndicesOptions;
2425
import org.elasticsearch.client.node.NodeClient;
2526
import org.elasticsearch.common.Strings;
27+
import org.elasticsearch.common.logging.DeprecationLogger;
2628
import org.elasticsearch.common.settings.Settings;
2729
import org.elasticsearch.rest.BaseRestHandler;
2830
import org.elasticsearch.rest.RestController;
@@ -35,6 +37,8 @@
3537

3638
public class RestForceMergeAction extends BaseRestHandler {
3739

40+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestForceMergeAction.class));
41+
3842
public RestForceMergeAction(final Settings settings, final RestController controller) {
3943
super(settings);
4044
controller.registerHandler(POST, "/_forcemerge", this);
@@ -53,6 +57,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
5357
mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
5458
mergeRequest.onlyExpungeDeletes(request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes()));
5559
mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush()));
60+
if (mergeRequest.onlyExpungeDeletes() && mergeRequest.maxNumSegments() != ForceMergeRequest.Defaults.MAX_NUM_SEGMENTS) {
61+
deprecationLogger.deprecatedAndMaybeLog("force_merge_expunge_deletes_and_max_num_segments_deprecation",
62+
"setting only_expunge_deletes and max_num_segments at the same time is deprecated and will be rejected in a future version");
63+
}
5664
return channel -> client.admin().indices().forceMerge(mergeRequest, new RestToXContentListener<>(channel));
5765
}
5866

server/src/test/java/org/elasticsearch/action/admin/indices/forcemerge/RestForceMergeActionTests.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@
2626
import org.elasticsearch.common.xcontent.XContentType;
2727
import org.elasticsearch.common.xcontent.json.JsonXContent;
2828
import org.elasticsearch.rest.RestController;
29+
import org.elasticsearch.rest.RestRequest;
2930
import org.elasticsearch.rest.action.admin.indices.RestForceMergeAction;
30-
import org.elasticsearch.test.ESTestCase;
3131
import org.elasticsearch.test.rest.FakeRestChannel;
3232
import org.elasticsearch.test.rest.FakeRestRequest;
33+
import org.elasticsearch.test.rest.RestActionTestCase;
34+
import org.junit.Before;
35+
36+
import java.util.HashMap;
37+
import java.util.Map;
3338

3439
import static org.hamcrest.Matchers.equalTo;
3540
import static org.mockito.Mockito.mock;
3641

37-
public class RestForceMergeActionTests extends ESTestCase {
42+
public class RestForceMergeActionTests extends RestActionTestCase {
43+
44+
@Before
45+
public void setUpAction() {
46+
new RestForceMergeAction(Settings.EMPTY, controller());
47+
}
3848

3949
public void testBodyRejection() throws Exception {
4050
final RestForceMergeAction handler = new RestForceMergeAction(Settings.EMPTY, mock(RestController.class));
@@ -48,4 +58,20 @@ public void testBodyRejection() throws Exception {
4858
assertThat(e.getMessage(), equalTo("request [GET /_forcemerge] does not support having a body"));
4959
}
5060

61+
public void testDeprecationMessage() {
62+
final Map<String, String> params = new HashMap<>();
63+
params.put("only_expunge_deletes", Boolean.TRUE.toString());
64+
params.put("max_num_segments", Integer.toString(randomIntBetween(0, 10)));
65+
params.put("flush", Boolean.toString(randomBoolean()));
66+
67+
final RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
68+
.withPath("/_forcemerge")
69+
.withMethod(RestRequest.Method.POST)
70+
.withParams(params)
71+
.build();
72+
73+
dispatchRequest(request);
74+
assertWarnings("setting only_expunge_deletes and max_num_segments at the same time is deprecated " +
75+
"and will be rejected in a future version");
76+
}
5177
}

0 commit comments

Comments
 (0)