Skip to content

Commit 286a82b

Browse files
committed
Redis Cluster: fix CompleteHostAndPortMapper.getHostAndPort + Utils.adjustHost
1 parent f281fac commit 286a82b

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

driver/src/main/java/jdbc/client/impl/RedisJedisURIBase.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,12 @@ public CompleteHostAndPortMapper(@NotNull Map<HostAndPort, HostAndPort> mapping)
267267
@Override
268268
public HostAndPort getHostAndPort(HostAndPort hap) throws JedisConnectionException {
269269
if (hap == null) return null;
270-
HostAndPort mappedHsp = mapping.get(hap);
271-
if (mappedHsp == null)
270+
HostAndPort mappedHap = mapping.get(hap);
271+
if (mappedHap == null) {
272+
if (mapping.containsValue(hap)) return hap;
272273
throw new JedisConnectionException(String.format("Port forwarding is not specified for %s", hap));
273-
return mappedHsp;
274+
}
275+
return mappedHap;
274276
}
275277
}
276278
}

driver/src/main/java/jdbc/client/impl/cluster/RedisJedisClusterURI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static jdbc.properties.RedisDefaultConfig.CONFIG;
1616
import static jdbc.properties.RedisDriverPropertyInfoHelper.MAX_ATTEMPTS;
17+
import static jdbc.utils.Utils.adjustHost;
1718

1819
public class RedisJedisClusterURI extends RedisJedisURIBase {
1920

@@ -60,7 +61,7 @@ protected void setHostAndPort(@NotNull String nodesBlock) {
6061
}
6162
}
6263

63-
nodes.add(new HostAndPort(host, port));
64+
nodes.add(new HostAndPort(adjustHost(host), port));
6465
}
6566

6667
this.nodes = nodes;

driver/src/main/java/jdbc/client/impl/standalone/RedisJedisURI.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.sql.SQLException;
99
import java.util.Properties;
1010

11+
import static jdbc.utils.Utils.adjustHost;
12+
1113
public class RedisJedisURI extends RedisJedisURIBase {
1214

1315
private static final String PREFIX = "jdbc:redis://";
@@ -46,7 +48,7 @@ protected void setHostAndPort(@NotNull String hostAndPortBlock) {
4648
}
4749
}
4850

49-
this.hostAndPort = new HostAndPort(host, port);
51+
this.hostAndPort = new HostAndPort(adjustHost(host), port);
5052
}
5153

5254

driver/src/main/java/jdbc/utils/Utils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,18 @@ private static <T> T getObject(Map<?, ?> map, String name, T defaultValue, Funct
109109

110110
public static @NotNull HostAndPort parseHostAndPort(@NotNull String hostAndPortStr) {
111111
HostAndPort hostAndPort = HostAndPort.from(hostAndPortStr);
112-
String adjustedHost = hostAndPort.getHost().trim();
112+
String host = hostAndPort.getHost();
113+
String adjustedHost = adjustHost(host);
114+
if (adjustedHost.equals(host)) return hostAndPort;
115+
return new HostAndPort(adjustedHost, hostAndPort.getPort());
116+
}
117+
118+
public static @NotNull String adjustHost(@NotNull String host) {
119+
String adjustedHost = host.trim();
113120
if ("localhost".equalsIgnoreCase(adjustedHost)) {
114-
adjustedHost = Protocol.DEFAULT_HOST;
121+
return Protocol.DEFAULT_HOST;
115122
}
116-
if (adjustedHost.equals(hostAndPort.getHost())) return hostAndPort;
117-
return new HostAndPort(adjustedHost, hostAndPort.getPort());
123+
return adjustedHost;
118124
}
119125

120126

0 commit comments

Comments
 (0)