Search in sources :

Example 31 with RandomLoadBalancer

use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.

the class RedissonTopicTest method testReattachInClusterMaster.

@Test
public void testReattachInClusterMaster() throws Exception {
    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().setSubscriptionMode(SubscriptionMode.MASTER).setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    final AtomicBoolean executed = new AtomicBoolean();
    final AtomicInteger subscriptions = new AtomicInteger();
    RTopic topic = redisson.getTopic("3");
    topic.addListener(new StatusListener() {

        @Override
        public void onUnsubscribe(String channel) {
        }

        @Override
        public void onSubscribe(String channel) {
            subscriptions.incrementAndGet();
        }
    });
    topic.addListener(Integer.class, new MessageListener<Integer>() {

        @Override
        public void onMessage(CharSequence channel, Integer msg) {
            executed.set(true);
        }
    });
    sendCommands(redisson, "3");
    process.getNodes().stream().filter(x -> master1.getPort() == x.getRedisServerPort()).forEach(x -> {
        try {
            x.stop();
            Thread.sleep(18000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });
    Thread.sleep(25000);
    redisson.getTopic("3").publish(1);
    await().atMost(75, TimeUnit.SECONDS).until(() -> subscriptions.get() == 2);
    assertThat(executed.get()).isTrue();
    redisson.shutdown();
    process.shutdown();
}
Also used : java.util(java.util) RedisConnection(org.redisson.client.RedisConnection) StringCodec(org.redisson.client.codec.StringCodec) org.redisson.api.listener(org.redisson.api.listener) Config(org.redisson.config.Config) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedisClient(org.redisson.client.RedisClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) org.redisson.api(org.redisson.api) ClusterNodeInfo(org.redisson.cluster.ClusterNodeInfo) RedisClientConfig(org.redisson.client.RedisClientConfig) LongCodec(org.redisson.client.codec.LongCodec) Awaitility.await(org.awaitility.Awaitility.await) java.util.concurrent(java.util.concurrent) SubscriptionMode(org.redisson.config.SubscriptionMode) IOException(java.io.IOException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) Collectors(java.util.stream.Collectors) RedisCommands(org.redisson.client.protocol.RedisCommands) Serializable(java.io.Serializable) KEYSPACE_EVENTS_OPTIONS(org.redisson.RedisRunner.KEYSPACE_EVENTS_OPTIONS) AtomicLong(java.util.concurrent.atomic.AtomicLong) org.junit.jupiter.api(org.junit.jupiter.api) RedisStrictCommand(org.redisson.client.protocol.RedisStrictCommand) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Awaitility(org.awaitility.Awaitility) Config(org.redisson.config.Config) RedisClientConfig(org.redisson.client.RedisClientConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer)

Example 32 with RandomLoadBalancer

use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.

the class RedissonRedisNodesTest method testNodesInCluster.

@Test
public void testNodesInCluster() throws Exception {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slot1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slot2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slot3 = new RedisRunner().randomPort().randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slot1).addNode(master2, slot2).addNode(master3, slot3);
    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);
    RedisCluster nodes = redisson.getRedisNodes(RedisNodes.CLUSTER);
    assertThat(nodes.getMasters()).hasSize(3);
    for (RedisClusterMaster node : nodes.getMasters()) {
        assertThat(node.info(RedisNode.InfoSection.ALL)).isNotEmpty();
    }
    assertThat(nodes.getSlaves()).hasSize(3);
    for (RedisClusterSlave node : nodes.getSlaves()) {
        assertThat(node.info(RedisNode.InfoSection.ALL)).isNotEmpty();
    }
    redisson.shutdown();
    process.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 33 with RandomLoadBalancer

use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.

the class RedissonRedisNodesTest method testSentinelFailover.

@Test
public void testSentinelFailover() throws IOException, InterruptedException {
    RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().run();
    RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6379).run();
    RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6379).run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    Config config = new Config();
    config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    long t = System.currentTimeMillis();
    RedissonClient redisson = Redisson.create(config);
    RedisSentinelMasterSlave nodes = redisson.getRedisNodes(RedisNodes.SENTINEL_MASTER_SLAVE);
    RedisSentinel sentinel = nodes.getSentinels().iterator().next();
    sentinel.failover("myMaster");
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) 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