-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Replace failing PolicyStepsRegistry sanity check assert with a test #85346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
3c90f95
af7fb2b
a2fda5a
0b4e18c
ae1eda6
d7a063c
ae5df84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,8 @@ | |
| import java.util.Map; | ||
| import java.util.SortedMap; | ||
| import java.util.TreeMap; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
@@ -465,4 +467,71 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except | |
| assertThat(((ShrinkStep) shrinkStep).getNumberOfShards(), equalTo(2)); | ||
| assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1)); | ||
| } | ||
|
|
||
| public void testGetStepMultithreaded() throws Exception { | ||
| Client client = mock(Client.class); | ||
| Mockito.when(client.settings()).thenReturn(Settings.EMPTY); | ||
|
|
||
| LifecyclePolicy policy = LifecyclePolicyTests.randomTimeseriesLifecyclePolicyWithAllPhases("policy"); | ||
| String phaseName = randomFrom(policy.getPhases().keySet()); | ||
| Phase phase = policy.getPhases().get(phaseName); | ||
|
|
||
| LifecycleExecutionState lifecycleState = LifecycleExecutionState.builder() | ||
| .setPhaseDefinition(Strings.toString(new PhaseExecutionInfo(policy.getName(), phase, 1, randomNonNegativeLong()))) | ||
| .build(); | ||
| IndexMetadata indexMetadata = IndexMetadata.builder("test") | ||
| .settings( | ||
| Settings.builder() | ||
| .put("index.number_of_shards", 1) | ||
| .put("index.number_of_replicas", 0) | ||
| .put("index.version.created", Version.CURRENT) | ||
| .put(LifecycleSettings.LIFECYCLE_NAME, "policy") | ||
| .build() | ||
| ) | ||
| .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap()) | ||
| .build(); | ||
|
|
||
| SortedMap<String, LifecyclePolicyMetadata> metas = new TreeMap<>(); | ||
| metas.put("policy", new LifecyclePolicyMetadata(policy, Collections.emptyMap(), 1, randomNonNegativeLong())); | ||
| IndexLifecycleMetadata meta = new IndexLifecycleMetadata(metas, OperationMode.RUNNING); | ||
|
|
||
| PolicyStepsRegistry registry = new PolicyStepsRegistry(REGISTRY, client, null); | ||
| registry.update(meta); | ||
|
|
||
| // test a variety of getStep calls with random actions and steps | ||
| for (int i = 0; i < scaledRandomIntBetween(100, 1000); i++) { | ||
| LifecycleAction action = randomValueOtherThan(MigrateAction.DISABLED, () -> randomFrom(phase.getActions().values())); | ||
| Step step = randomFrom(action.toSteps(client, phaseName, MOCK_STEP_KEY, null)); | ||
| Step actualStep = registry.getStep(indexMetadata, step.getKey()); | ||
| assertThat(actualStep.getKey(), equalTo(step.getKey())); | ||
| } | ||
|
|
||
| final CountDownLatch latch = new CountDownLatch(1); | ||
| final AtomicBoolean done = new AtomicBoolean(false); | ||
|
|
||
| // now, in another thread, update the registry repeatedly as fast as possible. | ||
| // updating the registry has the side effect of clearing the cache. | ||
| new Thread(() -> { | ||
| latch.countDown(); // signal that we're starting | ||
| while (done.get() == false) { | ||
| registry.update(meta); | ||
| } | ||
| }).start(); | ||
|
|
||
| try { | ||
| latch.await(); // wait until the other thread started | ||
|
|
||
| // and, while the cache is being repeatedly cleared, | ||
| // test a variety of getStep calls with random actions and steps | ||
| for (int i = 0; i < scaledRandomIntBetween(100, 1000); i++) { | ||
| LifecycleAction action = randomValueOtherThan(MigrateAction.DISABLED, () -> randomFrom(phase.getActions().values())); | ||
| Step step = randomFrom(action.toSteps(client, phaseName, MOCK_STEP_KEY, null)); | ||
| Step actualStep = registry.getStep(indexMetadata, step.getKey()); | ||
| assertThat(actualStep.getKey(), equalTo(step.getKey())); | ||
| } | ||
| } finally { | ||
| // tell the other thread we're finished | ||
| done.set(true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super minor nit, but in the event this is the very last test run in a suite (or it's just run by itself), it's possible our thread leak detector will complain since we aren't doing a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing, I'll get that in.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused as to what this tests. There'll be a new step added, by-passing the cache, on every iteration - I'm quite confused specifically as we don't have any steps defined/registered before, yet we do setup some metadatas (both IndexMetadata and IndexLifecycleMetadata). Apologies if I'm missing something very obvious but could we document the intent here? (I'm guessing we want to populate the cache? )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough -- I spent a bit of time stressing over this exact point. It's a little bit tricky, I don't think you're missing something obvious. I'll add some comments and let's see where that gets us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ae5df84