Search in sources :

Example 6 with IdempotentConfirmer

use of com.sohu.cache.util.IdempotentConfirmer 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 7 with IdempotentConfirmer

use of com.sohu.cache.util.IdempotentConfirmer in project cachecloud by sohutv.

the class RedisIsolationPersistenceInspector method invokeBgRewriteAof.

public boolean invokeBgRewriteAof(final Jedis jedis) {
    return new IdempotentConfirmer() {

        @Override
        public boolean execute() {
            try {
                String response = jedis.bgrewriteaof();
                if (response != null && response.contains("rewriting started")) {
                    return true;
                }
            } catch (Exception e) {
                String message = e.getMessage();
                if (message.contains("rewriting already")) {
                    return true;
                }
                logger.error(message, e);
            }
            return false;
        }
    }.run();
}
Also used : IdempotentConfirmer(com.sohu.cache.util.IdempotentConfirmer)

Example 8 with IdempotentConfirmer

use of com.sohu.cache.util.IdempotentConfirmer in project cachecloud by sohutv.

the class RedisDeployCenterImpl method slaveOf.

private boolean slaveOf(final String masterHost, final int masterPort, final String slaveHost, final int slavePort) {
    final Jedis slave = new Jedis(slaveHost, slavePort, Protocol.DEFAULT_TIMEOUT * 3);
    try {
        boolean isSlave = new IdempotentConfirmer() {

            @Override
            public boolean execute() {
                String result = slave.slaveof(masterHost, masterPort);
                return result != null && result.equalsIgnoreCase("OK");
            }
        }.run();
        if (!isSlave) {
            logger.error(String.format("modifyAppConfig:ip=%s,port=%s failed", slaveHost, slavePort));
            return false;
        }
        redisCenter.configRewrite(slaveHost, slavePort);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        if (slave != null)
            slave.close();
    }
    return true;
}
Also used : Jedis(redis.clients.jedis.Jedis) IdempotentConfirmer(com.sohu.cache.util.IdempotentConfirmer)

Example 9 with IdempotentConfirmer

use of com.sohu.cache.util.IdempotentConfirmer in project cachecloud by sohutv.

the class RedisDeployCenterImpl method modifyInstanceConfig.

@Override
public boolean modifyInstanceConfig(final String host, final int port, final String parameter, final String value) {
    final Jedis jedis = new Jedis(host, port, 5000);
    try {
        boolean isConfig = new IdempotentConfirmer() {

            @Override
            public boolean execute() {
                boolean isRun = redisCenter.isRun(host, port);
                if (!isRun) {
                    logger.warn("modifyInstanceConfig{}:{} is shutdown", host, port);
                    return true;
                }
                String result = jedis.configSet(parameter, value);
                boolean isConfig = result != null && result.equalsIgnoreCase("OK");
                if (!isConfig) {
                    logger.error(String.format("modifyConfigError:ip=%s,port=%s,result=%s", host, port, result));
                    return false;
                }
                return isConfig;
            }
        }.run();
        boolean isRewrite = redisCenter.configRewrite(host, port);
        if (!isRewrite) {
            logger.error("configRewrite={}:{} failed", host, port);
        }
        return isConfig;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        if (jedis != null)
            jedis.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) IdempotentConfirmer(com.sohu.cache.util.IdempotentConfirmer)

Example 10 with IdempotentConfirmer

use of com.sohu.cache.util.IdempotentConfirmer 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)

Aggregations

IdempotentConfirmer (com.sohu.cache.util.IdempotentConfirmer)13 Jedis (redis.clients.jedis.Jedis)7 JedisException (redis.clients.jedis.exceptions.JedisException)4 AppDesc (com.sohu.cache.entity.AppDesc)3 InstanceInfo (com.sohu.cache.entity.InstanceInfo)3 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1