Watcher: Fix NPE in watcher index template registry (elastic/x-pack-elasticsearch#3571)

The current code throws an NPE, when there is no master node available
in the cluster state. This commit adds an additional check for an
existing master node.

relates elastic/x-pack-elasticsearch#3569

Original commit: elastic/x-pack-elasticsearch@e0024b5170
This commit is contained in:
Alexander Reelsen 2018-01-16 10:11:12 +01:00 committed by GitHub
parent 6c6dcafd0e
commit ac2884f034
2 changed files with 17 additions and 1 deletions

View File

@ -81,10 +81,15 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C
return;
}
// no master node, exit immediately
DiscoveryNode masterNode = event.state().getNodes().getMasterNode();
if (masterNode == null) {
return;
}
// if this node is newer than the master node, we probably need to add the history template, which might be newer than the
// history template the master node has, so we need potentially add new templates despite being not the master node
DiscoveryNode localNode = event.state().getNodes().getLocalNode();
DiscoveryNode masterNode = event.state().getNodes().getMasterNode();
boolean localNodeVersionAfterMaster = localNode.getVersion().after(masterNode.getVersion());
if (event.localNodeMaster() || localNodeVersionAfterMaster) {

View File

@ -128,6 +128,17 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
verifyZeroInteractions(client);
}
public void testThatMissingMasterNodeDoesNothing() {
DiscoveryNode localNode = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").add(localNode).build();
ClusterChangedEvent event = createClusterChangedEvent(Arrays.asList(WatcherIndexTemplateRegistry.TRIGGERED_TEMPLATE_NAME,
WatcherIndexTemplateRegistry.WATCHES_TEMPLATE_NAME, ".watch-history-6"), nodes);
registry.clusterChanged(event);
verifyZeroInteractions(client);
}
private ClusterChangedEvent createClusterChangedEvent(List<String> existingTemplateNames, DiscoveryNodes nodes) {
ClusterChangedEvent event = mock(ClusterChangedEvent.class);
when(event.localNodeMaster()).thenReturn(nodes.isLocalNodeElectedMaster());