Skip to content

Commit

Permalink
Add high-level REST client API for _freeze and _unfreeze
Browse files Browse the repository at this point in the history
This change adds support for `_freeze` and `_unfreeze` to the HLRC

Relates to elastic#34352
  • Loading branch information
s1monw committed Nov 20, 2018
1 parent a5f5ceb commit f1f26ab
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client;

import java.util.Objects;

/**
* Request for the _freeze index API
*/
public final class FreezeIndexRequest implements Validatable {

private final String indices;

/**
* Creates a new freeze index request
* @param indices the index to freeze
*/
public FreezeIndexRequest(String indices) {
this.indices = Objects.requireNonNull(indices);
}

/**
* Returns the indices to freeze
*/
public String getIndices() {
return indices;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,50 @@ public void analyzeAsync(AnalyzeRequest request, RequestOptions options,
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::analyze, options,
AnalyzeResponse::fromXContent, listener, emptySet());
}

/**
* Asynchronously calls the _freeze API
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void freezeAsync(FreezeIndexRequest request, RequestOptions options, ActionListener<ShardAcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::freezeIndex, options,
ShardAcknowledgedResponse::fromXContent, listener, emptySet());
}

/**
* Asynchronously calls the _unfreeze API
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options, ActionListener<ShardAcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::unFreezeIndex, options,
ShardAcknowledgedResponse::fromXContent, listener, emptySet());
}

/**
* Synchronously calls the _freeze API
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public ShardAcknowledgedResponse freeze(FreezeIndexRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::freezeIndex, options,
ShardAcknowledgedResponse::fromXContent, emptySet());
}

/**
* Synchronously calls the _freeze API
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public ShardAcknowledgedResponse unfreeze(UnfreezeIndexRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::unFreezeIndex, options,
ShardAcknowledgedResponse::fromXContent, emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,20 @@ static Request analyze(AnalyzeRequest request) throws IOException {
req.setEntity(RequestConverters.createEntity(request, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
return req;
}

static Request freezeIndex(FreezeIndexRequest freezeIndexRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPart(freezeIndexRequest.getIndices())
.addPathPartAsIs("_freeze")
.build();
return new Request(HttpPost.METHOD_NAME, endpoint);
}

static Request unFreezeIndex(UnfreezeIndexRequest unFreezeIndexRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPart(unFreezeIndexRequest.getIndices())
.addPathPartAsIs("_unfreeze")
.build();
return new Request(HttpPost.METHOD_NAME, endpoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client;

import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

public class ShardAcknowledgedResponse extends AcknowledgedResponse implements Validatable {

private static ConstructingObjectParser<ShardAcknowledgedResponse, Void> buildParser() {

ConstructingObjectParser<ShardAcknowledgedResponse, Void> p = new ConstructingObjectParser<>("freeze", true,
args -> new ShardAcknowledgedResponse((boolean) args[0], (boolean) args[1]));
p.declareBoolean(constructorArg(), new ParseField(AcknowledgedResponse.PARSE_FIELD_NAME));
p.declareBoolean(constructorArg(), new ParseField("shards_acknowledged"));
return p;
}

private static final ConstructingObjectParser<ShardAcknowledgedResponse, Void> PARSER = buildParser();

private final boolean shardsAcknowledged;

public ShardAcknowledgedResponse(boolean acknowledged, boolean shardsAcknowledged) {
super(acknowledged);
this.shardsAcknowledged = shardsAcknowledged;
}

public boolean isShardsAcknowledged() {
return shardsAcknowledged;
}

public static ShardAcknowledgedResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client;

import java.util.Objects;

/**
* Request for the _unfreeze index API
*/
public final class UnfreezeIndexRequest implements Validatable {

private final String indices;

/**
* Creates a new unfreeze index request
* @param indices the index to unfreeze
*/
public UnfreezeIndexRequest(String indices) {
this.indices = Objects.requireNonNull(indices);
}

/**
* Returns the indices to unfreeze
*/
public String getIndices() {
return indices;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,19 @@ public void testAnalyze() throws Exception {
AnalyzeResponse detailsResponse = execute(detailsRequest, client.indices()::analyze, client.indices()::analyzeAsync);

assertNotNull(detailsResponse.detail());
}

public void testFreezeAndUnfreeze() throws IOException {
createIndex("test", Settings.EMPTY);
RestHighLevelClient client = highLevelClient();

ShardAcknowledgedResponse freeze = execute(new FreezeIndexRequest("test"), client.indices()::freeze, client.indices()::freezeAsync);
assertTrue(freeze.isShardsAcknowledged());
assertTrue(freeze.isAcknowledged());

ShardAcknowledgedResponse unfreeze = execute(new UnfreezeIndexRequest("test"), client.indices()::unfreeze,
client.indices()::unfreezeAsync);
assertTrue(unfreeze.isShardsAcknowledged());
assertTrue(unfreeze.isAcknowledged());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ public void testApiNamingConventions() throws Exception {
apiName.startsWith("migration.") == false &&
apiName.startsWith("security.") == false &&
apiName.startsWith("index_lifecycle.") == false &&
apiName.startsWith("ccr.") == false) {
apiName.startsWith("ccr.") == false &&
apiName.endsWith("freeze") == false) {
apiNotFound.add(apiName);
}
}
Expand Down

0 comments on commit f1f26ab

Please sign in to comment.