Skip to content

Commit

Permalink
[REST API compatibility] special handling for _freeze (#79861)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Oct 26, 2021
1 parent 722e653 commit a780594
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ tasks.named("yamlRestTestV7CompatTransform").configure{ task ->
task.replaceValueInMatch("_type", "_doc")
task.addAllowedWarningRegex("\\[types removal\\].*")
task.addAllowedWarningRegexForTest("Including \\[accept_enterprise\\] in get license.*", "Installing enterprise license")

task.replaceValueTextByKeyValue("catch",
'bad_request',
'/It is no longer possible to freeze indices, but existing frozen indices can still be unfrozen/',
"Cannot freeze write index for data stream")
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,62 @@
*/
package org.elasticsearch.xpack.frozen.rest.action;

import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.protocol.xpack.frozen.FreezeRequest;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;

import java.util.List;

import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.GONE;

public final class RestFreezeIndexAction extends BaseRestHandler {

public static final String DEPRECATION_WARNING = "Frozen indices are deprecated because they provide no benefit given improvements "
private static final String FREEZE_REMOVED = "It is no longer possible to freeze indices, but existing frozen indices can still be "
+ "unfrozen";

private static final String UNFREEZE_DEPRECATED = "Frozen indices are deprecated because they provide no benefit given improvements "
+ "in heap memory utilization. They will be removed in a future release.";
private static final RestApiVersion DEPRECATION_VERSION = RestApiVersion.V_8;

@Override
public List<Route> routes() {
return List.of(Route.builder(POST, "/{index}/_unfreeze").deprecated(DEPRECATION_WARNING, DEPRECATION_VERSION).build());
return List.of(
Route.builder(POST, "/{index}/_freeze").deprecated(FREEZE_REMOVED, RestApiVersion.V_7).build(),
Route.builder(POST, "/{index}/_unfreeze").deprecated(UNFREEZE_DEPRECATED, RestApiVersion.V_8).build()
);
}

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
if (request.getRestApiVersion() == RestApiVersion.V_7 && request.path().endsWith("/_freeze")) {
// translate to a get indices request, so that we'll 404 on non-existent indices
final GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
getIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getIndexRequest.masterNodeTimeout()));
getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions()));
return channel -> client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<>(channel) {
@Override
public RestResponse buildResponse(GetIndexResponse getIndexResponse, XContentBuilder builder) throws Exception {
builder.close();
// but if the index *does* exist, we still just respond with 410 -- there's no such thing as _freeze anymore
return new BytesRestResponse(channel, GONE, new UnsupportedOperationException(FREEZE_REMOVED));
}
});
}

FreezeRequest freezeRequest = new FreezeRequest(Strings.splitStringByCommaToArray(request.param("index")));
freezeRequest.timeout(request.paramAsTime("timeout", freezeRequest.timeout()));
freezeRequest.masterNodeTimeout(request.paramAsTime("master_timeout", freezeRequest.masterNodeTimeout()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
setup:
- skip:
version: "9.0.0 - "
reason: "compatible from 8.x to 7.x"
features:
- "headers"
- "warnings_regex"
- do:
index:
index: some-test-index-1
id: 1
body: { foo: bar }

---
"Freezing a non-existent index throws 404":
- do:
headers:
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
indices.freeze:
index: some-test-index-404
catch: missing

---
"Freezing an index throws 410":
- do:
headers:
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
indices.freeze:
index: some-test-index-1
catch: /It is no longer possible to freeze indices, but existing frozen indices can still be unfrozen/

---
"Without compat headers throws 400":
- do:
indices.freeze:
index: some-test-index-1
catch: /no handler found for uri/

0 comments on commit a780594

Please sign in to comment.