use of org.redisson.cluster.ClusterSlotRange 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.cluster.ClusterSlotRange in project redisson by redisson.
the class ClusterNodesDecoder method decode.
@Override
public List<ClusterNodeInfo> decode(ByteBuf buf, State state) throws IOException {
String response = buf.toString(CharsetUtil.UTF_8);
List<ClusterNodeInfo> nodes = new ArrayList<>();
for (String nodeInfo : response.split("\n")) {
ClusterNodeInfo node = new ClusterNodeInfo(nodeInfo);
String[] params = nodeInfo.split(" ");
String nodeId = params[0];
node.setNodeId(nodeId);
String flags = params[2];
for (String flag : flags.split(",")) {
for (Flag nodeInfoFlag : ClusterNodeInfo.Flag.values()) {
if (nodeInfoFlag.getValue().equalsIgnoreCase(flag)) {
node.addFlag(nodeInfoFlag);
break;
}
}
}
if (!node.containsFlag(Flag.NOADDR)) {
String protocol = "redis://";
if (ssl) {
protocol = "rediss://";
}
String addr = params[1].split("@")[0];
String name = addr.substring(0, addr.lastIndexOf(":"));
if (name.isEmpty()) {
// skip nodes with empty address
continue;
}
String uri = protocol + addr;
node.setAddress(uri);
}
String slaveOf = params[3];
if (!"-".equals(slaveOf)) {
node.setSlaveOf(slaveOf);
}
if (params.length > 8) {
for (int i = 0; i < params.length - 8; i++) {
String slots = params[i + 8];
if (slots.contains("-<-") || slots.contains("->-")) {
continue;
}
String[] parts = slots.split("-");
if (parts.length == 1) {
node.addSlotRange(new ClusterSlotRange(Integer.valueOf(parts[0]), Integer.valueOf(parts[0])));
} else if (parts.length == 2) {
node.addSlotRange(new ClusterSlotRange(Integer.valueOf(parts[0]), Integer.valueOf(parts[1])));
}
}
}
nodes.add(node);
}
return nodes;
}
use of org.redisson.cluster.ClusterSlotRange in project redisson by redisson.
the class SlotsDecoder method decode.
@Override
public Object decode(List<Object> parts, State state) {
if (parts.get(0) instanceof List) {
Map<ClusterSlotRange, Set<String>> result = new HashMap<>();
List<List<Object>> rows = (List<List<Object>>) (Object) parts;
for (List<Object> row : rows) {
Iterator<Object> iterator = row.iterator();
Long startSlot = (Long) iterator.next();
Long endSlot = (Long) iterator.next();
ClusterSlotRange range = new ClusterSlotRange(startSlot.intValue(), endSlot.intValue());
Set<String> addresses = new HashSet<>();
while (iterator.hasNext()) {
List<Object> addressParts = (List<Object>) iterator.next();
addresses.add(addressParts.get(0) + ":" + addressParts.get(1));
}
result.put(range, addresses);
}
return result;
}
return parts;
}
Aggregations