Skip to content

Commit f10735a

Browse files
talevydakrone
authored andcommitted
ILM integration test with full policy (#33402)
- this adds an integration test that runs through a policy with all the actions defined. - adds a test specific to a policy having just a rollover action - bumps the node count to 4
1 parent 90c55f5 commit f10735a

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

x-pack/plugin/ilm/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ dependencies {
2121
check.dependsOn 'qa:with-security:integTestRunner'
2222

2323
integTestCluster {
24-
numNodes = 2
24+
numNodes = 4
2525
clusterName = 'ilm'
2626
setting 'xpack.security.enabled', 'false'
2727
setting 'xpack.watcher.enabled', 'false'
2828
setting 'xpack.monitoring.enabled', 'false'
2929
setting 'xpack.ml.enabled', 'false'
3030
setting 'xpack.ilm.enabled', 'true'
31-
setting 'indices.lifecycle.poll_interval', '500ms'
31+
setting 'indices.lifecycle.poll_interval', '2500ms'
3232
module project(xpackModule('core'))
3333
}
3434

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.http.entity.StringEntity;
1111
import org.elasticsearch.client.Request;
1212
import org.elasticsearch.client.Response;
13+
import org.elasticsearch.client.RestClient;
1314
import org.elasticsearch.client.ResponseException;
1415
import org.elasticsearch.cluster.metadata.IndexMetaData;
1516
import org.elasticsearch.common.Strings;
@@ -18,16 +19,15 @@
1819
import org.elasticsearch.common.xcontent.XContentBuilder;
1920
import org.elasticsearch.common.xcontent.XContentHelper;
2021
import org.elasticsearch.common.xcontent.XContentType;
21-
import org.elasticsearch.common.xcontent.json.JsonXContent;
2222
import org.elasticsearch.test.rest.ESRestTestCase;
2323
import org.elasticsearch.xpack.core.indexlifecycle.AllocateAction;
2424
import org.elasticsearch.xpack.core.indexlifecycle.DeleteAction;
2525
import org.elasticsearch.xpack.core.indexlifecycle.ForceMergeAction;
2626
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction;
2727
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
28-
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
2928
import org.elasticsearch.xpack.core.indexlifecycle.Phase;
3029
import org.elasticsearch.xpack.core.indexlifecycle.ReadOnlyAction;
30+
import org.elasticsearch.xpack.core.indexlifecycle.RolloverAction;
3131
import org.elasticsearch.xpack.core.indexlifecycle.ShrinkAction;
3232
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
3333
import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep;
@@ -36,6 +36,7 @@
3636
import java.io.IOException;
3737
import java.io.InputStream;
3838
import java.util.Collections;
39+
import java.util.HashMap;
3940
import java.util.List;
4041
import java.util.Locale;
4142
import java.util.Map;
@@ -52,25 +53,70 @@ public class TimeSeriesLifecycleActionsIT extends ESRestTestCase {
5253
private String policy;
5354

5455
@Before
55-
public void refreshIndex() throws IOException {
56+
public void refreshIndex() {
5657
index = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
5758
policy = randomAlphaOfLength(5);
58-
Request request = new Request("PUT", "/_cluster/settings");
59-
XContentBuilder pollIntervalEntity = JsonXContent.contentBuilder();
60-
pollIntervalEntity.startObject();
61-
{
62-
pollIntervalEntity.startObject("transient");
63-
{
64-
pollIntervalEntity.field(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, "1s");
65-
}pollIntervalEntity.endObject();
66-
} pollIntervalEntity.endObject();
67-
request.setJsonEntity(Strings.toString(pollIntervalEntity));
68-
assertOK(adminClient().performRequest(request));
6959
}
7060

7161
public static void updatePolicy(String indexName, String policy) throws IOException {
7262
Request request = new Request("PUT", "/" + indexName + "/_ilm/" + policy);
73-
client().performRequest(request);
63+
assertOK(client().performRequest(request));
64+
}
65+
66+
public void testFullPolicy() throws Exception {
67+
String originalIndex = index + "-000001";
68+
String shrunkenOriginalIndex = ShrinkAction.SHRUNKEN_INDEX_PREFIX + originalIndex;
69+
String secondIndex = index + "-000002";
70+
createIndexWithSettings(originalIndex, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 4)
71+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
72+
.put("index.routing.allocation.include._name", "node-0")
73+
.put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias"));
74+
75+
// create policy
76+
Map<String, LifecycleAction> warmActions = new HashMap<>();
77+
warmActions.put(ForceMergeAction.NAME, new ForceMergeAction(1));
78+
warmActions.put(AllocateAction.NAME, new AllocateAction(1, singletonMap("_name", "node-1,node-2"), null, null));
79+
warmActions.put(ShrinkAction.NAME, new ShrinkAction(1));
80+
Map<String, Phase> phases = new HashMap<>();
81+
phases.put("hot", new Phase("hot", TimeValue.ZERO, singletonMap(RolloverAction.NAME,
82+
new RolloverAction(null, null, 1L))));
83+
phases.put("warm", new Phase("warm", TimeValue.ZERO, warmActions));
84+
phases.put("cold", new Phase("cold", TimeValue.ZERO, singletonMap(AllocateAction.NAME,
85+
new AllocateAction(0, singletonMap("_name", "node-3"), null, null))));
86+
phases.put("delete", new Phase("delete", TimeValue.ZERO, singletonMap(DeleteAction.NAME, new DeleteAction())));
87+
LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, phases);
88+
// PUT policy
89+
XContentBuilder builder = jsonBuilder();
90+
lifecyclePolicy.toXContent(builder, null);
91+
final StringEntity entity = new StringEntity(
92+
"{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON);
93+
Request request = new Request("PUT", "_ilm/" + policy);
94+
request.setEntity(entity);
95+
assertOK(client().performRequest(request));
96+
// update policy on index
97+
updatePolicy(originalIndex, policy);
98+
// index document {"foo": "bar"} to trigger rollover
99+
index(client(), originalIndex, "_id", "foo", "bar");
100+
assertBusy(() -> assertTrue(indexExists(secondIndex)));
101+
assertBusy(() -> assertFalse(indexExists(shrunkenOriginalIndex)));
102+
assertBusy(() -> assertFalse(indexExists(originalIndex)));
103+
}
104+
105+
public void testRolloverAction() throws Exception {
106+
String originalIndex = index + "-000001";
107+
String secondIndex = index + "-000002";
108+
createIndexWithSettings(originalIndex, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
109+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
110+
.put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias"));
111+
112+
// create policy
113+
createNewSingletonPolicy("hot", new RolloverAction(null, null, 1L));
114+
// update policy on index
115+
updatePolicy(originalIndex, policy);
116+
// index document {"foo": "bar"} to trigger rollover
117+
index(client(), originalIndex, "_id", "foo", "bar");
118+
assertBusy(() -> assertTrue(indexExists(secondIndex)));
119+
assertBusy(() -> assertTrue(indexExists(originalIndex)));
74120
}
75121

76122
public void testAllocateOnlyAllocation() throws Exception {
@@ -210,12 +256,26 @@ private void createNewSingletonPolicy(String phaseName, LifecycleAction action,
210256

211257
private void createIndexWithSettings(String index, Settings.Builder settings) throws IOException {
212258
// create the test-index index
213-
createIndex(index, settings.build());
259+
Request request = new Request("PUT", "/" + index);
260+
request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings.build())
261+
+ ", \"aliases\" : { \"alias\": { \"is_write_index\": true } } }");
262+
client().performRequest(request);
214263
// wait for the shards to initialize
215264
ensureGreen(index);
216265

217266
}
218267

268+
private static void index(RestClient client, String index, String id, Object... fields) throws IOException {
269+
XContentBuilder document = jsonBuilder().startObject();
270+
for (int i = 0; i < fields.length; i += 2) {
271+
document.field((String) fields[i], fields[i + 1]);
272+
}
273+
document.endObject();
274+
final Request request = new Request("POST", "/" + index + "/_doc/" + id);
275+
request.setJsonEntity(Strings.toString(document));
276+
assertOK(client.performRequest(request));
277+
}
278+
219279
@SuppressWarnings("unchecked")
220280
private Map<String, Object> getOnlyIndexSettings(String index) throws IOException {
221281
Map<String, Object> response = (Map<String, Object>) getIndexSettings(index).get(index);

0 commit comments

Comments
 (0)