Search in sources :

Example 1 with ClusterProcesses

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

the class RedissonBatchTest 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());
    RedissonClient redisson = Redisson.create(config);
    batchOptions.executionMode(ExecutionMode.IN_MEMORY_ATOMIC).syncSlaves(1, 1, TimeUnit.SECONDS);
    RBatch batch = redisson.createBatch(batchOptions);
    for (int i = 0; i < 10; i++) {
        batch.getAtomicLong("{test}" + i).addAndGetAsync(i);
    }
    BatchResult<?> result = 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with ClusterProcesses

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

the class RedissonBlockingQueueTest method testTakeReattachCluster.

@Test
public void testTakeReattachCluster() throws IOException, InterruptedException {
    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(1000);
    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<Integer>> futures = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        RBlockingQueue<Integer> queue = redisson.getBlockingQueue("queue" + i);
        RFuture<Integer> f = queue.takeAsync();
        try {
            f.toCompletableFuture().get(1, TimeUnit.SECONDS);
        } catch (ExecutionException | TimeoutException e) {
        // skip
        }
        futures.add(f);
    }
    master.stop();
    Thread.sleep(TimeUnit.SECONDS.toMillis(80));
    for (int i = 0; i < 10; i++) {
        RBlockingQueue<Integer> queue = redisson.getBlockingQueue("queue" + i);
        queue.put(i * 100);
    }
    for (int i = 0; i < 10; i++) {
        RFuture<Integer> f = futures.get(i);
        try {
            f.toCompletableFuture().get(20, TimeUnit.SECONDS);
        } catch (ExecutionException | TimeoutException e) {
        // skip
        }
        if (f.cause() != null) {
            f.cause().printStackTrace();
        }
        Integer result = f.toCompletableFuture().getNow(null);
        assertThat(result).isEqualTo(i * 100);
    }
    redisson.shutdown();
    process.shutdown();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 3 with ClusterProcesses

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

the class RedissonKeysTest method testKeysByPattern.

@Test
public void testKeysByPattern() 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;
    for (int i = 0; i < size; i++) {
        redisson.getBucket("test" + i).set(i);
    }
    assertThat(redisson.getKeys().count()).isEqualTo(size);
    Long noOfKeysDeleted = 0L;
    int chunkSize = 20;
    Iterable<String> keysIterator = redisson.getKeys().getKeysByPattern("test*", chunkSize);
    Set<String> keys = new HashSet<>();
    for (String key : keysIterator) {
        keys.add(key);
        if (keys.size() % chunkSize == 0) {
            long res = redisson.getKeys().delete(keys.toArray(new String[keys.size()]));
            assertThat(res).isEqualTo(chunkSize);
            noOfKeysDeleted += res;
            keys.clear();
        }
    }
    // Delete remaining keys
    if (!keys.isEmpty()) {
        noOfKeysDeleted += redisson.getKeys().delete(keys.toArray(new String[keys.size()]));
    }
    assertThat(noOfKeysDeleted).isEqualTo(size);
    redisson.shutdown();
    process.shutdown();
}
Also used : Config(org.redisson.config.Config) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 4 with ClusterProcesses

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

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

the class RedissonTopicTest method testSlotMigrationInCluster.

@Test
public void testSlotMigrationInCluster() 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);
    ClusterProcesses process = clusterRunner.run();
    Config config = new Config();
    config.useClusterServers().setScanInterval(1000).setSubscriptionMode(SubscriptionMode.MASTER).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RedisClientConfig cfg = new RedisClientConfig();
    cfg.setAddress(process.getNodes().iterator().next().getRedisServerAddressAndPort());
    RedisClient c = RedisClient.create(cfg);
    RedisConnection cc = c.connect();
    List<ClusterNodeInfo> mastersList = cc.sync(RedisCommands.CLUSTER_NODES);
    mastersList = mastersList.stream().filter(i -> i.containsFlag(ClusterNodeInfo.Flag.MASTER)).collect(Collectors.toList());
    c.shutdown();
    ClusterNodeInfo destination = mastersList.stream().filter(i -> i.getSlotRanges().iterator().next().getStartSlot() != 10922).findAny().get();
    ClusterNodeInfo source = mastersList.stream().filter(i -> i.getSlotRanges().iterator().next().getStartSlot() == 10922).findAny().get();
    RedisClientConfig sourceCfg = new RedisClientConfig();
    sourceCfg.setAddress(source.getAddress());
    RedisClient sourceClient = RedisClient.create(sourceCfg);
    RedisConnection sourceConnection = sourceClient.connect();
    RedisClientConfig destinationCfg = new RedisClientConfig();
    destinationCfg.setAddress(destination.getAddress());
    RedisClient destinationClient = RedisClient.create(destinationCfg);
    RedisConnection destinationConnection = destinationClient.connect();
    AtomicReference<String> reference = new AtomicReference();
    String channelName = "test{kaO}";
    RTopic topic = redisson.getTopic(channelName);
    topic.addListener(String.class, (ch, m) -> {
        reference.set(m);
    });
    List<String> destList = destinationConnection.sync(RedisCommands.PUBSUB_CHANNELS);
    assertThat(destList).isEmpty();
    List<String> sourceList = sourceConnection.sync(RedisCommands.PUBSUB_CHANNELS);
    assertThat(sourceList).containsOnly(channelName);
    destinationConnection.sync(RedisCommands.CLUSTER_SETSLOT, source.getSlotRanges().iterator().next().getStartSlot(), "IMPORTING", source.getNodeId());
    sourceConnection.sync(RedisCommands.CLUSTER_SETSLOT, source.getSlotRanges().iterator().next().getStartSlot(), "MIGRATING", destination.getNodeId());
    List<String> keys = sourceConnection.sync(RedisCommands.CLUSTER_GETKEYSINSLOT, source.getSlotRanges().iterator().next().getStartSlot(), 100);
    List<Object> params = new ArrayList<Object>();
    params.add(destination.getAddress().getHost());
    params.add(destination.getAddress().getPort());
    params.add("");
    params.add(0);
    params.add(2000);
    params.add("KEYS");
    params.addAll(keys);
    sourceConnection.async(RedisCommands.MIGRATE, params.toArray());
    for (ClusterNodeInfo node : mastersList) {
        RedisClientConfig cc1 = new RedisClientConfig();
        cc1.setAddress(node.getAddress());
        RedisClient ccc = RedisClient.create(cc1);
        RedisConnection connection = ccc.connect();
        connection.sync(RedisCommands.CLUSTER_SETSLOT, source.getSlotRanges().iterator().next().getStartSlot(), "NODE", destination.getNodeId());
        ccc.shutdownAsync();
    }
    Thread.sleep(2000);
    topic.publish("mymessage");
    Awaitility.waitAtMost(Duration.ofSeconds(1)).until(() -> reference.get().equals("mymessage"));
    List<String> destList2 = destinationConnection.sync(RedisCommands.PUBSUB_CHANNELS);
    assertThat(destList2).containsOnly(channelName);
    List<String> sourceList2 = sourceConnection.sync(RedisCommands.PUBSUB_CHANNELS);
    assertThat(sourceList2).isEmpty();
    sourceClient.shutdown();
    destinationClient.shutdown();
    redisson.shutdown();
    process.shutdown();
}
Also used : Config(org.redisson.config.Config) RedisClientConfig(org.redisson.client.RedisClientConfig) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterNodeInfo(org.redisson.cluster.ClusterNodeInfo) RedisClient(org.redisson.client.RedisClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RedisClientConfig(org.redisson.client.RedisClientConfig) RedisConnection(org.redisson.client.RedisConnection)

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