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