Skip to content

Commit fa46f10

Browse files
committed
add tests
1 parent 05e4282 commit fa46f10

File tree

7 files changed

+155
-20
lines changed

7 files changed

+155
-20
lines changed

x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ public void runPolicy(String policy, IndexMetaData indexMetaData, ClusterState c
5656
return;
5757
}
5858
Step currentStep = getCurrentStep(stepRegistry, policy, indexSettings);
59-
// if (maintenanceModeRequested && currentStep != null && ShrinkAction.NAME.equals(currentStep.getKey().getAction()) == false) {
60-
// logger.info("skipping policy [" + policy + "] for index [" + indexMetaData.getIndex().getName()
61-
// + "]. maintenance mode requested");
62-
// return;
63-
// }
6459
logger.debug("running policy with current-step[" + currentStep.getKey() + "]");
6560
if (currentStep instanceof TerminalPolicyStep) {
6661
logger.debug("policy [" + policy + "] for index [" + indexMetaData.getIndex().getName() + "] complete, skipping execution");

x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public void triggerPolicies(ClusterState clusterState, boolean fromClusterStateC
195195
StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(idxMeta.getSettings());
196196
if (OperationMode.MAINTENANCE_REQUESTED.equals(currentMode)
197197
&& IGNORE_ACTIONS_MAINTENANCE_REQUESTED.contains(stepKey.getAction()) == false) {
198+
logger.info("skipping policy [" + policyName + "] for index [" + idxMeta.getIndex().getName()
199+
+ "]. maintenance mode requested");
198200
continue;
199201
}
200202
lifecycleRunner.runPolicy(policyName, idxMeta, clusterState, fromClusterStateChange);

x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/MaintenanceModeUpdateTask.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ public MaintenanceModeUpdateTask(OperationMode mode) {
2121
this.mode = mode;
2222
}
2323

24+
OperationMode getOperationMode() {
25+
return mode;
26+
}
27+
2428
@Override
2529
public ClusterState execute(ClusterState currentState) {
2630
IndexLifecycleMetadata currentMetadata = currentState.metaData().custom(IndexLifecycleMetadata.TYPE);
2731

2832
boolean maintenanceModeToChange = currentMetadata.getMaintenanceMode().equals(mode) == false;
2933
boolean maintenanceModeRequested = OperationMode.MAINTENANCE_REQUESTED.equals(mode);
30-
boolean inMaintenanceMode = OperationMode.IN.equals(currentMetadata.getMaintenanceMode());
34+
boolean inMaintenanceMode = OperationMode.MAINTENANCE.equals(currentMetadata.getMaintenanceMode());
3135
if ((inMaintenanceMode && maintenanceModeRequested) || maintenanceModeToChange == false) {
3236
return currentState;
3337
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
7676
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
7777
LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders);
7878
newPolicies.put(lifecyclePolicyMetadata.getName(), lifecyclePolicyMetadata);
79-
IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, OperationMode.OUT);
79+
IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, OperationMode.NORMAL);
8080
newState.metaData(MetaData.builder(currentState.getMetaData())
8181
.putCustom(IndexLifecycleMetadata.TYPE, newMetadata).build());
8282
return newState.build();

x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void prepareState() {
9999

100100
MetaData metaData = MetaData.builder()
101101
.persistentSettings(settings(Version.CURRENT).build())
102-
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.OUT))
102+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.NORMAL))
103103
.put(IndexMetaData.builder(indexMetadata))
104104
.build();
105105
String nodeId = randomAlphaOfLength(10);

x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java

