Search in sources :

Example 1 with Flag

use of org.redisson.cluster.ClusterNodeInfo.Flag in project redisson by redisson.

the class ClusterConnectionManager method addMasterEntry.

private CompletableFuture<Void> addMasterEntry(ClusterPartition partition, ClusterServersConfig cfg) {
    CompletableFuture<Void> result = new CompletableFuture<>();
    if (partition.isMasterFail()) {
        RedisException e = new RedisException("Failed to add master: " + partition.getMasterAddress() + " for slot ranges: " + partition.getSlotRanges() + ". Reason - server has FAIL flag");
        if (partition.getSlotsAmount() == 0) {
            e = new RedisException("Failed to add master: " + partition.getMasterAddress() + ". Reason - server has FAIL flag");
        }
        result.completeExceptionally(e);
        return result;
    }
    CompletionStage<RedisConnection> connectionFuture = connectToNode(cfg, partition.getMasterAddress(), configEndpointHostName);
    connectionFuture.whenComplete((connection, ex1) -> {
        if (ex1 != null) {
            log.error("Can't connect to master: {} with slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
            result.completeExceptionally(ex1);
            return;
        }
        MasterSlaveServersConfig config = create(cfg);
        config.setMasterAddress(partition.getMasterAddress().toString());
        MasterSlaveEntry entry;
        if (config.checkSkipSlavesInit()) {
            entry = new SingleEntry(ClusterConnectionManager.this, config);
        } else {
            Set<String> slaveAddresses = partition.getSlaveAddresses().stream().map(r -> r.toString()).collect(Collectors.toSet());
            config.setSlaveAddresses(slaveAddresses);
            entry = new MasterSlaveEntry(ClusterConnectionManager.this, config);
        }
        CompletableFuture<RedisClient> f = entry.setupMasterEntry(new RedisURI(config.getMasterAddress()), configEndpointHostName);
        f.whenComplete((masterClient, ex3) -> {
            if (ex3 != null) {
                log.error("Can't add master: " + partition.getMasterAddress() + " for slot ranges: " + partition.getSlotRanges(), ex3);
                result.completeExceptionally(ex3);
                return;
            }
            for (Integer slot : partition.getSlots()) {
                addEntry(slot, entry);
                lastPartitions.put(slot, partition);
            }
            if (!config.checkSkipSlavesInit()) {
                CompletableFuture<Void> fs = entry.initSlaveBalancer(partition.getFailedSlaveAddresses(), configEndpointHostName);
                fs.whenComplete((r, ex) -> {
                    if (ex != null) {
                        log.error("unable to add slave for: " + partition.getMasterAddress() + " slot ranges: " + partition.getSlotRanges(), ex);
                        result.completeExceptionally(ex);
                        return;
                    }
                    if (!partition.getSlaveAddresses().isEmpty()) {
                        log.info("slaves: {} added for slot ranges: {}", partition.getSlaveAddresses(), partition.getSlotRanges());
                        if (!partition.getFailedSlaveAddresses().isEmpty()) {
                            log.warn("slaves: {} are down for slot ranges: {}", partition.getFailedSlaveAddresses(), partition.getSlotRanges());
                        }
                    }
                    if (result.complete(null)) {
                        log.info("master: {} added for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                    } else {
                        log.error("unable to add master: {} for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                    }
                });
            } else {
                if (result.complete(null)) {
                    log.info("master: {} added for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                } else {
                    log.error("unable to add master: {} for slot ranges: {}", partition.getMasterAddress(), partition.getSlotRanges());
                }
            }
        });
    });
    return result;
}
Also used : java.util(java.util) NodeType(org.redisson.api.NodeType) ClusterServersConfig(org.redisson.config.ClusterServersConfig) Config(org.redisson.config.Config) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) RedisURI(org.redisson.misc.RedisURI) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) RFuture(org.redisson.api.RFuture) ReadMode(org.redisson.config.ReadMode) Type(org.redisson.cluster.ClusterPartition.Type) org.redisson.connection(org.redisson.connection) FreezeReason(org.redisson.connection.ClientConnectionsEntry.FreezeReason) AddressResolver(io.netty.resolver.AddressResolver) Logger(org.slf4j.Logger) FutureListener(io.netty.util.concurrent.FutureListener) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) NatMapper(org.redisson.api.NatMapper) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) RedisCommands(org.redisson.client.protocol.RedisCommands) RedisStrictCommand(org.redisson.client.protocol.RedisStrictCommand) Future(io.netty.util.concurrent.Future) Flag(org.redisson.cluster.ClusterNodeInfo.Flag) RedisURI(org.redisson.misc.RedisURI) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig)

Example 2 with Flag

use of org.redisson.cluster.ClusterNodeInfo.Flag 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)

Aggregations

Flag (org.redisson.cluster.ClusterNodeInfo.Flag)2 AddressResolver (io.netty.resolver.AddressResolver)1 Future (io.netty.util.concurrent.Future)1 FutureListener (io.netty.util.concurrent.FutureListener)1 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)1 InetSocketAddress (java.net.InetSocketAddress)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 java.util.concurrent (java.util.concurrent)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)1 Collectors (java.util.stream.Collectors)1 NatMapper (org.redisson.api.NatMapper)1 NodeType (org.redisson.api.NodeType)1 RFuture (org.redisson.api.RFuture)1 org.redisson.client (org.redisson.client)1 RedisCommands (org.redisson.client.protocol.RedisCommands)1 RedisStrictCommand (org.redisson.client.protocol.RedisStrictCommand)1 ClusterNodeInfo (org.redisson.cluster.ClusterNodeInfo)1 Type (org.redisson.cluster.ClusterPartition.Type)1