Search in sources :

Example 6 with SlaveRole

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;
}
Also used : Role(com.ctrip.xpipe.redis.core.protocal.pojo.Role) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) MasterRole(com.ctrip.xpipe.redis.core.protocal.pojo.MasterRole) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) Server(com.ctrip.xpipe.simpleserver.Server) MasterRole(com.ctrip.xpipe.redis.core.protocal.pojo.MasterRole)

Example 7 with SlaveRole

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());
    }
}
Also used : SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) Test(org.junit.Test)

Example 8 with SlaveRole

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());
    }
}
Also used : SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) Pair(com.ctrip.xpipe.tuple.Pair) Test(org.junit.Test)

Example 9 with SlaveRole

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayParser(com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser) ByteBuf(io.netty.buffer.ByteBuf) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 10 with SlaveRole

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;
}
Also used : Role(com.ctrip.xpipe.redis.core.protocal.pojo.Role) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) InetSocketAddress(java.net.InetSocketAddress) RedisMeta(com.ctrip.xpipe.redis.core.entity.RedisMeta) RoleCommand(com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand) LinkedList(java.util.LinkedList)

Aggregations

SlaveRole (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole)14 Test (org.junit.Test)12 Server (com.ctrip.xpipe.simpleserver.Server)8 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)5 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)4 ExecutionException (java.util.concurrent.ExecutionException)4 RoleCommand (com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand)3 Role (com.ctrip.xpipe.redis.core.protocal.pojo.Role)3 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)1 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)1 MASTER_STATE (com.ctrip.xpipe.redis.core.protocal.MASTER_STATE)1 MasterRole (com.ctrip.xpipe.redis.core.protocal.pojo.MasterRole)1 ArrayParser (com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser)1 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)1 Pair (com.ctrip.xpipe.tuple.Pair)1 ByteBuf (io.netty.buffer.ByteBuf)1 InetSocketAddress (java.net.InetSocketAddress)1 LinkedList (java.util.LinkedList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1