Search in sources :

Example 26 with RedisMeta

use of com.ctrip.xpipe.redis.core.entity.RedisMeta in project x-pipe by ctripcorp.

the class PrimaryDcKeeperMasterChooserAlgorithmTest method testMultiMaster.

@Test
public void testMultiMaster() throws Exception {
    SlaveRole role = new SlaveRole(SERVER_ROLE.MASTER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0L);
    for (RedisMeta redisMeta : redises) {
        startServer(redisMeta.getPort(), ByteBufUtils.readToString(role.format()));
    }
    when(currentMetaManager.getKeeperMaster(clusterId, shardId)).thenReturn(null);
    Assert.assertEquals(new Pair<String, Integer>(redises.get(0).getIp(), redises.get(0).getPort()), primaryAlgorithm.choose());
    for (RedisMeta redisMeta : redises) {
        Pair<String, Integer> currentMaster = new Pair<String, Integer>(redisMeta.getIp(), redisMeta.getPort());
        when(currentMetaManager.getKeeperMaster(clusterId, shardId)).thenReturn(currentMaster);
        Assert.assertEquals(currentMaster, primaryAlgorithm.choose());
    }
}
Also used : SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) Pair(com.ctrip.xpipe.tuple.Pair) Test(org.junit.Test)

Example 27 with RedisMeta

use of com.ctrip.xpipe.redis.core.entity.RedisMeta in project x-pipe by ctripcorp.

the class RedisMetaServiceImpl method getRedisMeta.

@Override
public RedisMeta getRedisMeta(ShardMeta shardMeta, RedisTbl redisInfo) {
    RedisMeta redisMeta = new RedisMeta();
    if (null != redisInfo) {
        redisMeta.setId(redisInfo.getRunId());
        redisMeta.setIp(redisInfo.getRedisIp());
        redisMeta.setPort(redisInfo.getRedisPort());
        if (redisInfo.isMaster()) {
            redisMeta.setMaster("");
        } else {
            redisMeta.setMaster(XPipeConsoleConstant.DEFAULT_ADDRESS);
        }
    }
    redisMeta.setParent(shardMeta);
    return redisMeta;
}
Also used : RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta)

Example 28 with RedisMeta

use of com.ctrip.xpipe.redis.core.entity.RedisMeta in project x-pipe by ctripcorp.

the class BecomeBackupAction method doChangePrimaryDc.

@Override
protected PrimaryDcChangeMessage doChangePrimaryDc(String clusterId, String shardId, String newPrimaryDc, MasterInfo masterInfo) {
    doChangeMetaCache(clusterId, shardId, newPrimaryDc);
    changeSentinel(clusterId, shardId, null);
    Pair<String, Integer> newMaster = chooseNewMaster(clusterId, shardId);
    if (newMaster == null) {
        executionLog.error("[doChangePrimaryDc][new master null]");
        return new PrimaryDcChangeMessage(PRIMARY_DC_CHANGE_RESULT.FAIL, executionLog.getLog());
    }
    executionLog.info(String.format("[chooseNewMaster]%s:%d", newMaster.getKey(), newMaster.getValue()));
    makeKeepersOk(clusterId, shardId, newMaster);
    List<RedisMeta> slaves = getAllSlaves(newMaster, dcMetaCache.getShardRedises(clusterId, shardId));
    KeeperMeta activeKeeper = currentMetaManager.getKeeperActive(clusterId, shardId);
    makeRedisesOk(new Pair<>(activeKeeper.getIp(), activeKeeper.getPort()), slaves);
    return new PrimaryDcChangeMessage(PRIMARY_DC_CHANGE_RESULT.SUCCESS, executionLog.getLog());
}
Also used : RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) PrimaryDcChangeMessage(com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService.PrimaryDcChangeMessage) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta)

Example 29 with RedisMeta

use of com.ctrip.xpipe.redis.core.entity.RedisMeta in project x-pipe by ctripcorp.

the class AbstractNewMasterChooser method getMasters.