Lines changed: 142 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,44 @@
1414
import org.elasticsearch.cluster.ClusterName;
1515
import org.elasticsearch.cluster.ClusterState;
1616
import org.elasticsearch.cluster.ClusterStateUpdateTask;
17+
import org.elasticsearch.cluster.metadata.IndexMetaData;
1718
import org.elasticsearch.cluster.metadata.MetaData;
1819
import org.elasticsearch.cluster.node.DiscoveryNode;
1920
import org.elasticsearch.cluster.node.DiscoveryNodes;
2021
import org.elasticsearch.cluster.service.ClusterService;
22+
import org.elasticsearch.common.collect.ImmutableOpenMap;
2123
import org.elasticsearch.common.settings.Settings;
2224
import org.elasticsearch.common.transport.TransportAddress;
2325
import org.elasticsearch.common.unit.TimeValue;
26+
import org.elasticsearch.index.Index;
2427
import org.elasticsearch.test.ESTestCase;
28+
import org.elasticsearch.test.rest.yaml.section.Assertion;
2529
import org.elasticsearch.threadpool.ThreadPool;
2630
import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata;
31+
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
32+
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata;
2733
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
34+
import org.elasticsearch.xpack.core.indexlifecycle.MockAction;
2835
import org.elasticsearch.xpack.core.indexlifecycle.OperationMode;
36+
import org.elasticsearch.xpack.core.indexlifecycle.Phase;
37+
import org.elasticsearch.xpack.core.indexlifecycle.ShrinkAction;
38+
import org.elasticsearch.xpack.core.indexlifecycle.Step;
39+
import org.elasticsearch.xpack.core.indexlifecycle.TestLifecycleType;
2940
import org.elasticsearch.xpack.core.scheduler.SchedulerEngine;
3041
import org.junit.After;
3142
import org.junit.Before;
3243
import org.mockito.Mockito;
3344

34-
import java.io.IOException;
3545
import java.time.Clock;
3646
import java.time.Instant;
3747
import java.time.ZoneId;
3848
import java.util.Collections;
49+
import java.util.SortedMap;
50+
import java.util.TreeMap;
3951
import java.util.concurrent.ExecutorService;
4052

4153
import static org.elasticsearch.node.Node.NODE_MASTER_SETTING;
54+
import static org.elasticsearch.xpack.core.indexlifecycle.AbstractStepTestCase.randomStepKey;
4255
import static org.hamcrest.Matchers.equalTo;
4356
import static org.mockito.Matchers.any;
4457
import static org.mockito.Matchers.anyString;
@@ -84,12 +97,18 @@ public void prepareServices() {
8497
indicesClient = mock(IndicesAdminClient.class);
8598
when(client.admin()).thenReturn(adminClient);
8699
when(adminClient.indices()).thenReturn(indicesClient);
100+
when(client.settings()).thenReturn(Settings.EMPTY);
87101

88102
indexLifecycleService = new IndexLifecycleService(Settings.EMPTY, client, clusterService, clock,
89103
threadPool, () -> now);
90104
Mockito.verify(clusterService).addListener(indexLifecycleService);
91105
}
92106

107+
@After
108+
public void cleanup() {
109+
indexLifecycleService.close();
110+
}
111+
93112
public void testOnlyChangesStateOnMaster() throws Exception {
94113
MetaData metaData = MetaData.builder()
95114
.persistentSettings(settings(Version.CURRENT)
@@ -110,7 +129,7 @@ public void testElectUnElectMaster() throws Exception {
110129
MetaData metaData = MetaData.builder()
111130
.persistentSettings(settings(Version.CURRENT)
112131
.put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING.getKey(), TimeValue.timeValueSeconds(3)).build())
113-
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Collections.emptySortedMap(), OperationMode.OUT))
132+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Collections.emptySortedMap(), OperationMode.NORMAL))
114133
.build();
115134

116135
// First check that when the node has never been master the scheduler
@@ -197,15 +216,10 @@ public void testServiceSetupOnFirstClusterChange() {
197216
assertNull(indexLifecycleService.getScheduler());
198217
}
199218

