Search in sources :

Example 36 with AppDesc

use of com.sohu.cache.entity.AppDesc in project cachecloud by sohutv.

the class InstanceRunInspector method generateMessage.

/**
     * 返回示例消息
     *
     * @param info
     * @return
     */
private String generateMessage(InstanceInfo info, boolean isEmail) {
    StringBuffer message = new StringBuffer();
    long appId = info.getAppId();
    AppDesc appDesc = appDao.getAppDescById(appId);
    message.append("CacheCloud系统-实例(" + info.getIp() + ":" + info.getPort() + ")-");
    if (info.getStatus() == InstanceStatusEnum.ERROR_STATUS.getStatus()) {
        message.append("由运行中变为心跳停止");
    } else if (info.getStatus() == InstanceStatusEnum.GOOD_STATUS.getStatus()) {
        message.append("由心跳停止变为运行中");
    }
    if (isEmail) {
        message.append(", appId:");
        message.append(appId + "-" + appDesc.getName());
    } else {
        message.append("-appId(" + appId + "-" + appDesc.getName() + ")");
    }
    return message.toString();
}
Also used : AppDesc(com.sohu.cache.entity.AppDesc)

Example 37 with AppDesc

use of com.sohu.cache.entity.AppDesc in project cachecloud by sohutv.

the class RedisDeployCenterImpl method addSentinel.

@Override
public boolean addSentinel(long appId, String sentinelHost) {
    AppDesc appDesc = appDao.getAppDescById(appId);
    JedisSentinelPool jedisSentinelPool = redisCenter.getJedisSentinelPool(appDesc);
    if (jedisSentinelPool == null) {
        return false;
    }
    List<InstanceInfo> instanceInfos = instanceDao.getInstListByAppId(appId);
    String masterName = null;
    for (Iterator<InstanceInfo> i = instanceInfos.iterator(); i.hasNext(); ) {
        InstanceInfo instanceInfo = i.next();
        if (instanceInfo.getType() != ConstUtils.CACHE_REDIS_SENTINEL) {
            i.remove();
            continue;
        }
        if (masterName == null && StringUtils.isNotBlank(instanceInfo.getCmd())) {
            masterName = instanceInfo.getCmd();
        }
    }
    Jedis jedis = null;
    String masterHost = null;
    Integer masterPort = null;
    try {
        jedis = jedisSentinelPool.getResource();
        masterHost = jedis.getClient().getHost();
        masterPort = jedis.getClient().getPort();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        jedis.close();
        jedisSentinelPool.destroy();
    }
    boolean isRun = runSentinel(sentinelHost, masterName, masterHost, masterPort, appId);
    if (!isRun) {
        return false;
    }
    return true;
}
Also used : Jedis(redis.clients.jedis.Jedis) AppDesc(com.sohu.cache.entity.AppDesc) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 38 with AppDesc

use of com.sohu.cache.entity.AppDesc in project cachecloud by sohutv.

the class RedisDeployCenterImpl method addSlave.