protected Pair<List<RedisMeta>, List<RedisMeta>> getMasters(List<RedisMeta> allRedises) {
    List<RedisMeta> masters = new LinkedList<>();
    List<RedisMeta> tmpAliveServers = new LinkedList<>();
    CountDownLatch latch = new CountDownLatch(allRedises.size());
    for (RedisMeta redisMeta : allRedises) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            protected void doRun() throws Exception {
                try {
                    SERVER_ROLE role = serverRole(redisMeta);
                    if (role == SERVER_ROLE.MASTER) {
                        synchronized (masters) {
                            masters.add(redisMeta);
                        }
                    }
                    if (role != SERVER_ROLE.UNKNOWN) {
                        synchronized (tmpAliveServers) {
                            tmpAliveServers.add(redisMeta);
                        }
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    try {
        latch.await(CHECK_NEW_MASTER_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        logger.error("[getMasters]" + allRedises, e);
    }
    List<RedisMeta> aliveServers = sortAccording(allRedises, tmpAliveServers);
    return new Pair<>(masters, aliveServers);
}
Also used : RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) SERVER_ROLE(com.ctrip.xpipe.api.server.Server.SERVER_ROLE) LinkedList(java.util.LinkedList) ChooseNewMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.ChooseNewMasterFailException) Pair(com.ctrip.xpipe.tuple.Pair)

Example 30 with RedisMeta

use of com.ctrip.xpipe.redis.core.entity.RedisMeta in project x-pipe by ctripcorp.

the class BecomePrimaryAction method doChangePrimaryDc.

protected PrimaryDcChangeMessage doChangePrimaryDc(String clusterId, String shardId, String newPrimaryDc, MasterInfo masterInfo) {
    doChangeMetaCache(clusterId, shardId, newPrimaryDc);
    executionLog.info(String.format("[chooseNewMaster][begin]"));
    Pair<String, Integer> newMaster = chooseNewMaster(clusterId, shardId);
    executionLog.info(String.format("[chooseNewMaster]%s:%d", newMaster.getKey(), newMaster.getValue()));
    // wait for slave to achieve master offset
    offsetWaiter.tryWaitfor(new HostPort(newMaster.getKey(), newMaster.getValue()), masterInfo, executionLog);
    List<RedisMeta> slaves = getAllSlaves(newMaster, dcMetaCache.getShardRedises(clusterId, shardId));
    makeRedisesOk(newMaster, slaves);
    makeKeepersOk(clusterId, shardId, newMaster);
    changeSentinel(clusterId, shardId, newMaster);
    return new PrimaryDcChangeMessage(executionLog.getLog(), newMaster.getKey(), newMaster.getValue());
}
Also used : HostPort(com.ctrip.xpipe.endpoint.HostPort) RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) PrimaryDcChangeMessage(com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService.PrimaryDcChangeMessage)

Aggregations

RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)55 Test (org.junit.Test)26 LinkedList (java.util.LinkedList)14 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)9 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)8 Before (org.junit.Before)7 SimpleErrorMessage (com.ctrip.xpipe.exception.SimpleErrorMessage)5 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)5 AtLeastOneChecker (com.ctrip.xpipe.redis.meta.server.dcchange.impl.AtLeastOneChecker)5 ClusterMeta (com.ctrip.xpipe.redis.core.entity.ClusterMeta)4 ShardMeta (com.ctrip.xpipe.redis.core.entity.ShardMeta)4 SlaveRole (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole)4 Pair (com.ctrip.xpipe.tuple.Pair)4 DcMeta (com.ctrip.xpipe.redis.core.entity.DcMeta)3 PrimaryDcChangeMessage (com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleService.PrimaryDcChangeMessage)3 Server (com.ctrip.xpipe.simpleserver.Server)3 InetSocketAddress (java.net.InetSocketAddress)3 SERVER_ROLE (com.ctrip.xpipe.api.server.Server.SERVER_ROLE)2 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)2 HostPort (com.ctrip.xpipe.endpoint.HostPort)2