mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-25 22:36:20 +00:00
HLRC: Add Put Lifecycle Policy API to HLRC (#33250)
* Move Lifecycle objects from protcol to core * HLRC: Add Put Lifecycle API to the HLRC * Use HLRC Put for ILM integration tests
This commit is contained in:
parent
4257d05869
commit
246545173a
@ -22,6 +22,7 @@ package org.elasticsearch.client;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
||||
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
|
||||
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
|
||||
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
|
||||
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
|
||||
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
|
||||
@ -40,6 +41,35 @@ public class IndexLifecycleClient {
|
||||
this.restHighLevelClient = restHighLevelClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or modify a lifecycle definition
|
||||
* See <a href="https://fix-me-when-we-have-docs.com">
|
||||
* the docs</a> for more.
|
||||
* @param request the request
|
||||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
||||
* @return the response
|
||||
* @throws IOException in case there is a problem sending the request or parsing back the response
|
||||
*/
|
||||
public AcknowledgedResponse putLifecyclePolicy(PutLifecyclePolicyRequest request,
|
||||
RequestOptions options) throws IOException {
|
||||
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::putLifecyclePolicy, options,
|
||||
AcknowledgedResponse::fromXContent, emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously create or modify a lifecycle definition
|
||||
* See <a href="https://fix-me-when-we-have-docs.com">
|
||||
* the docs</a> for more.
|
||||
* @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 putLifecyclePolicyAsync(PutLifecyclePolicyRequest request, RequestOptions options,
|
||||
ActionListener<AcknowledgedResponse> listener) {
|
||||
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::putLifecyclePolicy, options,
|
||||
AcknowledgedResponse::fromXContent, listener, emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a lifecycle definition
|
||||
* See <a href="https://fix-me-when-we-have-docs.com">
|
||||
|
@ -88,6 +88,7 @@ import org.elasticsearch.action.support.ActiveShardCount;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.action.support.WriteRequest;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
|
||||
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
|
||||
import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
@ -1191,6 +1192,19 @@ final class RequestConverters {
|
||||
return request;
|
||||
}
|
||||
|
||||
static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException {
|
||||
String endpoint = new EndpointBuilder()
|
||||
.addPathPartAsIs("_ilm")
|
||||
.addPathPartAsIs(putLifecycleRequest.getName())
|
||||
.build();
|
||||
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
|
||||
Params params = new Params(request);
|
||||
params.withMasterTimeout(putLifecycleRequest.masterNodeTimeout());
|
||||
params.withTimeout(putLifecycleRequest.timeout());
|
||||
request.setEntity(createEntity(putLifecycleRequest, REQUEST_BODY_CONTENT_TYPE));
|
||||
return request;
|
||||
}
|
||||
|
||||
static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) {
|
||||
Request request = new Request(HttpDelete.METHOD_NAME,
|
||||
new EndpointBuilder()
|
||||
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
/**
|
||||
* interface for index lifecycle management actions
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.action.admin.indices.shrink.ShrinkAction;
|
||||
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.client.TimedRequest;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.xcontent.ToXContentObject;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PutLifecyclePolicyRequest extends TimedRequest implements ToXContentObject {
|
||||
|
||||
private final LifecyclePolicy policy;
|
||||
|
||||
public PutLifecyclePolicyRequest(LifecyclePolicy policy) {
|
||||
if (policy == null) {
|
||||
throw new IllegalArgumentException("policy definition cannot be null");
|
||||
}
|
||||
if (Strings.isNullOrEmpty(policy.getName())) {
|
||||
throw new IllegalArgumentException("policy name must be present");
|
||||
}
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return policy.getName();
|
||||
}
|
||||
|
||||
public LifecyclePolicy getLifecyclePolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field("policy", policy);
|
||||
builder.endObject();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PutLifecyclePolicyRequest that = (PutLifecyclePolicyRequest) o;
|
||||
return Objects.equals(getLifecyclePolicy(), that.getLifecyclePolicy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getLifecyclePolicy());
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
@ -19,15 +19,22 @@
|
||||
|
||||
package org.elasticsearch.client;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.nio.entity.NStringEntity;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
|
||||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
|
||||
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
||||
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
|
||||
import org.elasticsearch.client.indexlifecycle.AllocateAction;
|
||||
import org.elasticsearch.client.indexlifecycle.DeleteAction;
|
||||
import org.elasticsearch.client.indexlifecycle.ForceMergeAction;
|
||||
import org.elasticsearch.client.indexlifecycle.LifecycleAction;
|
||||
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
|
||||
import org.elasticsearch.client.indexlifecycle.Phase;
|
||||
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
|
||||
import org.elasticsearch.client.indexlifecycle.RolloverAction;
|
||||
import org.elasticsearch.client.indexlifecycle.ShrinkAction;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
|
||||
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
|
||||
import org.elasticsearch.protocol.xpack.indexlifecycle.IndexLifecycleExplainResponse;
|
||||
@ -38,8 +45,11 @@ import org.elasticsearch.protocol.xpack.indexlifecycle.StopILMRequest;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.client.indexlifecycle.LifecyclePolicyTests.createRandomPolicy;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
@ -47,61 +57,15 @@ import static org.hamcrest.Matchers.is;
|
||||
public class IndexLifecycleIT extends ESRestHighLevelClientTestCase {
|
||||
|
||||
public void testSetIndexLifecyclePolicy() throws Exception {
|
||||
String policy = randomAlphaOfLength(10);
|
||||
|
||||
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
|
||||
String jsonString = "{\n" +
|
||||
" \"policy\": {\n" +
|
||||
" \"phases\": {\n" +
|
||||
" \"hot\": {\n" +
|
||||
" \"after\": \"60s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"rollover\": {\n" +
|
||||
" \"max_age\": \"500s\"\n" +
|
||||
" } \n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"warm\": {\n" +
|
||||
" \"after\": \"1000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"require\": { \"_name\": \"node-1\" },\n" +
|
||||
" \"include\": {},\n" +
|
||||
" \"exclude\": {}\n" +
|
||||
" },\n" +
|
||||
" \"shrink\": {\n" +
|
||||
" \"number_of_shards\": 1\n" +
|
||||
" },\n" +
|
||||
" \"forcemerge\": {\n" +
|
||||
" \"max_num_segments\": 1000\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"cold\": {\n" +
|
||||
" \"after\": \"2000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"number_of_replicas\": 0\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"delete\": {\n" +
|
||||
" \"after\": \"3000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"delete\": {}\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
Request request = new Request("PUT", "/_ilm/" + policy);
|
||||
request.setEntity(entity);
|
||||
client().performRequest(request);
|
||||
String policyName = randomAlphaOfLength(10);
|
||||
LifecyclePolicy policy = createRandomPolicy(policyName);
|
||||
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
|
||||
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
|
||||
|
||||
createIndex("foo", Settings.builder().put("index.lifecycle.name", "bar").build());
|
||||
createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build());
|
||||
SetIndexLifecyclePolicyRequest req = new SetIndexLifecyclePolicyRequest(policy, "foo", "baz");
|
||||
SetIndexLifecyclePolicyRequest req = new SetIndexLifecyclePolicyRequest(policyName, "foo", "baz");
|
||||
SetIndexLifecyclePolicyResponse response = execute(req, highLevelClient().indexLifecycle()::setIndexLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::setIndexLifecyclePolicyAsync);
|
||||
assertThat(response.hasFailures(), is(false));
|
||||
@ -109,61 +73,16 @@ public class IndexLifecycleIT extends ESRestHighLevelClientTestCase {
|
||||
|
||||
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo", "baz");
|
||||
GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
|
||||
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policy));
|
||||
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policy));
|
||||
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policyName));
|
||||
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
|
||||
}
|
||||
|
||||
public void testStartStopILM() throws Exception {
|
||||
String policy = randomAlphaOfLength(10);
|
||||
|
||||
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
|
||||
String jsonString = "{\n" +
|
||||
" \"policy\": {\n" +
|
||||
" \"phases\": {\n" +
|
||||
" \"hot\": {\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"rollover\": {\n" +
|
||||
" \"max_age\": \"50d\"\n" +
|
||||
" } \n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"warm\": {\n" +
|
||||
" \"after\": \"1000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"require\": { \"_name\": \"node-1\" },\n" +
|
||||
" \"include\": {},\n" +
|
||||
" \"exclude\": {}\n" +
|
||||
" },\n" +
|
||||
" \"shrink\": {\n" +
|
||||
" \"number_of_shards\": 1\n" +
|
||||
" },\n" +
|
||||
" \"forcemerge\": {\n" +
|
||||
" \"max_num_segments\": 1000\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"cold\": {\n" +
|
||||
" \"after\": \"2000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"number_of_replicas\": 0\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"delete\": {\n" +
|
||||
" \"after\": \"3000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"delete\": {}\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
Request request = new Request("PUT", "/_ilm/" + policy);
|
||||
request.setEntity(entity);
|
||||
client().performRequest(request);
|
||||
String policyName = randomAlphaOfLength(10);
|
||||
LifecyclePolicy policy = createRandomPolicy(policyName);
|
||||
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
|
||||
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
|
||||
|
||||
createIndex("foo", Settings.builder().put("index.lifecycle.name", "bar").build());
|
||||
createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build());
|
||||
@ -201,65 +120,39 @@ public class IndexLifecycleIT extends ESRestHighLevelClientTestCase {
|
||||
}
|
||||
|
||||
public void testExplainLifecycle() throws Exception {
|
||||
String policy = randomAlphaOfLength(10);
|
||||
Map<String, Phase> lifecyclePhases = new HashMap<>();
|
||||
Map<String, LifecycleAction> hotActions = Collections.singletonMap(
|
||||
RolloverAction.NAME,
|
||||
new RolloverAction(null, TimeValue.timeValueHours(50 * 24), null));
|
||||
lifecyclePhases.put("hot", new Phase("hot", randomFrom(TimeValue.ZERO, null), hotActions));
|
||||
|
||||
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
|
||||
String jsonString = "{\n" +
|
||||
" \"policy\": {\n" +
|
||||
" \"phases\": {\n" +
|
||||
" \"hot\": {\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"rollover\": {\n" +
|
||||
" \"max_age\": \"50d\"\n" +
|
||||
" } \n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"warm\": {\n" +
|
||||
" \"after\": \"1000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"require\": { \"_name\": \"node-1\" },\n" +
|
||||
" \"include\": {},\n" +
|
||||
" \"exclude\": {}\n" +
|
||||
" },\n" +
|
||||
" \"shrink\": {\n" +
|
||||
" \"number_of_shards\": 1\n" +
|
||||
" },\n" +
|
||||
" \"forcemerge\": {\n" +
|
||||
" \"max_num_segments\": 1000\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"cold\": {\n" +
|
||||
" \"after\": \"2000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"number_of_replicas\": 0\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"delete\": {\n" +
|
||||
" \"after\": \"3000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"delete\": {}\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
Request request = new Request("PUT", "/_ilm/" + policy);
|
||||
request.setEntity(entity);
|
||||
client().performRequest(request);
|
||||
Map<String, LifecycleAction> warmActions = new HashMap<>();
|
||||
warmActions.put(AllocateAction.NAME, new AllocateAction(null, null, null, Collections.singletonMap("_name", "node-1")));
|
||||
warmActions.put(ShrinkAction.NAME, new ShrinkAction(1));
|
||||
warmActions.put(ForceMergeAction.NAME, new ForceMergeAction(1000));
|
||||
lifecyclePhases.put("warm", new Phase("warm", TimeValue.timeValueSeconds(1000), warmActions));
|
||||
|
||||
createIndex("foo", Settings.builder().put("index.lifecycle.name", policy).build());
|
||||
createIndex("baz", Settings.builder().put("index.lifecycle.name", policy).build());
|
||||
Map<String, LifecycleAction> coldActions = new HashMap<>();
|
||||
coldActions.put(AllocateAction.NAME, new AllocateAction(0, null, null, null));
|
||||
lifecyclePhases.put("cold", new Phase("cold", TimeValue.timeValueSeconds(2000), coldActions));
|
||||
|
||||
Map<String, LifecycleAction> deleteActions = Collections.singletonMap(DeleteAction.NAME, new DeleteAction());
|
||||
lifecyclePhases.put("delete", new Phase("delete", TimeValue.timeValueSeconds(3000), deleteActions));
|
||||
|
||||
LifecyclePolicy policy = new LifecyclePolicy(randomAlphaOfLength(10), lifecyclePhases);
|
||||
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
|
||||
AcknowledgedResponse putResponse = execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync);
|
||||
assertTrue(putResponse.isAcknowledged());
|
||||
|
||||
createIndex("foo", Settings.builder().put("index.lifecycle.name", policy.getName()).build());
|
||||
createIndex("baz", Settings.builder().put("index.lifecycle.name", policy.getName()).build());
|
||||
createIndex("squash", Settings.EMPTY);
|
||||
assertBusy(() -> {
|
||||
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo", "baz");
|
||||
GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
|
||||
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policy));
|
||||
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policy));
|
||||
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policy.getName()));
|
||||
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policy.getName()));
|
||||
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.phase"), equalTo("hot"));
|
||||
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.phase"), equalTo("hot"));
|
||||
});
|
||||
@ -291,63 +184,18 @@ public class IndexLifecycleIT extends ESRestHighLevelClientTestCase {
|
||||
}
|
||||
|
||||
public void testDeleteLifecycle() throws IOException {
|
||||
String policy = randomAlphaOfLength(10);
|
||||
String policyName = randomAlphaOfLength(10);
|
||||
LifecyclePolicy policy = createRandomPolicy(policyName);
|
||||
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
|
||||
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
|
||||
|
||||
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
|
||||
String jsonString = "{\n" +
|
||||
" \"policy\": {\n" +
|
||||
" \"phases\": {\n" +
|
||||
" \"hot\": {\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"rollover\": {\n" +
|
||||
" \"max_age\": \"50d\"\n" +
|
||||
" } \n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"warm\": {\n" +
|
||||
" \"after\": \"1000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"require\": { \"_name\": \"node-1\" },\n" +
|
||||
" \"include\": {},\n" +
|
||||
" \"exclude\": {}\n" +
|
||||
" },\n" +
|
||||
" \"shrink\": {\n" +
|
||||
" \"number_of_shards\": 1\n" +
|
||||
" },\n" +
|
||||
" \"forcemerge\": {\n" +
|
||||
" \"max_num_segments\": 1000\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"cold\": {\n" +
|
||||
" \"after\": \"2000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"allocate\": {\n" +
|
||||
" \"number_of_replicas\": 0\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"delete\": {\n" +
|
||||
" \"after\": \"3000s\",\n" +
|
||||
" \"actions\": {\n" +
|
||||
" \"delete\": {}\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
Request request = new Request("PUT", "/_ilm/" + policy);
|
||||
request.setEntity(entity);
|
||||
client().performRequest(request);
|
||||
|
||||
DeleteLifecyclePolicyRequest deleteRequest = new DeleteLifecyclePolicyRequest(policy);
|
||||
DeleteLifecyclePolicyRequest deleteRequest = new DeleteLifecyclePolicyRequest(policy.getName());
|
||||
assertAcked(execute(deleteRequest, highLevelClient().indexLifecycle()::deleteLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::deleteLifecyclePolicyAsync));
|
||||
|
||||
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
|
||||
Request getLifecycle = new Request("GET", "/_ilm/" + policy);
|
||||
Request getLifecycle = new Request("GET", "/_ilm/" + policyName);
|
||||
try {
|
||||
client().performRequest(getLifecycle);
|
||||
fail("index should not exist after being deleted");
|
||||
@ -355,4 +203,18 @@ public class IndexLifecycleIT extends ESRestHighLevelClientTestCase {
|
||||
assertEquals(404, ex.getResponse().getStatusLine().getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPutLifecycle() throws IOException {
|
||||
String name = randomAlphaOfLengthBetween(5, 20);
|
||||
LifecyclePolicy policy = createRandomPolicy(name);
|
||||
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
|
||||
|
||||
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
|
||||
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
|
||||
|
||||
// TODO: NORELEASE convert this to using the high level client once there are APIs for it
|
||||
Request getLifecycle = new Request("GET", "/_ilm/" + name);
|
||||
Response response = client().performRequest(getLifecycle);
|
||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
}
|
||||
|
@ -97,6 +97,8 @@ import org.elasticsearch.action.support.master.MasterNodeRequest;
|
||||
import org.elasticsearch.action.support.replication.ReplicationRequest;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
|
||||
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
|
||||
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
|
||||
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
|
||||
import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.common.CheckedBiConsumer;
|
||||
@ -184,6 +186,7 @@ import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE;
|
||||
import static org.elasticsearch.client.RequestConverters.enforceSameContentType;
|
||||
import static org.elasticsearch.client.indexlifecycle.LifecyclePolicyTests.createRandomPolicy;
|
||||
import static org.elasticsearch.index.RandomCreateIndexGenerator.randomAliases;
|
||||
import static org.elasticsearch.index.RandomCreateIndexGenerator.randomCreateIndexRequest;
|
||||
import static org.elasticsearch.index.RandomCreateIndexGenerator.randomIndexSettings;
|
||||
@ -2699,6 +2702,20 @@ public class RequestConvertersTests extends ESTestCase {
|
||||
assertToXContentBody(graphExploreRequest, request.getEntity());
|
||||
}
|
||||
|
||||
public void testPutLifecyclePolicy() throws Exception {
|
||||
String name = randomAlphaOfLengthBetween(2, 20);
|
||||
LifecyclePolicy policy = createRandomPolicy(name);
|
||||
PutLifecyclePolicyRequest req = new PutLifecyclePolicyRequest(policy);
|
||||
Map<String, String> expectedParams = new HashMap<>();
|
||||
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams);
|
||||
setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams);
|
||||
|
||||
Request request = RequestConverters.putLifecyclePolicy(req);
|
||||
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
|
||||
assertEquals("/_ilm/" + name, request.getEndpoint());
|
||||
assertEquals(expectedParams, request.getParameters());
|
||||
}
|
||||
|
||||
public void testDeleteLifecycle() {
|
||||
String lifecycleName = randomAlphaOfLengthBetween(2,20);
|
||||
DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(lifecycleName);
|
||||
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractXContentTestCase;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractXContentTestCase;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.DeprecationHandler;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterModule;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
@ -74,48 +74,7 @@ public class LifecyclePolicyTests extends AbstractXContentTestCase<LifecyclePoli
|
||||
@Override
|
||||
protected LifecyclePolicy createTestInstance() {
|
||||
lifecycleName = randomAlphaOfLength(5);
|
||||
List<String> phaseNames = randomSubsetOf(Arrays.asList("hot", "warm", "cold", "delete"));
|
||||
Map<String, Phase> phases = new HashMap<>(phaseNames.size());
|
||||
Function<String, Set<String>> validActions = (phase) -> {
|
||||
switch (phase) {
|
||||
case "hot":
|
||||
return VALID_HOT_ACTIONS;
|
||||
case "warm":
|
||||
return VALID_WARM_ACTIONS;
|
||||
case "cold":
|
||||
return VALID_COLD_ACTIONS;
|
||||
case "delete":
|
||||
return VALID_DELETE_ACTIONS;
|
||||
default:
|
||||
throw new IllegalArgumentException("invalid phase [" + phase + "]");
|
||||
}};
|
||||
Function<String, LifecycleAction> randomAction = (action) -> {
|
||||
switch (action) {
|
||||
case AllocateAction.NAME:
|
||||
return AllocateActionTests.randomInstance();
|
||||
case DeleteAction.NAME:
|
||||
return new DeleteAction();
|
||||
case ForceMergeAction.NAME:
|
||||
return ForceMergeActionTests.randomInstance();
|
||||
case ReadOnlyAction.NAME:
|
||||
return new ReadOnlyAction();
|
||||
case RolloverAction.NAME:
|
||||
return RolloverActionTests.randomInstance();
|
||||
case ShrinkAction.NAME:
|
||||
return ShrinkActionTests.randomInstance();
|
||||
default:
|
||||
throw new IllegalArgumentException("invalid action [" + action + "]");
|
||||
}};
|
||||
for (String phase : phaseNames) {
|
||||
TimeValue after = TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after");
|
||||
Map<String, LifecycleAction> actions = new HashMap<>();
|
||||
List<String> actionNames = randomSubsetOf(validActions.apply(phase));
|
||||
for (String action : actionNames) {
|
||||
actions.put(action, randomAction.apply(action));
|
||||
}
|
||||
phases.put(phase, new Phase(phase, after, actions));
|
||||
}
|
||||
return new LifecyclePolicy(lifecycleName, phases);
|
||||
return createRandomPolicy(lifecycleName);
|
||||
}
|
||||
|
||||
public void testValidatePhases() {
|
||||
@ -218,6 +177,51 @@ public class LifecyclePolicyTests extends AbstractXContentTestCase<LifecyclePoli
|
||||
}
|
||||
}
|
||||
|
||||
public static LifecyclePolicy createRandomPolicy(String lifecycleName) {
|
||||
List<String> phaseNames = randomSubsetOf(Arrays.asList("hot", "warm", "cold", "delete"));
|
||||
Map<String, Phase> phases = new HashMap<>(phaseNames.size());
|
||||
Function<String, Set<String>> validActions = (phase) -> {
|
||||
switch (phase) {
|
||||
case "hot":
|
||||
return VALID_HOT_ACTIONS;
|
||||
case "warm":
|
||||
return VALID_WARM_ACTIONS;
|
||||
case "cold":
|
||||
return VALID_COLD_ACTIONS;
|
||||
case "delete":
|
||||
return VALID_DELETE_ACTIONS;
|
||||
default:
|
||||
throw new IllegalArgumentException("invalid phase [" + phase + "]");
|
||||
}};
|
||||
Function<String, LifecycleAction> randomAction = (action) -> {
|
||||
switch (action) {
|
||||
case AllocateAction.NAME:
|
||||
return AllocateActionTests.randomInstance();
|
||||
case DeleteAction.NAME:
|
||||
return new DeleteAction();
|
||||
case ForceMergeAction.NAME:
|
||||
return ForceMergeActionTests.randomInstance();
|
||||
case ReadOnlyAction.NAME:
|
||||
return new ReadOnlyAction();
|
||||
case RolloverAction.NAME:
|
||||
return RolloverActionTests.randomInstance();
|
||||
case ShrinkAction.NAME:
|
||||
return ShrinkActionTests.randomInstance();
|
||||
default:
|
||||
throw new IllegalArgumentException("invalid action [" + action + "]");
|
||||
}};
|
||||
for (String phase : phaseNames) {
|
||||
TimeValue after = TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after");
|
||||
Map<String, LifecycleAction> actions = new HashMap<>();
|
||||
List<String> actionNames = randomSubsetOf(validActions.apply(phase));
|
||||
for (String action : actionNames) {
|
||||
actions.put(action, randomAction.apply(action));
|
||||
}
|
||||
phases.put(phase, new Phase(phase, after, actions));
|
||||
}
|
||||
return new LifecyclePolicy(lifecycleName, phases);
|
||||
}
|
||||
|
||||
private LifecycleAction getTestAction(String actionName) {
|
||||
switch (actionName) {
|
||||
case AllocateAction.NAME:
|
@ -17,7 +17,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterModule;
|
||||
import org.elasticsearch.common.ParseField;
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import static org.elasticsearch.client.indexlifecycle.LifecyclePolicyTests.createRandomPolicy;
|
||||
|
||||
public class PutLifecyclePolicyRequestTests extends ESTestCase {
|
||||
|
||||
private PutLifecyclePolicyRequest createTestInstance() {
|
||||
return new PutLifecyclePolicyRequest(createRandomPolicy(randomAlphaOfLengthBetween(5, 20)));
|
||||
}
|
||||
|
||||
public void testValidation() {
|
||||
PutLifecyclePolicyRequest req = createTestInstance();
|
||||
assertFalse(req.validate().isPresent());
|
||||
}
|
||||
|
||||
public void testNullPolicy() {
|
||||
expectThrows(IllegalArgumentException.class, () -> new PutLifecyclePolicyRequest(null));
|
||||
}
|
||||
|
||||
public void testNullPolicyName() {
|
||||
expectThrows(IllegalArgumentException.class, () -> new PutLifecyclePolicyRequest(createRandomPolicy(randomFrom("", null))));
|
||||
}
|
||||
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractXContentTestCase;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.unit.ByteSizeUnit;
|
||||
import org.elasticsearch.common.unit.ByteSizeValue;
|
@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.protocol.xpack.indexlifecycle;
|
||||
package org.elasticsearch.client.indexlifecycle;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.test.AbstractXContentTestCase;
|
Loading…
x
Reference in New Issue
Block a user