Search in sources :

Example 6 with KeeperMeta

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);
}
Also used : KeeperActiveElectAlgorithm(com.ctrip.xpipe.redis.meta.server.keeper.KeeperActiveElectAlgorithm) ArrayList(java.util.ArrayList) ChildData(org.apache.curator.framework.recipes.cache.ChildData) LinkedList(java.util.LinkedList) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta)

Example 7 with KeeperMeta

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());
}
Also used : KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test)

Example 8 with KeeperMeta

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());
}
Also used : KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test)

Example 9 with KeeperMeta

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);
        }
    }
}
Also used : RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest)

Example 10 with KeeperMeta

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) {
    }
}
Also used : KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest)

Aggregations

KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)50 Test (org.junit.Test)28 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)9 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)8 KeeperTransMeta (com.ctrip.xpipe.redis.core.entity.KeeperTransMeta)6 LinkedList (java.util.LinkedList)6 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)4 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)4 KeeperStateChangeJob (com.ctrip.xpipe.redis.meta.server.job.KeeperStateChangeJob)4 InetSocketAddress (java.net.InetSocketAddress)4 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)3 ShardMeta (com.ctrip.xpipe.redis.core.entity.ShardMeta)3 ShardStatus (com.ctrip.xpipe.redis.core.meta.ShardStatus)3 Before (org.junit.Before)3 RedisKeeperServerState (com.ctrip.xpipe.redis.keeper.RedisKeeperServerState)2 DefaultSlaveOfJob (com.ctrip.xpipe.redis.meta.server.job.DefaultSlaveOfJob)2 Server (com.ctrip.xpipe.simpleserver.Server)2 Pair (com.ctrip.xpipe.tuple.Pair)2 ExecutionException (java.util.concurrent.ExecutionException)2 ChildData (org.apache.curator.framework.recipes.cache.ChildData)2