Search in sources :

Example 6 with Pair

use of com.ctrip.xpipe.tuple.Pair 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 7 with Pair

use of com.ctrip.xpipe.tuple.Pair in project x-pipe by ctripcorp.

the class CurrentMetaTest method testSetInfo.

@Test
public void testSetInfo() {
    // set survice keepers
    Assert.assertEquals(0, currentMeta.getSurviveKeepers(clusterId, shardId).size());
    Assert.assertEquals(null, currentMeta.getKeeperActive(clusterId, shardId));
    List<KeeperMeta> allKeepers = getDcKeepers(getDc(), clusterId, shardId);
    KeeperMeta active = allKeepers.get(0);
    currentMeta.setSurviveKeepers(clusterId, shardId, allKeepers, active);
    Assert.assertEquals(allKeepers.size(), currentMeta.getSurviveKeepers(clusterId, shardId).size());
    active.setActive(true);
    Assert.assertEquals(active, currentMeta.getKeeperActive(clusterId, shardId));
    // set keeper active
    KeeperMeta keeperMeta = getDcKeepers(getDc(), clusterId, shardId).get(1);
    boolean result = currentMeta.setKeeperActive(clusterId, shardId, keeperMeta);
    Assert.assertTrue(result);
    keeperMeta.setActive(true);
    Assert.assertEquals(keeperMeta, currentMeta.getKeeperActive(clusterId, shardId));
    Assert.assertFalse(currentMeta.setKeeperActive(clusterId, shardId, keeperMeta));
    // set keeper active not exist
    keeperMeta.setIp(randomString(10));
    try {
        currentMeta.setKeeperActive(clusterId, shardId, keeperMeta);
        Assert.fail();
    } catch (Exception e) {
    }
    Assert.assertEquals(new Pair<String, Integer>("127.0.0.1", 6379), currentMeta.getKeeperMaster(clusterId, shardId));
    Pair<String, Integer> keeperMaster = new Pair<String, Integer>("localhost", randomPort());
    currentMeta.setKeeperMaster(clusterId, shardId, keeperMaster);
    Assert.assertEquals(keeperMaster, currentMeta.getKeeperMaster(clusterId, shardId));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Pair(com.ctrip.xpipe.tuple.Pair) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 8 with Pair

use of com.ctrip.xpipe.tuple.Pair in project x-pipe by ctripcorp.

the class DefaultMetaCache method findClusterShard.

@Override
public Pair<String, String> findClusterShard(HostPort hostPort) {
    XpipeMetaManager xpipeMetaManager = meta.getValue();
    XpipeMetaManager.MetaDesc metaDesc = xpipeMetaManager.findMetaDesc(hostPort);
    if (metaDesc == null) {
        return null;
    }
    return new Pair<>(metaDesc.getClusterId(), metaDesc.getShardId());
}
Also used : XpipeMetaManager(com.ctrip.xpipe.redis.core.meta.XpipeMetaManager) DefaultXpipeMetaManager(com.ctrip.xpipe.redis.core.meta.impl.DefaultXpipeMetaManager) Pair(com.ctrip.xpipe.tuple.Pair)

Example 9 with Pair

use of com.ctrip.xpipe.tuple.Pair 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 10 with Pair

use of com.ctrip.xpipe.tuple.Pair in project x-pipe by ctripcorp.

the class BackupDcKeeperMasterChooserAlgorithm method doChoose.

@Override
protected Pair<String, Integer> doChoose() {
    String dcName = dcMetaCache.getPrimaryDc(clusterId, shardId);
    KeeperMeta keeperMeta = multiDcService.getActiveKeeper(dcName, clusterId, shardId);
    logger.debug("[doChooseKeeperMaster]{}, {}, {}, {}", dcName, clusterId, shardId, keeperMeta);
    if (keeperMeta == null) {
        return null;
    }
    return new Pair<>(keeperMeta.getIp(), keeperMeta.getPort());
}
Also used : KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Pair(com.ctrip.xpipe.tuple.Pair)

Aggregations

Pair (com.ctrip.xpipe.tuple.Pair)13 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)4 Test (org.junit.Test)4 AlertMessageDecorator (com.ctrip.xpipe.redis.console.alert.decorator.AlertMessageDecorator)2 Decorator (com.ctrip.xpipe.redis.console.alert.decorator.Decorator)2 RecoverMessageDecorator (com.ctrip.xpipe.redis.console.alert.decorator.RecoverMessageDecorator)2 ScheduledAlertMessageDecorator (com.ctrip.xpipe.redis.console.alert.decorator.ScheduledAlertMessageDecorator)2 RedisTbl (com.ctrip.xpipe.redis.console.model.RedisTbl)2 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)2 LinkedList (java.util.LinkedList)2 SERVER_ROLE (com.ctrip.xpipe.api.server.Server.SERVER_ROLE)1 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 ClusterMeta (com.ctrip.xpipe.redis.core.entity.ClusterMeta)1 DcMeta (com.ctrip.xpipe.redis.core.entity.DcMeta)1 Redis (com.ctrip.xpipe.redis.core.entity.Redis)1 ShardMeta (com.ctrip.xpipe.redis.core.entity.ShardMeta)1 XpipeMetaManager (com.ctrip.xpipe.redis.core.meta.XpipeMetaManager)1 DefaultXpipeMetaManager (com.ctrip.xpipe.redis.core.meta.impl.DefaultXpipeMetaManager)1 SlaveRole (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole)1 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)1