add phase and action timestamps to cluster-state (#3726)

This commit sets `index.lifecycle.phase_time` and `index.lifecycle.action_time`
appropriately during setPhase and setAction in IndexLifecycleContext.
This commit is contained in:
Tal Levy 2018-01-25 09:02:26 -08:00 committed by GitHub
parent faf067080b
commit ab49e40f91
11 changed files with 224 additions and 91 deletions

View File

@ -38,11 +38,21 @@ public interface IndexLifecycleContext {
*/ */
String getAction(); String getAction();
/**
* @return the start time of the current {@link LifecycleAction}.
*/
long getActionTime();
/** /**
* @return the current {@link Phase} name for the target. * @return the current {@link Phase} name for the target.
*/ */
String getPhase(); String getPhase();
/**
* @return the start time of the current {@link Phase}.
*/
long getPhaseTime();
/** /**
* @return the name of the target. * @return the name of the target.
*/ */

View File

@ -17,6 +17,8 @@ public class LifecycleSettings {
public static final String LIFECYCLE_PHASE = "index.lifecycle.phase"; public static final String LIFECYCLE_PHASE = "index.lifecycle.phase";
public static final String LIFECYCLE_ACTION = "index.lifecycle.action"; public static final String LIFECYCLE_ACTION = "index.lifecycle.action";
public static final String LIFECYCLE_INDEX_CREATION_DATE = "index.lifecycle.date"; public static final String LIFECYCLE_INDEX_CREATION_DATE = "index.lifecycle.date";
public static final String LIFECYCLE_PHASE_TIME = "index.lifecycle.phase_time";
public static final String LIFECYCLE_ACTION_TIME = "index.lifecycle.action_time";
// NORELEASE: we should probably change the default to something other than three seconds for initial release // NORELEASE: we should probably change the default to something other than three seconds for initial release
public static final Setting<TimeValue> LIFECYCLE_POLL_INTERVAL_SETTING = Setting.positiveTimeSetting(LIFECYCLE_POLL_INTERVAL, public static final Setting<TimeValue> LIFECYCLE_POLL_INTERVAL_SETTING = Setting.positiveTimeSetting(LIFECYCLE_POLL_INTERVAL,
@ -29,4 +31,8 @@ public class LifecycleSettings {
Setting.Property.Dynamic, Setting.Property.IndexScope); Setting.Property.Dynamic, Setting.Property.IndexScope);
public static final Setting<Long> LIFECYCLE_INDEX_CREATION_DATE_SETTING = Setting.longSetting(LIFECYCLE_INDEX_CREATION_DATE, public static final Setting<Long> LIFECYCLE_INDEX_CREATION_DATE_SETTING = Setting.longSetting(LIFECYCLE_INDEX_CREATION_DATE,
-1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope); -1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope);
public static final Setting<Long> LIFECYCLE_PHASE_TIME_SETTING = Setting.longSetting(LIFECYCLE_PHASE_TIME,
-1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope);
public static final Setting<Long> LIFECYCLE_ACTION_TIME_SETTING = Setting.longSetting(LIFECYCLE_ACTION_TIME,
-1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope);
} }

View File

@ -5,6 +5,8 @@
*/ */
package org.elasticsearch.xpack.core.indexlifecycle; package org.elasticsearch.xpack.core.indexlifecycle;
import java.util.function.LongSupplier;
public abstract class MockIndexLifecycleContext implements IndexLifecycleContext { public abstract class MockIndexLifecycleContext implements IndexLifecycleContext {
private final String targetName; private final String targetName;
@ -12,12 +14,19 @@ public abstract class MockIndexLifecycleContext implements IndexLifecycleContext
private String action; private String action;
private Exception exceptionToThrow; private Exception exceptionToThrow;
private int numberOfReplicas; private int numberOfReplicas;
private LongSupplier nowSupplier;
private long phaseTime;
private long actionTime;
public MockIndexLifecycleContext(String targetName, String initialPhase, String initialAction, int numberOfReplicas) { public MockIndexLifecycleContext(String targetName, String initialPhase, String initialAction, int numberOfReplicas,
LongSupplier nowSupplier) {
this.targetName = targetName; this.targetName = targetName;
this.phase = initialPhase; this.phase = initialPhase;
this.action = initialAction; this.action = initialAction;
this.numberOfReplicas = numberOfReplicas; this.numberOfReplicas = numberOfReplicas;
this.nowSupplier = nowSupplier;
this.phaseTime = -1L;
this.actionTime = -1L;
} }
public void failOnSetters(Exception exceptionToThrow) { public void failOnSetters(Exception exceptionToThrow) {
@ -31,7 +40,9 @@ public abstract class MockIndexLifecycleContext implements IndexLifecycleContext
return; return;
} }
this.phase = phase; this.phase = phase;
this.phaseTime = nowSupplier.getAsLong();
this.action = ""; this.action = "";
this.actionTime = -1L;
listener.onSuccess(); listener.onSuccess();
} }
@ -42,6 +53,7 @@ public abstract class MockIndexLifecycleContext implements IndexLifecycleContext
return; return;
} }
this.action = action; this.action = action;
this.actionTime = nowSupplier.getAsLong();
listener.onSuccess(); listener.onSuccess();
} }
@ -50,11 +62,21 @@ public abstract class MockIndexLifecycleContext implements IndexLifecycleContext
return action; return action;
} }
@Override
public long getActionTime() {
return actionTime;
}
@Override @Override
public String getPhase() { public String getPhase() {
return phase; return phase;
} }
@Override
public long getPhaseTime() {
return phaseTime;
}
@Override @Override
public String getLifecycleTarget() { public String getLifecycleTarget() {
return targetName; return targetName;

View File

@ -14,9 +14,10 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
public void testSetPhase() { public void testSetPhase() {
String targetName = randomAlphaOfLengthBetween(1, 20); String targetName = randomAlphaOfLengthBetween(1, 20);
String newPhase = randomAlphaOfLengthBetween(1, 20); String newPhase = randomAlphaOfLengthBetween(1, 20);
long now = randomNonNegativeLong();
MockIndexLifecycleContext context = new MockIndexLifecycleContext(targetName, MockIndexLifecycleContext context = new MockIndexLifecycleContext(targetName,
randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10)) { randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10), () -> now) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -43,6 +44,8 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
assertEquals(true, listenerCalled.get()); assertEquals(true, listenerCalled.get());
assertEquals(newPhase, context.getPhase()); assertEquals(newPhase, context.getPhase());
assertEquals("", context.getAction()); assertEquals("", context.getAction());
assertEquals(now, context.getPhaseTime());
assertEquals(-1L, context.getActionTime());
assertEquals(targetName, context.getLifecycleTarget()); assertEquals(targetName, context.getLifecycleTarget());
} }
@ -50,7 +53,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
String phase = randomAlphaOfLengthBetween(1, 20); String phase = randomAlphaOfLengthBetween(1, 20);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), phase, MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), phase,
randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10)) { randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10), () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -65,7 +68,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
int replicas = randomIntBetween(1, 10); int replicas = randomIntBetween(1, 10);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20),
randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), replicas) { randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), replicas, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -80,9 +83,10 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
String targetName = randomAlphaOfLengthBetween(1, 20); String targetName = randomAlphaOfLengthBetween(1, 20);
String phase = randomAlphaOfLengthBetween(1, 20); String phase = randomAlphaOfLengthBetween(1, 20);
String newAction = randomAlphaOfLengthBetween(1, 20); String newAction = randomAlphaOfLengthBetween(1, 20);
long now = randomNonNegativeLong();
MockIndexLifecycleContext context = new MockIndexLifecycleContext(targetName, phase, MockIndexLifecycleContext context = new MockIndexLifecycleContext(targetName, phase,
randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10)) { randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10), () -> now) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -109,6 +113,8 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
assertEquals(true, listenerCalled.get()); assertEquals(true, listenerCalled.get());
assertEquals(newAction, context.getAction()); assertEquals(newAction, context.getAction());
assertEquals(phase, context.getPhase()); assertEquals(phase, context.getPhase());
assertEquals(-1L, context.getPhaseTime()); // no setPhase was called to set this yet
assertEquals(now, context.getActionTime());
assertEquals(targetName, context.getLifecycleTarget()); assertEquals(targetName, context.getLifecycleTarget());
} }
@ -116,7 +122,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
String action = randomAlphaOfLengthBetween(1, 20); String action = randomAlphaOfLengthBetween(1, 20);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20),
randomAlphaOfLengthBetween(1, 20), action, randomIntBetween(0, 10)) { randomAlphaOfLengthBetween(1, 20), action, randomIntBetween(0, 10), () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -130,7 +136,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
public void testGetLifecycleTarget() { public void testGetLifecycleTarget() {
String target = randomAlphaOfLengthBetween(1, 20); String target = randomAlphaOfLengthBetween(1, 20);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(target, randomAlphaOfLengthBetween(1, 20), MockIndexLifecycleContext context = new MockIndexLifecycleContext(target, randomAlphaOfLengthBetween(1, 20),
randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10)) { randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10), () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -143,7 +149,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
public void testExecuteAction() { public void testExecuteAction() {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20),
randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10)) { randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), randomIntBetween(0, 10), () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -182,7 +188,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
String phase = randomAlphaOfLengthBetween(1, 20); String phase = randomAlphaOfLengthBetween(1, 20);
String action = randomAlphaOfLengthBetween(1, 20); String action = randomAlphaOfLengthBetween(1, 20);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20),
phase, action, randomIntBetween(0, 10)) { phase, action, randomIntBetween(0, 10), () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -218,7 +224,7 @@ public class MockIndexLifecycleContextTests extends ESTestCase {
String action = randomAlphaOfLengthBetween(1, 20); String action = randomAlphaOfLengthBetween(1, 20);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20), MockIndexLifecycleContext context = new MockIndexLifecycleContext(randomAlphaOfLengthBetween(1, 20),
phase, action, randomIntBetween(0, 10)) { phase, action, randomIntBetween(0, 10), () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {

View File

@ -22,8 +22,10 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.LongSupplier;
public class PhaseTests extends AbstractSerializingTestCase<Phase> { public class PhaseTests extends AbstractSerializingTestCase<Phase> {
private static final LongSupplier TEST_NOW_SUPPLIER = () -> 0L;
private String phaseName; private String phaseName;
@Before @Before
@ -125,7 +127,7 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -185,7 +187,7 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -244,7 +246,7 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -285,7 +287,7 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
TimeValue after = TimeValue.timeValueSeconds(randomIntBetween(10, 100)); TimeValue after = TimeValue.timeValueSeconds(randomIntBetween(10, 100));
Phase phase = new Phase(phaseName, after, Collections.emptyMap()); Phase phase = new Phase(phaseName, after, Collections.emptyMap());
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "", 0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -327,7 +329,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
}; };
Phase phase = new Phase(phaseName, after, Collections.emptyMap()); Phase phase = new Phase(phaseName, after, Collections.emptyMap());
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, Phase.PHASE_COMPLETED, 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, Phase.PHASE_COMPLETED,
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -389,7 +392,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, firstAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, firstAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -480,7 +484,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, firstAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, firstAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -566,7 +571,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -652,7 +658,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, thirdAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, thirdAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -735,7 +742,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "does_not_exist", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, "does_not_exist",
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -789,7 +797,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -914,7 +923,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -1038,7 +1048,8 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
actions.put(thirdAction.getWriteableName(), thirdAction); actions.put(thirdAction.getWriteableName(), thirdAction);
Phase phase = new Phase(phaseName, after, actions); Phase phase = new Phase(phaseName, after, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, thirdAction.getWriteableName(), 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, thirdAction.getWriteableName(),
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {

View File

@ -26,6 +26,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.elasticsearch.xpack.core.indexlifecycle.TimeseriesLifecycleType.VALID_COLD_ACTIONS; import static org.elasticsearch.xpack.core.indexlifecycle.TimeseriesLifecycleType.VALID_COLD_ACTIONS;
@ -44,6 +45,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
private static final ReplicasAction TEST_REPLICAS_ACTION = new ReplicasAction(1); private static final ReplicasAction TEST_REPLICAS_ACTION = new ReplicasAction(1);
private static final RolloverAction TEST_ROLLOVER_ACTION = new RolloverAction("", new ByteSizeValue(1), null, null); private static final RolloverAction TEST_ROLLOVER_ACTION = new RolloverAction("", new ByteSizeValue(1), null, null);
private static final ShrinkAction TEST_SHRINK_ACTION = new ShrinkAction(1); private static final ShrinkAction TEST_SHRINK_ACTION = new ShrinkAction(1);
private static final LongSupplier TEST_NOW_SUPPLIER = () -> 0L;
public void testGetFirstPhase() { public void testGetFirstPhase() {
Map<String, Phase> phases = new HashMap<>(); Map<String, Phase> phases = new HashMap<>();
@ -188,7 +190,8 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
Map<String, LifecycleAction> actions = VALID_HOT_ACTIONS Map<String, LifecycleAction> actions = VALID_HOT_ACTIONS
.stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity()));
Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions); Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "",
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -209,7 +212,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION); actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION);
Phase warmPhase = new Phase("warm", TimeValue.ZERO, actions); Phase warmPhase = new Phase("warm", TimeValue.ZERO, actions);
MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "", MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -241,7 +244,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION); actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION);
Phase warmPhase = new Phase("warm", TimeValue.ZERO, actions); Phase warmPhase = new Phase("warm", TimeValue.ZERO, actions);
MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "", MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -253,7 +256,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
LifecyclePolicy.NextActionProvider provider = policy.getActionProvider(context, warmPhase); LifecyclePolicy.NextActionProvider provider = policy.getActionProvider(context, warmPhase);
assertThat(provider.next(null), equalTo(TEST_REPLICAS_ACTION)); assertThat(provider.next(null), equalTo(TEST_REPLICAS_ACTION));
context = new MockIndexLifecycleContext(indexName, "", "", context = new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() - 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() - 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -283,7 +286,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION); actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION);
Phase coldPhase = new Phase("cold", TimeValue.ZERO, actions); Phase coldPhase = new Phase("cold", TimeValue.ZERO, actions);
MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "", MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() - 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() - 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -302,7 +305,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
} }
context = new MockIndexLifecycleContext(indexName, "", "", context = new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -327,7 +330,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION); actions.put(ReplicasAction.NAME, TEST_REPLICAS_ACTION);
Phase coldPhase = new Phase("cold", TimeValue.ZERO, actions); Phase coldPhase = new Phase("cold", TimeValue.ZERO, actions);
MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "", MockIndexLifecycleContext context =new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() + 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -339,7 +342,7 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
LifecyclePolicy.NextActionProvider provider = policy.getActionProvider(context, coldPhase); LifecyclePolicy.NextActionProvider provider = policy.getActionProvider(context, coldPhase);
assertThat(provider.next(null), equalTo(TEST_REPLICAS_ACTION)); assertThat(provider.next(null), equalTo(TEST_REPLICAS_ACTION));
context = new MockIndexLifecycleContext(indexName, "", "", context = new MockIndexLifecycleContext(indexName, "", "",
TEST_REPLICAS_ACTION.getNumberOfReplicas() - 1) { TEST_REPLICAS_ACTION.getNumberOfReplicas() - 1, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -363,7 +366,8 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase {
.stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity()));
Phase deletePhase = new Phase("delete", TimeValue.ZERO, actions); Phase deletePhase = new Phase("delete", TimeValue.ZERO, actions);
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "",
0, TEST_NOW_SUPPLIER) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {

View File

@ -103,6 +103,8 @@ public class IndexLifecycle extends Plugin implements ActionPlugin {
LifecycleSettings.LIFECYCLE_NAME_SETTING, LifecycleSettings.LIFECYCLE_NAME_SETTING,
LifecycleSettings.LIFECYCLE_PHASE_SETTING, LifecycleSettings.LIFECYCLE_PHASE_SETTING,
LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING, LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING,
LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING,
LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING,
LifecycleSettings.LIFECYCLE_ACTION_SETTING); LifecycleSettings.LIFECYCLE_ACTION_SETTING);
} }

