Search in sources :

Example 11 with RandomLoadBalancer

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

the class RedissonTest method testSentinelStartup.

@Test
public void testSentinelStartup() 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");
    long t = System.currentTimeMillis();
    RedissonClient redisson = Redisson.create(config);
    assertThat(System.currentTimeMillis() - t).isLessThan(2000L);
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) RedisProcess(org.redisson.RedisRunner.RedisProcess) Test(org.junit.jupiter.api.Test)

Example 12 with RandomLoadBalancer

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

the class RedissonTest method testSentinelStartupWithPassword.

@Test
public void testSentinelStartupWithPassword() throws Exception {
    RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().requirepass("123").run();
    RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6379).requirepass("123").masterauth("123").run();
    RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6379).requirepass("123").masterauth("123").run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).sentinelAuthPass("myMaster", "123").requirepass("123").run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).sentinelAuthPass("myMaster", "123").requirepass("123").run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6379, 2).sentinelAuthPass("myMaster", "123").requirepass("123").run();
    Thread.sleep(5000);
    Config config = new Config();
    config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).setPassword("123").addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    long t = System.currentTimeMillis();
    RedissonClient redisson = Redisson.create(config);
    assertThat(System.currentTimeMillis() - t).isLessThan(2000L);
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) RedisProcess(org.redisson.RedisRunner.RedisProcess) Test(org.junit.jupiter.api.Test)

Example 13 with RandomLoadBalancer

use of org.redisson.connection.balancer.RandomLoadBalancer 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 14 with RandomLoadBalancer

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

the class RedissonTest method testEvalCache.

@Test
public void testEvalCache() throws InterruptedException, IOException {
    RedisRunner master1 = new RedisRunner().port(6896).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);
    ClusterRunner.ClusterProcesses process = clusterRunner.run();
    Thread.sleep(5000);
    Config config = new Config();
    config.setUseScriptCache(true);
    config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RTimeSeries<String> t = redisson.getTimeSeries("test");
    t.add(4, "40");
    t.add(2, "20");
    t.add(1, "10", 1, TimeUnit.SECONDS);
    t.size();
}
Also used : ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) RandomString(net.bytebuddy.utility.RandomString) Test(org.junit.jupiter.api.Test)

Example 15 with RandomLoadBalancer

use of org.redisson.connection.balancer.RandomLoadBalancer 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

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