Search in sources :

Example 11 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class DefaultSlaveOfCommand method doExecute.

@Override
protected void doExecute() throws CommandExecutionException {
    SimpleObjectPool<NettyClient> clientPool = getClientPool();
    UntilSuccess slaveOf = new UntilSuccess();
    slaveOf.add(new XSlaveofCommand(clientPool, ip, port, scheduled));
    slaveOf.add(new SlaveOfCommand(clientPool, ip, port, scheduled));
    SequenceCommandChain chain = new SequenceCommandChain(false);
    chain.add(slaveOf);
    chain.add(new ConfigRewrite(clientPool, scheduled));
    chain.execute().addListener(new CommandFutureListener<Object>() {

        @Override
        public void operationComplete(CommandFuture<Object> commandFuture) throws Exception {
            if (commandFuture.isSuccess()) {
                future().setSuccess(RedisClientProtocol.OK);
            } else {
                future().setFailure(commandFuture.cause());
            }
        }
    });
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) UntilSuccess(com.ctrip.xpipe.command.UntilSuccess) CommandExecutionException(com.ctrip.xpipe.command.CommandExecutionException) SequenceCommandChain(com.ctrip.xpipe.command.SequenceCommandChain)

Example 12 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class AbstractFakeRedisTest method sendInmemoryPsync.

protected InMemoryPsync sendInmemoryPsync(String ip, int port, String runid, long offset) throws Exception {
    SequenceCommandChain chain = new SequenceCommandChain(false);
    SimpleObjectPool<NettyClient> pool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress(ip, port));
    NettyClient nettyClient = null;
    try {
        nettyClient = pool.borrowObject();
        SimpleObjectPool<NettyClient> clientPool = new FixedObjectPool<NettyClient>(nettyClient);
        chain.add(new Replconf(clientPool, ReplConfType.CAPA, scheduled, CAPA.EOF.toString()));
        InMemoryPsync psync = new InMemoryPsync(clientPool, runid, offset, scheduled);
        chain.add(psync);
        psync.addPsyncObserver(new PsyncObserver() {

            private long masterRdbOffset = 0;

            @Override
            public void reFullSync() {
            }

            @Override
            public void onFullSync() {
            }

            @Override
            public void onContinue(String requestReplId, String responseReplId) {
            }

            @Override
            public void endWriteRdb() {
                new Replconf(clientPool, ReplConfType.ACK, scheduled, String.valueOf(masterRdbOffset)).execute();
            }

            @Override
            public void beginWriteRdb(EofType eofType, long masterRdbOffset) throws IOException {
                this.masterRdbOffset = masterRdbOffset;
            }
        });
        chain.execute();
        return psync;
    } finally {
        if (nettyClient != null) {
            pool.returnObject(nettyClient);
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Replconf(com.ctrip.xpipe.redis.core.protocal.cmd.Replconf) EofType(com.ctrip.xpipe.redis.core.protocal.protocal.EofType) IOException(java.io.IOException) SequenceCommandChain(com.ctrip.xpipe.command.SequenceCommandChain) NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) FixedObjectPool(com.ctrip.xpipe.pool.FixedObjectPool) InMemoryPsync(com.ctrip.xpipe.redis.core.protocal.cmd.InMemoryPsync) PsyncObserver(com.ctrip.xpipe.redis.core.protocal.PsyncObserver)

Example 13 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient 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 14 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class DefaultPsyncTest method beforeDefaultPsyncTest.

@Before
public void beforeDefaultPsyncTest() throws Exception {
    FakeRedisServer fakeRedisServer = startFakeRedisServer();
    Endpoint masterEndPoint = new DefaultEndPoint("localhost", fakeRedisServer.getPort());
    SimpleObjectPool<NettyClient> pool = NettyPoolUtil.createNettyPool(new InetSocketAddress("localhost", fakeRedisServer.getPort()));
    when(replicationStoreManager.createIfNotExist()).thenReturn(replicationStore);
    when(replicationStore.getMetaStore()).thenReturn(metaStore);
    when(metaStore.getReplId()).thenReturn("?");
    when(replicationStore.getEndOffset()).thenReturn(-1L);
    defaultPsync = new DefaultPsync(pool, masterEndPoint, replicationStoreManager, scheduled);
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) Endpoint(com.ctrip.xpipe.api.endpoint.Endpoint) InetSocketAddress(java.net.InetSocketAddress) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) FakeRedisServer(com.ctrip.xpipe.redis.core.server.FakeRedisServer) Before(org.junit.Before)

Example 15 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class RedisCommandTest method testTimeoutNext.

@Test
public void testTimeoutNext() throws Exception {
    Server server = startEchoPrefixServer(String.valueOf((char) RedisClientProtocol.PLUS_BYTE));
    SimpleObjectPool<NettyClient> keyPool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress("127.0.0.1", server.getPort()));
    int sleepTime = timeoutMilli + 50;
    String str1 = String.format("sleep %d %s\r\n", sleepTime, randomString(10));
    String str2 = randomString(10) + "\r\n";
    try {
        new TestCommand(str1, timeoutMilli, keyPool, scheduled).execute().get();
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof CommandTimeoutException);
    }
    sleep(sleepTime * 2);
    new TestCommand(str2, sleepTime, keyPool, scheduled).execute().get();
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) Server(com.ctrip.xpipe.simpleserver.Server) InetSocketAddress(java.net.InetSocketAddress) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest)

Aggregations

NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)37 InetSocketAddress (java.net.InetSocketAddress)31 Test (org.junit.Test)8 ExecutionException (java.util.concurrent.ExecutionException)6 AbstractTest (com.ctrip.xpipe.AbstractTest)4 InfoCommand (com.ctrip.xpipe.redis.core.protocal.cmd.InfoCommand)4 Server (com.ctrip.xpipe.simpleserver.Server)4 IOException (java.io.IOException)4 CommandExecutionException (com.ctrip.xpipe.command.CommandExecutionException)3 SequenceCommandChain (com.ctrip.xpipe.command.SequenceCommandChain)3 FixedObjectPool (com.ctrip.xpipe.pool.FixedObjectPool)3 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)3 AbstractSentinelCommand (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand)3 Replconf (com.ctrip.xpipe.redis.core.protocal.cmd.Replconf)3 DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)2 XpipeException (com.ctrip.xpipe.exception.XpipeException)2 NettyClientFactory (com.ctrip.xpipe.netty.commands.NettyClientFactory)2 XpipeObjectPoolFromKeyed (com.ctrip.xpipe.pool.XpipeObjectPoolFromKeyed)2 DefaultSlaveOfCommand (com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand)2 InMemoryPsync (com.ctrip.xpipe.redis.core.protocal.cmd.InMemoryPsync)2