use of com.ctrip.xpipe.redis.keeper.RedisKeeperServerState in project x-pipe by ctripcorp.
the class XRedisPartialTest method createRedisKeeperServer.
@Override
protected RedisKeeperServer createRedisKeeperServer(KeeperMeta keeperMeta, File baseDir, KeeperConfig keeperConfig, MetaServerKeeperService metaService, LeaderElectorManager leaderElectorManager, KeepersMonitorManager keeperMonitorManager) {
return new DefaultRedisKeeperServer(keeperMeta, keeperConfig, baseDir, metaService, leaderElectorManager, keeperMonitorManager) {
private int count = 0;
@Override
public RedisKeeperServerState getRedisKeeperServerState() {
RedisKeeperServerState redisKeeperServerState = super.getRedisKeeperServerState();
return (RedisKeeperServerState) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { RedisKeeperServerState.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("psync")) {
count++;
logger.info("[psync][count]{}, {}", args, count);
if (count == 2) {
RedisClient redisClient = (RedisClient) args[0];
logger.info("[psync][close client]{}, {}", count, redisClient);
redisClient.close();
}
}
return method.invoke(redisKeeperServerState, args);
}
});
}
};
}
use of com.ctrip.xpipe.redis.keeper.RedisKeeperServerState in project x-pipe by ctripcorp.
the class RedisKeeperServerStateUnknownTest method testActive.
@Test
public void testActive() throws IOException {
// active
KeeperMeta keeperMeta = redisKeeperServer.getCurrentKeeperMeta();
ShardStatus shardStatus = createShardStatus(keeperMeta, null, redisMasterMeta);
unknown.setShardStatus(shardStatus);
RedisKeeperServerState newState = redisKeeperServer.getRedisKeeperServerState();
Assert.assertTrue(newState instanceof RedisKeeperServerStateActive);
Assert.assertEquals(new InetSocketAddress(redisMasterMeta.getIp(), redisMasterMeta.getPort()), newState.getMaster().getSocketAddress());
}
use of com.ctrip.xpipe.redis.keeper.RedisKeeperServerState in project x-pipe by ctripcorp.
the class RedisKeeperServerStateUnknownTest method testBackup.
@Test
public void testBackup() throws IOException {
// active
KeeperMeta keeperMeta = SerializationUtils.clone(redisKeeperServer.getCurrentKeeperMeta());
keeperMeta.setPort(keeperMeta.getPort() + 1);
ShardStatus shardStatus = createShardStatus(keeperMeta, null, redisMasterMeta);
unknown.setShardStatus(shardStatus);
RedisKeeperServerState newState = redisKeeperServer.getRedisKeeperServerState();
Assert.assertTrue(newState instanceof RedisKeeperServerStateBackup);
Assert.assertEquals(new InetSocketAddress(keeperMeta.getIp(), keeperMeta.getPort()), newState.getMaster().getSocketAddress());
}
use of com.ctrip.xpipe.redis.keeper.RedisKeeperServerState in project x-pipe by ctripcorp.
the class KeeperCommandHandler method doSetKeeperState.
private void doSetKeeperState(RedisClient redisClient, KeeperState keeperState, InetSocketAddress masterAddress) {
RedisKeeperServer redisKeeperServer = redisClient.getRedisKeeperServer();
RedisKeeperServerState currentState = redisKeeperServer.getRedisKeeperServerState();
try {
switch(keeperState) {
case ACTIVE:
currentState.becomeActive(masterAddress);
break;
case BACKUP:
currentState.becomeBackup(masterAddress);
break;
case UNKNOWN:
throw new IllegalStateException("state can not change to unknown!");
default:
throw new IllegalStateException("unrecognised state:" + keeperState);
}
redisClient.sendMessage(new SimpleStringParser(RedisProtocol.OK).format());
} catch (Exception e) {
logger.error("[doSetKeeperState]" + String.format("%s, %s, %s", redisClient, keeperState, masterAddress), e);
redisClient.sendMessage(new RedisErrorParser(e.getMessage()).format());
}
}
Aggregations