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();
});
}
});
});
}
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;
});
});
}
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);
}
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);
});
}
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;
}
Aggregations