RedisConnectionException should be throw if Redisson can't connect to servers. #343

pull/365/head
Nikita 9 years ago
parent d3b2cb3213
commit bcde8ca15e

@ -64,7 +64,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
init(this.config); init(this.config);
for (URI addr : cfg.getNodeAddresses()) { for (URI addr : cfg.getNodeAddresses()) {
RedisConnection connection = connect(cfg, addr); RedisConnection connection = connect(cfg, addr, true);
if (connection == null) { if (connection == null) {
continue; continue;
} }
@ -73,7 +73,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
Collection<ClusterPartition> partitions = parsePartitions(nodesValue); Collection<ClusterPartition> partitions = parsePartitions(nodesValue);
for (ClusterPartition partition : partitions) { for (ClusterPartition partition : partitions) {
Collection<Future<Void>> s = addMasterEntry(partition, cfg); Collection<Future<Void>> s = addMasterEntry(partition, cfg, true);
for (Future<Void> future : s) { for (Future<Void> future : s) {
future.syncUninterruptibly(); future.syncUninterruptibly();
} }
@ -82,10 +82,14 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
break; break;
} }
if (lastPartitions.isEmpty()) {
throw new RedisConnectionException("Can't connect to servers!");
}
monitorClusterChange(cfg); monitorClusterChange(cfg);
} }
private RedisConnection connect(ClusterServersConfig cfg, URI addr) { private RedisConnection connect(ClusterServersConfig cfg, URI addr, boolean skipLogging) {
RedisConnection connection = nodeConnections.get(addr); RedisConnection connection = nodeConnections.get(addr);
if (connection != null) { if (connection != null) {
return connection; return connection;
@ -95,12 +99,18 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
connection = client.connect(); connection = client.connect();
nodeConnections.put(addr, connection); nodeConnections.put(addr, connection);
} catch (RedisConnectionException e) { } catch (RedisConnectionException e) {
if (!skipLogging) {
log.warn(e.getMessage(), e); log.warn(e.getMessage(), e);
}
} catch (Exception e) { } catch (Exception e) {
if (!skipLogging) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
}
if (connection != null && !connection.isActive()) { if (connection != null && !connection.isActive()) {
log.warn("connection for {} is not active!", connection.getRedisClient().getAddr()); if (!skipLogging) {
log.warn("connection to {} is not active!", connection.getRedisClient().getAddr());
}
connection.closeAsync(); connection.closeAsync();
connection = null; connection = null;
} }
@ -114,14 +124,14 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
protected void initEntry(MasterSlaveServersConfig config) { protected void initEntry(MasterSlaveServersConfig config) {
} }
private Collection<Future<Void>> addMasterEntry(final ClusterPartition partition, ClusterServersConfig cfg) { private Collection<Future<Void>> addMasterEntry(final ClusterPartition partition, ClusterServersConfig cfg, boolean skipLogging) {
if (partition.isMasterFail()) { if (partition.isMasterFail()) {
log.warn("add master: {} for slot ranges: {} failed. Reason - server has FAIL flag", partition.getMasterAddress(), partition.getSlotRanges()); log.warn("add master: {} for slot ranges: {} failed. Reason - server has FAIL flag", partition.getMasterAddress(), partition.getSlotRanges());
Future<Void> f = newSucceededFuture(null); Future<Void> f = newSucceededFuture(null);
return Collections.singletonList(f); return Collections.singletonList(f);
} }
RedisConnection connection = connect(cfg, partition.getMasterAddress()); RedisConnection connection = connect(cfg, partition.getMasterAddress(), skipLogging);
if (connection == null) { if (connection == null) {
Future<Void> f = newSucceededFuture(null); Future<Void> f = newSucceededFuture(null);
return Collections.singletonList(f); return Collections.singletonList(f);
@ -163,7 +173,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
try { try {
for (ClusterPartition partition : lastPartitions.values()) { for (ClusterPartition partition : lastPartitions.values()) {
for (URI uri : partition.getAllAddresses()) { for (URI uri : partition.getAllAddresses()) {
RedisConnection connection = connect(cfg, uri); RedisConnection connection = connect(cfg, uri, false);
if (connection == null) { if (connection == null) {
continue; continue;
} }
@ -306,7 +316,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
} }
} }
if (!masterFound) { if (!masterFound) {
addMasterEntry(partition, cfg); addMasterEntry(partition, cfg, false);
} }
} }
} }

@ -83,6 +83,11 @@ public class ElasticacheConnectionManager extends MasterSlaveConnectionManager {
this.config.addSlaveAddress(addr); this.config.addSlaveAddress(addr);
} }
} }
if (currentMaster.get() == null) {
throw new RedisConnectionException("Can't connect to servers!");
}
init(this.config); init(this.config);
monitorRoleChange(cfg); monitorRoleChange(cfg);

@ -116,14 +116,14 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
} }
break; break;
} catch (RedisConnectionException e) { } catch (RedisConnectionException e) {
log.warn("Can't connect to sentinel. {}", e.getMessage()); log.warn("Can't connect to sentinel server. {}", e.getMessage());
} finally { } finally {
client.shutdownAsync(); client.shutdownAsync();
} }
} }
if (currentMaster.get() == null) { if (currentMaster.get() == null) {
throw new IllegalStateException("Can't connect to servers!"); throw new RedisConnectionException("Can't connect to servers!");
} }
init(c); init(c);

Loading…
Cancel
Save