use of com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand in project x-pipe by ctripcorp.
the class AbstractNewMasterChooser method serverRole.
protected SERVER_ROLE serverRole(RedisMeta redisMeta) {
try {
SimpleObjectPool<NettyClient> clientPool = keyedObjectPool.getKeyPool(new InetSocketAddress(redisMeta.getIp(), redisMeta.getPort()));
Role role = new RoleCommand(clientPool, CHECK_NEW_MASTER_TIMEOUT_SECONDS * 1000, true, scheduled).execute().get(CHECK_NEW_MASTER_TIMEOUT_SECONDS, TimeUnit.SECONDS);
return role.getServerRole();
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.error("[isMaster]" + redisMeta, e);
}
return SERVER_ROLE.UNKNOWN;
}
use of com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand in project x-pipe by ctripcorp.
the class RoleCommandTest method testKeeper.
@Test
public void testKeeper() throws Exception {
Server slave = startServer("*5\r\n" + "$6\r\nkeeper\r\n" + "$9\r\nlocalhost\r\n" + ":6379\r\n" + "$9\r\nconnected\r\n" + ":477\r\n");
RoleCommand roleCommand = new RoleCommand(getXpipeNettyClientKeyedObjectPool().getKeyPool(localhostInetAddress(slave.getPort())), 2000, false, scheduled);
SlaveRole role = (SlaveRole) roleCommand.execute().get();
Assert.assertEquals(SERVER_ROLE.KEEPER, role.getServerRole());
Assert.assertEquals("localhost", role.getMasterHost());
Assert.assertEquals(6379, role.getMasterPort());
Assert.assertEquals(MASTER_STATE.REDIS_REPL_CONNECTED, role.getMasterState());
Assert.assertEquals(477, role.getMasterOffset());
}
use of com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand in project x-pipe by ctripcorp.
the class BackupDcClusterRedisStateAjust method getRedisesNeedToChange.
protected List<RedisMeta> getRedisesNeedToChange(ShardMeta shardMeta, KeeperMeta keeperActive) {
List<RedisMeta> redisesNeedChange = new LinkedList<>();
for (RedisMeta redisMeta : shardMeta.getRedises()) {
try {
boolean change = false;
RoleCommand roleCommand = new RoleCommand(pool.getKeyPool(new InetSocketAddress(redisMeta.getIp(), redisMeta.getPort())), 1000, false, scheduled);
Role role = roleCommand.execute().get();
if (role.getServerRole() == SERVER_ROLE.MASTER) {
change = true;
logger.info("[getRedisesNeedToChange][redis master, change to slave of keeper]{}, {}", redisMeta, keeperActive);
} else if (role.getServerRole() == SERVER_ROLE.SLAVE) {
SlaveRole slaveRole = (SlaveRole) role;
if (!keeperActive.getIp().equals(slaveRole.getMasterHost()) || !keeperActive.getPort().equals(slaveRole.getMasterPort())) {
logger.info("[getRedisesNeedToChange][redis master not active keeper, change to slaveof keeper]{}, {}, {}", slaveRole, redisMeta, keeperActive);
change = true;
}
} else {
logger.warn("[doRun][role error]{}, {}", redisMeta, role);
continue;
}
if (change) {
redisesNeedChange.add(redisMeta);
}
} catch (Exception e) {
logger.error("[doRun]" + redisMeta, e);
}
}
return redisesNeedChange;
}
use of com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand in project x-pipe by ctripcorp.
the class PrimaryDcKeeperMasterChooserAlgorithm method isMaster.
protected boolean isMaster(RedisMeta redisMeta) {
try {
SimpleObjectPool<NettyClient> clientPool = keyedObjectPool.getKeyPool(new InetSocketAddress(redisMeta.getIp(), redisMeta.getPort()));
Role role = new RoleCommand(clientPool, checkRedisTimeoutSeconds * 1000, false, scheduled).execute().get(checkRedisTimeoutSeconds, TimeUnit.SECONDS);
return SERVER_ROLE.MASTER == role.getServerRole();
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.error("[isMaster]" + redisMeta, e);
}
return false;
}
use of com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand in project x-pipe by ctripcorp.
the class RoleCommandTest method testMaster.
@Test
public void testMaster() throws Exception {
Server master = startServer("*3\r\n" + "$6\r\nmaster\r\n" + ":43\r\n" + "*3\r\n" + "$9\r\n127.0.0.1\r\n" + "$4\r\n6479\r\n" + "$1\r\n0\r\n");
RoleCommand roleCommand = new RoleCommand(getXpipeNettyClientKeyedObjectPool().getKeyPool(localhostInetAddress(master.getPort())), 2000, false, scheduled);
Role role = roleCommand.execute().get();
Assert.assertEquals(SERVER_ROLE.MASTER, role.getServerRole());
}
Aggregations