Search in sources :

Example 16 with RandomLoadBalancer

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

the class RedissonTest method testFailoverInClusterSlave.

@Test
public void testFailoverInClusterSlave() 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);
    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);
    RedisProcess slave = process.getNodes().stream().filter(x -> x.getRedisServerPort() == slave1.getPort()).findFirst().get();
    List<RFuture<?>> futures = new ArrayList<RFuture<?>>();
    CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread() {

        public void run() {
            for (int i = 0; i < 600; i++) {
                RFuture<?> f1 = redisson.getBucket("i" + i).getAsync();
                futures.add(f1);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (i % 100 == 0) {
                    System.out.println("step: " + i);
                }
            }
            latch.countDown();
        }
    };
    t.start();
    t.join(1000);
    slave.restart(20);
    System.out.println("slave " + slave.getRedisServerAddressAndPort() + " has been stopped!");
    assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue();
    int errors = 0;
    int success = 0;
    int readonlyErrors = 0;
    for (RFuture<?> rFuture : futures) {
        try {
            rFuture.toCompletableFuture().join();
        } catch (Exception e) {
        // skip
        }
        if (!rFuture.isSuccess()) {
            rFuture.cause().printStackTrace();
            errors++;
        } else {
            success++;
        }
    }
    redisson.shutdown();
    process.shutdown();
    assertThat(readonlyErrors).isZero();
    assertThat(errors).isLessThan(200);
    assertThat(success).isGreaterThan(600 - 200);
    assertThat(futures.get(futures.size() - 1).isSuccess()).isTrue();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) IOException(java.io.IOException) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 17 with RandomLoadBalancer

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

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

Example 18 with RandomLoadBalancer

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

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

Example 19 with RandomLoadBalancer

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

the class RedissonSentinelConnectionTest method before.

@Before
public void before() throws FailedToStartRedisException, IOException, InterruptedException {
    master = new RedisRunner().nosave().randomDir().run();
    slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6379).run();
    slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6379).run();
    sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).run();
    Thread.sleep(5000);
    Config config = new Config();
    config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    redisson = Redisson.create(config);
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    connection = factory.getSentinelConnection();
}
Also used : Config(org.redisson.config.Config) RedisRunner(org.redisson.RedisRunner) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Before(org.junit.Before)

Example 20 with RandomLoadBalancer

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

the class RedissonTest method testFailoverInSentinel.

@Test
public void testFailoverInSentinel() throws Exception {
    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();
    Thread.sleep(5000);
    Config config = new Config();
    config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    RedissonClient redisson = Redisson.create(config);
    List<RFuture<?>> futures = new ArrayList<RFuture<?>>();
    CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread() {

        public void run() {
            for (int i = 0; i < 1000; i++) {
                RFuture<?> f1 = redisson.getBucket("i" + i).getAsync();
                RFuture<?> f2 = redisson.getBucket("i" + i).setAsync("");
                RFuture<?> f3 = redisson.getTopic("topic").publishAsync("testmsg");
                futures.add(f1);
                futures.add(f2);
                futures.add(f3);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            latch.countDown();
        }
    };
    t.start();
    master.stop();
    System.out.println("master " + master.getRedisServerAddressAndPort() + " stopped!");
    Thread.sleep(TimeUnit.SECONDS.toMillis(70));
    master = new RedisRunner().port(master.getRedisServerPort()).nosave().randomDir().run();
    System.out.println("master " + master.getRedisServerAddressAndPort() + " started!");
    Thread.sleep(15000);
    assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    int errors = 0;
    int success = 0;
    int readonlyErrors = 0;
    for (RFuture<?> rFuture : futures) {
        try {
            rFuture.toCompletableFuture().join();
        } catch (Exception e) {
        // skip
        }
        if (!rFuture.isSuccess()) {
            if (rFuture.cause().getMessage().contains("READONLY You can't write against")) {
                readonlyErrors++;
            }
            errors++;
        } else {
            success++;
        }
    }
    System.out.println("errors " + errors + " success " + success + " readonly " + readonlyErrors);
    assertThat(futures.get(futures.size() - 1).isSuccess()).isTrue();
    assertThat(errors).isLessThan(820);
    assertThat(readonlyErrors).isZero();
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) IOException(java.io.IOException) RedisProcess(org.redisson.RedisRunner.RedisProcess) 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