Skip to content

Commit

Permalink
Fix testThatNonExistingTemplatesAreAddedImmediately (elastic#51668)
Browse files Browse the repository at this point in the history
This addresses another race condition that could yield this test flaky.
  • Loading branch information
andreidan authored Jan 31, 2020
1 parent c61888f commit d20d90a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ private void addTemplatesIfMissing(ClusterState state) {
creationCheck.set(false);
logger.trace("not adding index template [{}] for [{}], because it already exists", templateName, getOrigin());
}
} else {
logger.trace("skipping the creation of index template [{}] for [{}], because its creation is in progress",
templateName, getOrigin());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static org.elasticsearch.xpack.core.slm.history.SnapshotLifecycleTemplateRegistry.SLM_POLICY_NAME;
import static org.elasticsearch.xpack.core.slm.history.SnapshotLifecycleTemplateRegistry.SLM_TEMPLATE_NAME;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -136,10 +137,17 @@ public void testThatNonExistingTemplatesAreAddedImmediately() throws Exception {
assertBusy(() -> assertThat(calledTimes.get(), equalTo(registry.getTemplateConfigs().size())));

calledTimes.set(0);
// now delete one template from the cluster state and lets retry
ClusterChangedEvent newEvent = createClusterChangedEvent(Collections.emptyList(), nodes);
registry.clusterChanged(newEvent);
assertBusy(() -> assertThat(calledTimes.get(), equalTo(1)));

// attempting to register the event multiple times as a race condition can yield this test flaky, namely:
// when calling registry.clusterChanged(newEvent) the templateCreationsInProgress state that the IndexTemplateRegistry maintains
// might've not yet been updated to reflect that the first template registration was complete, so a second template registration
// will not be issued anymore, leaving calledTimes to 0
assertBusy(() -> {
// now delete one template from the cluster state and lets retry
ClusterChangedEvent newEvent = createClusterChangedEvent(Collections.emptyList(), nodes);
registry.clusterChanged(newEvent);
assertThat(calledTimes.get(), greaterThan(1));
});
}

public void testThatNonExistingPoliciesAreAddedImmediately() throws Exception {
Expand Down

0 comments on commit d20d90a

Please sign in to comment.