Skip to content

Commit 3d45faf

Browse files
committed
Modify JedisBroadcastException (#3518)
* Modify JedisBroadcastException * Added tests
1 parent d10a7fc commit 3d45faf

File tree

4 files changed

+65
-48
lines changed

4 files changed

+65
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package redis.clients.jedis.exceptions;
22

3+
import java.util.Collections;
34
import java.util.HashMap;
45
import java.util.Map;
56
import redis.clients.jedis.HostAndPort;
@@ -8,45 +9,22 @@
89
* Note: This exception extends {@link JedisDataException} just so existing applications catching
910
* JedisDataException do not get broken.
1011
*/
12+
// TODO: extends JedisException
1113
public class JedisBroadcastException extends JedisDataException {
1214

1315
private static final String BROADCAST_ERROR_MESSAGE = "A failure occurred while broadcasting the command.";
1416

15-
private final Map<HostAndPort, SingleReply> replies = new HashMap<>();
17+
private final Map<HostAndPort, Object> replies = new HashMap<>();
1618

1719
public JedisBroadcastException() {
1820
super(BROADCAST_ERROR_MESSAGE);
1921
}
2022

2123
public void addReply(HostAndPort node, Object reply) {
22-
replies.put(node, new SingleReply(reply));
24+
replies.put(node, reply);
2325
}
2426

25-
public void addError(HostAndPort node, JedisDataException error) {
26-
replies.put(node, new SingleReply(error));
27-
}
28-
29-
public static class SingleReply {
30-
31-
private final Object reply;
32-
private final JedisDataException error;
33-
34-
public SingleReply(Object reply) {
35-
this.reply = reply;
36-
this.error = null;
37-
}
38-
39-
public SingleReply(JedisDataException error) {
40-
this.reply = null;
41-
this.error = error;
42-
}
43-
44-
public Object getReply() {
45-
return reply;
46-
}
47-
48-
public JedisDataException getError() {
49-
return error;
50-
}
27+
public Map<HostAndPort, Object> getReplies() {
28+
return Collections.unmodifiableMap(replies);
5129
}
5230
}

src/main/java/redis/clients/jedis/executors/ClusterCommandExecutor.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,29 @@ public final <T> T broadcastCommand(CommandObject<T> commandObject) {
4343

4444
boolean isErrored = false;
4545
T reply = null;
46-
JedisBroadcastException holder = new JedisBroadcastException();
46+
JedisBroadcastException bcastError = new JedisBroadcastException();
4747
for (Map.Entry<String, ConnectionPool> entry : connectionMap.entrySet()) {
4848
HostAndPort node = HostAndPort.from(entry.getKey());
4949
ConnectionPool pool = entry.getValue();
5050
try (Connection connection = pool.getResource()) {
51-
try {
52-
T aReply = execute(connection, commandObject);
53-
holder.addReply(node, aReply);
54-
if (isErrored) { // already errored
55-
} else if (reply == null) {
56-
reply = aReply; // ok
57-
} else if (reply.equals(aReply)) {
58-
// ok
59-
} else {
60-
isErrored = true;
61-
reply = null;
62-
}
63-
} catch (JedisDataException anError) {
64-
holder.addError(node, anError);
51+
T aReply = execute(connection, commandObject);
52+
bcastError.addReply(node, aReply);
53+
if (isErrored) { // already errored
54+
} else if (reply == null) {
55+
reply = aReply; // ok
56+
} else if (reply.equals(aReply)) {
57+
// ok
58+
} else {
59+
isErrored = true;
60+
reply = null;
6561
}
62+
} catch (Exception anError) {
63+
bcastError.addReply(node, anError);
64+
isErrored = true;
6665
}
6766
}
6867
if (isErrored) {
69-
throw holder;
68+
throw bcastError;
7069
}
7170
return reply;
7271
}

src/test/java/redis/clients/jedis/commands/jedis/ClusterScriptingCommandsTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package redis.clients.jedis.commands.jedis;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertSame;
5+
import static org.junit.Assert.assertThrows;
46
import static org.junit.Assert.assertTrue;
57

68
import java.util.ArrayList;
79
import java.util.Arrays;
810
import java.util.Collections;
911
import java.util.List;
1012
import java.util.Map;
11-
import java.util.function.Supplier;
1213
import org.junit.Test;
1314

15+
import redis.clients.jedis.HostAndPort;
1416
import redis.clients.jedis.args.FlushMode;
17+
import redis.clients.jedis.exceptions.JedisBroadcastException;
1518
import redis.clients.jedis.exceptions.JedisClusterOperationException;
1619
import redis.clients.jedis.exceptions.JedisDataException;
1720

@@ -110,4 +113,17 @@ public void broadcast() {
110113

111114
assertEquals(Arrays.asList(false, false), cluster.scriptExists(Arrays.asList(sha1_1, sha1_2)));
112115
}
116+
117+
@Test
118+
public void broadcastWithError() {
119+
120+
JedisBroadcastException error = assertThrows(JedisBroadcastException.class, () -> cluster.functionDelete("xyz"));
121+
122+
Map<HostAndPort, Object> replies = error.getReplies();
123+
assertEquals(3, replies.size());
124+
replies.values().forEach(r -> {
125+
assertSame(JedisDataException.class, r.getClass());
126+
assertEquals("ERR Library not found", ((JedisDataException) r).getMessage());
127+
});
128+
}
113129
}

src/test/java/redis/clients/jedis/commands/unified/pooled/PooledPipeliningTest.java renamed to src/test/java/redis/clients/jedis/commands/unified/pooled/PooledMiscellaneousTest.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis.clients.jedis.commands.unified.pooled;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
45

56
import java.util.ArrayList;
67
import java.util.Arrays;
@@ -15,10 +16,10 @@
1516
import redis.clients.jedis.Pipeline;
1617
import redis.clients.jedis.Response;
1718
import redis.clients.jedis.Transaction;
18-
1919
import redis.clients.jedis.commands.unified.UnifiedJedisCommandsTestBase;
20+
import redis.clients.jedis.exceptions.JedisDataException;
2021

21-
public class PooledPipeliningTest extends UnifiedJedisCommandsTestBase {
22+
public class PooledMiscellaneousTest extends UnifiedJedisCommandsTestBase {
2223

2324
protected Pipeline pipeline;
2425
protected Transaction transaction;
@@ -47,7 +48,7 @@ public void tearDown() {
4748
}
4849

4950
@Test
50-
public void simple() {
51+
public void pipeline() {
5152
final int count = 10;
5253
int totalCount = 0;
5354
for (int i = 0; i < count; i++) {
@@ -104,4 +105,27 @@ public void transaction() {
104105
assertEquals(expected.get(i), responses.get(i));
105106
}
106107
}
108+
109+
@Test
110+
public void broadcast() {
111+
112+
String script_1 = "return 'jedis'";
113+
String sha1_1 = jedis.scriptLoad(script_1);
114+
115+
String script_2 = "return 79";
116+
String sha1_2 = jedis.scriptLoad(script_2);
117+
118+
assertEquals(Arrays.asList(true, true), jedis.scriptExists(Arrays.asList(sha1_1, sha1_2)));
119+
120+
jedis.scriptFlush();
121+
122+
assertEquals(Arrays.asList(false, false), jedis.scriptExists(Arrays.asList(sha1_1, sha1_2)));
123+
}
124+
125+
@Test
126+
public void broadcastWithError() {
127+
JedisDataException error = assertThrows(JedisDataException.class,
128+
() -> jedis.functionDelete("xyz"));
129+
assertEquals("ERR Library not found", error.getMessage());
130+
}
107131
}

0 commit comments

Comments
 (0)