@Override
public boolean addSlave(long appId, int instanceId, final String slaveHost) {
    Assert.isTrue(appId > 0);
    Assert.isTrue(instanceId > 0);
    Assert.isTrue(StringUtils.isNotBlank(slaveHost));
    AppDesc appDesc = appDao.getAppDescById(appId);
    Assert.isTrue(appDesc != null);
    int type = appDesc.getType();
    if (!TypeUtil.isRedisType(type)) {
        logger.error("{} is not redis type", appDesc);
        return false;
    }
    InstanceInfo instanceInfo = instanceDao.getInstanceInfoById(instanceId);
    Assert.isTrue(instanceInfo != null);
    String masterHost = instanceInfo.getIp();
    int masterPort = instanceInfo.getPort();
    final Integer slavePort = machineCenter.getAvailablePort(slaveHost, ConstUtils.CACHE_REDIS_STANDALONE);
    if (slavePort == null) {
        logger.error("host={} getAvailablePort is null", slaveHost);
        return false;
    }
    boolean isRun;
    if (TypeUtil.isRedisCluster(type)) {
        isRun = runInstance(slaveHost, slavePort, instanceInfo.getMem(), true);
    } else {
        isRun = runInstance(slaveHost, slavePort, instanceInfo.getMem(), false);
    }
    if (!isRun) {
        logger.error("{}:{} is not run", slaveHost, slavePort);
        return false;
    }
    boolean isCopy = copyCommonConfig(masterHost, masterPort, slaveHost, slavePort);
    if (!isCopy) {
        logger.error("{}:{} copy config {}:{} is error", masterHost, masterPort, slaveHost, slavePort);
        return false;
    }
    if (TypeUtil.isRedisCluster(type)) {
        final Jedis masterJedis = new Jedis(masterHost, masterPort, Protocol.DEFAULT_TIMEOUT);
        final Jedis slaveJedis = new Jedis(slaveHost, slavePort, Protocol.DEFAULT_TIMEOUT);
        try {
            boolean isClusterMeet = clusterMeet(masterJedis, slaveHost, slavePort);
            if (!isClusterMeet) {
                logger.error("{}:{} cluster is failed", slaveHost, slaveHost);
                return isClusterMeet;
            }
            final String nodeId = redisCenter.getNodeId(masterHost, masterPort);
            if (StringUtils.isBlank(nodeId)) {
                logger.error("{}:{} getNodeId failed", masterHost, masterPort);
                return false;
            }
            boolean isClusterReplicate = new IdempotentConfirmer() {

                @Override
                public boolean execute() {
                    try {
                        //等待广播节点
                        TimeUnit.SECONDS.sleep(2);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                    String response = slaveJedis.clusterReplicate(nodeId);
                    logger.info("clusterReplicate-{}:{}={}", slaveHost, slavePort, response);
                    return response != null && response.equalsIgnoreCase("OK");
                }
            }.run();
            if (!isClusterReplicate) {
                logger.error("{}:{} clusterReplicate {} is failed ", slaveHost, slavePort, nodeId);
                return false;
            }
            //保存配置
            masterJedis.clusterSaveConfig();
            slaveJedis.clusterSaveConfig();
            redisCenter.configRewrite(masterHost, masterPort);
            redisCenter.configRewrite(slaveHost, slavePort);
        } finally {
            masterJedis.close();
            slaveJedis.close();
        }
    } else {
        boolean isSlave = slaveOf(masterHost, masterPort, slaveHost, slavePort);
        if (!isSlave) {
            logger.error("{}:{} sync {}:{} is error", slaveHost, slavePort, masterHost, masterPort);
            return false;
        }
    }
    //写入instanceInfo 信息
    if (TypeUtil.isRedisCluster(type)) {
        saveInstance(appId, slaveHost, slavePort, instanceInfo.getMem(), ConstUtils.CACHE_TYPE_REDIS_CLUSTER, "");
    } else {
        saveInstance(appId, slaveHost, slavePort, instanceInfo.getMem(), ConstUtils.CACHE_REDIS_STANDALONE, "");
    }
    //启动监控trigger
    boolean isDeploy = redisCenter.deployRedisCollection(appId, slaveHost, slavePort);
    if (!isDeploy) {
        logger.warn("host={},port={},isMasterDeploy=false", slaveHost, slavePort);
    }
    return true;
}
Also used : Jedis(redis.clients.jedis.Jedis) IdempotentConfirmer(com.sohu.cache.util.IdempotentConfirmer) AppDesc(com.sohu.cache.entity.AppDesc) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 39 with AppDesc

use of com.sohu.cache.entity.AppDesc in project cachecloud by sohutv.

the class RedisDeployCenterImpl method addSlotsFailMaster.

@Override
public RedisOperateEnum addSlotsFailMaster(long appId, int lossSlotsInstanceId, String newMasterHost) throws Exception {
    // 1.参数、应用、实例信息确认
    Assert.isTrue(appId > 0);
    Assert.isTrue(lossSlotsInstanceId > 0);
    Assert.isTrue(StringUtils.isNotBlank(newMasterHost));
    AppDesc appDesc = appDao.getAppDescById(appId);
    Assert.isTrue(appDesc != null);
    int type = appDesc.getType();
    if (!TypeUtil.isRedisCluster(type)) {
        logger.error("{} is not redis cluster type", appDesc);
        return RedisOperateEnum.FAIL;
    }
    //获取失联slots的实例信息
    InstanceInfo lossSlotsInstanceInfo = instanceDao.getInstanceInfoById(lossSlotsInstanceId);
    Assert.isTrue(lossSlotsInstanceInfo != null);
    // 2.获取集群中一个健康的master作为clusterInfo Nodes的数据源
    InstanceInfo sourceMasterInstance = redisCenter.getHealthyInstanceInfo(appId);
    // 并未找到一个合适的实例可以
    if (sourceMasterInstance == null) {
        logger.warn("appId {} does not have right instance", appId);
        return RedisOperateEnum.FAIL;
    }
    // 3. 找到丢失的slots,如果没找到就说明集群正常,直接返回
    String healthyMasterHost = sourceMasterInstance.getIp();
    int healthyMasterPort = sourceMasterInstance.getPort();
    int healthyMasterMem = sourceMasterInstance.getMem();
    // 3.1 查看整个集群中是否有丢失的slots
    List<Integer> allLossSlots = redisCenter.getClusterLossSlots(healthyMasterHost, healthyMasterPort);
    if (CollectionUtils.isEmpty(allLossSlots)) {
        logger.warn("appId {} all slots is regular and assigned", appId);
        return RedisOperateEnum.ALREADY_SUCCESS;
    }
    // 3.2 查看目标实例丢失slots 
    List<Integer> clusterLossSlots = redisCenter.getInstanceSlots(healthyMasterHost, healthyMasterPort, lossSlotsInstanceInfo.getIp(), lossSlotsInstanceInfo.getPort());
    // 4.开启新的节点
    // 4.1 从newMasterHost找到可用的端口newMasterPort
    final Integer newMasterPort = machineCenter.getAvailablePort(newMasterHost, ConstUtils.CACHE_TYPE_REDIS_CLUSTER);
    if (newMasterPort == null) {
        logger.error("host={} getAvailablePort is null", newMasterHost);
        return RedisOperateEnum.FAIL;
    }
    // 4.2 按照sourceMasterInstance的内存启动
    boolean isRun = runInstance(newMasterHost, newMasterPort, healthyMasterMem, true);
    if (!isRun) {
        logger.error("{}:{} is not run", newMasterHost, newMasterPort);
        return RedisOperateEnum.FAIL;
    }
    // 4.3 拷贝配置
    boolean isCopy = copyCommonConfig(healthyMasterHost, healthyMasterPort, newMasterHost, newMasterPort);
    if (!isCopy) {
        logger.error("{}:{} copy config {}:{} is error", healthyMasterHost, healthyMasterPort, newMasterHost, newMasterPort);
        return RedisOperateEnum.FAIL;
    }
    // 5. meet
    boolean isClusterMeet = false;
    Jedis sourceMasterJedis = null;
    try {
        sourceMasterJedis = new Jedis(healthyMasterHost, healthyMasterPort, 5000);
        isClusterMeet = clusterMeet(sourceMasterJedis, newMasterHost, newMasterPort);
        if (!isClusterMeet) {
            logger.error("{}:{} cluster is failed", newMasterHost, newMasterPort);
            return RedisOperateEnum.FAIL;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (sourceMasterJedis != null) {
            sourceMasterJedis.close();
        }
    }
    if (!isClusterMeet) {
        logger.warn("{}:{} meet {}:{} is fail", healthyMasterHost, healthyMasterPort, newMasterHost, newMasterPort);
        return RedisOperateEnum.FAIL;
    }
    // 6. 分配slots
    String addSlotsResult = "";
    Jedis newMasterJedis = null;
    Jedis healthyMasterJedis = null;
    try {
        newMasterJedis = new Jedis(newMasterHost, newMasterPort, 5000);
        healthyMasterJedis = new Jedis(healthyMasterHost, healthyMasterPort, 5000);
        //获取新的补救节点的nodid
        final String nodeId = getClusterNodeId(newMasterJedis);
        for (Integer slot : clusterLossSlots) {
            addSlotsResult = healthyMasterJedis.clusterSetSlotNode(slot, nodeId);
            logger.warn("set slot {}, result is {}", slot, addSlotsResult);
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (newMasterJedis != null) {
            newMasterJedis.close();
        }
        if (healthyMasterJedis != null) {
            healthyMasterJedis.close();
        }
    }
    if (!"OK".equalsIgnoreCase(addSlotsResult)) {
        logger.warn("{}:{} set slots faily", newMasterHost, newMasterPort);
        return RedisOperateEnum.FAIL;
    }
    // 7.保存实例信息、并开启收集信息
    saveInstance(appId, newMasterHost, newMasterPort, healthyMasterMem, ConstUtils.CACHE_TYPE_REDIS_CLUSTER, "");
    redisCenter.deployRedisCollection(appId, newMasterHost, newMasterPort);
    // 休息一段时间,同步clusterNodes信息
    TimeUnit.SECONDS.sleep(2);
    // 8.最终打印出当前还没有补充的slots
    List<Integer> currentLossSlots = redisCenter.getClusterLossSlots(newMasterHost, newMasterPort);
    logger.warn("appId {} failslots assigned unsuccessfully, lossslots is {}", appId, currentLossSlots);
    return RedisOperateEnum.OP_SUCCESS;
}
Also used : Jedis(redis.clients.jedis.Jedis) AppDesc(com.sohu.cache.entity.AppDesc) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 40 with AppDesc

use of com.sohu.cache.entity.AppDesc in project cachecloud by sohutv.

the class AppDeployCenterImpl method allocateResourceApp.

@Override
public boolean allocateResourceApp(Long appAuditId, List<String> nodeInfoList, AppUser auditUser) {
    if (appAuditId == null || appAuditId <= 0L) {
        logger.error("appAuditId is null");
        return false;
    }
    if (nodeInfoList == null || nodeInfoList.isEmpty()) {
        logger.error("nodeInfoList is null");
        return false;
    }
    AppAudit appAudit = appAuditDao.getAppAudit(appAuditId);
    if (appAudit == null) {
        logger.error("appAudit:id={} is not exist", appAuditId);
        return false;
    }
    long appId = appAudit.getAppId();
    AppDesc appDesc = appService.getByAppId(appId);
    if (appDesc == null) {
        logger.error("appDesc:id={} is not exist");
        return false;
    }
    int type = appDesc.getType();
    List<String[]> nodes = new ArrayList<String[]>();
    for (String nodeInfo : nodeInfoList) {
        nodeInfo = StringUtils.trim(nodeInfo);
        if (StringUtils.isBlank(nodeInfo)) {
            continue;
        }
        String[] array = nodeInfo.split(":");
        //            if (array.length < 2) {
        //                logger.error("error nodeInfo:{}", Arrays.toString(array));
        //                continue;
        //            }
        nodes.add(array);
    }
    boolean isAudited = false;
    if (TypeUtil.isRedisType(type)) {
        if (TypeUtil.isRedisCluster(type)) {
            isAudited = deployCluster(appId, nodes);
        } else if (nodes.size() > 0) {
            if (TypeUtil.isRedisSentinel(type)) {
                isAudited = deploySentinel(appId, nodes);
            } else {
                isAudited = deployStandalone(appId, nodes.get(0));
            }
        } else {
            logger.error("nodeInfoList={} is error");
        }
    } else {
        logger.error("unknown type : {}", type);
        return false;
    }
    //审核通过
    if (isAudited) {
        // 改变审核状态
        appAuditDao.updateAppAudit(appAudit.getId(), AppCheckEnum.APP_ALLOCATE_RESOURCE.value());
    }
    return true;
}
Also used : AppAudit(com.sohu.cache.entity.AppAudit) ArrayList(java.util.ArrayList) AppDesc(com.sohu.cache.entity.AppDesc)

Aggregations

AppDesc (com.sohu.cache.entity.AppDesc)47 BaseTest (com.sohu.test.BaseTest)18 Test (org.junit.Test)18 ImportAppResult (com.sohu.cache.constant.ImportAppResult)15 InstanceInfo (com.sohu.cache.entity.InstanceInfo)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 Date (java.util.Date)5 Jedis (redis.clients.jedis.Jedis)5 AppAudit (com.sohu.cache.entity.AppAudit)4 InstanceStats (com.sohu.cache.entity.InstanceStats)3 IdempotentConfirmer (com.sohu.cache.util.IdempotentConfirmer)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 AppUser (com.sohu.cache.entity.AppUser)2 AppDetailVO (com.sohu.cache.web.vo.AppDetailVO)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AppClientCostTimeTotalStat (com.sohu.cache.entity.AppClientCostTimeTotalStat)1 AppClientExceptionStat (com.sohu.cache.entity.AppClientExceptionStat)1