Skip to content

Commit d20d90a

Browse files
authored
Fix testThatNonExistingTemplatesAreAddedImmediately (#51668)
This addresses another race condition that could yield this test flaky.
1 parent c61888f commit d20d90a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ private void addTemplatesIfMissing(ClusterState state) {
140140
creationCheck.set(false);
141141
logger.trace("not adding index template [{}] for [{}], because it already exists", templateName, getOrigin());
142142
}
143+
} else {
144+
logger.trace("skipping the creation of index template [{}] for [{}], because its creation is in progress",
145+
templateName, getOrigin());
143146
}
144147
}
145148
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistryTests.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import static org.elasticsearch.xpack.core.slm.history.SnapshotLifecycleTemplateRegistry.SLM_POLICY_NAME;
6666
import static org.elasticsearch.xpack.core.slm.history.SnapshotLifecycleTemplateRegistry.SLM_TEMPLATE_NAME;
6767
import static org.hamcrest.Matchers.equalTo;
68+
import static org.hamcrest.Matchers.greaterThan;
6869
import static org.hamcrest.Matchers.hasSize;
6970
import static org.hamcrest.Matchers.instanceOf;
7071
import static org.mockito.Mockito.mock;
@@ -136,10 +137,17 @@ public void testThatNonExistingTemplatesAreAddedImmediately() throws Exception {
136137
assertBusy(() -> assertThat(calledTimes.get(), equalTo(registry.getTemplateConfigs().size())));
137138

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

145153
public void testThatNonExistingPoliciesAreAddedImmediately() throws Exception {

0 commit comments

Comments
 (0)