Search in sources :

Example 1 with InfoReplicationComplementCommand

use of com.ctrip.xpipe.redis.core.protocal.cmd.InfoReplicationComplementCommand in project x-pipe by ctripcorp.

the class DefaultOffsetwaiter method doWait.

private boolean doWait(String masterReplId, Long masterOffset, HostPort hostPort, ExecutionLog executionLog) {
    int waitMilli = metaServerConfig.getWaitforOffsetMilli();
    long endTime = System.currentTimeMillis() + waitMilli;
    String slaveReplId = null;
    Long slaveOffset = null;
    executionLog.info(String.format("wait timeout config:%d ms", waitMilli));
    while (true) {
        InfoReplicationComplementCommand command = new InfoReplicationComplementCommand(keyedObjectPool.getKeyPool(new InetSocketAddress(hostPort.getHost(), hostPort.getPort())), scheduled);
        try {
            RedisInfo redisInfo = command.execute().get(waitMilli, TimeUnit.MILLISECONDS);
            if ((redisInfo instanceof MasterInfo) || !(redisInfo instanceof SlaveInfo)) {
                executionLog.info("target role:" + (redisInfo == null ? "null" : redisInfo.getClass().getSimpleName()));
                break;
            }
            SlaveInfo slaveInfo = (SlaveInfo) redisInfo;
            slaveReplId = slaveInfo.getMasterReplId();
            slaveOffset = slaveInfo.getSlaveReplOffset();
            if (!StringUtil.isEmpty(slaveReplId) && !StringUtil.isEmpty(masterReplId)) {
                if (!slaveReplId.equalsIgnoreCase(masterReplId)) {
                    executionLog.info(String.format("master replid not equal with slave replid, break. %s %s", masterReplId, slaveReplId));
                    break;
                }
            }
            if (slaveOffset >= masterOffset) {
                executionLog.info(String.format("wait succeed:%d >= %d", slaveOffset, masterOffset));
                return true;
            }
        } catch (Exception e) {
            executionLog.error(e.getMessage());
            logger.error("[waitfor]" + hostPort, e);
        }
        long current = System.currentTimeMillis();
        if (current >= endTime) {
            executionLog.error("wait time out, exit");
            break;
        }
        sleep(1);
    }
    if (slaveOffset != null) {
        executionLog.info(String.format("master offset:%s, slave offset:%d, sub:%d", masterOffset, slaveOffset, masterOffset - slaveOffset));
    }
    return false;
}
Also used : MasterInfo(com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo) InetSocketAddress(java.net.InetSocketAddress) RedisInfo(com.ctrip.xpipe.redis.core.protocal.pojo.RedisInfo) SlaveInfo(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveInfo) InfoReplicationComplementCommand(com.ctrip.xpipe.redis.core.protocal.cmd.InfoReplicationComplementCommand)

Example 2 with InfoReplicationComplementCommand

use of com.ctrip.xpipe.redis.core.protocal.cmd.InfoReplicationComplementCommand in project x-pipe by ctripcorp.

the class DefaultPrimaryDcPrepareToChange method getInfoReplication.

public RedisInfo getInfoReplication(Pair<String, Integer> redisMaster, ExecutionLog executionLog) {
    InfoReplicationComplementCommand command = new InfoReplicationComplementCommand(keyedObjectPool.getKeyPool(new InetSocketAddress(redisMaster.getKey(), redisMaster.getValue())), scheduled);
    try {
        executionLog.info("[getInfoReplication]" + redisMaster);
        RedisInfo redisInfo = command.execute().get(waitForMasterInfoMilli, TimeUnit.MILLISECONDS);
        executionLog.info("[getInfoReplication]" + redisInfo);
        return redisInfo;
    } catch (InterruptedException e) {
        logger.error("[getInfoReplication]" + redisMaster, e);
        executionLog.error(e.getMessage());
    } catch (ExecutionException e) {
        logger.error("[getInfoReplication]" + redisMaster, e);
        executionLog.error(e.getMessage());
    } catch (TimeoutException e) {
        logger.error("[getInfoReplication]" + redisMaster, e);
        executionLog.error(e.getMessage());
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) RedisInfo(com.ctrip.xpipe.redis.core.protocal.pojo.RedisInfo) ExecutionException(java.util.concurrent.ExecutionException) InfoReplicationComplementCommand(com.ctrip.xpipe.redis.core.protocal.cmd.InfoReplicationComplementCommand) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

InfoReplicationComplementCommand (com.ctrip.xpipe.redis.core.protocal.cmd.InfoReplicationComplementCommand)2 RedisInfo (com.ctrip.xpipe.redis.core.protocal.pojo.RedisInfo)2 InetSocketAddress (java.net.InetSocketAddress)2 MasterInfo (com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo)1 SlaveInfo (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveInfo)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1