Search in sources :

Example 1 with MasterInfo

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

the class KeeperMultiDcChangePrimary method testChangePrimary.

@Test
public void testChangePrimary() throws Exception {
    String primaryDc = getPrimaryDc();
    String backupDc = getBackupDc();
    // change backup to primary
    when(dcMetaCache.getShardRedises(getClusterId(), getShardId())).thenReturn(getDcRedises(backupDc, getClusterId(), getShardId()));
    when(currentMetaManager.getSurviveKeepers(getClusterId(), getShardId())).thenReturn(getDcKeepers(backupDc, getClusterId(), getShardId()));
    logger.info(remarkableMessage("[make dc primary]change dc primary to:" + backupDc));
    BecomePrimaryAction becomePrimaryAction = new BecomePrimaryAction(dcMetaCache, currentMetaManager, sentinelManager, offsetWaiter, new ExecutionLog(currentTestName()), getXpipeNettyClientKeyedObjectPool(), newMasterChooser, scheduled, executors);
    PrimaryDcChangeMessage message = becomePrimaryAction.changePrimaryDc(getClusterId(), getShardId(), backupDc, new MasterInfo());
    logger.info("{}", message);
    sleep(2000);
    logger.info(remarkableMessage("[make dc backup]change dc primary to:" + backupDc));
    when(dcMetaCache.getPrimaryDc(getClusterId(), getShardId())).thenReturn(backupDc);
    when(multiDcService.getActiveKeeper(backupDc, getClusterId(), getShardId())).thenReturn(getDcKeepers(backupDc, getClusterId(), getShardId()).get(0));
    when(dcMetaCache.getShardRedises(getClusterId(), getShardId())).thenReturn(getDcRedises(primaryDc, getClusterId(), getShardId()));
    when(currentMetaManager.getKeeperActive(getClusterId(), getShardId())).thenReturn(getKeeperActive(primaryDc));
    when(currentMetaManager.getSurviveKeepers(getClusterId(), getShardId())).thenReturn(getDcKeepers(primaryDc, getClusterId(), getShardId()));
    BecomeBackupAction becomeBackupAction = new BecomeBackupAction(dcMetaCache, currentMetaManager, sentinelManager, new ExecutionLog(currentTestName()), getXpipeNettyClientKeyedObjectPool(), multiDcService, scheduled, executors);
    message = becomeBackupAction.changePrimaryDc(getClusterId(), getShardId(), backupDc, new MasterInfo());
    logger.info("{}", message);
    sleep(2000);
    RedisMeta newRedisMaster = newMasterChooser.getLastChoosenMaster();
    List<RedisMeta> allRedises = getRedises(primaryDc);
    allRedises.addAll(getRedises(backupDc));
    allRedises.remove(newRedisMaster);
    logger.info("{}\n{}", newRedisMaster, allRedises);
    sendMesssageToMasterAndTest(newRedisMaster, allRedises);
}
Also used : MasterInfo(com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo) ExecutionLog(com.ctrip.xpipe.redis.meta.server.dcchange.ExecutionLog) BecomePrimaryAction(com.ctrip.xpipe.redis.meta.server.dcchange.impl.BecomePrimaryAction) BecomeBackupAction(com.ctrip.xpipe.redis.meta.server.dcchange.impl.BecomeBackupAction) RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) PrimaryDcChangeMessage(com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService.PrimaryDcChangeMessage) Test(org.junit.Test)

Example 2 with MasterInfo

use of com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo 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 3 with MasterInfo

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

the class DefaultPrimaryDcPrepareToChange method prepare.

@Override
public MetaServerConsoleService.PreviousPrimaryDcMessage prepare(String clusterId, String shardId) {
    logger.info("[prepare]{}, {}", clusterId, shardId);
    MetaServerConsoleService.PreviousPrimaryDcMessage message = new MetaServerConsoleService.PreviousPrimaryDcMessage();
    ExecutionLog executionLog = new ExecutionLog(String.format("meta server:%s", currentClusterServer.getClusterInfo()));
    Pair<String, Integer> keeperMaster = currentMetaManager.getKeeperMaster(clusterId, shardId);
    message.setMasterAddr(new HostPort(keeperMaster.getKey(), keeperMaster.getValue()));
    executionLog.info("[prepare]" + keeperMaster);
    RedisInfo redisInfo = getInfoReplication(keeperMaster, executionLog);
    MasterInfo masterInfo = convert(redisInfo, executionLog);
    message.setMasterInfo(masterInfo);
    logger.info("[prepare]{}, {}, {}", keeperMaster, redisInfo, masterInfo);
    makeMasterReadOnly(clusterId, shardId, keeperMaster, true, executionLog);
    removeSentinel(clusterId, shardId, executionLog);
    message.setMessage(executionLog.getLog());
    return message;
}
Also used : MasterInfo(com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo) ExecutionLog(com.ctrip.xpipe.redis.meta.server.dcchange.ExecutionLog) MetaServerConsoleService(com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService) HostPort(com.ctrip.xpipe.endpoint.HostPort) RedisInfo(com.ctrip.xpipe.redis.core.protocal.pojo.RedisInfo)

