Skip to content

Commit

Permalink
Fixed UnifiedJedis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
viragtripathi committed Mar 28, 2023
1 parent e54ca07 commit 8d7a607
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
15 changes: 10 additions & 5 deletions src/main/java/com/redis/benchmark/RedisBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
@Measurement(iterations = 1, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class RedisBenchmark {
private JedisCommands jedisCommands;
private static JedisCommands jedisCommands;
private static Integer jedisGetCount = 0;
private static Integer jedisSetCount = 0;

@Setup
public void setup() {
System.out.println("------------------- Setup");

Util.createOneMillionOfKeys();

jedisCommands = JedisConnectionManagement.getCommands();
Expand All @@ -35,8 +37,7 @@ public String jedisSimpleGet() {
try {
result = jedisCommands.get(String.format(Util.KeyPrefix, jedisGetCount));
} catch (Exception e) {
e.printStackTrace();
jedisCommands = JedisConnectionManagement.getCommands();
System.err.println("\n------------------- Failed GET Command\n" + e.getMessage());
}
return result;
}
Expand All @@ -48,9 +49,13 @@ public String jedisSimpleSet() {
try {
result = jedisCommands.set(String.format("JedisSetTest%s", jedisSetCount), jedisSetCount.toString());
} catch (Exception e) {
e.printStackTrace();
jedisCommands = JedisConnectionManagement.getCommands();
System.err.println("\n------------------- Failed SET Command\n" + e.getMessage());
}
return result;
}

@TearDown
public void tearDown() {
System.out.println("------------------- TearDown");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.commands.JedisCommands;
import redis.clients.jedis.MultiClusterJedisClientConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisValidationException;
import redis.clients.jedis.providers.MultiClusterPooledConnectionProvider;
import java.util.Set;

public final class JedisConnectionManagement {
private static final JedisConnectionManagement connectionManagement = new JedisConnectionManagement();
private static final Boolean connectionCreated = false;
private static Boolean connectionCreated = false;
private UnifiedJedis unifiedJedis;

private JedisConnectionManagement() {
}

private void createJedisConnection() {
MultiClusterPooledConnectionProvider provider = null;
MultiClusterPooledConnectionProvider provider;
MultiClusterJedisClientConfig.Builder multiClusterJedisClientConfig;
Set<HostAndPort> hostAndPorts = BenchmarkConfiguration.get().getRedisHostAndPorts();
int index = 0;
Expand All @@ -47,7 +45,6 @@ private void createJedisConnection() {
unifiedJedis = new UnifiedJedis(new HostAndPort(hostAndPort.getHost(), hostAndPort.getPort()), jedisClientConfig);
break;
}
//unifiedJedis = new UnifiedJedis(HostAndPort.from(String.valueOf(hostAndPorts.stream().iterator().next())), jedisClientConfig);
}
// Multi cluster
if (hostAndPorts.size() > 1) {
Expand All @@ -61,23 +58,18 @@ private void createJedisConnection() {
multiClusterJedisClientConfig.circuitBreakerSlidingWindowMinCalls(1);
provider = new MultiClusterPooledConnectionProvider(multiClusterJedisClientConfig.build());

if (provider.getConnection().ping())
provider.setActiveMultiClusterIndex(1);

unifiedJedis = new UnifiedJedis(provider);
}
} catch (Exception e) {
e.printStackTrace();
if (provider != null && (e instanceof JedisValidationException || e instanceof JedisConnectionException)) {
provider.incrementActiveMultiClusterIndex();
unifiedJedis = new UnifiedJedis(provider);
}
System.err.println("------------------- Failed UnifiedJedis " + e.getMessage());
}
}

public static JedisCommands getCommands() {
if (!connectionCreated)
if (!connectionCreated) {
connectionManagement.createJedisConnection();
connectionCreated = Boolean.TRUE;
}
return connectionManagement.unifiedJedis;
}
}

0 comments on commit 8d7a607

Please sign in to comment.