From 28fff9e6bc9e2470520a8fefed12a63496877755 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 22 Nov 2022 09:11:35 +0100 Subject: [PATCH] Guard NodeTopologyView against absent INFO #2243 --- .../cluster/topology/NodeTopologyView.java | 4 +++ .../topology/NodeTopologyViewsUnitTests.java | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java b/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java index f12c964868..4d396f65df 100644 --- a/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java +++ b/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java @@ -91,6 +91,10 @@ private long getReplicationOffset(String info) { private static T getMatchOrDefault(String haystack, Pattern pattern, Function converter, T defaultValue) { + if (haystack == null) { + return defaultValue; + } + Matcher matcher = pattern.matcher(haystack); if (matcher.find() && LettuceStrings.isNotEmpty(matcher.group(1))) { diff --git a/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java b/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java index 4ebefc660a..7e8ba630bd 100644 --- a/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java +++ b/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java @@ -56,6 +56,31 @@ void shouldReuseKnownUris() { assertThat(clusterNodes).contains(localhost, otherhost, host3); } + @Test + void shouldNotFailOnNullInfo() { + + RedisURI localhost = RedisURI.create("127.0.0.1", 6479); + RedisURI otherhost = RedisURI.create("127.0.0.2", 7000); + + RedisURI host3 = RedisURI.create("127.0.0.3", 7000); + + String viewByLocalhost = "1 127.0.0.1:6479 master,myself - 0 1401258245007 2 connected 8000-11999\n" + + "2 127.0.0.2:7000 master - 111 1401258245007 222 connected 7000 12000 12002-16383\n" + + "3 127.0.0.3:7000 master - 111 1401258245007 222 connected 7000 12000 12002-16383\n"; + + String viewByOtherhost = "1 127.0.0.2:6479 master - 0 1401258245007 2 connected 8000-11999\n" + + "2 127.0.0.2:7000 master,myself - 111 1401258245007 222 connected 7000 12000 12002-16383\n" + + "3 127.0.0.3:7000 master - 111 1401258245007 222 connected 7000 12000 12002-16383\n"; + + NodeTopologyView localhostView = new NodeTopologyView(localhost, viewByLocalhost, null, 0); + NodeTopologyView otherhostView = new NodeTopologyView(otherhost, viewByOtherhost, null, 0); + + NodeTopologyViews nodeTopologyViews = new NodeTopologyViews(Arrays.asList(localhostView, otherhostView)); + + Set clusterNodes = nodeTopologyViews.getClusterNodes(); + assertThat(clusterNodes).contains(localhost, otherhost, host3); + } + @Test void shouldFailWithoutOwnPartition() {