Search in sources :

Example 31 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonTest method testReconnection.

@Test
public void testReconnection() throws IOException, InterruptedException, TimeoutException {
    RedisProcess runner = new RedisRunner().appendonly(true).randomDir().randomPort().run();
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient r = Redisson.create(config);
    r.getBucket("myBucket").set(1);
    assertThat(r.getBucket("myBucket").get()).isEqualTo(1);
    Assertions.assertEquals(0, runner.stop());
    AtomicBoolean hasError = new AtomicBoolean();
    try {
        r.getBucket("myBucket").get();
    } catch (Exception e) {
        // skip error
        hasError.set(true);
    }
    assertThat(hasError.get()).isTrue();
    RedisProcess pp = new RedisRunner().appendonly(true).port(runner.getRedisServerPort()).dir(runner.getDefaultDir()).run();
    assertThat(r.getBucket("myBucket").get()).isEqualTo(1);
    r.shutdown();
    Assertions.assertEquals(0, pp.stop());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 32 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonTest method testMemoryScript.

@Test
public void testMemoryScript() throws IOException, InterruptedException {
    RedisProcess p = redisTestSmallMemory();
    Config config = new Config();
    config.useSingleServer().setAddress(p.getRedisServerAddressAndPort()).setTimeout(100000);
    Assertions.assertThrows(RedisOutOfMemoryException.class, () -> {
        RedissonClient r = null;
        try {
            r = Redisson.create(config);
            r.getKeys().flushall();
            for (int i = 0; i < 10000; i++) {
                r.getMap("test").put("" + i, "" + i);
            }
        } finally {
            r.shutdown();
            p.stop();
        }
    });
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.jupiter.api.Test)

Example 33 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess 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 34 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class JCacheTest method testRedissonConfig.

@Test
public void testRedissonConfig() throws InterruptedException, IllegalArgumentException, IOException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    URL configUrl = getClass().getResource("redisson-jcache.yaml");
    Config cfg = Config.fromYAML(configUrl);
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager().createCache("test", config);
    cache.put("1", "2");
    Assertions.assertEquals("2", cache.get("1"));
    cache.put("key", "value");
    String result = cache.getAndRemove("key");
    Assertions.assertEquals("value", result);
    Assertions.assertNull(cache.get("key"));
    cache.put("key", "value");
    cache.remove("key");
    Assertions.assertNull(cache.get("key"));
    cache.close();
    runner.stop();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) RedisRunner(org.redisson.RedisRunner) URL(java.net.URL) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 35 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class JCacheTest method testUpdateWithoutOldValue.

@Test
public void testUpdateWithoutOldValue() throws IOException, InterruptedException, URISyntaxException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    config.setStoreByValue(true);
    URI configUri = getClass().getResource("redisson-jcache.yaml").toURI();
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null).createCache("test", config);
    CountDownLatch latch = new CountDownLatch(1);
    String key = "123";
    UpdatedListener secondClientListener = new UpdatedListener(latch, key, null, "90");
    MutableCacheEntryListenerConfiguration<String, String> secondListenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(secondClientListener), null, false, true);
    cache.registerCacheEntryListener(secondListenerConfiguration);
    cache.put(key, "80");
    assertThat(cache.get(key)).isNotNull();
    cache.put(key, "90");
    latch.await();
    assertThat(cache.get(key)).isNotNull();
    cache.close();
    runner.stop();
}
Also used : CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) RedisProcess(org.redisson.RedisRunner.RedisProcess) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) RedisRunner(org.redisson.RedisRunner) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) MutableConfiguration(javax.cache.configuration.MutableConfiguration) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

RedisProcess (org.redisson.RedisRunner.RedisProcess)55 Test (org.junit.jupiter.api.Test)49 Config (org.redisson.config.Config)41 RedissonClient (org.redisson.api.RedissonClient)30 BaseTest (org.redisson.BaseTest)16 RedisRunner (org.redisson.RedisRunner)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 RLock (org.redisson.api.RLock)12 URL (java.net.URL)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)6 TimeoutException (java.util.concurrent.TimeoutException)6 MutableConfiguration (javax.cache.configuration.MutableConfiguration)6 URI (java.net.URI)5 ExecutionException (java.util.concurrent.ExecutionException)5 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)5 HashMap (java.util.HashMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)4