Skip to content

Commit eb6d98d

Browse files
committed
Redis Cluster: update error messages + suggest correct URL prefix if the connection mode does not match the server mode
1 parent e6d2377 commit eb6d98d

File tree

10 files changed

+37
-17
lines changed

10 files changed

+37
-17
lines changed

driver/src/main/java/jdbc/RedisConnection.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jdbc;
22

33
import jdbc.client.RedisClient;
4+
import jdbc.client.RedisClientFactory;
45
import jdbc.client.RedisMode;
56
import org.jetbrains.annotations.NotNull;
67

@@ -9,6 +10,8 @@
910
import java.util.Properties;
1011
import java.util.concurrent.Executor;
1112

13+
import static jdbc.utils.Utils.toLowerCase;
14+
1215
public class RedisConnection implements Connection {
1316

1417
private final RedisDriver driver;
@@ -25,7 +28,7 @@ public RedisConnection(RedisDriver driver, RedisClient client) {
2528
private static final String UNKNOWN_SERVER_MODE_MESSAGE = "Unable to get the server mode" +
2629
" using the \"INFO server\" command to check if the connection mode matches the server mode.";
2730

28-
public void checkConnectionMode() throws SQLException {
31+
public void checkMode() throws SQLException {
2932
RedisMode serverMode;
3033
try {
3134
serverMode = getMetaData().getDatabaseProductMode();
@@ -35,9 +38,12 @@ public void checkConnectionMode() throws SQLException {
3538
if (serverMode == null)
3639
throw new SQLException(UNKNOWN_SERVER_MODE_MESSAGE);
3740
RedisMode clientMode = client.getMode();
38-
if (clientMode != serverMode)
39-
throw new SQLException(String.format("The connection mode \"%s\" does not match the server mode \"%s\".",
40-
clientMode.name(), serverMode.name()));
41+
if (clientMode != serverMode) {
42+
String serverURLPrefix = RedisClientFactory.getURLPrefix(serverMode);
43+
String urlFix = serverURLPrefix != null ? String.format("\nPlease change the URL prefix to \"%s\".", serverURLPrefix) : null;
44+
throw new SQLException(String.format("The connection mode \"%s\" does not match the server mode \"%s\".%s",
45+
toLowerCase(clientMode.name()), toLowerCase(serverMode.name()), urlFix));
46+
}
4147
}
4248

4349
private void checkClosed() throws SQLException {

driver/src/main/java/jdbc/RedisDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Connection connect(String url, Properties info) throws SQLException {
3939

4040
private static void checkConnectionModeIfNeeded(Properties info, @NotNull RedisConnection connection) throws SQLException {
4141
if (!getBoolean(info, VERIFY_CONNECTION_MODE, VERIFY_CONNECTION_MODE_DEFAULT)) return;
42-
connection.checkConnectionMode();
42+
connection.checkMode();
4343
}
4444

4545
@Override

driver/src/main/java/jdbc/client/RedisClientFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import jdbc.client.impl.cluster.RedisJedisClusterURI;
55
import jdbc.client.impl.standalone.RedisJedisClient;
66
import jdbc.client.impl.standalone.RedisJedisURI;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
79

810
import java.sql.SQLException;
911
import java.util.Properties;
@@ -17,9 +19,22 @@ public static boolean acceptsURL(String url) {
1719
return RedisJedisURI.acceptsURL(url) || RedisJedisClusterURI.acceptsURL(url);
1820
}
1921

22+
@Nullable
2023
public static RedisClient create(String url, Properties info) throws SQLException {
2124
if (RedisJedisURI.acceptsURL(url)) return new RedisJedisClient(new RedisJedisURI(url, info));
2225
if (RedisJedisClusterURI.acceptsURL(url)) return new RedisJedisClusterClient(new RedisJedisClusterURI(url, info));
2326
return null;
2427
}
28+
29+
@Nullable
30+
public static String getURLPrefix(@NotNull RedisMode mode) {
31+
switch (mode) {
32+
case STANDALONE:
33+
return RedisJedisURI.PREFIX;
34+
case CLUSTER:
35+
return RedisJedisClusterURI.PREFIX;
36+
default:
37+
return null;
38+
}
39+
}
2540
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public HostAndPort getHostAndPort(HostAndPort hap) throws JedisConnectionExcepti
270270
HostAndPort mappedHap = mapping.get(hap);
271271
if (mappedHap == null) {
272272
if (mapping.containsValue(hap)) return hap;
273-
throw new JedisConnectionException(String.format("Port forwarding is not specified for %s", hap));
273+
throw new JedisConnectionException(String.format("Port forwarding is not specified for %s.", hap));
274274
}
275275
return mappedHap;
276276
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private synchronized Object execute(@NotNull HostAndPort nodeHostAndPort,
6868
ConnectionPool nodePool = nodes.get(nodeKey);
6969
Connection nodeConnection = nodePool != null ? nodePool.getResource() : null;
7070
if (nodeConnection == null)
71-
throw new SQLException(String.format("Cluster node not found: %s", nodeHostAndPort));
71+
throw new SQLException(String.format("Cluster node not found: %s.", nodeHostAndPort));
7272
return nodeConnection;
7373
}
7474

@@ -82,8 +82,8 @@ protected Object executeImpl(@NotNull RedisKeyPatternQuery query) throws SQLExce
8282
private static void checkSupportInClusterMode(@NotNull RedisKeyPatternQuery query) throws SQLException {
8383
String keyPattern = query.getKeyPattern();
8484
if (keyPattern == null || !JedisClusterHashTag.isClusterCompliantMatchPattern(keyPattern))
85-
throw new SQLException(String.format("Cluster mode only supports %s command"
86-
+ " with pattern containing hash-tag ( curly-brackets enclosed string )", query.getCommand()));
85+
throw new SQLException(String.format("Cluster mode only supports the %s command"
86+
+ " with a pattern containing a hash-tag (curly-brackets enclosed string).", query.getCommand()));
8787
}
8888

8989

@@ -107,15 +107,14 @@ protected synchronized Object executeImpl(@NotNull RedisQuery query) throws SQLE
107107
private static void checkSupportInClusterMode(@NotNull RedisQuery query) throws SQLException {
108108
Command command = query.getCommand();
109109
if (UNSUPPORTED_COMMANDS.contains(command))
110-
throw new SQLException(String.format("Cluster mode does not support %s command", command));
110+
throw new SQLException(String.format("Cluster mode does not support the %s command.", command));
111111
}
112112

113113

114114
@Override
115115
protected String setDatabase(int index) {
116116
if (index == 0) return "OK";
117-
// TODO (cluster): unify
118-
throw new JedisDataException("ERR SELECT is not allowed in cluster mode");
117+
throw new JedisDataException("ERR DB index is out of range");
119118
}
120119

121120
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
public class RedisJedisClusterURI extends RedisJedisURIBase {
2020

21-
private static final String PREFIX = "jdbc:redis:cluster://";
21+
public static final String PREFIX = "jdbc:redis:cluster://";
2222

2323
public static boolean acceptsURL(String url) {
2424
return url != null && url.startsWith(PREFIX);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
public class RedisJedisURI extends RedisJedisURIBase {
1414

15-
private static final String PREFIX = "jdbc:redis://";
15+
public static final String PREFIX = "jdbc:redis://";
1616

1717
public static boolean acceptsURL(String url) {
1818
return url != null && url.startsWith(PREFIX);

driver/src/main/java/jdbc/properties/RedisDriverPropertyInfoHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static DriverPropertyInfo[] getPropertyInfo() {
4545
"Configure a connection that uses SSL but does not verify the identity of the server.", booleanChoices);
4646
addPropInfo(propInfos, HOST_AND_PORT_MAPPING, HOST_AND_PORT_MAPPING_DEFAULT, "Host and port mapping.");
4747
addPropInfo(propInfos, VERIFY_CONNECTION_MODE, String.valueOf(VERIFY_CONNECTION_MODE_DEFAULT),
48-
"Verify that mode specified for a connection in the URL scheme matches the server mode.", booleanChoices);
48+
"Verify that the mode specified for a connection in the URL prefix matches the server mode (standalone, cluster, sentinel).", booleanChoices);
4949
return propInfos.toArray(new DriverPropertyInfo[0]);
5050
}
5151

driver/src/main/java/jdbc/resultset/RedisResultSetBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public Object getObject(String columnLabel) throws SQLException {
306306
public int findColumn(String columnLabel) throws SQLException {
307307
checkClosed();
308308
int col = getMetaData().findColumn(columnLabel);
309-
if (col == -1) throw new SQLException("No such column " + columnLabel);
309+
if (col == -1) throw new SQLException(String.format("No such column %s.", columnLabel));
310310
return col;
311311
}
312312

driver/src/main/java/jdbc/types/ArrayImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public int getBaseType() throws SQLException {
3232

3333
@Override
3434
public Object[] getArray() throws SQLException {
35-
if (array == null) throw new SQLException("Array was freed");
35+
if (array == null) throw new SQLException("Array was freed.");
3636
return array;
3737
}
3838

0 commit comments

Comments
 (0)