Search in sources :

Example 36 with InstanceInfo

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

the class AppDeployCenterImpl method checkHorizontal.

@Override
public HorizontalResult checkHorizontal(long appId, long appAuditId, long sourceId, long targetId, int startSlot, int endSlot, int migrateType) {
    // 0.当前应用正在迁移
    boolean isInProcess = isInProcess(appId);
    if (isInProcess) {
        return HorizontalResult.fail(String.format("appId=%s正在迁移!", appId));
    }
    // 1.应用信息
    AppDesc appDesc = appService.getByAppId(appId);
    if (appDesc == null) {
        return HorizontalResult.fail("应用信息为空");
    }
    // 2.0 源实例ID不能等于目标实例ID
    if (sourceId == targetId) {
        return HorizontalResult.fail(String.format("源实例ID=%s不能等于目标实例ID=%s", sourceId, targetId));
    }
    // 2.1 源实例信息
    InstanceInfo sourceInstanceInfo = instanceDao.getInstanceInfoById(sourceId);
    if (sourceInstanceInfo == null) {
        return HorizontalResult.fail(String.format("源实例id=%s为空", sourceId));
    }
    // 2.2 对比源实例的appId是否正确
    long sourceAppId = sourceInstanceInfo.getAppId();
    if (sourceAppId != appId) {
        return HorizontalResult.fail(String.format("源实例id=%s不属于appId=%s", sourceId, appId));
    }
    // 2.3 源实例是否在线
    boolean sourceIsRun = redisCenter.isRun(sourceInstanceInfo.getIp(), sourceInstanceInfo.getPort());
    if (!sourceIsRun) {
        return HorizontalResult.fail(String.format("源实例%s必须运行中", sourceInstanceInfo.getHostPort()));
    }
    // 2.4必须是master节点
    boolean sourceIsMaster = redisCenter.isMaster(sourceInstanceInfo.getIp(), sourceInstanceInfo.getPort());
    if (!sourceIsMaster) {
        return HorizontalResult.fail(String.format("源实例%s必须是主节点", sourceInstanceInfo.getHostPort()));
    }
    // 3.1 目标实例信息
    InstanceInfo targetInstanceInfo = instanceDao.getInstanceInfoById(targetId);
    if (targetInstanceInfo == null) {
        return HorizontalResult.fail(String.format("目标实例id=%s为空", targetId));
    }
    // 3.2 对比目标实例的appId是否正确
    long targetAppId = targetInstanceInfo.getAppId();
    if (targetAppId != appId) {
        return HorizontalResult.fail(String.format("目标实例id=%s不属于appId=%s", targetId, appId));
    }
    // 3.3 目标实例是否在线
    boolean targetIsRun = redisCenter.isRun(targetInstanceInfo.getIp(), targetInstanceInfo.getPort());
    if (!targetIsRun) {
        return HorizontalResult.fail(String.format("目标实例%s必须运行中", targetInstanceInfo.getHostPort()));
    }
    // 3.4 必须是master节点
    boolean targetIsMaster = redisCenter.isMaster(targetInstanceInfo.getIp(), targetInstanceInfo.getPort());
    if (!targetIsMaster) {
        return HorizontalResult.fail(String.format("目标实例%s必须是主节点", targetInstanceInfo.getHostPort()));
    }
    // 4.startSlot和endSlot是否在源实例中
    // 4.1 判断数值
    int maxSlot = 16383;
    if (startSlot < 0 || startSlot > maxSlot) {
        return HorizontalResult.fail(String.format("startSlot=%s必须在0-%s", startSlot, maxSlot));
    }
    if (endSlot < 0 || endSlot > maxSlot) {
        return HorizontalResult.fail(String.format("endSlot=%s必须在0-%s", endSlot, maxSlot));
    }
    if (startSlot > endSlot) {
        return HorizontalResult.fail("startSlot不能大于endSlot");
    }
    // 4.2 判断startSlot和endSlot属于sourceId
    // 获取所有slot分布
    Map<String, InstanceSlotModel> clusterSlotsMap = redisCenter.getClusterSlotsMap(appId);
    if (MapUtils.isEmpty(clusterSlotsMap)) {
        return HorizontalResult.fail("无法获取slot分布!");
    }
    // 获取源实例负责的slot
    String sourceHostPort = sourceInstanceInfo.getHostPort();
    InstanceSlotModel instanceSlotModel = clusterSlotsMap.get(sourceHostPort);
    if (instanceSlotModel == null || CollectionUtils.isEmpty(instanceSlotModel.getSlotList())) {
        return HorizontalResult.fail("源实例上没有slot!");
    }
    List<Integer> slotList = instanceSlotModel.getSlotList();
    for (int i = startSlot; i <= endSlot; i++) {
        if (!slotList.contains(i)) {
            return HorizontalResult.fail(String.format("源实例没有包含尽startSlot=%s到endSlot=%s", startSlot, endSlot));
        }
    }
    //5.是否支持批量,版本要大于等于3.0.6
    String sourceRedisVersion = redisCenter.getRedisVersion(sourceInstanceInfo.getIp(), sourceInstanceInfo.getPort());
    if (StringUtils.isBlank(sourceRedisVersion)) {
        return HorizontalResult.fail(String.format("源实例%s版本为空", sourceInstanceInfo.getHostPort()));
    }
    String targetRedisVersion = redisCenter.getRedisVersion(targetInstanceInfo.getIp(), targetInstanceInfo.getPort());
    if (StringUtils.isBlank(targetRedisVersion)) {
        return HorizontalResult.fail(String.format("目标实例%s版本为空", targetInstanceInfo.getHostPort()));
    }
    RedisVersion sourceRedisVersionModel = getRedisVersion(sourceRedisVersion);
    //选择了批量,但是当前版本不支持pipeline
    if (migrateType == 1 && !sourceRedisVersionModel.isSupportPipelineMigrate()) {
        return HorizontalResult.fail(String.format("源实例%s版本为%s,不支持pipeline migrate!", sourceInstanceInfo.getHostPort(), sourceRedisVersion));
    }
    RedisVersion targetRedisVersionModel = getRedisVersion(targetRedisVersion);
    //选择了批量,但是当前版本不支持pipeline
    if (migrateType == 1 && !targetRedisVersionModel.isSupportPipelineMigrate()) {
        return HorizontalResult.fail(String.format("目标实例%s版本为%s,不支持pipeline migrate!", targetInstanceInfo.getHostPort(), targetRedisVersion));
    }
    return HorizontalResult.checkSuccess();
}
Also used : AppDesc(com.sohu.cache.entity.AppDesc) InstanceInfo(com.sohu.cache.entity.InstanceInfo) InstanceSlotModel(com.sohu.cache.entity.InstanceSlotModel)