200-
@After
201-
public void cleanup() throws IOException {
202-
indexLifecycleService.close();
203-
}
204-
205219
public void testSchedulerInitializationAndUpdate() {
206220
TimeValue pollInterval = TimeValue.timeValueSeconds(randomIntBetween(1, 59));
207221
MetaData metaData = MetaData.builder()
208-
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Collections.emptySortedMap(), OperationMode.OUT))
222+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Collections.emptySortedMap(), OperationMode.NORMAL))
209223
.persistentSettings(settings(Version.CURRENT).build())
210224
.build();
211225
MetaData updatedPollMetaData = MetaData.builder(metaData).persistentSettings(settings(Version.CURRENT)
@@ -256,6 +270,126 @@ public void testInstallMetadataFail() {
256270
assertNull(indexLifecycleService.getScheduler());
257271
}
258272

273+
public void testMaintenanceModeSkip() {
274+
String policyName = randomAlphaOfLengthBetween(1, 20);
275+
IndexLifecycleRunnerTests.MockInitializePolicyContextStep mockStep =
276+
new IndexLifecycleRunnerTests.MockInitializePolicyContextStep(randomStepKey(), randomStepKey());
277+
MockAction mockAction = new MockAction(Collections.singletonList(mockStep));
278+
Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction));
279+
LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, policyName,
280+
Collections.singletonMap(phase.getName(), phase));
281+
SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>();
282+
policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap()));
283+
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
284+
IndexMetaData indexMetadata = IndexMetaData.builder(index.getName())
285+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName))
286+
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
287+
ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder()
288+
.fPut(index.getName(), indexMetadata);
289+
MetaData metaData = MetaData.builder()
290+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.MAINTENANCE))
291+
.indices(indices.build())
292+
.persistentSettings(settings(Version.CURRENT).build())
293+
.build();
294+
ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT)
295+
.metaData(metaData)
296+
.nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build())
297+
.build();
298+
indexLifecycleService.triggerPolicies(currentState, randomBoolean());
299+
assertThat(mockStep.getExecuteCount(), equalTo(0L));
300+
}
301+
302+
public void testRequestedMaintenanceOnShrink() {
303+
Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, randomAlphaOfLength(5));
304+
String policyName = randomAlphaOfLengthBetween(1, 20);
305+
IndexLifecycleRunnerTests.MockInitializePolicyContextStep mockStep =
306+
new IndexLifecycleRunnerTests.MockInitializePolicyContextStep(mockShrinkStep, randomStepKey());
307+
MockAction mockAction = new MockAction(Collections.singletonList(mockStep));
308+
Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction));
309+
LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, policyName,
310+
Collections.singletonMap(phase.getName(), phase));
311+
SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>();
312+
policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap()));
313+
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
314+
IndexMetaData indexMetadata = IndexMetaData.builder(index.getName())
315+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)
316+
.put(LifecycleSettings.LIFECYCLE_PHASE, mockShrinkStep.getPhase())
317+
.put(LifecycleSettings.LIFECYCLE_ACTION, mockShrinkStep.getAction())
318+
.put(LifecycleSettings.LIFECYCLE_STEP, mockShrinkStep.getName()))
319+
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
320+
ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder()
321+
.fPut(index.getName(), indexMetadata);
322+
MetaData metaData = MetaData.builder()
323+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.MAINTENANCE_REQUESTED))
324+
.indices(indices.build())
325+
.persistentSettings(settings(Version.CURRENT).build())
326+
.build();
327+
ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT)
328+
.metaData(metaData)
329+
.nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build())
330+
.build();
331+
332+
ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE);
333+
SetOnce<Boolean> executedShrink = new SetOnce<>();
334+
doAnswer(invocationOnMock -> {
335+
executedShrink.set(true);
336+
return null;
337+
}).when(clusterService).submitStateUpdateTask(anyString(), any(ExecuteStepsUpdateTask.class));
338+
indexLifecycleService.clusterChanged(event);
339+
assertTrue(executedShrink.get());
340+
}
341+
342+
public void testRequestedMaintenanceOnSafeAction() {
343+
String policyName = randomAlphaOfLengthBetween(1, 20);
344+
Step.StepKey currentStepKey = randomStepKey();
345+
IndexLifecycleRunnerTests.MockInitializePolicyContextStep mockStep =
346+
new IndexLifecycleRunnerTests.MockInitializePolicyContextStep(currentStepKey, randomStepKey());
347+
MockAction mockAction = new MockAction(Collections.singletonList(mockStep));
348+
Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction));
349+
LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, policyName,
350+
Collections.singletonMap(phase.getName(), phase));
351+
SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>();
352+
policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap()));
353+
Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
354+
IndexMetaData indexMetadata = IndexMetaData.builder(index.getName())
355+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)
356+
.put(LifecycleSettings.LIFECYCLE_PHASE, currentStepKey.getPhase())
357+
.put(LifecycleSettings.LIFECYCLE_ACTION, currentStepKey.getAction())
358+
.put(LifecycleSettings.LIFECYCLE_STEP, currentStepKey.getName()))
359+
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
360+
ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder()
361+
.fPut(index.getName(), indexMetadata);
362+
MetaData metaData = MetaData.builder()
363+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.MAINTENANCE_REQUESTED))
364+
.indices(indices.build())
365+
.persistentSettings(settings(Version.CURRENT).build())
366+
.build();
367+
ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT)
368+
.metaData(metaData)
369+
.nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build())
370+
.build();
371+
372+
ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE);
373+
374+
SetOnce<Boolean> ranPolicy = new SetOnce<>();
375+
SetOnce<Boolean> moveToMaintenance = new SetOnce<>();
376+
doAnswer(invocationOnMock -> {
377+
ranPolicy.set(true);
378+
throw new AssertionError("invalid invocation");
379+
}).when(clusterService).submitStateUpdateTask(anyString(), any(ExecuteStepsUpdateTask.class));
380+
381+
doAnswer(invocationOnMock -> {
382+
MaintenanceModeUpdateTask task = (MaintenanceModeUpdateTask) invocationOnMock.getArguments()[1];
383+
assertThat(task.getOperationMode(), equalTo(OperationMode.MAINTENANCE));
384+
moveToMaintenance.set(true);
385+
return null;
386+
}).when(clusterService).submitStateUpdateTask(anyString(), any(MaintenanceModeUpdateTask.class));
387+
388+
indexLifecycleService.clusterChanged(event);
389+
assertNull(ranPolicy.get());
390+
assertTrue(moveToMaintenance.get());
391+
}
392+
259393
// /**
260394
// * Checks that a new index does the following successfully:
261395
// *

