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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations