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());
}
}
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));
}
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());
}
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);
}
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());
}
Aggregations