Search in sources :

Example 1 with HeartbeatInfo

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;
    }
}
Also used : HeartbeatInfo(com.sohu.tv.cachecloud.client.basic.heartbeat.HeartbeatInfo) IOException(java.io.IOException) HostAndPort(redis.clients.jedis.HostAndPort) Random(java.util.Random) JedisCluster(redis.clients.jedis.JedisCluster) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HeartbeatInfo (com.sohu.tv.cachecloud.client.basic.heartbeat.HeartbeatInfo)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 HostAndPort (redis.clients.jedis.HostAndPort)1 JedisCluster (redis.clients.jedis.JedisCluster)1