Search in sources :

Example 6 with ClusterProcesses

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

the class RedissonReadWriteLockTest method testInCluster.

@Test
public void testInCluster() throws Exception {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1).addNode(master2).addNode(master3);
    ClusterProcesses process = clusterRunner.run();
    Config config = new Config();
    config.useClusterServers().addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RReadWriteLock s = redisson.getReadWriteLock("1234");
    s.writeLock().lock();
    s.readLock().lock();
    s.readLock().unlock();
    s.writeLock().unlock();
    redisson.shutdown();
    process.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 7 with ClusterProcesses

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

the class RedissonTopicTest method testReattachPatternTopicListenersOnClusterFailover.

@Test
public void testReattachPatternTopicListenersOnClusterFailover() throws Exception {
    final KEYSPACE_EVENTS_OPTIONS[] keyspaceEvents = { KEYSPACE_EVENTS_OPTIONS.K, KEYSPACE_EVENTS_OPTIONS.E, KEYSPACE_EVENTS_OPTIONS.A };
    final RedisRunner master = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(keyspaceEvents);
    final RedisRunner slave = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(keyspaceEvents);
    final ClusterRunner clusterRunner = new ClusterRunner().addNode(master, slave);
    final ClusterProcesses process = clusterRunner.run();
    final Config config = new Config();
    config.useClusterServers().addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    final RedissonClient redisson = Redisson.create(config);
    final AtomicInteger subscriptions = new AtomicInteger();
    final AtomicInteger messagesReceived = new AtomicInteger();
    final RPatternTopic topic = redisson.getPatternTopic("__keyspace*__:i*", StringCodec.INSTANCE);
    topic.addListener(new PatternStatusListener() {

        @Override
        public void onPUnsubscribe(String pattern) {
        }

        @Override
        public void onPSubscribe(String pattern) {
            subscriptions.incrementAndGet();
        }
    });
    topic.addListener(String.class, (pattern, channel, msg) -> messagesReceived.incrementAndGet());
    assertThat(subscriptions.get()).isEqualTo(1);
    sendCommands(redisson, "dummy").join();
    await().atMost(30, TimeUnit.SECONDS).until(() -> messagesReceived.get() == 100);
    failover(process, master, slave);
    redisson.getBucket("i100").set("");
    await().atMost(30, TimeUnit.SECONDS).until(() -> subscriptions.get() == 2);
    await().atMost(5, TimeUnit.SECONDS).until(() -> messagesReceived.get() == 101);
    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) KEYSPACE_EVENTS_OPTIONS(org.redisson.RedisRunner.KEYSPACE_EVENTS_OPTIONS)

Example 8 with ClusterProcesses

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

Example 9 with ClusterProcesses

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

the class RedissonTest method testMovedRedirectInCluster.

@Test
public void testMovedRedirectInCluster() 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(100000).setLoadBalancer(new RandomLoadBalancer()).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> cn = cc.sync(RedisCommands.CLUSTER_NODES);
    c.shutdownAsync();
    cn = cn.stream().filter(i -> i.containsFlag(Flag.MASTER)).collect(Collectors.toList());
    Iterator<ClusterNodeInfo> nodesIter = cn.iterator();
    ClusterNodeInfo source = nodesIter.next();
    ClusterNodeInfo destination = nodesIter.next();
    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();
    String key = null;
    int slot = 0;
    for (int i = 0; i < 100000; i++) {
        key = "" + i;
        slot = CRC16.crc16(key.getBytes()) % MasterSlaveConnectionManager.MAX_SLOT;
        if (source.getSlotRanges().iterator().next().getStartSlot() == slot) {
            break;
        }
    }
    redisson.getBucket(key).set("123");
    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 : cn) {
        RedisClientConfig cc1 = new RedisClientConfig();
        cc1.setAddress(node.getAddress());
        RedisClient ccc = RedisClient.create(cc1);
        RedisConnection connection = ccc.connect();
        connection.sync(RedisCommands.CLUSTER_SETSLOT, slot, "NODE", destination.getNodeId());
        ccc.shutdownAsync();
    }
    redisson.getBucket(key).set("123");
    redisson.getBucket(key).get();
    sourceClient.shutdown();
    destinationClient.shutdown();
    redisson.shutdown();
    process.shutdown();
}
Also used : Config(org.redisson.config.Config) RandomString(net.bytebuddy.utility.RandomString) ClusterNodeInfo(org.redisson.cluster.ClusterNodeInfo) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 10 with ClusterProcesses

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

the class RedissonTest method testFailoverWithoutErrorsInCluster.

@Test
public void testFailoverWithoutErrorsInCluster() 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();
    Config config = new Config();
    config.useClusterServers().setRetryAttempts(30).setReadMode(ReadMode.MASTER).setSubscriptionMode(SubscriptionMode.MASTER).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<?>>();
    Set<InetSocketAddress> oldMasters = new HashSet<>();
    Collection<RedisClusterMaster> masterNodes = redisson.getRedisNodes(RedisNodes.CLUSTER).getMasters();
    for (RedisClusterMaster clusterNode : masterNodes) {
        oldMasters.add(clusterNode.getAddr());
    }
    master.stop();
    for (int j = 0; j < 2000; j++) {
        RFuture<?> f2 = redisson.getBucket("" + j).setAsync("");
        futures.add(f2);
    }
    System.out.println("master " + master.getRedisServerAddressAndPort() + " has been stopped!");
    Thread.sleep(TimeUnit.SECONDS.toMillis(40));
    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();
    for (RFuture<?> rFuture : futures) {
        try {
            rFuture.toCompletableFuture().join();
        } catch (Exception e) {
        // skip
        }
        if (!rFuture.isSuccess()) {
            Assertions.fail();
        }
    }
    redisson.shutdown();
    process.shutdown();
}
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)

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