Search in sources :

Example 1 with RandomLoadBalancer

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());
}
Also used : Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) BeforeClass(org.junit.BeforeClass)

Example 2 with RandomLoadBalancer

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();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 3 with RandomLoadBalancer

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();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) RLock(org.redisson.api.RLock) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 4 with RandomLoadBalancer

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();
}
Also used : Config(org.redisson.config.Config) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 5 with RandomLoadBalancer

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();
}
Also used : BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) StringCodec(org.redisson.client.codec.StringCodec) Config(org.redisson.config.Config) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedisProcess(org.redisson.RedisRunner.RedisProcess) ArrayList(java.util.ArrayList) RPatternTopic(org.redisson.api.RPatternTopic) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) RedissonClient(org.redisson.api.RedissonClient) ExecutorService(java.util.concurrent.ExecutorService) Awaitility.await(org.awaitility.Awaitility.await) RTopic(org.redisson.api.RTopic) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PatternMessageListener(org.redisson.api.listener.PatternMessageListener) Assertions(org.junit.jupiter.api.Assertions) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Awaitility(org.awaitility.Awaitility) PatternStatusListener(org.redisson.api.listener.PatternStatusListener) RPatternTopic(org.redisson.api.RPatternTopic) Config(org.redisson.config.Config) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) PatternStatusListener(org.redisson.api.listener.PatternStatusListener) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Aggregations

Config (org.redisson.config.Config)33 RandomLoadBalancer (org.redisson.connection.balancer.RandomLoadBalancer)33 Test (org.junit.jupiter.api.Test)24 ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)14 RedisProcess (org.redisson.RedisRunner.RedisProcess)14 RedissonClient (org.redisson.api.RedissonClient)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IOException (java.io.IOException)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Awaitility.await (org.awaitility.Awaitility.await)6 StringCodec (org.redisson.client.codec.StringCodec)6 RedisClientConfig (org.redisson.client.RedisClientConfig)5 ClusterNodeInfo (org.redisson.cluster.ClusterNodeInfo)5 Serializable (java.io.Serializable)4 Duration (java.time.Duration)4 java.util (java.util)4 java.util.concurrent (java.util.concurrent)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4