From b7af96c6832cfc62d1390956d44aab7ae2edb4cc Mon Sep 17 00:00:00 2001 From: "henrique.b.campos" Date: Thu, 14 Jul 2022 10:10:55 -0300 Subject: [PATCH] Fix INFO response parsing when line breaks are CR-LF #2161 INFO command parsing inside NodeTopologyView used to throw when encountering '\' characters due to the use of the Java Property parser. This happens when using some Redis on Windows forks due to the Windows path separator being '\'. Original pull request: #2162. --- .../cluster/topology/NodeTopologyView.java | 49 ++++++++++--------- .../topology/NodeTopologyViewsUnitTests.java | 15 ++++++ 2 files changed, 41 insertions(+), 23 deletions(-) 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 d6cca46409..af0265ef17 100644 --- a/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java +++ b/src/main/java/io/lettuce/core/cluster/topology/NodeTopologyView.java @@ -15,16 +15,16 @@ */ package io.lettuce.core.cluster.topology; -import java.io.IOException; -import java.io.StringReader; -import java.util.Properties; - import io.lettuce.core.RedisFuture; import io.lettuce.core.RedisURI; import io.lettuce.core.cluster.models.partitions.ClusterPartitionParser; import io.lettuce.core.cluster.models.partitions.Partitions; import io.lettuce.core.cluster.models.partitions.RedisClusterNode; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * @author Mark Paluch * @author Xujs @@ -47,6 +47,9 @@ class NodeTopologyView { private final String info; + private static final Pattern CONNECTED_CLIENTS_PATTERN = makePatternForProperty("connected_clients"); + private static final Pattern MASTER_REPL_OFFSET_PATTERN = makePatternForProperty("master_repl_offset"); + private NodeTopologyView(RedisURI redisURI) { this.available = false; @@ -64,21 +67,33 @@ private NodeTopologyView(RedisURI redisURI) { this.available = true; this.redisURI = redisURI; - Properties properties = info == null ? new Properties() : parseInfo(info); this.partitions = ClusterPartitionParser.parse(clusterNodes); - this.connectedClients = getClientCount(properties); - this.replicationOffset = getReplicationOffset(properties); + this.connectedClients = getClientCount(info); + this.replicationOffset = getReplicationOffset(info); this.clusterNodes = clusterNodes; this.info = info; this.latency = latency; } - private int getClientCount(Properties properties) { - return Integer.parseInt(properties.getProperty("connected_clients", "0")); + private static Pattern makePatternForProperty(String propertyName) { + return Pattern.compile("^" + Pattern.quote(propertyName) + ":(.*)$", Pattern.MULTILINE); } - private long getReplicationOffset(Properties properties) { - return Long.parseLong(properties.getProperty("master_repl_offset", "-1")); + private static T matchOrDefault(String info, Pattern pattern, T defaultValue, Function converterIfFound) { + Matcher matcher = pattern.matcher(info); + if(!matcher.find()) { + return defaultValue; + } + String foundValue = matcher.group(1); + return foundValue == null ? defaultValue : converterIfFound.apply(foundValue); + } + + private int getClientCount(String info) { + return matchOrDefault(info, CONNECTED_CLIENTS_PATTERN, 0, Integer::parseInt); + } + + private long getReplicationOffset(String info) { + return matchOrDefault(info, MASTER_REPL_OFFSET_PATTERN, -1L, Long::parseLong); } static NodeTopologyView from(RedisURI redisURI, Requests clusterNodesRequests, Requests infoRequests) { @@ -109,18 +124,6 @@ private static boolean resultAvailable(RedisFuture redisFuture) { return false; } - private static Properties parseInfo(String info) { - - Properties properties = new Properties(); - try { - properties.load(new StringReader(info)); - } catch (IOException e) { - // wtf? - } - - return properties; - } - String getNodeId() { return getOwnPartition().getNodeId(); } 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 d997d2f0f3..9e4f363a11 100644 --- a/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java +++ b/src/test/java/io/lettuce/core/cluster/topology/NodeTopologyViewsUnitTests.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Set; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import io.lettuce.core.RedisURI; @@ -65,4 +66,18 @@ void shouldFailWithoutOwnPartition() { assertThatThrownBy(() -> new NodeTopologyView(localhost, viewByLocalhost, "", 0).getOwnPartition()).isInstanceOf( IllegalStateException.class); } + + @Test + void infoParsingShouldNotFailWithWindowsPaths() { + RedisURI localhost = RedisURI.create("127.0.0.1", 6479); + String viewByLocalhost = "1 127.0.0.1:6479 master - 0 1401258245007 2 connected 8000-11999\n"; + String info = "executable:c:\\users\\user~1.after\\appdata\\local\\temp\\1657742252598-0\\redis-server-7.0.2.exe\n" + + "connected_clients:2\n" + + "config_file:C:\\Users\\user~1.after\\AppData\\Local\\Temp\\redis-server_496893189609231874520793.conf\n" + + "master_repl_offset:5\n"; + NodeTopologyView nodeTopologyView = new NodeTopologyView(localhost, viewByLocalhost, info, 0); + Assertions.assertEquals(2, nodeTopologyView.getConnectedClients()); + Assertions.assertEquals(5, nodeTopologyView.getReplicationOffset()); + } + }