Search in sources :

Example 21 with RedisURI

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

the class ReplicatedConnectionManager method checkNode.

private void checkNode(AsyncCountDownLatch latch, RedisURI uri, ReplicatedServersConfig cfg, Set<InetSocketAddress> slaveIPs) {
    CompletionStage<RedisConnection> connectionFuture = connectToNode(cfg, uri, uri.getHost());
    connectionFuture.whenComplete((connection, exc) -> {
        if (exc != null) {
            log.error(exc.getMessage(), exc);
            latch.countDown();
            return;
        }
        if (isShuttingDown()) {
            return;
        }
        RFuture<Map<String, String>> result = connection.async(RedisCommands.INFO_REPLICATION);
        result.whenComplete((r, ex) -> {
            if (ex != null) {
                log.error(ex.getMessage(), ex);
                closeNodeConnection(connection);
                latch.countDown();
                return;
            }
            InetSocketAddress addr = connection.getRedisClient().getAddr();
            Role role = Role.valueOf(r.get(ROLE_KEY));
            if (Role.master.equals(role)) {
                InetSocketAddress master = currentMaster.get();
                if (master.equals(addr)) {
                    log.debug("Current master {} unchanged", master);
                } else if (currentMaster.compareAndSet(master, addr)) {
                    CompletableFuture<RedisClient> changeFuture = changeMaster(singleSlotRange.getStartSlot(), uri);
                    changeFuture.exceptionally(e -> {
                        log.error("Unable to change master to " + addr, e);
                        currentMaster.compareAndSet(addr, master);
                        return null;
                    });
                }
                latch.countDown();
            } else if (!config.checkSkipSlavesInit()) {
                CompletableFuture<Void> f = slaveUp(addr, uri);
                slaveIPs.add(addr);
                f.whenComplete((res, e) -> {
                    latch.countDown();
                });
            }
        });
    });
}
Also used : NodeType(org.redisson.api.NodeType) RedisConnection(org.redisson.client.RedisConnection) org.redisson.config(org.redisson.config) Logger(org.slf4j.Logger) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) AsyncCountDownLatch(org.redisson.misc.AsyncCountDownLatch) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) RedisURI(org.redisson.misc.RedisURI) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisCommands(org.redisson.client.protocol.RedisCommands) RFuture(org.redisson.api.RFuture) TimeUnit(java.util.concurrent.TimeUnit) CompletionStage(java.util.concurrent.CompletionStage) FreezeReason(org.redisson.connection.ClientConnectionsEntry.FreezeReason) Map(java.util.Map) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) RedisConnection(org.redisson.client.RedisConnection)

Example 22 with RedisURI

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

the class SentinelConnectionManager method registerSentinel.

private CompletionStage<Void> registerSentinel(RedisURI addr, MasterSlaveServersConfig c, String sslHostname) {
    boolean isHostname = NetUtil.createByteArrayFromIpAddressString(addr.getHost()) == null;
    if (!isHostname) {
        RedisClient sentinel = sentinels.get(addr);
        if (sentinel != null) {
            return CompletableFuture.completedFuture(null);
        }
    }
    RedisClient client = createClient(NodeType.SENTINEL, addr, c.getConnectTimeout(), c.getTimeout(), sslHostname);
    CompletableFuture<InetSocketAddress> future = client.resolveAddr();
    return future.thenCompose(res -> {
        RedisURI ipAddr = toURI(client.getAddr());
        if (isHostname) {
            RedisClient sentinel = sentinels.get(ipAddr);
            if (sentinel != null) {
                return CompletableFuture.completedFuture(null);
            }
        }
        CompletionStage<RedisConnection> f = client.connectAsync();
        return f.thenApply(resp -> {
            if (sentinels.putIfAbsent(ipAddr, client) == null) {
                log.info("sentinel: {} added", ipAddr);
            }
            return null;
        });
    });
}
Also used : InetSocketAddress(java.net.InetSocketAddress) RedisURI(org.redisson.misc.RedisURI)

Example 23 with RedisURI

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

the class SentinelConnectionManager method checkAuth.

private void checkAuth(SentinelServersConfig cfg) {
    if (cfg.getPassword() == null) {
        return;
    }
    for (String address : cfg.getSentinelAddresses()) {
        RedisURI addr = new RedisURI(address);
        addr = applyNatMap(addr);
        RedisClient client = createClient(NodeType.SENTINEL, addr, this.config.getConnectTimeout(), this.config.getTimeout(), null);
        try {
            RedisConnection c = client.connect();
            if (config.getPingConnectionInterval() == 0) {
                c.sync(RedisCommands.PING);
            }
            return;
        } catch (RedisAuthRequiredException e) {
            usePassword = true;
            return;
        } catch (RedisConnectionException e) {
            log.warn("Can't connect to sentinel server", e);
        } catch (Exception e) {
        // skip
        } finally {
            client.shutdown();
        }
    }
    stopThreads();
    StringBuilder list = new StringBuilder();
    for (String address : cfg.getSentinelAddresses()) {
        list.append(address).append(", ");
    }
    throw new RedisConnectionException("Unable to connect to Redis sentinel servers: " + list);
}
Also used : RedisURI(org.redisson.misc.RedisURI)

Example 24 with RedisURI

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

the class SentinelConnectionManager method checkState.

private void checkState(SentinelServersConfig cfg, Iterator<RedisClient> iterator, AtomicReference<Throwable> lastException) {
    if (!iterator.hasNext()) {
        if (lastException.get() != null) {
            log.error("Can't update cluster state", lastException.get());
        }
        performSentinelDNSCheck(null);
        scheduleChangeCheck(cfg, null);
        return;
    }
    if (!getShutdownLatch().acquire()) {
        return;
    }
    RedisClient client = iterator.next();
    RedisURI addr = toURI(client.getAddr());
    CompletionStage<RedisConnection> connectionFuture = connectToNode(NodeType.SENTINEL, cfg, addr, null);
    connectionFuture.whenComplete((connection, e) -> {
        if (e != null) {
            lastException.set(e);
            getShutdownLatch().release();
            checkState(cfg, iterator, lastException);
            return;
        }
        updateState(cfg, connection, iterator);
    });
}
Also used : RedisURI(org.redisson.misc.RedisURI)

Example 25 with RedisURI

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

the class RedissonBaseNodes method getNode.

protected RedisNode getNode(String address, NodeType nodeType) {
    Collection<MasterSlaveEntry> entries = connectionManager.getEntrySet();
    RedisURI addr = new RedisURI(address);
    for (MasterSlaveEntry masterSlaveEntry : entries) {
        if (nodeType == NodeType.MASTER && masterSlaveEntry.getAllEntries().isEmpty() && RedisURI.compare(masterSlaveEntry.getClient().getAddr(), addr)) {
            return new RedisNode(masterSlaveEntry.getClient(), commandExecutor, NodeType.MASTER);
        }
        for (ClientConnectionsEntry entry : masterSlaveEntry.getAllEntries()) {
            if (RedisURI.compare(entry.getClient().getAddr(), addr) && entry.getFreezeReason() != ClientConnectionsEntry.FreezeReason.MANAGER) {
                return new RedisNode(entry.getClient(), commandExecutor, entry.getNodeType());
            }
        }
    }
    return null;
}
Also used : RedisURI(org.redisson.misc.RedisURI) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) ClientConnectionsEntry(org.redisson.connection.ClientConnectionsEntry)

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