use of com.sohu.tv.cachecloud.client.basic.heartbeat.HeartbeatInfo in project cachecloud by sohutv.
the class RedisClusterBuilder method build.
public JedisCluster build() {
if (jedisCluster == null) {
while (true) {
try {
lock.tryLock(10, TimeUnit.SECONDS);
if (jedisCluster != null) {
return jedisCluster;
}
String url = String.format(ConstUtils.REDIS_CLUSTER_URL, String.valueOf(appId));
String response = HttpUtils.doGet(url);
ObjectMapper objectMapper = new ObjectMapper();
HeartbeatInfo heartbeatInfo = null;
try {
heartbeatInfo = objectMapper.readValue(response, HeartbeatInfo.class);
} catch (IOException e) {
logger.error("remote build error, appId: {}", appId, e);
}
if (heartbeatInfo == null) {
continue;
}
/** 检查客户端版本 **/
if (heartbeatInfo.getStatus() == ClientStatusEnum.ERROR.getStatus()) {
throw new IllegalStateException(heartbeatInfo.getMessage());
} else if (heartbeatInfo.getStatus() == ClientStatusEnum.WARN.getStatus()) {
logger.warn(heartbeatInfo.getMessage());
} else {
logger.info(heartbeatInfo.getMessage());
}
Set<HostAndPort> nodeList = new HashSet<HostAndPort>();
//形如 ip1:port1,ip2:port2,ip3:port3
String nodeInfo = heartbeatInfo.getShardInfo();
//为了兼容,如果允许直接nodeInfo.split(" ")
nodeInfo = nodeInfo.replace(" ", ",");
String[] nodeArray = nodeInfo.split(",");
for (String node : nodeArray) {
String[] ipAndPort = node.split(":");
if (ipAndPort.length < 2) {
continue;
}
String ip = ipAndPort[0];
int port = Integer.parseInt(ipAndPort[1]);
nodeList.add(new HostAndPort(ip, port));
}
//收集上报数据
// ClientDataCollectReportExecutor.getInstance();
jedisCluster = new JedisCluster(nodeList, connectionTimeout, soTimeout, maxRedirections, jedisPoolConfig);
return jedisCluster;
} catch (Throwable e) {
logger.error(e.getMessage(), e);
} finally {
lock.unlock();
}
try {
//活锁
TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
} else {
return jedisCluster;
}
}
Aggregations