use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonBucketsTest method testGetInCluster.
@Test
public void testGetInCluster() 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;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put("test" + i, i);
}
for (int i = 10; i < size; i++) {
map.put("test" + i + "{" + (i % 100) + "}", i);
}
redisson.getBuckets().set(map);
Set<String> queryKeys = new HashSet<>(map.keySet());
queryKeys.add("test_invalid");
Map<String, Integer> buckets = redisson.getBuckets().get(queryKeys.toArray(new String[map.size()]));
assertThat(buckets).isEqualTo(map);
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonKeysTest method testDeleteInCluster.
@Test
public void testDeleteInCluster() 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;
List<String> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add("test" + i);
redisson.getBucket("test" + i).set(i);
}
long deletedSize = redisson.getKeys().delete(list.toArray(new String[list.size()]));
assertThat(deletedSize).isEqualTo(size);
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonTopicPatternTest method testCluster.
@Test
public void testCluster() throws IOException, InterruptedException {
RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
ClusterRunner.ClusterProcesses process = clusterRunner.run();
Thread.sleep(3000);
Config config = new Config();
config.useClusterServers().setPingConnectionInterval(0).setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
AtomicInteger subscribeCounter = new AtomicInteger();
RPatternTopic topic = redisson.getPatternTopic("__keyevent@*", StringCodec.INSTANCE);
topic.addListener(new PatternStatusListener() {
@Override
public void onPSubscribe(String pattern) {
subscribeCounter.incrementAndGet();
}
@Override
public void onPUnsubscribe(String pattern) {
System.out.println("onPUnsubscribe: " + pattern);
}
});
AtomicInteger counter = new AtomicInteger();
PatternMessageListener<String> listener = (pattern, channel, msg) -> {
System.out.println("mes " + channel + " counter " + counter.get());
counter.incrementAndGet();
};
topic.addListener(String.class, listener);
for (int i = 0; i < 10; i++) {
redisson.getBucket("" + i).set(i);
redisson.getBucket("" + i).delete();
Thread.sleep(7);
}
Awaitility.await().atMost(Duration.ofSeconds(2)).until(() -> counter.get() > 9);
assertThat(subscribeCounter.get()).isEqualTo(1);
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonTopicTest method testClusterSharding.
@Test
public void testClusterSharding() throws 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);
AtomicInteger counter = new AtomicInteger();
for (int i = 0; i < 10; i++) {
int j = i;
RTopic topic = redisson.getTopic("test" + i);
topic.addListener(Integer.class, (c, v) -> {
assertThat(v).isEqualTo(j);
counter.incrementAndGet();
});
}
for (int i = 0; i < 10; i++) {
RTopic topic = redisson.getTopic("test" + i);
topic.publish(i);
}
Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> counter.get() == 10);
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonTopicTest method testReattachInSentinel.
@Test
public void testReattachInSentinel() throws Exception {
RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().port(6399).run();
RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6399).run();
RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6399).run();
RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6399, 2).run();
RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6399, 2).run();
RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6399, 2).run();
Thread.sleep(5000);
Config config = new Config();
config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
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);
}
});
sendCommands(redisson, "topic");
sentinel1.stop();
sentinel2.stop();
sentinel3.stop();
master.stop();
slave1.stop();
slave2.stop();
Thread.sleep(TimeUnit.SECONDS.toMillis(20));
master = new RedisRunner().port(6390).nosave().randomDir().run();
slave1 = new RedisRunner().port(6391).nosave().randomDir().slaveof("127.0.0.1", 6390).run();
slave2 = new RedisRunner().port(6392).nosave().randomDir().slaveof("127.0.0.1", 6390).run();
sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6390, 2).run();
sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6390, 2).run();
sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6390, 2).run();
redisson.getTopic("topic").publish(1);
await().atMost(20, TimeUnit.SECONDS).until(() -> subscriptions.get() == 2);
assertThat(executed.get()).isTrue();
redisson.shutdown();
sentinel1.stop();
sentinel2.stop();
sentinel3.stop();
master.stop();
slave1.stop();
slave2.stop();
}
Aggregations