Search in sources :

Example 1 with DefaultEndPoint

use of com.ctrip.xpipe.endpoint.DefaultEndPoint in project x-pipe by ctripcorp.

the class RedisFakeSlaveTest method startKeeperCreateReplicationLogClient.

private void startKeeperCreateReplicationLogClient() throws UnknownHostException, IOException {
    try {
        Endpoint endpoint = new DefaultEndPoint(redisMasterUri);
        socket = new Socket(endpoint.getHost(), endpoint.getPort());
        if (logger.isInfoEnabled()) {
            logger.info("[startKeeperClient]" + socket);
        }
        final OutputStream ous = socket.getOutputStream();
        InputStream ins = socket.getInputStream();
        ous.write(("replconf listening-port " + fakeSlavePort + "\r\n").getBytes());
        ous.flush();
        readLine(ins);
        ous.write("fsync\r\n".getBytes());
        ous.flush();
        readLine(ins);
        scheduled.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                try {
                    ous.write("replconf ack 0\r\n".getBytes());
                    ous.flush();
                } catch (IOException e) {
                    closeSocket();
                    logger.error("[run]", e);
                }
            }
        }, 0, 1, TimeUnit.SECONDS);
    } catch (IOException e) {
        closeSocket();
        logger.error("[startKeeperCreateReplicationLogClient]" + socket, e);
    }
}
Also used : Endpoint(com.ctrip.xpipe.api.endpoint.Endpoint) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) IOException(java.io.IOException) Socket(java.net.Socket)

Example 2 with DefaultEndPoint

use of com.ctrip.xpipe.endpoint.DefaultEndPoint in project x-pipe by ctripcorp.

the class DefaultRedisMasterReplicationTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    Server server = startEmptyServer();
    when(redisMaster.masterEndPoint()).thenReturn(new DefaultEndPoint("localhost", server.getPort()));
    AtomicInteger connectingCount = new AtomicInteger(0);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            connectingCount.incrementAndGet();
            return null;
        }
    }).when(redisMaster).setMasterState(MASTER_STATE.REDIS_REPL_CONNECTING);
    defaultRedisMasterReplication.setMasterConnectRetryDelaySeconds(0);
    defaultRedisMasterReplication.initialize();
    defaultRedisMasterReplication.start();
    waitConditionUntilTimeOut(() -> connectingCount.get() >= 2);
}
Also used : Answer(org.mockito.stubbing.Answer) RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) Server(com.ctrip.xpipe.simpleserver.Server) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest) Test(org.junit.Test)

Example 3 with DefaultEndPoint

use of com.ctrip.xpipe.endpoint.DefaultEndPoint in project x-pipe by ctripcorp.

the class DefaultReplicationStoreManagerTest method test.

@Test
public void test() throws Exception {
    String keeperRunid = randomKeeperRunid();
    File baseDir = new File(getTestFileDir());
    String clusterId = "cluster1";
    String shardId = "shard1";
    DefaultReplicationStoreManager mgr = (DefaultReplicationStoreManager) createReplicationStoreManager(clusterId, shardId, keeperRunid, baseDir);
    LifecycleHelper.initializeIfPossible(mgr);
    ReplicationStore currentStore = mgr.getCurrent();
    assertNull(currentStore);
    currentStore = mgr.create();
    assertEquals(clusterId, mgr.getClusterName());
    assertEquals(shardId, mgr.getShardName());
    assertEquals(currentStore, mgr.getCurrent());
    DefaultReplicationStore newCurrentStore = (DefaultReplicationStore) mgr.create();
    assertEquals(newCurrentStore, mgr.getCurrent());
    assertNotEquals(currentStore, mgr.getCurrent());
    MetaStore metaStore = newCurrentStore.getMetaStore();
    metaStore.setMasterAddress(new DefaultEndPoint("redis://127.0.0.1:6379"));
    newCurrentStore.beginRdb("masterRunid", 0, new LenEofType(100));
    ByteBuf cmdBuf = Unpooled.buffer();
    cmdBuf.writeByte(9);
    newCurrentStore.getCommandStore().appendCommands(cmdBuf);
    DefaultReplicationStoreManager mgr2 = (DefaultReplicationStoreManager) createReplicationStoreManager(clusterId, shardId, keeperRunid, baseDir);
    assertEquals(metaStore.getReplId(), mgr2.getCurrent().getMetaStore().getReplId());
    assertEquals(metaStore.beginOffset(), mgr2.getCurrent().getMetaStore().beginOffset());
    assertEquals(metaStore.getMasterAddress(), mgr2.getCurrent().getMetaStore().getMasterAddress());
    assertEquals(metaStore.beginOffset(), mgr2.getCurrent().getMetaStore().beginOffset());
}
Also used : MetaStore(com.ctrip.xpipe.redis.core.store.MetaStore) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) LenEofType(com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType) ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 4 with DefaultEndPoint

use of com.ctrip.xpipe.endpoint.DefaultEndPoint 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 5 with DefaultEndPoint

use of com.ctrip.xpipe.endpoint.DefaultEndPoint in project x-pipe by ctripcorp.

the class RoleCommandHandlerTest method beforeRoleCommandHandlerTest.

@Before
public void beforeRoleCommandHandlerTest() {
    host = "localhost";
    port = randomPort();
    when(redisClient.getRedisKeeperServer()).thenReturn(redisKeeperServer);
    when(redisKeeperServer.getRedisMaster()).thenReturn(redisMaster);
    when(redisKeeperServer.role()).thenReturn(SERVER_ROLE.KEEPER);
    when(redisMaster.masterEndPoint()).thenReturn(new DefaultEndPoint(host, port));
    when(redisMaster.getMasterState()).thenReturn(masterState);
}
Also used : DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) Before(org.junit.Before)

Aggregations

DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)9 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)3 IOException (java.io.IOException)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Endpoint (com.ctrip.xpipe.api.endpoint.Endpoint)2 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)2 InetSocketAddress (java.net.InetSocketAddress)2 XpipeException (com.ctrip.xpipe.exception.XpipeException)1 AbstractSlaveOfCommand (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSlaveOfCommand)1 DefaultPsync (com.ctrip.xpipe.redis.core.protocal.cmd.DefaultPsync)1 InfoCommand (com.ctrip.xpipe.redis.core.protocal.cmd.InfoCommand)1 SlaveOfCommand (com.ctrip.xpipe.redis.core.protocal.cmd.SlaveOfCommand)1 LenEofType (com.ctrip.xpipe.redis.core.protocal.protocal.LenEofType)1 FakeRedisServer (com.ctrip.xpipe.redis.core.server.FakeRedisServer)1 MetaStore (com.ctrip.xpipe.redis.core.store.MetaStore)1 ReplicationStore (com.ctrip.xpipe.redis.core.store.ReplicationStore)1 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)1 RedisSlavePromotionException (com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException)1 Server (com.ctrip.xpipe.simpleserver.Server)1