Search in sources :

Example 21 with InstanceInfo

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

the class AppAndInstanceAuthorityInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // 1. 获取用户
    long userId = userLoginStatusService.getUserIdFromLoginStatus(request);
    AppUser user = userService.get(userId);
    // 2. 管理员直接跳过
    if (AppUserTypeEnum.ADMIN_USER.value().equals(user.getType())) {
        return true;
    }
    // 3. 应用id
    String appId = request.getParameter("appId");
    if (StringUtils.isNotBlank(appId)) {
        checkUserAppPower(response, request.getSession(true), user, NumberUtils.toLong(appId));
    }
    // 4. 实例权限检测(其实也是应用)
    String instanceId = request.getParameter("instanceId");
    if (StringUtils.isNotBlank(instanceId)) {
        InstanceInfo instanceInfo = instanceStatsCenter.getInstanceInfo(Long.parseLong(instanceId));
        checkUserAppPower(response, request.getSession(true), user, instanceInfo.getAppId());
    }
    return true;
}
Also used : AppUser(com.sohu.cache.entity.AppUser) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 22 with InstanceInfo

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

the class RedisDeployCenterImpl method sentinelFailover.

@Override
public boolean sentinelFailover(long appId) throws Exception {
    Assert.isTrue(appId > 0);
    AppDesc appDesc = appDao.getAppDescById(appId);
    Assert.isTrue(appDesc != null);
    int type = appDesc.getType();
    if (!TypeUtil.isRedisSentinel(type)) {
        logger.warn("app={} is not sentinel", appDesc);
        return false;
    }
    final List<InstanceInfo> instanceList = instanceDao.getInstListByAppId(appId);
    if (instanceList == null || instanceList.isEmpty()) {
        logger.warn("app={} instances is empty");
        return false;
    }
    for (InstanceInfo instanceInfo : instanceList) {
        int instanceType = instanceInfo.getType();
        if (TypeUtil.isRedisSentinel(instanceType)) {
            final String host = instanceInfo.getIp();
            final int port = instanceInfo.getPort();
            final String masterName = instanceInfo.getCmd();
            if (StringUtils.isBlank(masterName)) {
                logger.warn("{} cmd is null", instanceInfo);
                continue;
            }
            boolean isRun = redisCenter.isRun(host, port);
            if (!isRun) {
                logger.warn("{} is not run");
                continue;
            }
            boolean isSentinelFailOver = new IdempotentConfirmer() {

                @Override
                public boolean execute() {
                    Jedis jedis = new Jedis(host, port, Protocol.DEFAULT_TIMEOUT);
                    try {
                        String response = jedis.sentinelFailover(masterName);
                        return response != null && response.equalsIgnoreCase("OK");
                    } finally {
                        jedis.close();
                    }
                }
            }.run();
            if (!isSentinelFailOver) {
                logger.warn("{}:{} sentienl isSentinelFailOver error", host, port);
                return false;
            } else {
                logger.warn("SentinelFailOver done! ");
                break;
            }
        }
    }
    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 23 with InstanceInfo

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

the class RedisDeployCenterImpl method clusterFailover.

@Override
public boolean clusterFailover(long appId, int slaveInstanceId) throws Exception {
    Assert.isTrue(appId > 0);
    Assert.isTrue(slaveInstanceId > 0);
    AppDesc appDesc = appDao.getAppDescById(appId);
    Assert.isTrue(appDesc != null);
    int type = appDesc.getType();
    if (!TypeUtil.isRedisCluster(type)) {
        logger.error("{} is not redis type", appDesc);
        return false;
    }
    InstanceInfo instanceInfo = instanceDao.getInstanceInfoById(slaveInstanceId);
    Assert.isTrue(instanceInfo != null);
    String slaveHost = instanceInfo.getIp();
    int slavePort = instanceInfo.getPort();
    final Jedis slaveJedis = new Jedis(slaveHost, slavePort);
    boolean isClusterFailOver = new IdempotentConfirmer() {

        @Override
        public boolean execute() {
            String response = slaveJedis.clusterFailoverForce();
            return response != null && response.equalsIgnoreCase("OK");
        }
    }.run();
    if (!isClusterFailOver) {
        logger.error("{}:{} clusterFailover failed", slaveHost, slavePort);
        return false;
    } else {
        logger.warn("{}:{} clusterFailover Done! ", 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 24 with InstanceInfo

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

the class AppDataMigrateController method appInstanceList.

/**
     * 通过应用id获取可用的Redis实例信息
     * @return
     */
@RequestMapping(value = "/appInstanceList")
public ModelAndView appInstanceList(HttpServletRequest request, HttpServletResponse response, Model model) {
    String appIdStr = request.getParameter("appId");
    long appId = NumberUtils.toLong(appIdStr);
    AppDesc appDesc = appService.getByAppId(appId);
    StringBuffer instances = new StringBuffer();
    List<InstanceInfo> instanceList = appService.getAppInstanceInfo(appId);
    if (CollectionUtils.isNotEmpty(instanceList)) {
        for (int i = 0; i < instanceList.size(); i++) {
            InstanceInfo instanceInfo = instanceList.get(i);
            if (instanceInfo == null) {
                continue;
            }
            if (instanceInfo.isOffline()) {
                continue;
            }
            // 如果是sentinel类型的应用只出master
            if (TypeUtil.isRedisSentinel(appDesc.getType())) {
                if (TypeUtil.isRedisSentinel(instanceInfo.getType())) {
                    continue;
                }
                if (!redisCenter.isMaster(instanceInfo.getIp(), instanceInfo.getPort())) {
                    continue;
                }
            }
            instances.append(instanceInfo.getIp() + ":" + instanceInfo.getPort());
            if (i != instanceList.size() - 1) {
                instances.append(ConstUtils.NEXT_LINE);
            }
        }
    }
    model.addAttribute("instances", instances.toString());
    model.addAttribute("appType", appDesc == null ? -1 : appDesc.getType());
    return new ModelAndView("");
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) AppDesc(com.sohu.cache.entity.AppDesc) InstanceInfo(com.sohu.cache.entity.InstanceInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with InstanceInfo

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

the class BaseController method fillAppMachineInstanceTopology.

/**
     * 应用机器实例分布图
     * @param appId
     * @param model
     */
protected void fillAppMachineInstanceTopology(Long appId, Model model) {
    List<InstanceInfo> instanceList = appService.getAppInstanceInfo(appId);
    int groupId = 1;
    // 1.分组,同一个主从在一组
    for (int i = 0; i < instanceList.size(); i++) {
        InstanceInfo instance = instanceList.get(i);
        // 有了groupId,不再设置
        if (instance.getGroupId() > 0) {
            continue;
        }
        if (instance.isOffline()) {
            continue;
        }
        for (int j = i + 1; j < instanceList.size(); j++) {
            InstanceInfo instanceCompare = instanceList.get(j);
            if (instanceCompare.isOffline()) {
                continue;
            }
            // 寻找主从对应关系
            if (instanceCompare.getMasterInstanceId() == instance.getId() || instance.getMasterInstanceId() == instanceCompare.getId()) {
                instanceCompare.setGroupId(groupId);
            }
        }
        instance.setGroupId(groupId++);
    }
    // 2.机器下的实例列表
    Map<String, List<InstanceInfo>> machineInstanceMap = new HashMap<String, List<InstanceInfo>>();
    for (InstanceInfo instance : instanceList) {
        String ip = instance.getIp();
        if (machineInstanceMap.containsKey(ip)) {
            machineInstanceMap.get(ip).add(instance);
        } else {
            List<InstanceInfo> tempInstanceList = new ArrayList<InstanceInfo>();
            tempInstanceList.add(instance);
            machineInstanceMap.put(ip, tempInstanceList);
        }
    }
    model.addAttribute("machineInstanceMap", machineInstanceMap);
    model.addAttribute("instancePairCount", groupId - 1);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) 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