Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix INFO response parsing when line breaks are CR-LF #2162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}