Search in sources :

Example 1 with ClusterServersConfig

use of org.redisson.config.ClusterServersConfig 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)

Aggregations

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 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 Flag (org.redisson.cluster.ClusterNodeInfo.Flag)1 Type (org.redisson.cluster.ClusterPartition.Type)1 ClusterServersConfig (org.redisson.config.ClusterServersConfig)1 Config (org.redisson.config.Config)1