Skip to content

Commit

Permalink
Fix INFO response parsing when line breaks are CR-LF #2161
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
henrique.b.campos authored and mp911de committed Oct 7, 2022
1 parent 0fc10d0 commit b7af96c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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> T matchOrDefault(String info, Pattern pattern, T defaultValue, Function<String, T> 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) {
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

}

0 comments on commit b7af96c

Please sign in to comment.