Skip to content

Commit

Permalink
Polishing #2161
Browse files Browse the repository at this point in the history
Reformat code. Tweak method names. Reorder method signatures.

Original pull request: #2162.
  • Loading branch information
mp911de committed Oct 7, 2022
1 parent f3e57c2 commit b9bd408
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package io.lettuce.core.cluster.topology;

import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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;
import io.lettuce.core.internal.LettuceStrings;

/**
* @author Mark Paluch
Expand All @@ -47,8 +48,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 static final Pattern CONNECTED_CLIENTS_PATTERN = patternFor("connected_clients");

private static final Pattern MASTER_REPL_OFFSET_PATTERN = patternFor("master_repl_offset");

private NodeTopologyView(RedisURI redisURI) {

Expand All @@ -75,25 +77,27 @@ private NodeTopologyView(RedisURI redisURI) {
this.latency = latency;
}

private static Pattern makePatternForProperty(String propertyName) {
return Pattern.compile("^" + Pattern.quote(propertyName) + ":(.*)$", Pattern.MULTILINE);
}

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 static Pattern patternFor(String propertyName) {
return Pattern.compile(String.format("^%s:(.*)$", Pattern.quote(propertyName)), Pattern.MULTILINE);
}

private int getClientCount(String info) {
return matchOrDefault(info, CONNECTED_CLIENTS_PATTERN, 0, Integer::parseInt);
return getMatchOrDefault(info, CONNECTED_CLIENTS_PATTERN, Integer::parseInt, 0);
}

private long getReplicationOffset(String info) {
return matchOrDefault(info, MASTER_REPL_OFFSET_PATTERN, -1L, Long::parseLong);
return getMatchOrDefault(info, MASTER_REPL_OFFSET_PATTERN, Long::parseLong, -1L);
}

private static <T> T getMatchOrDefault(String haystack, Pattern pattern, Function<String, T> converter, T defaultValue) {

Matcher matcher = pattern.matcher(haystack);

if (matcher.find() && LettuceStrings.isNotEmpty(matcher.group(1))) {
return converter.apply(matcher.group(1));
}

return defaultValue;
}

static NodeTopologyView from(RedisURI redisURI, Requests clusterNodesRequests, Requests infoRequests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
*/
package io.lettuce.core.cluster.topology;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;

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;

/**
* Unit tests for {@link NodeTopologyViews}.
*
* @author Mark Paluch
*/
class NodeTopologyViewsUnitTests {
Expand Down Expand Up @@ -63,21 +63,24 @@ void shouldFailWithoutOwnPartition() {

String viewByLocalhost = "1 127.0.0.1:6479 master - 0 1401258245007 2 connected 8000-11999\n";

assertThatThrownBy(() -> new NodeTopologyView(localhost, viewByLocalhost, "", 0).getOwnPartition()).isInstanceOf(
IllegalStateException.class);
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";
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());

assertThat(nodeTopologyView.getConnectedClients()).isEqualTo(2);
assertThat(nodeTopologyView.getReplicationOffset()).isEqualTo(5);
}

}

0 comments on commit b9bd408

Please sign in to comment.