use of com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole in project x-pipe by ctripcorp.
the class FirstNewMasterChooserTest method startSlaveFakeRedis.
private Server startSlaveFakeRedis(Integer port, SERVER_ROLE serverRole) throws Exception {
Role role = null;
if (serverRole == SERVER_ROLE.MASTER) {
role = new MasterRole();
} else {
role = new SlaveRole(serverRole, "localhost", port, MASTER_STATE.REDIS_REPL_CONNECT, 0L);
}
Server server = startServer(port, ByteBufUtils.readToString(role.format()));
return server;
}
use of com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole in project x-pipe by ctripcorp.
the class PrimaryDcKeeperMasterChooserAlgorithmTest method testOneMaster.
@Test
public void testOneMaster() throws Exception {
SlaveRole role = new SlaveRole(SERVER_ROLE.MASTER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0L);
RedisMeta chosen = redises.get(0);
startServer(chosen.getPort(), ByteBufUtils.readToString(role.format()));
when(currentMetaManager.getKeeperMaster(clusterId, shardId)).thenReturn(null);
Assert.assertEquals(new Pair<String, Integer>(chosen.getIp(), chosen.getPort()), primaryAlgorithm.choose());
for (RedisMeta redisMeta : redises) {
when(currentMetaManager.getKeeperMaster(clusterId, shardId)).thenReturn(new Pair<String, Integer>(redisMeta.getIp(), redisMeta.getPort()));
Assert.assertEquals(new Pair<String, Integer>(chosen.getIp(), chosen.getPort()), primaryAlgorithm.choose());
}
}
use of com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole in project x-pipe by ctripcorp.
the class PrimaryDcKeeperMasterChooserAlgorithmTest method testMultiMaster.
@Test
public void testMultiMaster() throws Exception {
SlaveRole role = new SlaveRole(SERVER_ROLE.MASTER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0L);
for (RedisMeta redisMeta : redises) {
startServer(redisMeta.getPort(), ByteBufUtils.readToString(role.format()));
}
when(currentMetaManager.getKeeperMaster(clusterId, shardId)).thenReturn(null);
Assert.assertEquals(new Pair<String, Integer>(redises.get(0).getIp(), redises.get(0).getPort()), primaryAlgorithm.choose());
for (RedisMeta redisMeta : redises) {
Pair<String, Integer> currentMaster = new Pair<String, Integer>(redisMeta.getIp(), redisMeta.getPort());
when(currentMetaManager.getKeeperMaster(clusterId, shardId)).thenReturn(currentMaster);
Assert.assertEquals(currentMaster, primaryAlgorithm.choose());
}
}
use of com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole in project x-pipe by ctripcorp.
the class RoleCommandHandlerTest method test.
@Test
public void test() {
final AtomicReference<ByteBuf> result = new AtomicReference<>();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
result.set((ByteBuf) invocation.getArguments()[0]);
return null;
}
}).when(redisClient).sendMessage(any(ByteBuf.class));
handler.doHandle(new String[0], redisClient);
sleep(10);
String real = ByteBufUtils.readToString(result.get());
String expected = String.format("*5\r\n+%s\r\n+%s\r\n:%d\r\n+%s\r\n:%d\r\n", SERVER_ROLE.KEEPER.toString(), host, port, masterState.getDesc(), masterOffset);
Assert.assertEquals(expected, real);
// reverse
Object[] reverse = new ArrayParser().read(Unpooled.wrappedBuffer(real.getBytes())).getPayload();
SlaveRole slaveRole = new SlaveRole(reverse);
Assert.assertEquals(SERVER_ROLE.KEEPER, slaveRole.getServerRole());
Assert.assertEquals(host, slaveRole.getMasterHost());
Assert.assertEquals(port, slaveRole.getMasterPort());
Assert.assertEquals(masterState, slaveRole.getMasterState());
Assert.assertEquals(masterOffset, slaveRole.getMasterOffset());
}
use of com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole 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;
}
Aggregations