Skip to content

Commit d50de84

Browse files
committed
Don't run async ascions when ILM is stopped
1 parent 54a2472 commit d50de84

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void refreshIndex() {
5151
index = "index-" + randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
5252
policy = "policy-" + randomAlphaOfLength(5);
5353
alias = "alias-" + randomAlphaOfLength(5);
54+
logger.info("--> running [{}] with index [{}], alias [{}] and policy [{}]", getTestName(), index, alias, policy);
5455
}
5556

5657
public void testMoveToAllocateStep() throws Exception {
@@ -245,6 +246,63 @@ public void testMoveToStepRereadsPolicy() throws Exception {
245246
assertBusy(() -> { indexExists("test-000002"); });
246247
}
247248

249+
public void test() throws Exception {
250+
String originalIndex = index + "-000001";
251+
// create policy
252+
// createFullPolicy(client(), policy, TimeValue.timeValueHours(10));
253+
// createNewSingletonPolicy(client(), policy, "hot", new ReadOnlyAction(), TimeValue.timeValueHours(1));
254+
Request createPolicyRequest = new Request("PUT", "_ilm/policy/" + policy);
255+
createPolicyRequest.setJsonEntity("""
256+
{
257+
"policy": {
258+
"phases": {
259+
"hot": {
260+
"actions": {
261+
"rollover" : {
262+
"max_age": "1h"
263+
},
264+
"readonly": {}
265+
}
266+
}
267+
}
268+
}
269+
}""");
270+
client().performRequest(createPolicyRequest);
271+
272+
createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy));
273+
274+
// Wait for ILM to do everything it can for this index
275+
assertBusy(() -> assertEquals(new StepKey("hot", "rollover", "check-rollover-ready"), getStepKeyForIndex(client(), originalIndex)));
276+
277+
// Stop ILM
278+
client().performRequest(new Request("POST", "/_ilm/stop"));
279+
280+
// move to a step
281+
Request moveToStepRequest = new Request("POST", "_ilm/move/" + originalIndex);
282+
moveToStepRequest.setJsonEntity("""
283+
{
284+
"current_step": {
285+
"phase": "hot",
286+
"action": "rollover",
287+
"name": "check-rollover-ready"
288+
},
289+
"next_step": {
290+
"phase": "hot",
291+
"action": "readonly",
292+
"name": "readonly"
293+
}
294+
}""");
295+
client().performRequest(moveToStepRequest);
296+
Thread.sleep(5000);
297+
assertTrue(getStepKeyForIndex(client(), originalIndex).equals(new StepKey("hot", "readonly", "readonly")));
298+
299+
// Restart ILM
300+
client().performRequest(new Request("POST", "/_ilm/start"));
301+
302+
// Make sure we actually complete the remainder of the policy after ILM is started again
303+
assertBusy(() -> assertEquals(new StepKey("hot", "complete", "complete"), getStepKeyForIndex(client(), originalIndex)));
304+
}
305+
248306
public void testMoveToStepWithInvalidNextStep() throws Exception {
249307
createNewSingletonPolicy(client(), policy, "delete", DeleteAction.WITH_SNAPSHOT_DELETE, TimeValue.timeValueDays(100));
250308
createIndexWithSettings(

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.xpack.core.ilm.ClusterStateWaitStep;
3434
import org.elasticsearch.xpack.core.ilm.ErrorStep;
3535
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
36+
import org.elasticsearch.xpack.core.ilm.OperationMode;
3637
import org.elasticsearch.xpack.core.ilm.PhaseCompleteStep;
3738
import org.elasticsearch.xpack.core.ilm.Step;
3839
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
@@ -48,6 +49,7 @@
4849

4950
import static org.elasticsearch.core.Strings.format;
5051
import static org.elasticsearch.index.IndexSettings.LIFECYCLE_ORIGINATION_DATE;
52+
import static org.elasticsearch.xpack.core.ilm.LifecycleOperationMetadata.currentILMMode;
5153

5254
class IndexLifecycleRunner {
5355
private static final Logger logger = LogManager.getLogger(IndexLifecycleRunner.class);
@@ -308,6 +310,12 @@ void onErrorMaybeRetryFailedStep(ProjectId projectId, String policy, StepKey cur
308310
void maybeRunAsyncAction(ProjectState state, IndexMetadata indexMetadata, String policy, StepKey expectedStepKey) {
309311
final var projectId = state.projectId();
310312
String index = indexMetadata.getIndex().getName();
313+
OperationMode currentMode = currentILMMode(state.metadata());
314+
if (OperationMode.RUNNING.equals(currentMode) == false) {
315+
logger.info("[{}] skipping policy [{}] because ILM is [{}]", index, policy, currentMode);
316+
return;
317+
}
318+
311319
if (LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(indexMetadata.getSettings())) {
312320
logger.info("[{}] skipping policy [{}] because [{}] is true", index, policy, LifecycleSettings.LIFECYCLE_SKIP);
313321
return;

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,19 @@ public void clusterChanged(ClusterChangedEvent event) {
333333
cancelJob();
334334
policyRegistry.clear();
335335
}
336+
} else if (this.isMaster) {
337+
for (ProjectMetadata project : event.state().metadata().projects().values()) {
338+
final var previousProject = event.previousState().metadata().projects().get(project.id());
339+
if (previousProject == null) {
340+
continue;
341+
}
342+
final OperationMode currentMode = currentILMMode(project);
343+
final OperationMode previousMode = currentILMMode(previousProject);
344+
if (currentMode == OperationMode.RUNNING && previousMode != OperationMode.RUNNING) {
345+
// We just changed to RUNNING mode, so kick off any async actions that may not have run while not in RUNNING mode
346+
onMaster(event.state().projectState(project.id()));
347+
}
348+
}
336349
}
337350

338351
// if we're the master, then process deleted indices and trigger policies

0 commit comments

Comments
 (0)