use of org.redisson.connection.balancer.RandomLoadBalancer 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();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonTopicTest method testReattachInSentinel2.
@Test
public void testReattachInSentinel2() throws Exception {
RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().port(6440).run();
RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6440).run();
RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6440).run();
RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6440, 2).run();
RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6440, 2).run();
RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6440, 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));
topic.removeAllListeners();
long t = System.currentTimeMillis();
topic.addListenerAsync(new StatusListener() {
@Override
public void onUnsubscribe(String channel) {
}
@Override
public void onSubscribe(String channel) {
System.out.println("onSubscribe " + (System.currentTimeMillis() - t));
subscriptions.incrementAndGet();
}
});
topic.addListenerAsync(Integer.class, new MessageListener<Integer>() {
@Override
public void onMessage(CharSequence channel, Integer msg) {
executed.set(true);
}
});
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
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();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonRedisNodesTest method testCluster.
@Test
public void testCluster() 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);
ClusterRunner.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);
RedisCluster nodes = redisson.getRedisNodes(RedisNodes.CLUSTER);
assertThat(nodes.getMaster("redis://127.0.0.1:6890")).isNotNull();
assertThat(nodes.getMaster("redis://127.0.0.1:6899")).isNull();
assertThat(nodes.getMasters()).hasSize(3);
assertThat(nodes.getSlaves()).hasSize(4);
for (RedisClusterMaster master : nodes.getMasters()) {
master.clusterDeleteSlots(1, 2);
master.clusterAddSlots(1, 2);
master.clusterCountKeysInSlot(1);
List<String> keys = master.clusterGetKeysInSlot(1, 10);
assertThat(keys).isEmpty();
;
String nodeId = master.clusterId();
assertThat(nodeId).isNotNull();
assertThat(master.clusterCountFailureReports(nodeId)).isZero();
Map<ClusterSlotRange, Set<String>> slots = master.clusterSlots();
assertThat(slots.entrySet().size()).isBetween(3, 5);
}
for (RedisClusterSlave slave : nodes.getSlaves()) {
slave.clusterDeleteSlots(1, 2);
slave.clusterAddSlots(1, 2);
slave.clusterCountKeysInSlot(1);
List<String> keys = slave.clusterGetKeysInSlot(1, 10);
assertThat(keys).isEmpty();
;
String nodeId = slave.clusterId();
assertThat(nodeId).isNotNull();
assertThat(slave.clusterCountFailureReports(nodeId)).isZero();
Map<ClusterSlotRange, Set<String>> slots = slave.clusterSlots();
assertThat(slots.entrySet().size()).isBetween(3, 5);
}
redisson.shutdown();
process.shutdown();
}
use of org.redisson.connection.balancer.RandomLoadBalancer in project redisson by redisson.
the class RedissonRedisNodesTest method testSentinel.
@Test
public void testSentinel() throws IOException, InterruptedException {
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();
Config config = new Config();
config.useSentinelServers().setLoadBalancer(new RandomLoadBalancer()).addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
long t = System.currentTimeMillis();
RedissonClient redisson = Redisson.create(config);
RedisSentinelMasterSlave nodes = redisson.getRedisNodes(RedisNodes.SENTINEL_MASTER_SLAVE);
assertThat(nodes.getSentinels()).hasSize(3);
assertThat(nodes.getSlaves()).hasSize(2);
assertThat(nodes.getMaster()).isNotNull();
for (RedisSentinel sentinel : nodes.getSentinels()) {
Assertions.assertTrue(sentinel.ping());
RedisURI addr = sentinel.getMasterAddr("myMaster");
assertThat(addr.getHost()).isEqualTo("127.0.0.1");
assertThat(addr.getPort()).isEqualTo(master.getRedisServerPort());
Map<String, String> masterMap = sentinel.getMaster("myMaster");
assertThat(masterMap).isNotEmpty();
List<Map<String, String>> masters = sentinel.getMasters();
assertThat(masters).hasSize(1);
Map<String, String> m = masters.get(0);
assertThat(m.get("ip")).isEqualTo("127.0.0.1");
assertThat(Integer.valueOf(m.get("port"))).isEqualTo(master.getRedisServerPort());
List<Map<String, String>> slaves = sentinel.getSlaves("myMaster");
assertThat(slaves).hasSize(2);
}
nodes.getSlaves().forEach((node) -> {
Assertions.assertTrue(node.ping());
});
redisson.shutdown();
sentinel1.stop();
sentinel2.stop();
sentinel3.stop();
master.stop();
slave1.stop();
slave2.stop();
}
use of org.redisson.connection.balancer.RandomLoadBalancer 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();
}
Aggregations