From f4c4fbcafc5943fac6e1553bf469a3336e563b85 Mon Sep 17 00:00:00 2001 From: Philipp Trulson Date: Wed, 26 Aug 2020 10:03:42 +0200 Subject: [PATCH] SOLR-14752: Fix error in Zookeeper status when Prometheus plugin is enabled in ZK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Høydahl --- solr/CHANGES.txt | 2 ++ .../handler/admin/ZookeeperStatusHandler.java | 4 +-- .../admin/ZookeeperStatusHandlerTest.java | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 9a0a7b7cd53..cc22c798043 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -195,6 +195,8 @@ Bug Fixes * SOLR-14700: Avoid NullPointerException in TupleStream.getShards() when streamContext is null. (Mads Bondo Dydensborg, Christine Poerschke, Mike Drob) +* SOLR-14752: Fix error in Zookeeper status when Prometheus plugin is enabled in ZK (Philipp Trulson via janhoy) + Other Changes --------------------- diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java index 62171cb20b2..8129e56b37f 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java @@ -156,8 +156,8 @@ public class ZookeeperStatusHandler extends RequestHandlerBase { } else if ("leader".equals(state)) { leaders++; reportedFollowers = Math.max( - Integer.parseInt((String) stat.getOrDefault("zk_followers", "0")), - Integer.parseInt((String) stat.getOrDefault("zk_synced_followers", "0")) + (int) Float.parseFloat((String) stat.getOrDefault("zk_followers", "0")), + (int) Float.parseFloat((String) stat.getOrDefault("zk_synced_followers", "0")) ); } else if ("standalone".equals(state)) { standalone++; diff --git a/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java index cf2dd744fde..24145819d1f 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java @@ -211,4 +211,39 @@ public class ZookeeperStatusHandlerTest extends SolrCloudTestCase { " \"status\":\"red\"}"; assertEquals(expected, JSONUtil.toJSON(mockStatus)); } + + @Test + public void testZkWithPrometheusSolr14752() { + assumeWorkingMockito(); + ZookeeperStatusHandler zkStatusHandler = mock(ZookeeperStatusHandler.class); + when(zkStatusHandler.getZkRawResponse("zoo1:2181", "ruok")).thenReturn(Arrays.asList("imok")); + when(zkStatusHandler.getZkRawResponse("zoo1:2181", "mntr")).thenReturn( + Arrays.asList("zk_version\t3.6.1--104dcb3e3fb464b30c5186d229e00af9f332524b, built on 04/21/2020 15:01 GMT", + "zk_avg_latency\t0.24", + "zk_server_state\tleader", + "zk_synced_followers\t0.0")); + when(zkStatusHandler.getZkRawResponse("zoo1:2181", "conf")).thenReturn( + Arrays.asList("clientPort=2181")); + when(zkStatusHandler.getZkStatus(anyString(), any())).thenCallRealMethod(); + when(zkStatusHandler.monitorZookeeper(anyString())).thenCallRealMethod(); + when(zkStatusHandler.validateZkRawResponse(ArgumentMatchers.any(), any(), any())).thenAnswer(Answers.CALLS_REAL_METHODS); + + // Verifying that parsing the status strings with floating point no longer triggers a NumberFormatException, although floats are still displayed in UI + Map mockStatus = zkStatusHandler.getZkStatus("zoo1:2181", ZkDynamicConfig.fromZkConnectString("zoo1:2181")); + String expected = "{\n" + + " \"mode\":\"ensemble\",\n" + + " \"dynamicReconfig\":true,\n" + + " \"ensembleSize\":1,\n" + + " \"details\":[{\n" + + " \"zk_synced_followers\":\"0.0\",\n" + + " \"zk_version\":\"3.6.1--104dcb3e3fb464b30c5186d229e00af9f332524b, built on 04/21/2020 15:01 GMT\",\n" + + " \"zk_avg_latency\":\"0.24\",\n" + + " \"host\":\"zoo1:2181\",\n" + + " \"clientPort\":\"2181\",\n" + + " \"ok\":true,\n" + + " \"zk_server_state\":\"leader\"}],\n" + + " \"zkHost\":\"zoo1:2181\",\n" + + " \"status\":\"green\"}"; + assertEquals(expected, JSONUtil.toJSON(mockStatus)); + } } \ No newline at end of file