Skip to content

Commit af7fb2b

Browse files
committed
Add a multithreaded stress test of the steps cache
1 parent 3c90f95 commit af7fb2b

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import java.util.Map;
5151
import java.util.SortedMap;
5252
import java.util.TreeMap;
53+
import java.util.concurrent.CountDownLatch;
54+
import java.util.concurrent.atomic.AtomicBoolean;
5355

5456
import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
5557
import static org.hamcrest.Matchers.containsString;
@@ -465,4 +467,71 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except
465467
assertThat(((ShrinkStep) shrinkStep).getNumberOfShards(), equalTo(2));
466468
assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1));
467469
}
470+
471+
public void testGetStepMultithreaded() throws Exception {
472+
Client client = mock(Client.class);
473+
Mockito.when(client.settings()).thenReturn(Settings.EMPTY);
474+
475+
LifecyclePolicy policy = LifecyclePolicyTests.randomTimeseriesLifecyclePolicyWithAllPhases("policy");
476+
String phaseName = randomFrom(policy.getPhases().keySet());
477+
Phase phase = policy.getPhases().get(phaseName);
478+
479+
LifecycleExecutionState lifecycleState = LifecycleExecutionState.builder()
480+
.setPhaseDefinition(Strings.toString(new PhaseExecutionInfo(policy.getName(), phase, 1, randomNonNegativeLong())))
481+
.build();
482+
IndexMetadata indexMetadata = IndexMetadata.builder("test")
483+
.settings(
484+
Settings.builder()
485+
.put("index.number_of_shards", 1)
486+
.put("index.number_of_replicas", 0)
487+
.put("index.version.created", Version.CURRENT)
488+
.put(LifecycleSettings.LIFECYCLE_NAME, "policy")
489+
.build()
490+
)
491+
.putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap())
492+
.build();
493+
494+
SortedMap<String, LifecyclePolicyMetadata> metas = new TreeMap<>();
495+
metas.put("policy", new LifecyclePolicyMetadata(policy, Collections.emptyMap(), 1, randomNonNegativeLong()));
496+
IndexLifecycleMetadata meta = new IndexLifecycleMetadata(metas, OperationMode.RUNNING);
497+
498+
PolicyStepsRegistry registry = new PolicyStepsRegistry(REGISTRY, client, null);
499+
registry.update(meta);
500+
501+
// test a variety of getStep calls with random actions and steps
502+
for (int i = 0; i < scaledRandomIntBetween(100, 1000); i++) {
503+
LifecycleAction action = randomValueOtherThan(MigrateAction.DISABLED, () -> randomFrom(phase.getActions().values()));
504+
Step step = randomFrom(action.toSteps(client, phaseName, MOCK_STEP_KEY, null));
505+
Step actualStep = registry.getStep(indexMetadata, step.getKey());
506+
assertThat(actualStep.getKey(), equalTo(step.getKey()));
507+
}
508+
509+
final CountDownLatch latch = new CountDownLatch(1);
510+
final AtomicBoolean done = new AtomicBoolean(false);
511+
512+
// now, in another thread, update the registry repeatedly as fast as possible.
513+
// updating the registry has the side effect of clearing the cache.
514+
new Thread(() -> {
515+
latch.countDown(); // signal that we're starting
516+
while (done.get() == false) {
517+
registry.update(meta);
518+
}
519+
}).start();
520+
521+
try {
522+
latch.await(); // wait until the other thread started
523+
524+
// and, while the cache is being repeatedly cleared,
525+
// test a variety of getStep calls with random actions and steps
526+
for (int i = 0; i < scaledRandomIntBetween(100, 1000); i++) {
527+
LifecycleAction action = randomValueOtherThan(MigrateAction.DISABLED, () -> randomFrom(phase.getActions().values()));
528+
Step step = randomFrom(action.toSteps(client, phaseName, MOCK_STEP_KEY, null));
529+
Step actualStep = registry.getStep(indexMetadata, step.getKey());
530+
assertThat(actualStep.getKey(), equalTo(step.getKey()));
531+
}
532+
} finally {
533+
// tell the other thread we're finished
534+
done.set(true);
535+
}
536+
}
468537
}

0 commit comments

Comments
 (0)