use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonReactiveClusterKeyCommandsTest method before.
@BeforeClass
public static void before() throws FailedToStartRedisException, IOException, InterruptedException {
RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
process = clusterRunner.run();
Config config = new Config();
config.useClusterServers().setSubscriptionMode(SubscriptionMode.SLAVE).setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
redisson = Redisson.create(config);
connection = new RedissonReactiveRedisClusterConnection(((RedissonReactive) redisson.reactive()).getCommandExecutor());
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonBlockingQueueTest method testTakeReattachCluster.
@Test
public void testTakeReattachCluster() throws IOException, InterruptedException {
RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
ClusterProcesses process = clusterRunner.run();
Thread.sleep(1000);
Config config = new Config();
config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
RedisProcess master = process.getNodes().stream().filter(x -> x.getRedisServerPort() == master1.getPort()).findFirst().get();
List<RFuture<Integer>> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
RBlockingQueue<Integer> queue = redisson.getBlockingQueue("queue" + i);
RFuture<Integer> f = queue.takeAsync();
try {
f.toCompletableFuture().get(1, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException e) {
// skip
}
futures.add(f);
}
master.stop();
Thread.sleep(TimeUnit.SECONDS.toMillis(80));
for (int i = 0; i < 10; i++) {
RBlockingQueue<Integer> queue = redisson.getBlockingQueue("queue" + i);
queue.put(i * 100);
}
for (int i = 0; i < 10; i++) {
RFuture<Integer> f = futures.get(i);
try {
f.toCompletableFuture().get(20, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException e) {
// skip
}
if (f.cause() != null) {
f.cause().printStackTrace();
}
Integer result = f.toCompletableFuture().getNow(null);
assertThat(result).isEqualTo(i * 100);
}
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonLockTest method testInCluster.
@Test
public void testInCluster() throws Exception {
RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
ClusterRunner.ClusterProcesses process = clusterRunner.run();
Thread.sleep(5000);
Config config = new Config();
config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
RLock lock = redisson.getLock("myLock");
lock.lock();
assertThat(lock.isLocked()).isTrue();
lock.unlock();
assertThat(lock.isLocked()).isFalse();
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonKeysTest method testKeysByPattern.
@Test
public void testKeysByPattern() throws FailedToStartRedisException, IOException, InterruptedException {
RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
ClusterProcesses process = clusterRunner.run();
Config config = new Config();
config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
int size = 10000;
for (int i = 0; i < size; i++) {
redisson.getBucket("test" + i).set(i);
}
assertThat(redisson.getKeys().count()).isEqualTo(size);
Long noOfKeysDeleted = 0L;
int chunkSize = 20;
Iterable<String> keysIterator = redisson.getKeys().getKeysByPattern("test*", chunkSize);
Set<String> keys = new HashSet<>();
for (String key : keysIterator) {
keys.add(key);
if (keys.size() % chunkSize == 0) {
long res = redisson.getKeys().delete(keys.toArray(new String[keys.size()]));
assertThat(res).isEqualTo(chunkSize);
noOfKeysDeleted += res;
keys.clear();
}
}
// Delete remaining keys
if (!keys.isEmpty()) {
noOfKeysDeleted += redisson.getKeys().delete(keys.toArray(new String[keys.size()]));
}
assertThat(noOfKeysDeleted).isEqualTo(size);
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonTopicPatternTest method testNonEventMessagesInCluster.
@Test
public void testNonEventMessagesInCluster() throws IOException, InterruptedException {
RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave();
RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
ClusterRunner.ClusterProcesses process = clusterRunner.run();
Config config = new Config();
config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
AtomicInteger subscribeCounter = new AtomicInteger();
RPatternTopic topic = redisson.getPatternTopic("my*", StringCodec.INSTANCE);
topic.addListener(new PatternStatusListener() {
@Override
public void onPSubscribe(String pattern) {
subscribeCounter.incrementAndGet();
}
@Override
public void onPUnsubscribe(String pattern) {
System.out.println("onPUnsubscribe: " + pattern);
}
});
AtomicInteger counter = new AtomicInteger();
PatternMessageListener<String> listener = (pattern, channel, msg) -> {
counter.incrementAndGet();
};
topic.addListener(String.class, listener);
for (int i = 0; i < 100; i++) {
redisson.getTopic("my" + i).publish(123);
}
Awaitility.await().atMost(Duration.ofSeconds(2)).until(() -> counter.get() == 100);
assertThat(subscribeCounter.get()).isEqualTo(1);
redisson.shutdown();
process.shutdown();
}
Aggregations