From 9cba84b6fcccdd3e46d1b9276670f59b7b63a89f Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Wed, 22 Nov 2017 16:09:04 +0000 Subject: [PATCH] Obeys the after parameter on phases excpet the first --- .../xpack/indexlifecycle/LifecyclePolicy.java | 35 +++++++++++++++++++ .../xpack/indexlifecycle/Phase.java | 24 +++++++++---- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java index 5316db96b9d..993ca3a4c60 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java @@ -90,6 +90,8 @@ public class LifecyclePolicy extends AbstractDiffable implement public void execute(IndexMetaData idxMeta, InternalClient client) { String currentPhaseName = IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.get(idxMeta.getSettings()); + boolean currentPhaseActionsComplete = IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.get(idxMeta.getSettings()) + .equals(Phase.PHASE_COMPLETED); String indexName = idxMeta.getIndex().getName(); if (Strings.isNullOrEmpty(currentPhaseName)) { String firstPhaseName = phases.get(0).getName(); @@ -109,6 +111,39 @@ public class LifecyclePolicy extends AbstractDiffable implement logger.error("Failed to initialised phase [" + firstPhaseName + "] for index [" + indexName + "]", e); } }); + } else if (currentPhaseActionsComplete) { + int currentPhaseIndex = -1; + for (int i = 0; i < phases.size(); i++) { + if (phases.get(i).getName().equals(currentPhaseName)) { + currentPhaseIndex = i; + break; + } + } + if (currentPhaseIndex < phases.size() - 1) { + Phase nextPhase = phases.get(currentPhaseIndex + 1); + if (nextPhase.canExecute(idxMeta)) { + String nextPhaseName = nextPhase.getName(); + client.admin().indices().prepareUpdateSettings(indexName) + .setSettings(Settings.builder() + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_PHASE_SETTING.getKey(), nextPhaseName) + .put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), "")) + .execute(new ActionListener() { + + @Override + public void onResponse(UpdateSettingsResponse response) { + if (response.isAcknowledged() == false) { + logger.error( + "Successfully initialised phase [" + nextPhaseName + "] for index [" + indexName + "]"); + } + } + + @Override + public void onFailure(Exception e) { + logger.error("Failed to initialised phase [" + nextPhaseName + "] for index [" + indexName + "]", e); + } + }); + } + } } else { Phase currentPhase = phases.stream().filter(phase -> phase.getName().equals(currentPhaseName)).findAny() .orElseThrow(() -> new IllegalStateException("Current phase [" + currentPhaseName + "] not found in lifecycle [" diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java index f0f6f3bee23..89885e6caf1 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java @@ -30,7 +30,7 @@ import java.io.IOException; import java.util.List; public class Phase implements ToXContentObject, Writeable { - private static final String PHASE_COMPLETED = "PHASE COMPLETED"; + public static final String PHASE_COMPLETED = "ACTIONS COMPLETED"; private static final Logger logger = ESLoggerFactory.getLogger(Phase.class); @@ -88,30 +88,40 @@ public class Phase implements ToXContentObject, Writeable { return actions; } + protected boolean canExecute(IndexMetaData idxMeta) { + long now = System.currentTimeMillis(); // NOCOMMIT use ES way for + // getting current time. + long indexCreated = idxMeta.getCreationDate(); + logger.error("canExecute: now: " + now + ", indexCreated: " + indexCreated + ", (indexCreated + after.millis()): " + + (indexCreated + after.millis()) + ", (indexCreated + after.millis()) >= now: " + + ((indexCreated + after.millis()) >= now)); + return (indexCreated + after.millis()) <= now; + } + protected void execute(IndexMetaData idxMeta, InternalClient client) { String currentActionName = IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.get(idxMeta.getSettings()); String indexName = idxMeta.getIndex().getName(); if (Strings.isNullOrEmpty(currentActionName)) { - String firstPhaseName; + String firstActionName; if (actions.isEmpty()) { - firstPhaseName = PHASE_COMPLETED; + firstActionName = PHASE_COMPLETED; } else { - firstPhaseName = actions.get(0).getWriteableName(); + firstActionName = actions.get(0).getWriteableName(); } client.admin().indices().prepareUpdateSettings(indexName) - .setSettings(Settings.builder().put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), firstPhaseName)) + .setSettings(Settings.builder().put(IndexLifecycle.LIFECYCLE_TIMESERIES_ACTION_SETTING.getKey(), firstActionName)) .execute(new ActionListener() { @Override public void onResponse(UpdateSettingsResponse response) { if (response.isAcknowledged() == false) { - logger.info("Successfully initialised phase [" + firstPhaseName + "] for index [" + indexName + "]"); + logger.error("Successfully initialised action [" + firstActionName + "] for index [" + indexName + "]"); } } @Override public void onFailure(Exception e) { - logger.error("Failed to initialised phase [" + firstPhaseName + "] for index [" + indexName + "]", e); + logger.error("Failed to initialised action [" + firstActionName + "] for index [" + indexName + "]", e); } }); } else if (currentActionName.equals(PHASE_COMPLETED) == false) {