Search in sources :

Example 26 with RandomLoadBalancer

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

the class RedissonBucketsTest method testGetInCluster.

@Test
public void testGetInCluster() 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;
    Map<String, Integer> map = new HashMap<>();
    for (int i = 0; i < 10; i++) {
        map.put("test" + i, i);
    }
    for (int i = 10; i < size; i++) {
        map.put("test" + i + "{" + (i % 100) + "}", i);
    }
    redisson.getBuckets().set(map);
    Set<String> queryKeys = new HashSet<>(map.keySet());
    queryKeys.add("test_invalid");
    Map<String, Integer> buckets = redisson.getBuckets().get(queryKeys.toArray(new String[map.size()]));
    assertThat(buckets).isEqualTo(map);
    redisson.shutdown();
    process.shutdown();
}
Also used : HashMap(java.util.HashMap) Config(org.redisson.config.Config) RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 27 with RandomLoadBalancer

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

the class RedissonKeysTest method testDeleteInCluster.

@Test
public void testDeleteInCluster() 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;
    List<String> list = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        list.add("test" + i);
        redisson.getBucket("test" + i).set(i);
    }
    long deletedSize = redisson.getKeys().delete(list.toArray(new String[list.size()]));
    assertThat(deletedSize).isEqualTo(size);
    redisson.shutdown();
    process.shutdown();
}
Also used : ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 28 with RandomLoadBalancer

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

the class RedissonTopicPatternTest method testCluster.

@Test
public void testCluster() throws IOException, InterruptedException {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
    ClusterRunner.ClusterProcesses process = clusterRunner.run();
    Thread.sleep(3000);
    Config config = new Config();
    config.useClusterServers().setPingConnectionInterval(0).setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    AtomicInteger subscribeCounter = new AtomicInteger();
    RPatternTopic topic = redisson.getPatternTopic("__keyevent@*", 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) -> {
        System.out.println("mes " + channel + " counter " + counter.get());
        counter.incrementAndGet();
    };
    topic.addListener(String.class, listener);
    for (int i = 0; i < 10; i++) {
        redisson.getBucket("" + i).set(i);
        redisson.getBucket("" + i).delete();
        Thread.sleep(7);
    }
    Awaitility.await().atMost(Duration.ofSeconds(2)).until(() -> counter.get() > 9);
    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)

Example 29 with RandomLoadBalancer

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

the class RedissonTopicTest method testClusterSharding.

@Test
public void testClusterSharding() 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);
    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 counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        int j = i;
        RTopic topic = redisson.getTopic("test" + i);
        topic.addListener(Integer.class, (c, v) -> {
            assertThat(v).isEqualTo(j);
            counter.incrementAndGet();
        });
    }
    for (int i = 0; i < 10; i++) {
        RTopic topic = redisson.getTopic("test" + i);
        topic.publish(i);
    }
    Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> counter.get() == 10);
    redisson.shutdown();
    process.shutdown();
}
Also used : ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(org.redisson.config.Config) RedisClientConfig(org.redisson.client.RedisClientConfig) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer)

Example 30 with RandomLoadBalancer

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

the class RedissonTopicTest method testReattachInSentinel.

@Test
public void testReattachInSentinel() throws Exception {
    RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().port(6399).run();
    RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6399).run();
    RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6399).run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6399, 2).run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6399, 2).run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6399, 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));
    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)

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