Skip to content

Commit 57f6cf6

Browse files
committed
TemplateUpgradeService should only run on the master (#27294)
The `TemplateUpgradeService` allows plugins to register a call back that mutates index templates upon recovery. This is handy for upgrade logic that needs to make sure that an existing index template is updated once the cluster is upgraded to a new version of the plugin (and ES). Currently, the service has complicated logic to decide which node should perform the upgrade. It will prefer the master node, if it is of the highest version of the cluster and otherwise it will fall back to one of the non-coordinating nodes which are on the latest version. While this attempts to make sure that new nodes can assume their template version is in place (but old node still need to be able to operate under both old and new template), it has an inherent problem in that the master (on an old version) may not be able to process the put template request with the new template - it may miss certain features. This PR changes the logic to be simpler and always rely on the current master nodes. This comes at the price that new nodes need to operate both with old templates and new. That price is small as they need to operate with old indices regardless of the template. On the flip side we reduce a lot of complexity in what can happen in the cluster.
1 parent 9a2ec69 commit 57f6cf6

File tree

2 files changed

+1
-83
lines changed

2 files changed

+1
-83
lines changed

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void clusterChanged(ClusterChangedEvent event) {
116116
return;
117117
}
118118

119-
if (shouldLocalNodeUpdateTemplates(state.nodes()) == false) {
119+
if (state.nodes().isLocalNodeElectedMaster() == false) {
120120
return;
121121
}
122122

@@ -133,43 +133,6 @@ public void clusterChanged(ClusterChangedEvent event) {
133133
}
134134
}
135135

136-
/**
137-
* Checks if the current node should update the templates
138-
*
139-
* If the master has the newest verison in the cluster - it will be dedicated template updater.
140-
* Otherwise the node with the highest id among nodes with the highest version should update the templates
141-
*/
142-
boolean shouldLocalNodeUpdateTemplates(DiscoveryNodes nodes) {
143-
DiscoveryNode localNode = nodes.getLocalNode();
144-
// Only data and master nodes should update the template
145-
if (localNode.isDataNode() || localNode.isMasterNode()) {
146-
DiscoveryNode masterNode = nodes.getMasterNode();
147-
if (masterNode == null) {
148-
return false;
149-
}
150-
Version maxVersion = nodes.getLargestNonClientNodeVersion();
151-
if (maxVersion.equals(masterNode.getVersion())) {
152-
// If the master has the latest version - we will allow it to handle the update
153-
return nodes.isLocalNodeElectedMaster();
154-
} else {
155-
if (maxVersion.equals(localNode.getVersion()) == false) {
156-
// The localhost node doesn't have the latest version - not going to update
157-
return false;
158-
}
159-
for (ObjectCursor<DiscoveryNode> node : nodes.getMasterAndDataNodes().values()) {
160-
if (node.value.getVersion().equals(maxVersion) && node.value.getId().compareTo(localNode.getId()) > 0) {
161-
// We have a node with higher id then mine - it should update
162-
return false;
163-
}
164-
}
165-
// We have the highest version and highest id - we should perform the update
166-
return true;
167-
}
168-
} else {
169-
return false;
170-
}
171-
}
172-
173136
void updateTemplates(Map<String, BytesReference> changes, Set<String> deletions) {
174137
for (Map.Entry<String, BytesReference> change : changes.entrySet()) {
175138
PutIndexTemplateRequest request =

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -341,51 +341,6 @@ public void testClusterStateUpdate() {
341341

342342
private static final int NODE_TEST_ITERS = 100;
343343

344-
public void testOnlyOneNodeRunsTemplateUpdates() {
345-
TemplateUpgradeService service = new TemplateUpgradeService(Settings.EMPTY, null, clusterService, null, Collections.emptyList());
346-
for (int i = 0; i < NODE_TEST_ITERS; i++) {
347-
int nodesCount = randomIntBetween(1, 10);
348-
int clientNodesCount = randomIntBetween(0, 4);
349-
DiscoveryNodes nodes = randomNodes(nodesCount, clientNodesCount);
350-
int updaterNode = -1;
351-
for (int j = 0; j < nodesCount; j++) {
352-
DiscoveryNodes localNodes = DiscoveryNodes.builder(nodes).localNodeId(nodes.resolveNode("node_" + j).getId()).build();
353-
if (service.shouldLocalNodeUpdateTemplates(localNodes)) {
354-
assertThat("Expected only one node to update template, found " + updaterNode + " and " + j, updaterNode, lessThan(0));
355-
updaterNode = j;
356-
}
357-
}
358-
assertThat("Expected one node to update template", updaterNode, greaterThanOrEqualTo(0));
359-
}
360-
}
361-
362-
public void testIfMasterHasTheHighestVersionItShouldRunsTemplateUpdates() {
363-
for (int i = 0; i < NODE_TEST_ITERS; i++) {
364-
int nodesCount = randomIntBetween(1, 10);
365-
int clientNodesCount = randomIntBetween(0, 4);
366-
DiscoveryNodes nodes = randomNodes(nodesCount, clientNodesCount);
367-
DiscoveryNodes.Builder builder = DiscoveryNodes.builder(nodes).localNodeId(nodes.resolveNode("_master").getId());
368-
nodes = builder.build();
369-
TemplateUpgradeService service = new TemplateUpgradeService(Settings.EMPTY, null, clusterService, null,
370-
Collections.emptyList());
371-
assertThat(service.shouldLocalNodeUpdateTemplates(nodes),
372-
equalTo(nodes.getLargestNonClientNodeVersion().equals(nodes.getMasterNode().getVersion())));
373-
}
374-
}
375-
376-
public void testClientNodeDontRunTemplateUpdates() {
377-
for (int i = 0; i < NODE_TEST_ITERS; i++) {
378-
int nodesCount = randomIntBetween(1, 10);
379-
int clientNodesCount = randomIntBetween(1, 4);
380-
DiscoveryNodes nodes = randomNodes(nodesCount, clientNodesCount);
381-
int testClient = randomIntBetween(0, clientNodesCount - 1);
382-
DiscoveryNodes.Builder builder = DiscoveryNodes.builder(nodes).localNodeId(nodes.resolveNode("client_" + testClient).getId());
383-
TemplateUpgradeService service = new TemplateUpgradeService(Settings.EMPTY, null, clusterService, null,
384-
Collections.emptyList());
385-
assertThat(service.shouldLocalNodeUpdateTemplates(builder.build()), equalTo(false));
386-
}
387-
}
388-
389344
private DiscoveryNodes randomNodes(int dataAndMasterNodes, int clientNodes) {
390345
DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
391346
String masterNodeId = null;

0 commit comments

Comments
 (0)