use of com.ctrip.xpipe.redis.core.meta.QuorumConfig in project x-pipe by ctripcorp.
the class DefaultSentinelCollector method collect.
@Override
public void collect(SentinelSample sentinelSample) {
Set<SentinelHello> hellos = sentinelSample.getHellos();
String clusterId = sentinelSample.getSamplePlan().getClusterId();
String shardId = sentinelSample.getSamplePlan().getShardId();
String sentinelMonitorName = metaCache.getSentinelMonitorName(clusterId, shardId);
Set<HostPort> masterDcSentinels = metaCache.getActiveDcSentinels(clusterId, shardId);
QuorumConfig quorumConfig = consoleConfig.getDefaultSentinelQuorumConfig();
HostPort masterAddr = null;
try {
masterAddr = metaCache.findMaster(clusterId, shardId);
} catch (MasterNotFoundException e) {
logger.error("[collect]" + e.getMessage(), e);
}
logger.debug("[collect]{},{},{}", clusterId, shardId, hellos);
// check delete
Set<SentinelHello> toDelete = checkAndDelete(sentinelMonitorName, masterDcSentinels, hellos, quorumConfig);
// checkReset
checkReset(clusterId, shardId, sentinelMonitorName, hellos);
// check add
Set<SentinelHello> toAdd = checkToAdd(clusterId, shardId, sentinelMonitorName, masterDcSentinels, hellos, masterAddr, quorumConfig);
doAction(toDelete, toAdd, quorumConfig);
}
use of com.ctrip.xpipe.redis.core.meta.QuorumConfig in project x-pipe by ctripcorp.
the class DefaultSentinelCollector method doAction.
private void doAction(Set<SentinelHello> toDelete, Set<SentinelHello> toAdd, QuorumConfig quorumConfig) {
if ((toDelete == null || toDelete.size() == 0) && (toAdd == null || toAdd.size() == 0)) {
return;
}
if (toAdd != null && toAdd.size() > 0) {
logger.info("[doAction][add]{}", toAdd);
}
if (toDelete != null && toDelete.size() > 0) {
logger.info("[doAction][del]{}", toDelete);
}
if (toDelete != null) {
toDelete.forEach((hello -> {
HostPort sentinelAddr = hello.getSentinelAddr();
RedisClient redisConnection = null;
try {
CatEventMonitor.DEFAULT.logEvent(SENTINEL_TYPE, "[del]" + hello);
redisConnection = sessionManager.findRedisConnection(sentinelAddr.getHost(), sentinelAddr.getPort());
redisConnection.connectSentinel().sync().remove(hello.getMonitorName());
} catch (Exception e) {
logger.error("[doAction][delete]" + hello, e);
} finally {
if (redisConnection != null) {
redisConnection.shutdown();
}
}
}));
}
if (toAdd != null) {
toAdd.forEach((hello) -> {
HostPort sentinelAddr = hello.getSentinelAddr();
RedisClient redisConnection = null;
try {
// TODO: Re-use connection instead of creating them each time
redisConnection = sessionManager.findRedisConnection(sentinelAddr.getHost(), sentinelAddr.getPort());
boolean doAdd = true;
try {
Map<String, String> map = redisConnection.connectSentinel().sync().master(hello.getMonitorName());
if (equals(hello.getMonitorName(), hello.getMasterAddr(), map)) {
doAdd = false;
logger.info("[doAction][already exist]{}, {}", map, hello.getSentinelAddr());
} else {
redisConnection.connectSentinel().sync().remove(hello.getMonitorName());
}
} catch (Exception e) {
// ingnore
}
if (doAdd) {
CatEventMonitor.DEFAULT.logEvent(SENTINEL_TYPE, "[add]" + hello);
redisConnection.connectSentinel().sync().monitor(hello.getMonitorName(), hello.getMasterAddr().getHost(), hello.getMasterAddr().getPort(), quorumConfig.getQuorum());
}
} catch (Exception e) {
logger.error("[doAction][add]" + hello, e);
} finally {
if (redisConnection != null) {
redisConnection.shutdown();
}
}
});
}
}
use of com.ctrip.xpipe.redis.core.meta.QuorumConfig in project x-pipe by ctripcorp.
the class DefaultSentinelCollector method checkToAdd.
protected Set<SentinelHello> checkToAdd(String clusterId, String shardId, String sentinelMonitorName, Set<HostPort> masterDcSentinels, Set<SentinelHello> hellos, HostPort masterAddr, QuorumConfig quorumConfig) {
if (masterAddr == null) {
logger.warn("[checkToAdd][no monitor name]{}, {}", clusterId, shardId);
return Sets.newHashSet();
}
if (StringUtil.isEmpty(sentinelMonitorName)) {
logger.warn("[checkToAdd][no monitor name]{}, {}", clusterId, shardId);
return Sets.newHashSet();
}
if (hellos.size() >= quorumConfig.getTotal()) {
return Sets.newHashSet();
}
if (!checkMasterConsistent(masterAddr, hellos)) {
logger.info("[collect][master not consistent]{}, {}, {}", sentinelMonitorName, masterAddr, hellos);
return Sets.newHashSet();
}
Set<HostPort> currentSentinels = new HashSet<>();
hellos.forEach((hello -> currentSentinels.add(hello.getSentinelAddr())));
Set<SentinelHello> toAdd = new HashSet<>();
int toAddSize = quorumConfig.getTotal() - hellos.size();
int i = 0;
for (HostPort hostPort : masterDcSentinels) {
if (!currentSentinels.contains(hostPort)) {
i++;
if (i > toAddSize) {
break;
}
toAdd.add(new SentinelHello(hostPort, masterAddr, sentinelMonitorName));
}
}
return toAdd;
}
Aggregations