Example 4 with MasterInfo

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

the class DefaultMetaServer method doChangePrimaryDc.

@Override
public PrimaryDcChangeMessage doChangePrimaryDc(String clusterId, String shardId, String newPrimaryDc, MetaServerConsoleService.PrimaryDcChangeRequest request, ForwardInfo forwardInfo) {
    logger.info("[doChangePrimaryDc]{}, {}, {}, {}", clusterId, shardId, newPrimaryDc, request);
    dcMetaCache.primaryDcChanged(clusterId, shardId, newPrimaryDc);
    MasterInfo masterInfo = null;
    if (request != null) {
        masterInfo = request.getMasterInfo();
    }
    return changePrimaryDcAction.changePrimaryDc(clusterId, shardId, newPrimaryDc, masterInfo);
}
Also used : MasterInfo(com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo)

Example 5 with MasterInfo

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

the class ClusterServersApiTest method testDoChangePrimaryDc.

@Test
public void testDoChangePrimaryDc() throws Exception {
    createMetaServers(metaServerCount);
    sleep(waitForMetaServerOkTime);
    logger.info(remarkableMessage("[testDoChangePrimaryDc][begin send change primary dc message]"));
    for (TestMetaServer server : getServers()) {
        logger.info(remarkableMessage("[testDoChangePrimaryDc][jq]"));
        MetaServerConsoleService.PrimaryDcChangeRequest request = new MetaServerConsoleService.PrimaryDcChangeRequest();
        request.setMasterInfo(new MasterInfo(RunidGenerator.DEFAULT.generateRunid(), 100L));
        MetaServerConsoleService consoleService = new DefaultMetaServerConsoleService(server.getAddress());
        PrimaryDcChangeMessage message = consoleService.doChangePrimaryDc(getClusterId(), getShardId(), "jq", request);
        logger.info("{}", message);
        logger.info(remarkableMessage("[testDoChangePrimaryDc][oy]"));
        message = consoleService.doChangePrimaryDc(getClusterId(), getShardId(), "oy", request);
        logger.info("{}", message);
    }
}
Also used : MasterInfo(com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo) TestMetaServer(com.ctrip.xpipe.redis.meta.server.TestMetaServer) MetaServerConsoleService(com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService) DefaultMetaServerConsoleService(com.ctrip.xpipe.redis.core.metaserver.impl.DefaultMetaServerConsoleService) DefaultMetaServerConsoleService(com.ctrip.xpipe.redis.core.metaserver.impl.DefaultMetaServerConsoleService) PrimaryDcChangeMessage(com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService.PrimaryDcChangeMessage) Test(org.junit.Test)

Aggregations

MasterInfo (com.ctrip.xpipe.redis.core.protocal.pojo.MasterInfo)12 Test (org.junit.Test)9 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)7 HostPort (com.ctrip.xpipe.endpoint.HostPort)6 PrimaryDcChangeMessage (com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService.PrimaryDcChangeMessage)4 ExecutionLog (com.ctrip.xpipe.redis.meta.server.dcchange.ExecutionLog)4 MetaServerConsoleService (com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService)2 RedisInfo (com.ctrip.xpipe.redis.core.protocal.pojo.RedisInfo)2 BecomeBackupAction (com.ctrip.xpipe.redis.meta.server.dcchange.impl.BecomeBackupAction)2 BecomePrimaryAction (com.ctrip.xpipe.redis.meta.server.dcchange.impl.BecomePrimaryAction)2 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)1 DefaultMetaServerConsoleService (com.ctrip.xpipe.redis.core.metaserver.impl.DefaultMetaServerConsoleService)1 InfoReplicationComplementCommand (com.ctrip.xpipe.redis.core.protocal.cmd.InfoReplicationComplementCommand)1 SlaveInfo (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveInfo)1 TestMetaServer (com.ctrip.xpipe.redis.meta.server.TestMetaServer)1 InetSocketAddress (java.net.InetSocketAddress)1