Search in sources :

Example 1 with DefaultSlaveOfCommand

use of com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand in project x-pipe by ctripcorp.

the class DefaultSlaveOfCommandTest method testMultiServer.

@Test
public void testMultiServer() throws Exception {
    int begin = 10000;
    int count = 32;
    // make connection active
    for (int i = 0; i < count; i++) {
        int port = begin + i;
        SimpleObjectPool<NettyClient> keyPool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress("127.0.0.1", port));
        new PingCommand(keyPool, scheduled).execute().get();
    }
    CountDownLatch latch = new CountDownLatch(count);
    for (int i = 0; i < count; i++) {
        int port = begin + i;
        SimpleObjectPool<NettyClient> keyPool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress("127.0.0.1", port));
        long beginTime = System.currentTimeMillis();
        new DefaultSlaveOfCommand(keyPool, "127.0.0.1", 0, scheduled).execute(executors).addListener(new CommandFutureListener<String>() {

            @Override
            public void operationComplete(CommandFuture<String> commandFuture) throws Exception {
                long endTime = System.currentTimeMillis();
                long duration = endTime - beginTime;
                if (duration > 50) {
                    logger.warn("[timeout]127.0.0.1:{}, {}", port, duration);
                }
                latch.countDown();
            }
        });
    }
    latch.await();
    sleep(1000);
    logger.info("[testMultiServer][end]");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultSlaveOfCommand(com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand) CountDownLatch(java.util.concurrent.CountDownLatch) PingCommand(com.ctrip.xpipe.redis.core.protocal.cmd.PingCommand) NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest) Test(org.junit.Test)

Example 2 with DefaultSlaveOfCommand

use of com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand in project x-pipe by ctripcorp.

the class BecomePrimaryAction method makeRedisesOk.

@Override
protected void makeRedisesOk(Pair<String, Integer> newMaster, List<RedisMeta> slaves) {
    executionLog.info("[make redis master]" + newMaster);
    SimpleObjectPool<NettyClient> masterPool = keyedObjectPool.getKeyPool(new InetSocketAddress(newMaster.getKey(), newMaster.getValue()));
    Command<String> command = new DefaultSlaveOfCommand(masterPool, null, 0, scheduled);
    try {
        String result = command.execute().get();
        executionLog.info("[make redis master]" + result);
        RedisReadonly redisReadOnly = RedisReadonly.create(newMaster.getKey(), newMaster.getValue(), keyedObjectPool, scheduled);
        if (!(redisReadOnly instanceof SlaveOfRedisReadOnly)) {
            redisReadOnly.makeWritable();
        }
    } catch (Exception e) {
        logger.error("[makeRedisesOk]" + newMaster, e);
        executionLog.error("[make redis master fail]" + e.getMessage());
        throw new MakeRedisMasterFailException("make redis master:" + newMaster, e);
    }
    executionLog.info("[make slaves slaveof][begin]" + newMaster + "," + slaves);
    Command<Void> slavesJob = new DefaultSlaveOfJob(slaves, newMaster.getKey(), newMaster.getValue(), keyedObjectPool, scheduled, executors);
    try {
        slavesJob.execute().get(waitTimeoutSeconds, TimeUnit.SECONDS);
        executionLog.info("[make slaves slaveof]success");
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        logger.error("[makeRedisesOk]" + slaves + "->" + newMaster, e);
        executionLog.error("[make slaves slaveof][fail]" + e.getMessage());
        throw new MakeRedisSlaveOfMasterFailException(String.format("fail make slaves slave of:%s, %s", newMaster, e.getMessage()), e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DefaultSlaveOfJob(com.ctrip.xpipe.redis.meta.server.job.DefaultSlaveOfJob) MakeRedisSlaveOfMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.MakeRedisSlaveOfMasterFailException) DefaultSlaveOfCommand(com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand) MakeRedisMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.MakeRedisMasterFailException) MakeRedisMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.MakeRedisMasterFailException) MakeRedisSlaveOfMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.MakeRedisSlaveOfMasterFailException) ChooseNewMasterFailException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.ChooseNewMasterFailException) NettyClient(com.ctrip.xpipe.netty.commands.NettyClient)

Aggregations

NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)2 DefaultSlaveOfCommand (com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand)2 InetSocketAddress (java.net.InetSocketAddress)2 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)1 PingCommand (com.ctrip.xpipe.redis.core.protocal.cmd.PingCommand)1 ChooseNewMasterFailException (com.ctrip.xpipe.redis.meta.server.dcchange.exception.ChooseNewMasterFailException)1 MakeRedisMasterFailException (com.ctrip.xpipe.redis.meta.server.dcchange.exception.MakeRedisMasterFailException)1 MakeRedisSlaveOfMasterFailException (com.ctrip.xpipe.redis.meta.server.dcchange.exception.MakeRedisSlaveOfMasterFailException)1 DefaultSlaveOfJob (com.ctrip.xpipe.redis.meta.server.job.DefaultSlaveOfJob)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1