Search in sources :

Example 1 with SlaveRole

use of com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole 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 2 with SlaveRole

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

the class AddKeeperCommandTest method testFailTimeout.

@Test
public void testFailTimeout() throws Exception {
    SlaveRole slaveRole = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0);
    startServer(keeperPort, ByteBufUtils.readToString(slaveRole.format()));
    long begin = System.currentTimeMillis();
    try {
        addKeeperCommand.execute().get();
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof KeeperMasterStateNotAsExpectedException);
    }
    long end = System.currentTimeMillis();
    long interval = end - begin;
    Assert.assertTrue(interval >= timeoutMilli);
    Assert.assertTrue(interval <= timeoutMilli * 2);
}
Also used : SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 3 with SlaveRole

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

the class AddKeeperCommandTest method testCheckStateCommandNoDelay.

@Test
public void testCheckStateCommandNoDelay() throws Exception {
    int sleepTime = 2000;
    SlaveRole keeperRole = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECTED, 0);
    Server server = startServer(keeperPort, new Callable<String>() {

        @Override
        public String call() throws Exception {
            sleep(sleepTime);
            return ByteBufUtils.readToString(keeperRole.format());
        }
    });
    SettableFuture<Boolean> objectSettableFuture = SettableFuture.create();
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        public void doRun() throws Exception {
            AddKeeperCommand.CheckStateCommand checkStateCommand = new AddKeeperCommand.CheckStateCommand(new KeeperMeta().setIp("127.0.0.1").setPort(server.getPort()), scheduled);
            checkStateCommand.doExecute();
            objectSettableFuture.set(true);
        }
    });
    // should return immediately
    objectSettableFuture.get(500, TimeUnit.MILLISECONDS);
}
Also used : Server(com.ctrip.xpipe.simpleserver.Server) ExecutionException(java.util.concurrent.ExecutionException) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 4 with SlaveRole

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

the class AddKeeperCommandTest method testIoExceptionNotRetry.

@Test
public void testIoExceptionNotRetry() throws Exception {
    SlaveRole slaveRole = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0);
    final Server server = startServer(keeperPort, ByteBufUtils.readToString(slaveRole.format()));
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                sleep(timeoutMilli / 2);
                LifecycleHelper.stopIfPossible(server);
                LifecycleHelper.disposeIfPossible(server);
            } catch (Exception e) {
                logger.error("[run]" + server, e);
            }
        }
    }).start();
    try {
        addKeeperCommand.execute().get();
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(ExceptionUtils.isSocketIoException(e));
    }
}
Also used : SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) Server(com.ctrip.xpipe.simpleserver.Server) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 5 with SlaveRole

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

the class AddKeeperCommandTest method testSuccess.

@SuppressWarnings("unused")
@Test
public void testSuccess() throws Exception {
    SlaveRole keeperRole = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECTED, 0);
    Server server = startServer(keeperPort, ByteBufUtils.readToString(keeperRole.format()));
    SlaveRole real = addKeeperCommand.execute().get();
    Assert.assertEquals(keeperRole, real);
}
Also used : SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) Server(com.ctrip.xpipe.simpleserver.Server) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

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