Example 37 with InstanceInfo

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

the class InstanceDeployCenterImpl method startExistInstance.

@Override
public boolean startExistInstance(int instanceId) {
    Assert.isTrue(instanceId > 0L);
    InstanceInfo instanceInfo = instanceDao.getInstanceInfoById(instanceId);
    Assert.isTrue(instanceInfo != null);
    int type = instanceInfo.getType();
    String host = instanceInfo.getIp();
    int port = instanceInfo.getPort();
    boolean isRun;
    if (TypeUtil.isRedisType(type)) {
        isRun = redisCenter.isRun(host, port);
        if (isRun) {
            logger.warn("{}:{} instance is Running", host, port);
        } else {
            String runShell;
            if (TypeUtil.isRedisCluster(type)) {
                runShell = RedisProtocol.getRunShell(port, true);
            } else if (TypeUtil.isRedisSentinel(type)) {
                runShell = RedisProtocol.getSentinelShell(port);
            } else {
                runShell = RedisProtocol.getRunShell(port, false);
            }
            boolean isRunShell = machineCenter.startProcessAtPort(host, port, runShell);
            if (!isRunShell) {
                logger.error("startProcessAtPort-> {}:{} shell= {} failed", host, port, runShell);
                return false;
            } else {
                logger.warn("{}:{} instance has Run", host, port);
            }
            isRun = redisCenter.isRun(host, port);
        }
    } else {
        logger.error("type={} not match!", type);
        isRun = false;
    }
    if (isRun) {
        instanceInfo.setStatus(InstanceStatusEnum.GOOD_STATUS.getStatus());
        instanceDao.update(instanceInfo);
        if (TypeUtil.isRedisType(type)) {
            redisCenter.deployRedisCollection(instanceInfo.getAppId(), instanceInfo.getIp(), instanceInfo.getPort());
        }
    }
    return isRun;
}
Also used : InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 38 with InstanceInfo

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

