Search in sources :

Example 16 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class ClusterConnectionManager method addRemoveSlaves.

private Set<RedisURI> addRemoveSlaves(MasterSlaveEntry entry, ClusterPartition currentPart, ClusterPartition newPart) {
    Set<RedisURI> removedSlaves = new HashSet<>(currentPart.getSlaveAddresses());
    removedSlaves.removeAll(newPart.getSlaveAddresses());
    for (RedisURI uri : removedSlaves) {
        currentPart.removeSlaveAddress(uri);
        if (entry.slaveDown(uri, FreezeReason.MANAGER)) {
            log.info("slave {} removed for slot ranges: {}", uri, currentPart.getSlotRanges());
        }
    }
    Set<RedisURI> addedSlaves = new HashSet<>(newPart.getSlaveAddresses());
    addedSlaves.removeAll(currentPart.getSlaveAddresses());
    for (RedisURI uri : addedSlaves) {
        ClientConnectionsEntry slaveEntry = entry.getEntry(uri);
        if (slaveEntry != null) {
            currentPart.addSlaveAddress(uri);
            entry.slaveUp(uri, FreezeReason.MANAGER);
            log.info("slave: {} added for slot ranges: {}", uri, currentPart.getSlotRanges());
            continue;
        }
        CompletableFuture<Void> future = entry.addSlave(uri, false, NodeType.SLAVE, configEndpointHostName);
        future.whenComplete((res, ex) -> {
            if (ex != null) {
                log.error("Can't add slave: " + uri, ex);
                return;
            }
            currentPart.addSlaveAddress(uri);
            entry.slaveUp(uri, FreezeReason.MANAGER);
            log.info("slave: {} added for slot ranges: {}", uri, currentPart.getSlotRanges());
        });
    }
    return addedSlaves;
}
Also used : RedisURI(org.redisson.misc.RedisURI)

Example 17 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class ClusterConnectionManager method checkMasterNodesChange.

private CompletableFuture<Void> checkMasterNodesChange(ClusterServersConfig cfg, Collection<ClusterPartition> newPartitions) {
    Map<RedisURI, ClusterPartition> lastPartitions = getLastPartitonsByURI();
    Map<RedisURI, ClusterPartition> addedPartitions = new HashMap<>();
    Set<RedisURI> mastersElected = new HashSet<>();
    for (ClusterPartition newPart : newPartitions) {
        if (newPart.getSlotsAmount() == 0) {
            continue;
        }
        ClusterPartition currentPart = lastPartitions.get(newPart.getMasterAddress());
        boolean masterFound = currentPart != null;
        if (masterFound && newPart.isMasterFail()) {
            for (Integer slot : currentPart.getSlots()) {
                ClusterPartition newMasterPart = find(newPartitions, slot);
                // does partition has a new master?
                if (!Objects.equals(newMasterPart.getMasterAddress(), currentPart.getMasterAddress())) {
                    RedisURI newUri = newMasterPart.getMasterAddress();
                    RedisURI oldUri = currentPart.getMasterAddress();
                    mastersElected.add(newUri);
                    CompletableFuture<RedisClient> future = changeMaster(slot, newUri);
                    currentPart.setMasterAddress(newUri);
                    future.whenComplete((res, e) -> {
                        if (e != null) {
                            currentPart.setMasterAddress(oldUri);
                        } else {
                            disconnectNode(oldUri);
                        }
                    });
                }
            }
        }
        if (!masterFound && !newPart.isMasterFail()) {
            addedPartitions.put(newPart.getMasterAddress(), newPart);
        }
    }
    addedPartitions.keySet().removeAll(mastersElected);
    if (addedPartitions.isEmpty()) {
        return CompletableFuture.completedFuture(null);
    }
    List<CompletableFuture<?>> futures = new ArrayList<>();
    for (ClusterPartition newPart : addedPartitions.values()) {
        CompletableFuture<Void> future = addMasterEntry(newPart, cfg);
        futures.add(future);
    }
    return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).exceptionally(e -> null);
}
Also used : RedisURI(org.redisson.misc.RedisURI)

