Search in sources :

Example 31 with InstanceInfo

use of com.sohu.cache.entity.InstanceInfo 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 32 with InstanceInfo

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

the class RedisDeployCenterImpl method modifyAppConfig.

@Override
public boolean modifyAppConfig(long appId, String parameter, String value) {
    List<InstanceInfo> list = instanceDao.getInstListByAppId(appId);
    if (list == null || list.isEmpty()) {
        logger.error(String.format("appId=%s no instances", appId));
        return false;
    }
    for (InstanceInfo instance : list) {
        int type = instance.getType();
        if (!TypeUtil.isRedisType(type)) {
            logger.error("appId={};type={};is not redisType", appId, type);
            return false;
        }
        //忽略sentinel
        if (TypeUtil.isRedisSentinel(type)) {
            continue;
        }
        //忽略下线
        if (instance.isOffline()) {
            continue;
        }
        String host = instance.getIp();
        int port = instance.getPort();
        if (!modifyInstanceConfig(host, port, parameter, value)) {
            return false;
        }
    }
    return true;
}
Also used : InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 33 with InstanceInfo

use of com.sohu.cache.entity.InstanceInfo 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 34 with InstanceInfo

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

the class RedisDeployCenterImpl method saveInstance.

private InstanceInfo saveInstance(long appId, String host, int port, int maxMemory, int type, String cmd) {
    InstanceInfo instanceInfo = new InstanceInfo();
    instanceInfo.setAppId(appId);
    MachineInfo machineInfo = machineDao.getMachineInfoByIp(host);
    instanceInfo.setHostId(machineInfo.getId());
    instanceInfo.setConn(0);
    instanceInfo.setMem(maxMemory);
    instanceInfo.setStatus(InstanceStatusEnum.GOOD_STATUS.getStatus());
    instanceInfo.setPort(port);
    instanceInfo.setType(type);
    instanceInfo.setCmd(cmd);
    instanceInfo.setIp(host);
    instanceDao.saveInstance(instanceInfo);
    return instanceInfo;
}
Also used : MachineInfo(com.sohu.cache.entity.MachineInfo) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 35 with InstanceInfo

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

the class TriggerCenterImpl method addTrigger.

/**
     * 增加一个新trigger
     *
     * @param jobGroup  trigger所属的job分组:redis/machine/machineMonitor
     * @param ip
     * @param port
     * @return
     */
@Override
public boolean addTrigger(String jobGroup, String ip, int port) {
    Assert.hasText(jobGroup, "jobGroup is invalid: " + jobGroup);
    Assert.hasText(ip, "ip is invalid, ip: " + ip);
    Assert.isTrue(port > 0, "port is invalid, port: " + port);
    boolean opResult = false;
    if (jobGroup.equals(ConstUtils.REDIS_JOB_GROUP)) {
        InstanceInfo instanceInfo = instanceDao.getInstByIpAndPort(ip, port);
        opResult = redisCenter.deployRedisCollection(instanceInfo.getAppId(), ip, port);
    } else if (jobGroup.equals(ConstUtils.REDIS_SLOWLOG_JOB_GROUP)) {
        InstanceInfo instanceInfo = instanceDao.getInstByIpAndPort(ip, port);
        opResult = redisCenter.deployRedisSlowLogCollection(instanceInfo.getAppId(), ip, port);
    } else if (jobGroup.equals(ConstUtils.MACHINE_JOB_GROUP)) {
        MachineInfo machineInfo = machineDao.getMachineInfoByIp(ip);
        opResult = machineCenter.deployMachineCollection(machineInfo.getId(), ip);
    } else if (jobGroup.equals(ConstUtils.MACHINE_MONITOR_JOB_GROUP)) {
        MachineInfo machineInfo = machineDao.getMachineInfoByIp(ip);
        opResult = machineCenter.deployMachineMonitor(machineInfo.getId(), ip);
    } else if (jobGroup.equals(ConstUtils.SERVER_TRIGGER_GROUP)) {
        MachineInfo machineInfo = machineDao.getMachineInfoByIp(ip);
        opResult = machineCenter.deployServerCollection(machineInfo.getId(), ip);
    }
    return opResult;
}
Also used : MachineInfo(com.sohu.cache.entity.MachineInfo) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Aggregations

InstanceInfo (com.sohu.cache.entity.InstanceInfo)45 AppDesc (com.sohu.cache.entity.AppDesc)11 List (java.util.List)7 Jedis (redis.clients.jedis.Jedis)7 InstanceStats (com.sohu.cache.entity.InstanceStats)6 MachineInfo (com.sohu.cache.entity.MachineInfo)5 ArrayList (java.util.ArrayList)5 IdempotentConfirmer (com.sohu.cache.util.IdempotentConfirmer)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 InstanceCommandStats (com.sohu.cache.entity.InstanceCommandStats)2 InstanceSlotModel (com.sohu.cache.entity.InstanceSlotModel)2 AppDetailVO (com.sohu.cache.web.vo.AppDetailVO)2 BaseTest (com.sohu.test.BaseTest)2 HashSet (java.util.HashSet)2 TreeMap (java.util.TreeMap)2 Test (org.junit.Test)2 HostAndPort (redis.clients.jedis.HostAndPort)2