use of com.ctrip.xpipe.redis.core.entity.KeeperMeta in project x-pipe by ctripcorp.
the class DefaultKeeperElectorManager method updateShardLeader.
protected void updateShardLeader(String leaderLatchPath, List<ChildData> childrenData, String clusterId, String shardId) {
List<String> childrenPaths = new LinkedList<>();
childrenData.forEach(childData -> childrenPaths.add(childData.getPath()));
logger.info("[updateShardLeader]{}, {}, {}", clusterId, shardId, childrenPaths);
List<String> sortedChildren = LockInternals.getSortedChildren("latch-", sorter, childrenPaths);
List<KeeperMeta> survivalKeepers = new ArrayList<>(childrenData.size());
for (String path : sortedChildren) {
for (ChildData childData : childrenData) {
if (path.equals(childData.getPath())) {
String data = new String(childData.getData());
KeeperMeta keeper = JsonCodec.INSTANCE.decode(data, KeeperMeta.class);
survivalKeepers.add(keeper);
break;
}
}
}
if (survivalKeepers.size() != childrenData.size()) {
throw new IllegalStateException(String.format("[children data not equal with survival keepers]%s, %s", childrenData, survivalKeepers));
}
KeeperActiveElectAlgorithm klea = keeperActiveElectAlgorithmManager.get(clusterId, shardId);
KeeperMeta activeKeeper = klea.select(clusterId, shardId, survivalKeepers);
currentMetaManager.setSurviveKeepers(clusterId, shardId, survivalKeepers, activeKeeper);
}
use of com.ctrip.xpipe.redis.core.entity.KeeperMeta in project x-pipe by ctripcorp.
the class ShardMetaComparatorTest method testRemove.
@Test
public void testRemove() {
// equal
KeeperMeta removed = differentKeeper(current);
current.addKeeper(removed);
ShardMetaComparator comparator = new ShardMetaComparator(current, future);
comparator.compare();
Assert.assertEquals(0, comparator.getAdded().size());
Assert.assertEquals(1, comparator.getRemoved().size());
Assert.assertEquals(removed, comparator.getRemoved().toArray()[0]);
Assert.assertEquals(0, comparator.getMofified().size());
}
use of com.ctrip.xpipe.redis.core.entity.KeeperMeta in project x-pipe by ctripcorp.
the class ShardMetaComparatorTest method testAdd.
@Test
public void testAdd() {
// equal
KeeperMeta added = differentKeeper(current);
future.addKeeper(added);
ShardMetaComparator comparator = new ShardMetaComparator(current, future);
comparator.compare();
Assert.assertEquals(1, comparator.getAdded().size());
Assert.assertEquals(added, comparator.getAdded().toArray()[0]);
Assert.assertEquals(0, comparator.getRemoved().size());
Assert.assertEquals(0, comparator.getMofified().size());
}
use of com.ctrip.xpipe.redis.core.entity.KeeperMeta in project x-pipe by ctripcorp.
the class DefaultXpipeMetaManagerTest method testUpdateRedisMaster.
@Test
public void testUpdateRedisMaster() throws MetaException {
Pair<String, RedisMeta> redisMaster = metaManager.getRedisMaster(clusterId, shardId);
Assert.assertEquals(redisMaster.getKey(), "jq");
boolean result = metaManager.updateRedisMaster(redisMaster.getKey(), clusterId, shardId, redisMaster.getValue());
Assert.assertTrue(!result);
KeeperMeta activeKeeper = null;
for (KeeperMeta keeperMeta : metaManager.getKeepers(dc, clusterId, shardId)) {
if (keeperMeta.getMaster().equals(String.format("%s:%d", redisMaster.getValue().getIp(), redisMaster.getValue().getPort()))) {
activeKeeper = keeperMeta;
}
}
Assert.assertNotNull(activeKeeper);
for (RedisMeta redis : metaManager.getRedises(dc, clusterId, shardId)) {
if (!redis.equals(redisMaster.getValue())) {
String master = String.format("%s:%d", redis.getIp(), redis.getPort());
Assert.assertNotEquals(activeKeeper.getMaster(), master);
result = metaManager.updateRedisMaster(redisMaster.getKey(), clusterId, shardId, redis);
Assert.assertTrue(result);
KeeperMeta active = metaManager.getKeeperActive(redisMaster.getKey(), clusterId, shardId);
Assert.assertEquals(active.getMaster(), master);
}
}
}
use of com.ctrip.xpipe.redis.core.entity.KeeperMeta in project x-pipe by ctripcorp.
the class DefaultXpipeMetaManagerTest method testSetKeeperAlive.
@Test
public void testSetKeeperAlive() {
List<KeeperMeta> allSurvice = metaManager.getAllSurviceKeepers(dc, clusterId, shardId);
logger.info("[testSetKeeperAlive][allAlive]{}", allSurvice);
Assert.assertEquals(0, allSurvice.size());
List<KeeperMeta> allKeepers = metaManager.getKeepers(dc, clusterId, shardId);
for (KeeperMeta allOne : allKeepers) {
allOne.setSurvive(true);
}
allSurvice = metaManager.getAllSurviceKeepers(dc, clusterId, shardId);
Assert.assertEquals(0, allSurvice.size());
metaManager.setSurviveKeepers(dc, clusterId, shardId, allKeepers);
allSurvice = metaManager.getAllSurviceKeepers(dc, clusterId, shardId);
Assert.assertEquals(allKeepers.size(), allSurvice.size());
try {
KeeperMeta nonExist = createNonExistKeeper(allKeepers);
allKeepers.add(nonExist);
metaManager.setSurviveKeepers(dc, clusterId, shardId, allKeepers);
Assert.fail();
} catch (IllegalArgumentException e) {
}
}
Aggregations