View File

@ -54,8 +54,10 @@ public class InternalIndexLifecycleContext implements IndexLifecycleContext {
@Override @Override
public void setPhase(String phase, Listener listener) { public void setPhase(String phase, Listener listener) {
Settings newLifecyclePhaseSettings = Settings.builder() Settings newLifecyclePhaseSettings = Settings.builder()
.put(LifecycleSettings.LIFECYCLE_PHASE_SETTING.getKey(), phase) .put(LifecycleSettings.LIFECYCLE_PHASE, phase)
.put(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey(), "").build(); .put(LifecycleSettings.LIFECYCLE_PHASE_TIME, nowSupplier.getAsLong())
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, -1L)
.put(LifecycleSettings.LIFECYCLE_ACTION, "").build();
writeSettings(index.getName(), newLifecyclePhaseSettings, listener); writeSettings(index.getName(), newLifecyclePhaseSettings, listener);
} }
@ -64,9 +66,17 @@ public class InternalIndexLifecycleContext implements IndexLifecycleContext {
return LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(getIdxMetaData().getSettings()); return LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(getIdxMetaData().getSettings());
} }
@Override
public long getPhaseTime() {
return LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(getIdxMetaData().getSettings());
}
@Override @Override
public void setAction(String action, Listener listener) { public void setAction(String action, Listener listener) {
Settings newLifecycleActionSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey(), action).build(); Settings newLifecycleActionSettings = Settings.builder()
.put(LifecycleSettings.LIFECYCLE_ACTION, action)
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, nowSupplier.getAsLong())
.build();
writeSettings(index.getName(), newLifecycleActionSettings, listener); writeSettings(index.getName(), newLifecycleActionSettings, listener);
} }
@ -75,6 +85,11 @@ public class InternalIndexLifecycleContext implements IndexLifecycleContext {
return LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(getIdxMetaData().getSettings()); return LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(getIdxMetaData().getSettings());
} }
@Override
public long getActionTime() {
return LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(getIdxMetaData().getSettings());
}
@Override @Override
public String getLifecycleTarget() { public String getLifecycleTarget() {
return index.getName(); return index.getName();

View File

@ -97,7 +97,7 @@ public class IndexLifecycleServiceTests extends ESTestCase {
when(adminClient.indices()).thenReturn(indicesClient); when(adminClient.indices()).thenReturn(indicesClient);
indexLifecycleService = new IndexLifecycleService(Settings.EMPTY, client, clusterService, clock, indexLifecycleService = new IndexLifecycleService(Settings.EMPTY, client, clusterService, clock,
threadPool, () -> now); threadPool, () -> now);
Mockito.verify(clusterService).addListener(indexLifecycleService); Mockito.verify(clusterService).addListener(indexLifecycleService);
} }
@ -320,8 +320,10 @@ public class IndexLifecycleServiceTests extends ESTestCase {
UpdateSettingsRequest request = (UpdateSettingsRequest) invocationOnMock.getArguments()[0]; UpdateSettingsRequest request = (UpdateSettingsRequest) invocationOnMock.getArguments()[0];
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocationOnMock.getArguments()[1]; ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocationOnMock.getArguments()[1];
UpdateSettingsTestHelper.assertSettingsRequest(request, Settings.builder() UpdateSettingsTestHelper.assertSettingsRequest(request, Settings.builder()
.put(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey(), "") .put(LifecycleSettings.LIFECYCLE_ACTION, "")
.put(LifecycleSettings.LIFECYCLE_PHASE_SETTING.getKey(), "phase").build(), index.getName()); .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, -1L)
.put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now)
.put(LifecycleSettings.LIFECYCLE_PHASE, "phase").build(), index.getName());
phaseUpdated.set(true); phaseUpdated.set(true);
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true)); listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
return null; return null;
@ -329,7 +331,8 @@ public class IndexLifecycleServiceTests extends ESTestCase {
UpdateSettingsRequest request = (UpdateSettingsRequest) invocationOnMock.getArguments()[0]; UpdateSettingsRequest request = (UpdateSettingsRequest) invocationOnMock.getArguments()[0];
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocationOnMock.getArguments()[1]; ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocationOnMock.getArguments()[1];
UpdateSettingsTestHelper.assertSettingsRequest(request, Settings.builder() UpdateSettingsTestHelper.assertSettingsRequest(request, Settings.builder()
.put(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey(), MockAction.NAME).build(), index.getName()); .put(LifecycleSettings.LIFECYCLE_ACTION, MockAction.NAME)
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now).build(), index.getName());
actionUpdated.set(true); actionUpdated.set(true);
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true)); listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
return null; return null;
@ -398,7 +401,6 @@ public class IndexLifecycleServiceTests extends ESTestCase {
assertNull(dateUpdated.get()); assertNull(dateUpdated.get());
assertThat(mockAction.getExecutedCount(), equalTo(1L)); assertThat(mockAction.getExecutedCount(), equalTo(1L));
} }
/** /**

View File

@ -34,6 +34,7 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import java.util.Collections; import java.util.Collections;
import java.util.function.LongSupplier;
public class InternalIndexLifecycleContextTests extends ESTestCase { public class InternalIndexLifecycleContextTests extends ESTestCase {
private static final Index TEST_INDEX = new Index("test", "test"); private static final Index TEST_INDEX = new Index("test", "test");
@ -47,16 +48,23 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
public void testSetPhase() { public void testSetPhase() {
long oldNow = randomNonNegativeLong();
long now = randomNonNegativeLong();
LongSupplier nowSupplier = () -> now;
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
String oldPhase = randomAlphaOfLengthBetween(1, 5); String oldPhase = randomAlphaOfLengthBetween(1, 5);
String newPhase = randomAlphaOfLengthBetween(6, 10); String newPhase = randomAlphaOfLengthBetween(6, 10);
Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, newPhase) Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, newPhase)
.put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now)
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, -1L)
.put(LifecycleSettings.LIFECYCLE_ACTION, "").build(); .put(LifecycleSettings.LIFECYCLE_ACTION, "").build();
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_PHASE, oldPhase) .put(LifecycleSettings.LIFECYCLE_PHASE, oldPhase)
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build()) .put(LifecycleSettings.LIFECYCLE_PHASE_TIME, oldNow)
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, oldNow)
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta); ClusterState clusterState = getClusterState(idxMeta);
ClusterState updatedClusterState = getClusterState(IndexMetaData.builder(idxMeta) ClusterState updatedClusterState = getClusterState(IndexMetaData.builder(idxMeta)
.settings(Settings.builder().put(idxMeta.getSettings()).put(expectedSettings)).build()); .settings(Settings.builder().put(idxMeta.getSettings()).put(expectedSettings)).build());
@ -84,9 +92,7 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, () -> { InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, nowSupplier);
throw new AssertionError("nowSupplier should not be called");
});
// Use setOnce so it throws an error if we call the listener multiple // Use setOnce so it throws an error if we call the listener multiple
// times // times
@ -114,13 +120,16 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
public void testSetPhaseNotAcknowledged() { public void testSetPhaseNotAcknowledged() {
long now = randomNonNegativeLong();
LongSupplier nowSupplier = () -> now;
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
String newPhase = randomAlphaOfLengthBetween(1, 20); String newPhase = randomAlphaOfLengthBetween(1, 20);
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_PHASE, randomAlphaOfLengthBetween(1, 20)) .put(LifecycleSettings.LIFECYCLE_PHASE, randomAlphaOfLengthBetween(1, 20))
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build()) .put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now)
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); .put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta); ClusterState clusterState = getClusterState(idxMeta);
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(clusterState); Mockito.when(clusterService.state()).thenReturn(clusterState);
@ -139,16 +148,16 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1]; ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, newPhase) Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, newPhase)
.put(LifecycleSettings.LIFECYCLE_ACTION, "").build(); .put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now)
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, -1L)
.put(LifecycleSettings.LIFECYCLE_ACTION, "").build();
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName()); UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName());
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(false)); listener.onResponse(UpdateSettingsTestHelper.createMockResponse(false));
return null; return null;
} }
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, () -> { InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, nowSupplier);
throw new AssertionError("nowSupplier should not be called");
});
// Use setOnce so it throws an error if we call the listener multiple // Use setOnce so it throws an error if we call the listener multiple
// times // times
@ -175,12 +184,15 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
public void testSetPhaseFailure() { public void testSetPhaseFailure() {
long now = randomNonNegativeLong();
LongSupplier nowSupplier = () -> now;
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
String newPhase = randomAlphaOfLengthBetween(1, 20); String newPhase = randomAlphaOfLengthBetween(1, 20);
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_PHASE, randomAlphaOfLengthBetween(1, 20)) .put(LifecycleSettings.LIFECYCLE_PHASE, randomAlphaOfLengthBetween(1, 20))
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build()) .put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now)
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta); ClusterState clusterState = getClusterState(idxMeta);
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
@ -202,16 +214,15 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1]; ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, newPhase) Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, newPhase)
.put(LifecycleSettings.LIFECYCLE_ACTION, "").build(); .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, -1L).put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now)
.put(LifecycleSettings.LIFECYCLE_ACTION, "").build();
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName()); UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName());
listener.onFailure(exception); listener.onFailure(exception);
return null; return null;
} }
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, () -> { InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, nowSupplier);
throw new AssertionError("nowSupplier should not be called");
});
// Use setOnce so it throws an error if we call the listener multiple // Use setOnce so it throws an error if we call the listener multiple
// times // times
@ -255,6 +266,24 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
assertEquals(phase, context.getPhase()); assertEquals(phase, context.getPhase());
} }
public void testGetPhaseTime() {
long creationDate = randomNonNegativeLong();
long phaseTime = randomNonNegativeLong();
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_PHASE_TIME, phaseTime).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta);
ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(clusterState);
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, null, clusterService, () -> {
throw new AssertionError("nowSupplier should not be called");
});
assertEquals(phaseTime, context.getPhaseTime());
}
public void testGetReplicas() { public void testGetReplicas() {
int replicas = randomIntBetween(0, 5); int replicas = randomIntBetween(0, 5);
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
@ -272,13 +301,16 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
public void testSetAction() { public void testSetAction() {
long now = randomNonNegativeLong();
LongSupplier nowSupplier = () -> now;
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
String oldAction = randomAlphaOfLengthBetween(1, 5); String oldAction = randomAlphaOfLengthBetween(1, 5);
String newAction = randomAlphaOfLengthBetween(6, 10); String newAction = randomAlphaOfLengthBetween(6, 10);
Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION, newAction).build(); Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION, newAction)
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now).build();
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_ACTION, oldAction).build()) .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now).put(LifecycleSettings.LIFECYCLE_ACTION, oldAction).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta); ClusterState clusterState = getClusterState(idxMeta);
ClusterState updatedClusterState = getClusterState(IndexMetaData.builder(idxMeta) ClusterState updatedClusterState = getClusterState(IndexMetaData.builder(idxMeta)
@ -305,9 +337,7 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, () -> { InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, nowSupplier);
throw new AssertionError("nowSupplier should not be called");
});
// Use setOnce so it throws an error if we call the listener multiple // Use setOnce so it throws an error if we call the listener multiple
// times // times
@ -334,13 +364,16 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any());
} }
public void testSetActionNotAcknoledged() { public void testSetActionNotAcknowledged() {
long now = randomNonNegativeLong();
LongSupplier nowSupplier = () -> now;
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
String newAction = randomAlphaOfLengthBetween(1, 20); String newAction = randomAlphaOfLengthBetween(1, 20);
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build()) .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now)
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); .put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta); ClusterState clusterState = getClusterState(idxMeta);
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(clusterState); Mockito.when(clusterService.state()).thenReturn(clusterState);
@ -359,16 +392,14 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1]; ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION, newAction) Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION, newAction)
.build(); .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now).build();
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName()); UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName());
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(false)); listener.onResponse(UpdateSettingsTestHelper.createMockResponse(false));
return null; return null;
} }
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, () -> { InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, nowSupplier);
throw new AssertionError("nowSupplier should not be called");
});
// Use setOnce so it throws an error if we call the listener multiple // Use setOnce so it throws an error if we call the listener multiple
// times // times
@ -395,12 +426,15 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
} }
public void testSetActionFailure() { public void testSetActionFailure() {
long now = randomNonNegativeLong();
LongSupplier nowSupplier = () -> now;
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
String newAction = randomAlphaOfLengthBetween(1, 20); String newAction = randomAlphaOfLengthBetween(1, 20);
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName()) IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate) .settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build()) .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now)
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); .put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLengthBetween(1, 20)).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta); ClusterState clusterState = getClusterState(idxMeta);
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(clusterState); Mockito.when(clusterService.state()).thenReturn(clusterState);
@ -421,16 +455,14 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1]; ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION, newAction) Settings expectedSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ACTION, newAction)
.build(); .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now).build();
UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName()); UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, TEST_INDEX.getName());
listener.onFailure(exception); listener.onFailure(exception);
return null; return null;
} }
}).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, () -> { InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, client, clusterService, nowSupplier);
throw new AssertionError("nowSupplier should not be called");
});
// Use setOnce so it throws an error if we call the listener multiple // Use setOnce so it throws an error if we call the listener multiple
// times // times
@ -474,6 +506,24 @@ public class InternalIndexLifecycleContextTests extends ESTestCase {
assertEquals(action, context.getAction()); assertEquals(action, context.getAction());
} }
public void testGetActionTime() {
long creationDate = randomNonNegativeLong();
long actionTime = randomNonNegativeLong();
IndexMetaData idxMeta = IndexMetaData.builder(TEST_INDEX.getName())
.settings(Settings.builder().put("index.version.created", 7000001L).put("index.creation_date", creationDate)
.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, actionTime).build())
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
ClusterState clusterState = getClusterState(idxMeta);
ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(clusterState);
InternalIndexLifecycleContext context = new InternalIndexLifecycleContext(TEST_INDEX, null, clusterService, () -> {
throw new AssertionError("nowSupplier should not be called");
});
assertEquals(actionTime, context.getActionTime());
}
public void testGetLifecycleTarget() { public void testGetLifecycleTarget() {
long creationDate = randomNonNegativeLong(); long creationDate = randomNonNegativeLong();
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));

View File

@ -136,7 +136,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteNewIndexBeforeTrigger() throws Exception { public void testExecuteNewIndexBeforeTrigger() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -163,7 +163,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteNewIndexAfterTrigger() throws Exception { public void testExecuteNewIndexAfterTrigger() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -190,7 +190,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteNewIndexAfterTriggerFailure() throws Exception { public void testExecuteNewIndexAfterTriggerFailure() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "", "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -221,7 +221,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteFirstPhase() throws Exception { public void testExecuteFirstPhase() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, firstPhase.getName(), "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, firstPhase.getName(), "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -244,7 +244,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteSecondPhase() throws Exception { public void testExecuteSecondPhase() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, secondPhase.getName(), "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, secondPhase.getName(), "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -267,7 +267,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteThirdPhase() throws Exception { public void testExecuteThirdPhase() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, thirdPhase.getName(), "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, thirdPhase.getName(), "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -290,7 +290,7 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteMissingPhase() throws Exception { public void testExecuteMissingPhase() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "does_not_exist", "", 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, "does_not_exist", "", 0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -316,7 +316,8 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteFirstPhaseCompletedBeforeTrigger() throws Exception { public void testExecuteFirstPhaseCompletedBeforeTrigger() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, firstPhase.getName(), Phase.PHASE_COMPLETED, 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, firstPhase.getName(), Phase.PHASE_COMPLETED,
0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -343,7 +344,8 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteFirstPhaseCompletedAfterTrigger() throws Exception { public void testExecuteFirstPhaseCompletedAfterTrigger() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, firstPhase.getName(), Phase.PHASE_COMPLETED, 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, firstPhase.getName(), Phase.PHASE_COMPLETED,
0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -370,7 +372,8 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteSecondPhaseCompletedBeforeTrigger() throws Exception { public void testExecuteSecondPhaseCompletedBeforeTrigger() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, secondPhase.getName(), Phase.PHASE_COMPLETED, 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, secondPhase.getName(), Phase.PHASE_COMPLETED,
0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -397,7 +400,8 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteSecondPhaseCompletedAfterTrigger() throws Exception { public void testExecuteSecondPhaseCompletedAfterTrigger() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, secondPhase.getName(), Phase.PHASE_COMPLETED, 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, secondPhase.getName(), Phase.PHASE_COMPLETED,
0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
@ -424,7 +428,8 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
} }
public void testExecuteThirdPhaseCompleted() throws Exception { public void testExecuteThirdPhaseCompleted() throws Exception {
MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, thirdPhase.getName(), Phase.PHASE_COMPLETED, 0) { MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, thirdPhase.getName(), Phase.PHASE_COMPLETED,
0, () -> 0L) {
@Override @Override
public boolean canExecute(Phase phase) { public boolean canExecute(Phase phase) {
throw new AssertionError("canExecute should not have been called"); throw new AssertionError("canExecute should not have been called");