Example 18 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class ClusterConnectionManager method scheduleClusterChangeCheck.

private void scheduleClusterChangeCheck(ClusterServersConfig cfg) {
    monitorFuture = group.schedule(new Runnable() {

        @Override
        public void run() {
            if (configEndpointHostName != null) {
                String address = cfg.getNodeAddresses().iterator().next();
                RedisURI uri = new RedisURI(address);
                AddressResolver<InetSocketAddress> resolver = resolverGroup.getResolver(getGroup().next());
                Future<List<InetSocketAddress>> allNodes = resolver.resolveAll(InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort()));
                allNodes.addListener(new FutureListener<List<InetSocketAddress>>() {

                    @Override
                    public void operationComplete(Future<List<InetSocketAddress>> future) throws Exception {
                        AtomicReference<Throwable> lastException = new AtomicReference<Throwable>(future.cause());
                        if (!future.isSuccess()) {
                            checkClusterState(cfg, Collections.emptyIterator(), lastException);
                            return;
                        }
                        List<RedisURI> nodes = new ArrayList<>();
                        for (InetSocketAddress addr : future.getNow()) {
                            RedisURI address = toURI(uri.getScheme(), addr.getAddress().getHostAddress(), "" + addr.getPort());
                            nodes.add(address);
                        }
                        Iterator<RedisURI> nodesIterator = nodes.iterator();
                        checkClusterState(cfg, nodesIterator, lastException);
                    }
                });
            } else {
                AtomicReference<Throwable> lastException = new AtomicReference<Throwable>();
                List<RedisURI> nodes = new ArrayList<>();
                List<RedisURI> slaves = new ArrayList<>();
                for (ClusterPartition partition : getLastPartitions()) {
                    if (!partition.isMasterFail()) {
                        nodes.add(partition.getMasterAddress());
                    }
                    Set<RedisURI> partitionSlaves = new HashSet<>(partition.getSlaveAddresses());
                    partitionSlaves.removeAll(partition.getFailedSlaveAddresses());
                    slaves.addAll(partitionSlaves);
                }
                Collections.shuffle(nodes);
                Collections.shuffle(slaves);
                // master nodes first
                nodes.addAll(slaves);
                Iterator<RedisURI> nodesIterator = nodes.iterator();
                checkClusterState(cfg, nodesIterator, lastException);
            }
        }
    }, cfg.getScanInterval(), TimeUnit.MILLISECONDS);
}
Also used : RedisURI(org.redisson.misc.RedisURI) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 19 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class MasterSlaveConnectionManager method connectToNode.

protected final CompletionStage<RedisConnection> connectToNode(NodeType type, BaseConfig<?> cfg, RedisURI addr, String sslHostname) {
    RedisConnection conn = nodeConnections.get(addr);
    if (conn != null) {
        if (!conn.isActive()) {
            closeNodeConnection(conn);
        } else {
            return CompletableFuture.completedFuture(conn);
        }
    }
    RedisClient client = createClient(type, addr, cfg.getConnectTimeout(), cfg.getTimeout(), sslHostname);
    CompletionStage<RedisConnection> future = client.connectAsync();
    return future.thenCompose(connection -> {
        if (connection.isActive()) {
            if (!addr.isIP()) {
                RedisURI address = new RedisURI(addr.getScheme() + "://" + connection.getRedisClient().getAddr().getAddress().getHostAddress() + ":" + connection.getRedisClient().getAddr().getPort());
                nodeConnections.put(address, connection);
            }
            nodeConnections.put(addr, connection);
            return CompletableFuture.completedFuture(connection);
        } else {
            connection.closeAsync();
            CompletableFuture<RedisConnection> f = new CompletableFuture<>();
            f.completeExceptionally(new RedisException("Connection to " + connection.getRedisClient().getAddr() + " is not active!"));
            return f;
        }
    });
}
Also used : RedisURI(org.redisson.misc.RedisURI)

