Search in sources :

Example 1 with RoleCommand

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;
}
Also used : Role(com.ctrip.xpipe.redis.core.protocal.pojo.Role) NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) InetSocketAddress(java.net.InetSocketAddress) RoleCommand(com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand)

Example 2 with RoleCommand

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

Example 3 with RoleCommand

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

Example 4 with RoleCommand

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;
}
Also used : Role(com.ctrip.xpipe.redis.core.protocal.pojo.Role) NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) InetSocketAddress(java.net.InetSocketAddress) RoleCommand(com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with RoleCommand

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

Aggregations

RoleCommand (com.ctrip.xpipe.redis.core.protocal.cmd.RoleCommand)6 Role (com.ctrip.xpipe.redis.core.protocal.pojo.Role)4 SlaveRole (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole)4 Server (com.ctrip.xpipe.simpleserver.Server)3 InetSocketAddress (java.net.InetSocketAddress)3 Test (org.junit.Test)3 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)2 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)1 MASTER_STATE (com.ctrip.xpipe.redis.core.protocal.MASTER_STATE)1 LinkedList (java.util.LinkedList)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1