Skip to content

Commit 79a8336

Browse files
authored
Tests: Improve stability and logging of TemplateUpgradeServiceIT tests (#25386)
Relates to #25382
1 parent da0b991 commit 79a8336

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

core/src/main/java/org/elasticsearch/cluster/metadata/TemplateUpgradeService.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ public void clusterChanged(ClusterChangedEvent event) {
120120
return;
121121
}
122122

123-
124123
lastTemplateMetaData = templates;
125124
Optional<Tuple<Map<String, BytesReference>, Set<String>>> changes = calculateTemplateChanges(templates);
126125
if (changes.isPresent()) {
126+
logger.info("Starting template upgrade to version {}, {} templates will be updated and {} will be removed",
127+
Version.CURRENT,
128+
changes.get().v1().size(),
129+
changes.get().v2().size());
127130
if (updatesInProgress.compareAndSet(0, changes.get().v1().size() + changes.get().v2().size())) {
128131
threadPool.generic().execute(() -> updateTemplates(changes.get().v1(), changes.get().v2()));
129132
}
@@ -140,8 +143,12 @@ boolean shouldLocalNodeUpdateTemplates(DiscoveryNodes nodes) {
140143
DiscoveryNode localNode = nodes.getLocalNode();
141144
// Only data and master nodes should update the template
142145
if (localNode.isDataNode() || localNode.isMasterNode()) {
146+
DiscoveryNode masterNode = nodes.getMasterNode();
147+
if (masterNode == null) {
148+
return false;
149+
}
143150
Version maxVersion = nodes.getLargestNonClientNodeVersion();
144-
if (maxVersion.equals(nodes.getMasterNode().getVersion())) {
151+
if (maxVersion.equals(masterNode.getVersion())) {
145152
// If the master has the latest version - we will allow it to handle the update
146153
return nodes.isLocalNodeElectedMaster();
147154
} else {
@@ -171,15 +178,19 @@ void updateTemplates(Map<String, BytesReference> changes, Set<String> deletions)
171178
client.admin().indices().putTemplate(request, new ActionListener<PutIndexTemplateResponse>() {
172179
@Override
173180
public void onResponse(PutIndexTemplateResponse response) {
174-
updatesInProgress.decrementAndGet();
181+
if(updatesInProgress.decrementAndGet() == 0) {
182+
logger.info("Finished upgrading templates to version {}", Version.CURRENT);
183+
}
175184
if (response.isAcknowledged() == false) {
176185
logger.warn("Error updating template [{}], request was not acknowledged", change.getKey());
177186
}
178187
}
179188

180189
@Override
181190
public void onFailure(Exception e) {
182-
updatesInProgress.decrementAndGet();
191+
if(updatesInProgress.decrementAndGet() == 0) {
192+
logger.info("Templates were upgraded to version {}", Version.CURRENT);
193+
}
183194
logger.warn(new ParameterizedMessage("Error updating template [{}]", change.getKey()), e);
184195
}
185196
});

core/src/test/java/org/elasticsearch/cluster/metadata/TemplateUpgradeServiceIT.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.threadpool.ThreadPool;
3333
import org.elasticsearch.watcher.ResourceWatcherService;
3434

35-
import java.util.Arrays;
3635
import java.util.Collection;
3736
import java.util.Collections;
3837
import java.util.List;
@@ -41,8 +40,8 @@
4140
import java.util.function.UnaryOperator;
4241

4342
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
44-
import static org.hamcrest.Matchers.empty;
4543
import static org.hamcrest.Matchers.equalTo;
44+
import static org.hamcrest.Matchers.hasSize;
4645

4746
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST)
4847
public class TemplateUpgradeServiceIT extends ESIntegTestCase {
@@ -105,10 +104,15 @@ public void testTemplateUpdate() throws Exception {
105104
assertAcked(client().admin().indices().preparePutTemplate("test_removed_template").setOrder(1)
106105
.setPatterns(Collections.singletonList("*")).get());
107106

107+
AtomicInteger updateCount = new AtomicInteger();
108108
// Wait for the templates to be updated back to normal
109109
assertBusy(() -> {
110+
// the updates only happen on cluster state updates, so we need to make sure that the cluster state updates are happening
111+
// so we need to simulate updates to make sure the template upgrade kicks in
112+
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(
113+
Settings.builder().put(TestPlugin.UPDATE_TEMPLATE_DUMMY_SETTING.getKey(), updateCount.incrementAndGet())
114+
).get());
110115
List<IndexTemplateMetaData> templates = client().admin().indices().prepareGetTemplates("test_*").get().getIndexTemplates();
111-
assertThat(templates.size(), equalTo(3));
112116
boolean addedFound = false;
113117
boolean changedFound = false;
114118
boolean dummyFound = false;
@@ -133,10 +137,10 @@ public void testTemplateUpdate() throws Exception {
133137
break;
134138
}
135139
}
136-
137140
assertTrue(addedFound);
138141
assertTrue(changedFound);
139142
assertTrue(dummyFound);
143+
assertThat(templates.size(), equalTo(3));
140144
});
141145

142146
// Wipe out all templates
@@ -157,7 +161,6 @@ private void assertTemplates() throws Exception {
157161
).get());
158162

159163
List<IndexTemplateMetaData> templates = client().admin().indices().prepareGetTemplates("test_*").get().getIndexTemplates();
160-
assertThat(templates.size(), equalTo(2));
161164
boolean addedFound = false;
162165
boolean changedFound = false;
163166
for (int i = 0; i < 2; i++) {
@@ -180,6 +183,7 @@ private void assertTemplates() throws Exception {
180183

181184
assertTrue(addedFound);
182185
assertTrue(changedFound);
186+
assertThat(templates, hasSize(2));
183187
});
184188
}
185189

0 commit comments

Comments
 (0)