Search in sources :

Example 6 with RandomLoadBalancer

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

the class RedissonTopicTest method testReattachInClusterSlave.

@Test
public void testReattachInClusterSlave() 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();
    Thread.sleep(1000);
    Config config = new Config();
    config.useClusterServers().setSubscriptionMode(SubscriptionMode.SLAVE).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("topic");
    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);
        }
    });
    assertThat(topic.countListeners()).isEqualTo(2);
    sendCommands(redisson, "topic");
    process.getNodes().stream().filter(x -> Arrays.asList(slave1.getPort(), slave2.getPort(), slave3.getPort()).contains(x.getRedisServerPort())).forEach(x -> {
        try {
            x.stop();
            Thread.sleep(18000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });
    Thread.sleep(15000);
    redisson.getTopic("topic").publish(1);
    await().atMost(75, TimeUnit.SECONDS).until(() -> subscriptions.get() == 2);
    assertThat(topic.countListeners()).isEqualTo(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 7 with RandomLoadBalancer

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

the class RedissonTopicTest method testReattachInSentinel2.

@Test
public void testReattachInSentinel2() throws Exception {
    RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().port(6440).run();
    RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6440).run();
    RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6440).run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6440, 2).run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6440, 2).run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6440, 2).run();
    Thread.sleep(5000);
    Config config = new Config();
    config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    RedissonClient redisson = Redisson.create(config);
    final AtomicBoolean executed = new AtomicBoolean();
    final AtomicInteger subscriptions = new AtomicInteger();
    RTopic topic = redisson.getTopic("topic");
    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, "topic");
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
    Thread.sleep(TimeUnit.SECONDS.toMillis(20));
    topic.removeAllListeners();
    long t = System.currentTimeMillis();
    topic.addListenerAsync(new StatusListener() {

        @Override
        public void onUnsubscribe(String channel) {
        }

        @Override
        public void onSubscribe(String channel) {
            System.out.println("onSubscribe " + (System.currentTimeMillis() - t));
            subscriptions.incrementAndGet();
        }
    });
    topic.addListenerAsync(Integer.class, new MessageListener<Integer>() {

        @Override
        public void onMessage(CharSequence channel, Integer msg) {
            executed.set(true);
        }
    });
    Thread.sleep(TimeUnit.SECONDS.toMillis(5));
    master = new RedisRunner().port(6390).nosave().randomDir().run();
    slave1 = new RedisRunner().port(6391).nosave().randomDir().slaveof("127.0.0.1", 6390).run();
    slave2 = new RedisRunner().port(6392).nosave().randomDir().slaveof("127.0.0.1", 6390).run();
    sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6390, 2).run();
    sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6390, 2).run();
    sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6390, 2).run();
    redisson.getTopic("topic").publish(1);
    await().atMost(20, TimeUnit.SECONDS).until(() -> subscriptions.get() == 2);
    assertThat(executed.get()).isTrue();
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) RedisClientConfig(org.redisson.client.RedisClientConfig) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer)

Example 8 with RandomLoadBalancer

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

the class RedissonRedisNodesTest method testCluster.

@Test
public void testCluster() 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();
    RedisRunner slave4 = new RedisRunner().port(6903).randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1, slave4).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);
    RedisCluster nodes = redisson.getRedisNodes(RedisNodes.CLUSTER);
    assertThat(nodes.getMaster("redis://127.0.0.1:6890")).isNotNull();
    assertThat(nodes.getMaster("redis://127.0.0.1:6899")).isNull();
    assertThat(nodes.getMasters()).hasSize(3);
    assertThat(nodes.getSlaves()).hasSize(4);
    for (RedisClusterMaster master : nodes.getMasters()) {
        master.clusterDeleteSlots(1, 2);
        master.clusterAddSlots(1, 2);
        master.clusterCountKeysInSlot(1);
        List<String> keys = master.clusterGetKeysInSlot(1, 10);
        assertThat(keys).isEmpty();
        ;
        String nodeId = master.clusterId();
        assertThat(nodeId).isNotNull();
        assertThat(master.clusterCountFailureReports(nodeId)).isZero();
        Map<ClusterSlotRange, Set<String>> slots = master.clusterSlots();
        assertThat(slots.entrySet().size()).isBetween(3, 5);
    }
    for (RedisClusterSlave slave : nodes.getSlaves()) {
        slave.clusterDeleteSlots(1, 2);
        slave.clusterAddSlots(1, 2);
        slave.clusterCountKeysInSlot(1);
        List<String> keys = slave.clusterGetKeysInSlot(1, 10);
        assertThat(keys).isEmpty();
        ;
        String nodeId = slave.clusterId();
        assertThat(nodeId).isNotNull();
        assertThat(slave.clusterCountFailureReports(nodeId)).isZero();
        Map<ClusterSlotRange, Set<String>> slots = slave.clusterSlots();
        assertThat(slots.entrySet().size()).isBetween(3, 5);
    }
    redisson.shutdown();
    process.shutdown();
}
Also used : Set(java.util.Set) Config(org.redisson.config.Config) ClusterSlotRange(org.redisson.cluster.ClusterSlotRange) RedissonClient(org.redisson.api.RedissonClient) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 9 with RandomLoadBalancer

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

the class RedissonRedisNodesTest method testSentinel.

@Test
public void testSentinel() 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);
    assertThat(nodes.getSentinels()).hasSize(3);
    assertThat(nodes.getSlaves()).hasSize(2);
    assertThat(nodes.getMaster()).isNotNull();
    for (RedisSentinel sentinel : nodes.getSentinels()) {
        Assertions.assertTrue(sentinel.ping());
        RedisURI addr = sentinel.getMasterAddr("myMaster");
        assertThat(addr.getHost()).isEqualTo("127.0.0.1");
        assertThat(addr.getPort()).isEqualTo(master.getRedisServerPort());
        Map<String, String> masterMap = sentinel.getMaster("myMaster");
        assertThat(masterMap).isNotEmpty();
        List<Map<String, String>> masters = sentinel.getMasters();
        assertThat(masters).hasSize(1);
        Map<String, String> m = masters.get(0);
        assertThat(m.get("ip")).isEqualTo("127.0.0.1");
        assertThat(Integer.valueOf(m.get("port"))).isEqualTo(master.getRedisServerPort());
        List<Map<String, String>> slaves = sentinel.getSlaves("myMaster");
        assertThat(slaves).hasSize(2);
    }
    nodes.getSlaves().forEach((node) -> {
        Assertions.assertTrue(node.ping());
    });
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) RedisURI(org.redisson.misc.RedisURI) RedissonClient(org.redisson.api.RedissonClient) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 10 with RandomLoadBalancer

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

the class RedissonSetTest method testClusteredIterator.

@Test
public void testClusteredIterator() 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();
    RedisRunner slave4 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slave5 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slave6 = new RedisRunner().randomPort().randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1, slave4).addNode(master2, slave2, slave5).addNode(master3, slave3, slave6);
    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;
    RSet<String> set = redisson.getSet("{test");
    for (int i = 0; i < size; i++) {
        set.add("" + i);
    }
    Set<String> keys = new HashSet<>();
    for (String key : set) {
        keys.add(key);
    }
    assertThat(keys).hasSize(size);
    redisson.shutdown();
    process.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) HashSet(java.util.HashSet) 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