Example 20 with RedisURI

use of org.redisson.misc.RedisURI in project redisson by redisson.

the class MasterSlaveConnectionManager method resolveIP.

protected CompletableFuture<RedisURI> resolveIP(String scheme, RedisURI address) {
    if (address.isIP()) {
        RedisURI addr = applyNatMap(address);
        return CompletableFuture.completedFuture(addr);
    }
    CompletableFuture<RedisURI> result = new CompletableFuture<>();
    AddressResolver<InetSocketAddress> resolver = resolverGroup.getResolver(getGroup().next());
    InetSocketAddress addr = InetSocketAddress.createUnresolved(address.getHost(), address.getPort());
    Future<InetSocketAddress> future = resolver.resolve(addr);
    future.addListener((FutureListener<InetSocketAddress>) f -> {
        if (!f.isSuccess()) {
            log.error("Unable to resolve " + address, f.cause());
            result.completeExceptionally(f.cause());
            return;
        }
        InetSocketAddress s = f.getNow();
        RedisURI uri = toURI(scheme, s.getAddress().getHostAddress(), "" + address.getPort());
        result.complete(uri);
    });
    return result;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) java.util(java.util) NodeType(org.redisson.api.NodeType) Codec(org.redisson.client.codec.Codec) ClusterSlotRange(org.redisson.cluster.ClusterSlotRange) KQueueDatagramChannel(io.netty.channel.kqueue.KQueueDatagramChannel) LoggerFactory(org.slf4j.LoggerFactory) RedisURI(org.redisson.misc.RedisURI) InfinitySemaphoreLatch(org.redisson.misc.InfinitySemaphoreLatch) EpollDatagramChannel(io.netty.channel.epoll.EpollDatagramChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) InetAddress(java.net.InetAddress) io.netty.util.concurrent(io.netty.util.concurrent) PlatformDependent(io.netty.util.internal.PlatformDependent) KQueueSocketChannel(io.netty.channel.kqueue.KQueueSocketChannel) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) TimerTask(io.netty.util.TimerTask) SocketChannel(io.netty.channel.socket.SocketChannel) Version(org.redisson.Version) KQueueEventLoopGroup(io.netty.channel.kqueue.KQueueEventLoopGroup) AddressResolver(io.netty.resolver.AddressResolver) PublishSubscribeService(org.redisson.pubsub.PublishSubscribeService) EventLoopGroup(io.netty.channel.EventLoopGroup) org.redisson.config(org.redisson.config) Logger(org.slf4j.Logger) ElementsSubscribeService(org.redisson.ElementsSubscribeService) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) io.netty.util(io.netty.util) DnsServerAddressStreamProviders(io.netty.resolver.dns.DnsServerAddressStreamProviders) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Timer(io.netty.util.Timer) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) RedisCommand(org.redisson.client.protocol.RedisCommand) Future(io.netty.util.concurrent.Future) RedisURI(org.redisson.misc.RedisURI) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

RedisURI (org.redisson.misc.RedisURI)25 InetSocketAddress (java.net.InetSocketAddress)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Collectors (java.util.stream.Collectors)7 NodeType (org.redisson.api.NodeType)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)6 org.redisson.config (org.redisson.config)6 AddressResolver (io.netty.resolver.AddressResolver)5 Future (io.netty.util.concurrent.Future)5 java.util (java.util)5 java.util.concurrent (java.util.concurrent)5 RFuture (org.redisson.api.RFuture)5 RedisCommands (org.redisson.client.protocol.RedisCommands)5 FreezeReason (org.redisson.connection.ClientConnectionsEntry.FreezeReason)5 FutureListener (io.netty.util.concurrent.FutureListener)4 org.redisson.client (org.redisson.client)4 RedisStrictCommand (org.redisson.client.protocol.RedisStrictCommand)4 NetUtil (io.netty.util.NetUtil)3