Skip to content

Commit cb901bb

Browse files
authored
Preserve ILM operation mode when creating new lifecycles (#38134) (#38230)
There was a bug where creating a new policy would start the ILM service, even if it was stopped. This change ensures that there is no change to the existing operation mode Backport of #38134.
1 parent f107ac6 commit cb901bb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.cluster.service.ClusterService;
1919
import org.elasticsearch.common.inject.Inject;
2020
import org.elasticsearch.common.settings.Settings;
21-
import org.elasticsearch.xpack.core.indexlifecycle.OperationMode;
2221
import org.elasticsearch.threadpool.ThreadPool;
2322
import org.elasticsearch.transport.TransportService;
2423
import org.elasticsearch.xpack.core.ClientHelper;
@@ -89,7 +88,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
8988
LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders,
9089
nextVersion, Instant.now().toEpochMilli());
9190
newPolicies.put(lifecyclePolicyMetadata.getName(), lifecyclePolicyMetadata);
92-
IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, OperationMode.RUNNING);
91+
IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, currentMetadata.getOperationMode());
9392
newState.metaData(MetaData.builder(currentState.getMetaData())
9493
.putCustom(IndexLifecycleMetadata.TYPE, newMetadata).build());
9594
return newState.build();

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

+37
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@
3737
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
3838
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleType;
3939
import org.elasticsearch.xpack.core.indexlifecycle.MockAction;
40+
import org.elasticsearch.xpack.core.indexlifecycle.OperationMode;
4041
import org.elasticsearch.xpack.core.indexlifecycle.Phase;
4142
import org.elasticsearch.xpack.core.indexlifecycle.PhaseExecutionInfo;
4243
import org.elasticsearch.xpack.core.indexlifecycle.Step;
44+
import org.elasticsearch.xpack.core.indexlifecycle.StopILMRequest;
4345
import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep;
4446
import org.elasticsearch.xpack.core.indexlifecycle.action.ExplainLifecycleAction;
4547
import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction;
48+
import org.elasticsearch.xpack.core.indexlifecycle.action.GetStatusAction;
4649
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction;
50+
import org.elasticsearch.xpack.core.indexlifecycle.action.StopILMAction;
4751
import org.junit.Before;
4852

4953
import java.io.IOException;
@@ -364,6 +368,39 @@ public void testMasterFailover() throws Exception {
364368
});
365369
}
366370

371+
public void testCreatePolicyWhenStopped() throws Exception {
372+
// start master node
373+
logger.info("Starting server1");
374+
final String server_1 = internalCluster().startNode();
375+
final String node1 = getLocalNodeId(server_1);
376+
377+
assertAcked(client().execute(StopILMAction.INSTANCE, new StopILMRequest()).get());
378+
assertBusy(() -> assertThat(
379+
client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request()).get().getMode(),
380+
equalTo(OperationMode.STOPPED)));
381+
382+
logger.info("Creating lifecycle [test_lifecycle]");
383+
PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy);
384+
long lowerBoundModifiedDate = Instant.now().toEpochMilli();
385+
PutLifecycleAction.Response putLifecycleResponse = client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get();
386+
assertAcked(putLifecycleResponse);
387+
long upperBoundModifiedDate = Instant.now().toEpochMilli();
388+
389+
// assert version and modified_date
390+
GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE,
391+
new GetLifecycleAction.Request()).get();
392+
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
393+
GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0);
394+
assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy));
395+
assertThat(responseItem.getVersion(), equalTo(1L));
396+
long actualModifiedDate = Instant.parse(responseItem.getModifiedDate()).toEpochMilli();
397+
assertThat(actualModifiedDate,
398+
is(both(greaterThanOrEqualTo(lowerBoundModifiedDate)).and(lessThanOrEqualTo(upperBoundModifiedDate))));
399+
// assert ILM is still stopped
400+
GetStatusAction.Response statusResponse = client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request()).get();
401+
assertThat(statusResponse.getMode(), equalTo(OperationMode.STOPPED));
402+
}
403+
367404
public void testPollIntervalUpdate() throws Exception {
368405
TimeValue pollInterval = TimeValue.timeValueSeconds(randomLongBetween(1, 5));
369406
final String server_1 = internalCluster().startMasterOnlyNode(

0 commit comments

Comments
 (0)