Skip to content

Commit

Permalink
Modify JedisBroadcastException (#3518)
Browse files Browse the repository at this point in the history
* Modify JedisBroadcastException

* Added tests
  • Loading branch information
sazzad16 committed Aug 31, 2023
1 parent d10a7fc commit 3d45faf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package redis.clients.jedis.exceptions;

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

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

private final Map<HostAndPort, SingleReply> replies = new HashMap<>();
private final Map<HostAndPort, Object> replies = new HashMap<>();

public JedisBroadcastException() {
super(BROADCAST_ERROR_MESSAGE);
}

public void addReply(HostAndPort node, Object reply) {
replies.put(node, new SingleReply(reply));
replies.put(node, reply);
}

public void addError(HostAndPort node, JedisDataException error) {
replies.put(node, new SingleReply(error));
}

public static class SingleReply {

private final Object reply;
private final JedisDataException error;

public SingleReply(Object reply) {
this.reply = reply;
this.error = null;
}

public SingleReply(JedisDataException error) {
this.reply = null;
this.error = error;
}

public Object getReply() {
return reply;
}

public JedisDataException getError() {
return error;
}
public Map<HostAndPort, Object> getReplies() {
return Collections.unmodifiableMap(replies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,29 @@ public final <T> T broadcastCommand(CommandObject<T> commandObject) {

boolean isErrored = false;
T reply = null;
JedisBroadcastException holder = new JedisBroadcastException();
JedisBroadcastException bcastError = new JedisBroadcastException();
for (Map.Entry<String, ConnectionPool> entry : connectionMap.entrySet()) {
HostAndPort node = HostAndPort.from(entry.getKey());
ConnectionPool pool = entry.getValue();
try (Connection connection = pool.getResource()) {
try {
T aReply = execute(connection, commandObject);
holder.addReply(node, aReply);
if (isErrored) { // already errored
} else if (reply == null) {
reply = aReply; // ok
} else if (reply.equals(aReply)) {
// ok
} else {
isErrored = true;
reply = null;
}
} catch (JedisDataException anError) {
holder.addError(node, anError);
T aReply = execute(connection, commandObject);
bcastError.addReply(node, aReply);
if (isErrored) { // already errored
} else if (reply == null) {
reply = aReply; // ok
} else if (reply.equals(aReply)) {
// ok
} else {
isErrored = true;
reply = null;
}
} catch (Exception anError) {
bcastError.addReply(node, anError);
isErrored = true;
}
}
if (isErrored) {
throw holder;
throw bcastError;
}
return reply;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package redis.clients.jedis.commands.jedis;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.junit.Test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.args.FlushMode;
import redis.clients.jedis.exceptions.JedisBroadcastException;
import redis.clients.jedis.exceptions.JedisClusterOperationException;
import redis.clients.jedis.exceptions.JedisDataException;

Expand Down Expand Up @@ -110,4 +113,17 @@ public void broadcast() {

assertEquals(Arrays.asList(false, false), cluster.scriptExists(Arrays.asList(sha1_1, sha1_2)));
}

@Test
public void broadcastWithError() {

JedisBroadcastException error = assertThrows(JedisBroadcastException.class, () -> cluster.functionDelete("xyz"));

Map<HostAndPort, Object> replies = error.getReplies();
assertEquals(3, replies.size());
replies.values().forEach(r -> {
assertSame(JedisDataException.class, r.getClass());
assertEquals("ERR Library not found", ((JedisDataException) r).getMessage());
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis.clients.jedis.commands.unified.pooled;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -15,10 +16,10 @@
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;

import redis.clients.jedis.commands.unified.UnifiedJedisCommandsTestBase;
import redis.clients.jedis.exceptions.JedisDataException;

public class PooledPipeliningTest extends UnifiedJedisCommandsTestBase {
public class PooledMiscellaneousTest extends UnifiedJedisCommandsTestBase {

protected Pipeline pipeline;
protected Transaction transaction;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void tearDown() {
}

@Test
public void simple() {
public void pipeline() {
final int count = 10;
int totalCount = 0;
for (int i = 0; i < count; i++) {
Expand Down Expand Up @@ -104,4 +105,27 @@ public void transaction() {
assertEquals(expected.get(i), responses.get(i));
}
}

@Test
public void broadcast() {

String script_1 = "return 'jedis'";
String sha1_1 = jedis.scriptLoad(script_1);

String script_2 = "return 79";
String sha1_2 = jedis.scriptLoad(script_2);

assertEquals(Arrays.asList(true, true), jedis.scriptExists(Arrays.asList(sha1_1, sha1_2)));

jedis.scriptFlush();

assertEquals(Arrays.asList(false, false), jedis.scriptExists(Arrays.asList(sha1_1, sha1_2)));
}

@Test
public void broadcastWithError() {
JedisDataException error = assertThrows(JedisDataException.class,
() -> jedis.functionDelete("xyz"));
assertEquals("ERR Library not found", error.getMessage());
}
}

0 comments on commit 3d45faf

Please sign in to comment.