x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void testUpdateFromNothingToSomethingToNothing() {
109109
new LifecyclePolicyMetadata(newPolicy, headers));
110110
MetaData metaData = MetaData.builder()
111111
.persistentSettings(settings(Version.CURRENT).build())
112-
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.OUT))
112+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.NORMAL))
113113
.build();
114114
String nodeId = randomAlphaOfLength(10);
115115
DiscoveryNode masterNode = DiscoveryNode.createLocal(settings(Version.CURRENT)
@@ -152,7 +152,7 @@ public void testUpdateFromNothingToSomethingToNothing() {
152152
.metaData(
153153
MetaData.builder(metaData)
154154
.putCustom(IndexLifecycleMetadata.TYPE,
155-
new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.OUT))).build();
155+
new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.NORMAL))).build();
156156
registry.update(currentState, client, () -> 0L);
157157
assertTrue(registry.getLifecyclePolicyMap().isEmpty());
158158
assertTrue(registry.getFirstStepMap().isEmpty());
@@ -173,7 +173,7 @@ public void testUpdateChangedPolicy() {
173173
new LifecyclePolicyMetadata(newPolicy, headers));
174174
MetaData metaData = MetaData.builder()
175175
.persistentSettings(settings(Version.CURRENT).build())
176-
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.OUT))
176+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.NORMAL))
177177
.build();
178178
String nodeId = randomAlphaOfLength(10);
179179
DiscoveryNode masterNode = DiscoveryNode.createLocal(settings(Version.CURRENT)
@@ -194,7 +194,7 @@ public void testUpdateChangedPolicy() {
194194
MetaData.builder(metaData)
195195
.putCustom(IndexLifecycleMetadata.TYPE,
196196
new IndexLifecycleMetadata(Collections.singletonMap(policyName,
197-
new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap())), OperationMode.OUT)))
197+
new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap())), OperationMode.NORMAL)))
198198
.build();
199199
registry.update(currentState, client, () -> 0L);
200200
// TODO(talevy): assert changes... right now we do not support updates to policies. will require internal cleanup

0 commit comments

Comments
 (0)