Search in sources :

Example 1 with ClusterSlotRange

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();
}
Also used : Set(java.util.Set) Config(org.redisson.config.Config) ClusterSlotRange(org.redisson.cluster.ClusterSlotRange) RedissonClient(org.redisson.api.RedissonClient) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 2 with ClusterSlotRange

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;
}
Also used : ArrayList(java.util.ArrayList) ClusterNodeInfo(org.redisson.cluster.ClusterNodeInfo) Flag(org.redisson.cluster.ClusterNodeInfo.Flag) ClusterSlotRange(org.redisson.cluster.ClusterSlotRange)

Example 3 with ClusterSlotRange

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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ClusterSlotRange(org.redisson.cluster.ClusterSlotRange) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

ClusterSlotRange (org.redisson.cluster.ClusterSlotRange)3 Set (java.util.Set)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Test (org.junit.jupiter.api.Test)1 RedissonClient (org.redisson.api.RedissonClient)1 ClusterNodeInfo (org.redisson.cluster.ClusterNodeInfo)1 Flag (org.redisson.cluster.ClusterNodeInfo.Flag)1 Config (org.redisson.config.Config)1 RandomLoadBalancer (org.redisson.connection.balancer.RandomLoadBalancer)1