Search in sources :

Example 11 with ClusterProcesses

use of org.redisson.ClusterRunner.ClusterProcesses 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 12 with ClusterProcesses

use of org.redisson.ClusterRunner.ClusterProcesses in project redisson by redisson.

the class RedissonBatchRxTest method testSyncSlaves.

@ParameterizedTest
@MethodSource("data")
public void testSyncSlaves(BatchOptions batchOptions) 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().setTimeout(1000000).setRetryInterval(1000000).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonRxClient redisson = Redisson.create(config).rxJava();
    batchOptions.syncSlaves(1, 1, TimeUnit.SECONDS);
    RBatchRx batch = redisson.createBatch(batchOptions);
    for (int i = 0; i < 100; i++) {
        RMapRx<String, String> map = batch.getMap("test");
        map.put("" + i, "" + i);
    }
    BatchResult<?> result = sync(batch.execute());
    assertThat(result.getResponses()).hasSize(100);
    assertThat(result.getSyncedSlaves()).isEqualTo(1);
    process.shutdown();
    redisson.shutdown();
}
Also used : ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) RedisRunner(org.redisson.RedisRunner) ClusterRunner(org.redisson.ClusterRunner) RBatchRx(org.redisson.api.RBatchRx) RedissonRxClient(org.redisson.api.RedissonRxClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with ClusterProcesses

use of org.redisson.ClusterRunner.ClusterProcesses in project redisson by redisson.

the class RedissonBatchRxTest method testAtomicSyncSlaves.

@ParameterizedTest
@MethodSource("data")
public void testAtomicSyncSlaves(BatchOptions batchOptions) 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().setTimeout(123000).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonRxClient redisson = Redisson.create(config).rxJava();
    batchOptions.executionMode(ExecutionMode.IN_MEMORY_ATOMIC).syncSlaves(1, 1, TimeUnit.SECONDS);
    RBatchRx batch = redisson.createBatch(batchOptions);
    for (int i = 0; i < 10; i++) {
        batch.getAtomicLong("{test}" + i).addAndGet(i);
    }
    BatchResult<?> result = sync(batch.execute());
    assertThat(result.getSyncedSlaves()).isEqualTo(1);
    int i = 0;
    for (Object res : result.getResponses()) {
        assertThat((Long) res).isEqualTo(i++);
    }
    process.shutdown();
    redisson.shutdown();
}
Also used : ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) AtomicLong(java.util.concurrent.atomic.AtomicLong) RedisRunner(org.redisson.RedisRunner) ClusterRunner(org.redisson.ClusterRunner) RBatchRx(org.redisson.api.RBatchRx) RedissonRxClient(org.redisson.api.RedissonRxClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with ClusterProcesses

use of org.redisson.ClusterRunner.ClusterProcesses in project redisson by redisson.

the class RedissonTest method testFailoverInCluster.

@Test
public void testFailoverInCluster() 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);
    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 master = process.getNodes().stream().filter(x -> x.getRedisServerPort() == master1.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 < 2000; 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();
                }
                if (i % 100 == 0) {
                    System.out.println("step: " + i);
                }
            }
            latch.countDown();
        }
    };
    t.start();
    t.join(1000);
    Set<InetSocketAddress> oldMasters = new HashSet<>();
    Collection<RedisClusterMaster> masterNodes = redisson.getRedisNodes(RedisNodes.CLUSTER).getMasters();
    for (RedisClusterMaster clusterNode : masterNodes) {
        oldMasters.add(clusterNode.getAddr());
    }
    master.stop();
    System.out.println("master " + master.getRedisServerAddressAndPort() + " has been stopped!");
    Thread.sleep(TimeUnit.SECONDS.toMillis(90));
    RedisProcess newMaster = null;
    Collection<RedisClusterMaster> newMasterNodes = redisson.getRedisNodes(RedisNodes.CLUSTER).getMasters();
    for (RedisClusterMaster clusterNode : newMasterNodes) {
        if (!oldMasters.contains(clusterNode.getAddr())) {
            newMaster = process.getNodes().stream().filter(x -> x.getRedisServerPort() == clusterNode.getAddr().getPort()).findFirst().get();
            break;
        }
    }
    assertThat(newMaster).isNotNull();
    Thread.sleep(30000);
    newMaster.stop();
    System.out.println("new master " + newMaster.getRedisServerAddressAndPort() + " has been stopped!");
    Thread.sleep(TimeUnit.SECONDS.toMillis(80));
    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()) {
            errors++;
        } else {
            success++;
        }
    }
    redisson.shutdown();
    process.shutdown();
    assertThat(readonlyErrors).isZero();
    assertThat(errors).isLessThan(1000);
    assertThat(success).isGreaterThan(5000);
}
Also used : Encoder(org.redisson.client.protocol.Encoder) java.util(java.util) StringCodec(org.redisson.client.codec.StringCodec) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Config(org.redisson.config.Config) State(org.redisson.client.handler.State) JsonJacksonCodec(org.redisson.codec.JsonJacksonCodec) MasterSlaveConnectionManager(org.redisson.connection.MasterSlaveConnectionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RandomString(net.bytebuddy.utility.RandomString) RedisProcess(org.redisson.RedisRunner.RedisProcess) ConnectionListener(org.redisson.connection.ConnectionListener) BaseCodec(org.redisson.client.codec.BaseCodec) ReadMode(org.redisson.config.ReadMode) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) ByteBuf(io.netty.buffer.ByteBuf) RedisClusterMaster(org.redisson.api.redisnode.RedisClusterMaster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CharsetUtil(io.netty.util.CharsetUtil) org.redisson.api(org.redisson.api) Decoder(org.redisson.client.protocol.Decoder) ClusterNodeInfo(org.redisson.cluster.ClusterNodeInfo) Awaitility.await(org.awaitility.Awaitility.await) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) SerializationCodec(org.redisson.codec.SerializationCodec) SubscriptionMode(org.redisson.config.SubscriptionMode) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) RedisCommands(org.redisson.client.protocol.RedisCommands) Test(org.junit.jupiter.api.Test) CRC16(org.redisson.connection.CRC16) Assumptions(org.junit.jupiter.api.Assumptions) RedisNodes(org.redisson.api.redisnode.RedisNodes) Assertions(org.junit.jupiter.api.Assertions) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Flag(org.redisson.cluster.ClusterNodeInfo.Flag) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) InetSocketAddress(java.net.InetSocketAddress) RedisClusterMaster(org.redisson.api.redisnode.RedisClusterMaster) IOException(java.io.IOException) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 15 with ClusterProcesses