the class InstanceStatsCenterImpl method getCommandStatsList.

@Override
public List<InstanceCommandStats> getCommandStatsList(Long instanceId, long beginTime, long endTime, String commandName) {
    if (instanceId == null) {
        return Collections.emptyList();
    }
    InstanceInfo instanceInfo = instanceDao.getInstanceInfoById(instanceId);
    List<InstanceCommandStats> resultList = new ArrayList<InstanceCommandStats>();
    String ip = instanceInfo.getIp();
    int port = instanceInfo.getPort();
    int type = instanceInfo.getType();
    List<Map<String, Object>> objectList = this.queryDiffMapList(beginTime, endTime, ip, port, ConstUtils.REDIS);
    ;
    if (objectList != null) {
        for (Map<String, Object> map : objectList) {
            InstanceCommandStats stats = parseCommand(instanceId, commandName, map, true, type);
            if (stats != null) {
                resultList.add(stats);
            }
        }
    }
    return resultList;
}
Also used : InstanceCommandStats(com.sohu.cache.entity.InstanceCommandStats) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 39 with InstanceInfo

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

the class RedisInitLoad method initByType.

private void initByType(int type) {
    List<InstanceInfo> instanceInfoList = instanceDao.getInstListByType(type);
    for (InstanceInfo instanceInfo : instanceInfoList) {
        if (TypeUtil.isRedisSentinel(instanceInfo.getType())) {
            continue;
        }
        String host = instanceInfo.getIp();
        int port = instanceInfo.getPort();
        Long appId = instanceInfo.getAppId();
        redisCenter.deployRedisCollection(appId, host, port);
        redisCenter.deployRedisSlowLogCollection(appId, host, port);
    }
    logger.info("init redis type={} deploy instance done.", type);
}
Also used : InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 40 with InstanceInfo

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

the class AppClientConnInspector method inspect.

@Override
public boolean inspect(Map<InspectParamEnum, Object> paramMap) {
    Long appId = MapUtils.getLong(paramMap, InspectParamEnum.SPLIT_KEY);
    AppDetailVO appDetailVO = appStatsCenter.getAppDetail(appId);
    if (appDetailVO == null) {
        logger.warn("appId {} appDetailVO is empty", appId);
        return true;
    }
    List<InstanceInfo> appInstanceInfoList = (List<InstanceInfo>) paramMap.get(InspectParamEnum.INSTANCE_LIST);
    if (CollectionUtils.isEmpty(appInstanceInfoList)) {
        logger.warn("appId {} instanceList is empty", appId);
        return true;
    }
    // 报警阀值
    int appClientConnThreshold = getClientConnThreshold(appDetailVO.getAppDesc());
    int appClientConnNum = appDetailVO.getConn();
    // 阀值乘以分片个数
    int instanceCount = appInstanceInfoList.size();
    if (appClientConnNum > appClientConnThreshold * instanceCount) {
        alertAppClientConn(appDetailVO, appClientConnThreshold, instanceCount);
    } else {
        for (InstanceInfo instanceInfo : appInstanceInfoList) {
            if (instanceInfo == null) {
                continue;
            }
            if (instanceInfo.isOffline()) {
                continue;
            }
            if (!TypeUtil.isRedisType(instanceInfo.getType())) {
                continue;
            }
            // 忽略sentinel观察者
            if (TypeUtil.isRedisSentinel(instanceInfo.getType())) {
                continue;
            }
            long instanceId = instanceInfo.getId();
            InstanceStats instanceStats = instanceStatsCenter.getInstanceStats(instanceId);
            if (instanceStats == null) {
                continue;
            }
            double instanceClientConnNum = instanceStats.getCurrConnections();
            // 大于标准值
            if (instanceClientConnNum > appClientConnThreshold) {
                alertInstanceClientConn(instanceStats, appDetailVO, appClientConnThreshold);
            }
        }
    }
    return true;
}
Also used : InstanceStats(com.sohu.cache.entity.InstanceStats) AppDetailVO(com.sohu.cache.web.vo.AppDetailVO) List(java.util.List) 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