From ac2884f0344ba0212172833dc4bcc2e03d08e339 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Tue, 16 Jan 2018 10:11:12 +0100 Subject: [PATCH] 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@e0024b5170214f7dc05cd184db9056255887b6e6 --- .../watcher/support/WatcherIndexTemplateRegistry.java | 7 ++++++- .../support/WatcherIndexTemplateRegistryTests.java | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java index abae21dfe65..465ce1a7e6d 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java @@ -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) { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java index 8832aca8290..486a8b48d5e 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java @@ -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 existingTemplateNames, DiscoveryNodes nodes) { ClusterChangedEvent event = mock(ClusterChangedEvent.class); when(event.localNodeMaster()).thenReturn(nodes.isLocalNodeElectedMaster());