use of org.redisson.ClusterRunner.ClusterProcesses in project redisson by redisson.

the class RedissonBlockingQueueTest method testPollFromAnyInCluster.

@Test
public void testPollFromAnyInCluster() 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);
    RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("queue:pollany");
    Executors.newSingleThreadScheduledExecutor().schedule(() -> {
        RBlockingQueue<Integer> queue2 = redisson.getBlockingQueue("queue:pollany1");
        RBlockingQueue<Integer> queue3 = redisson.getBlockingQueue("queue:pollany2");
        try {
            queue3.put(2);
            queue1.put(1);
            queue2.put(3);
        } catch (InterruptedException e) {
            Assertions.fail();
        }
    }, 3, TimeUnit.SECONDS);
    Awaitility.await().between(Duration.ofSeconds(2), Duration.ofSeconds(4)).untilAsserted(() -> {
        int value = queue1.pollFromAny(4, TimeUnit.SECONDS, "queue:pollany1", "queue:pollany2");
        assertThat(value).isEqualTo(1);
    });
    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) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)20 Config (org.redisson.config.Config)20 RandomLoadBalancer (org.redisson.connection.balancer.RandomLoadBalancer)13 Test (org.junit.jupiter.api.Test)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RedisProcess (org.redisson.RedisRunner.RedisProcess)6 ClusterNodeInfo (org.redisson.cluster.ClusterNodeInfo)6 IOException (java.io.IOException)5 RedissonClient (org.redisson.api.RedissonClient)5 RedisClientConfig (org.redisson.client.RedisClientConfig)5 java.util (java.util)4 java.util.concurrent (java.util.concurrent)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Collectors (java.util.stream.Collectors)4 RandomString (net.bytebuddy.utility.RandomString)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Awaitility.await (org.awaitility.Awaitility.await)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4