Search in sources :

Example 1 with Releasable

use of com.ctrip.xpipe.api.lifecycle.Releasable in project x-pipe by ctripcorp.

the class DefaultKeeperElectorManager method observerShardLeader.

protected void observerShardLeader(final String clusterId, final String shardId) {
    logger.info("[observerShardLeader]{}, {}", clusterId, shardId);
    final String leaderLatchPath = MetaZkConfig.getKeeperLeaderLatchPath(clusterId, shardId);
    final CuratorFramework client = zkClient.get();
    if (currentMetaManager.watchIfNotWatched(clusterId, shardId)) {
        try {
            logger.info("[observerShardLeader][add PathChildrenCache]{}, {}", clusterId, shardId);
            PathChildrenCache pathChildrenCache = new PathChildrenCache(client, leaderLatchPath, true, XpipeThreadFactory.create(String.format("PathChildrenCache:%s-%s", clusterId, shardId)));
            pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

                private AtomicBoolean isFirst = new AtomicBoolean(true);

                @Override
                public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                    if (isFirst.get()) {
                        isFirst.set(false);
                        try {
                            logger.info("[childEvent][first sleep]{}", FIRST_PATH_CHILDREN_CACHE_SLEEP_MILLI);
                            TimeUnit.MILLISECONDS.sleep(FIRST_PATH_CHILDREN_CACHE_SLEEP_MILLI);
                        } catch (Exception e) {
                            logger.error("[childEvent]", e);
                        }
                    }
                    logger.info("[childEvent]{}, {}, {}, {}", clusterId, shardId, event.getType(), ZkUtils.toString(event.getData()));
                    updateShardLeader(leaderLatchPath, pathChildrenCache.getCurrentData(), clusterId, shardId);
                }
            });
            currentMetaManager.addResource(clusterId, shardId, new Releasable() {

                @Override
                public void release() throws Exception {
                    try {
                        logger.info("[release][release children cache]{}, {}", clusterId, shardId);
                        pathChildrenCache.close();
                    } catch (Exception e) {
                        logger.error(String.format("[release][release children cache]%s, %s", clusterId, shardId), e);
                    }
                }
            });
            pathChildrenCache.start();
        } catch (Exception e) {
            logger.error("[observerShardLeader]" + clusterId + " " + shardId, e);
        }
    } else {
        logger.warn("[observerShardLeader][already watched]{}, {}", clusterId, shardId);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Releasable(com.ctrip.xpipe.api.lifecycle.Releasable)

Example 2 with Releasable

use of com.ctrip.xpipe.api.lifecycle.Releasable in project x-pipe by ctripcorp.

the class RedisKeeperServerStateBackup method psync.

@Override
public boolean psync(final RedisClient redisClient, final String[] args) throws Exception {
    logger.info("[psync][server state backup, ask slave to wait]{}, {}", redisClient, this);
    if (redisKeeperServer.compareAndDo(this, new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            redisClient.sendMessage(RedisProtocol.CRLF.getBytes());
            PsyncKeeperServerStateObserver psyncKeeperServerStateObserver = new PsyncKeeperServerStateObserver(args, redisClient);
            redisKeeperServer.addObserver(psyncKeeperServerStateObserver);
            redisClient.addChannelCloseReleaseResources(new Releasable() {

                @Override
                public void release() throws Exception {
                    psyncKeeperServerStateObserver.release();
                }
            });
        }
    })) {
        // state backup
        return false;
    }
    logger.info("[psync][state change, use new state to psync]{}, {}", redisClient, redisKeeperServer);
    return redisKeeperServer.getRedisKeeperServerState().psync(redisClient, args);
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) Releasable(com.ctrip.xpipe.api.lifecycle.Releasable) IOException(java.io.IOException)

Example 3 with Releasable

use of com.ctrip.xpipe.api.lifecycle.Releasable in project x-pipe by ctripcorp.

the class CurrentMetaTest method testRelease.

@Test
public void testRelease() {
    currentMeta.addResource(clusterId, shardId, new Releasable() {

        @Override
        public void release() throws Exception {
            releaseCount.incrementAndGet();
        }
    });
    currentMeta.removeCluster(clusterId);
    Assert.assertEquals(1, releaseCount.get());
}
Also used : Releasable(com.ctrip.xpipe.api.lifecycle.Releasable) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 4 with Releasable

use of com.ctrip.xpipe.api.lifecycle.Releasable in project x-pipe by ctripcorp.

the class CurrentMetaTest method testToString.

@Test
public void testToString() {
    List<KeeperMeta> allKeepers = getDcKeepers(getDc(), clusterId, shardId);
    currentMeta.setSurviveKeepers(clusterId, shardId, allKeepers, allKeepers.get(0));
    currentMeta.addResource(clusterId, shardId, new Releasable() {

        @Override
        public void release() throws Exception {
        }
    });
    String json = currentMeta.toString();
    CurrentMeta de = CurrentMeta.fromJson(json);
    Assert.assertEquals(json, de.toString());
    Assert.assertTrue(currentMeta.hasCluster(clusterId));
    for (ShardMeta shardMeta : clusterMeta.getShards().values()) {
        Assert.assertTrue(currentMeta.hasShard(clusterId, shardMeta.getId()));
    }
}
Also used : ShardMeta(com.ctrip.xpipe.redis.core.entity.ShardMeta) Releasable(com.ctrip.xpipe.api.lifecycle.Releasable) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Aggregations

Releasable (com.ctrip.xpipe.api.lifecycle.Releasable)4 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)2 Test (org.junit.Test)2 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)1 ShardMeta (com.ctrip.xpipe.redis.core.entity.ShardMeta)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)1 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)1 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)1