Search in sources :

Example 1 with RedisSlave

use of com.ctrip.xpipe.redis.keeper.RedisSlave in project x-pipe by ctripcorp.

the class KeeperSingleDcWaitForOffset method testMakeBackupActive.

@Test
public void testMakeBackupActive() throws Exception {
    RedisKeeperServer redisKeeperServer = getRedisKeeperServer(backupKeeper);
    Assert.assertEquals(PARTIAL_STATE.FULL, redisKeeperServer.getRedisMaster().partialState());
    logger.info(remarkableMessage("make keeper active to wrong addr{}"), backupKeeper);
    String ip = "localhost";
    int port = randomPort();
    setKeeperState(backupKeeper, KeeperState.ACTIVE, ip, port);
    sleep(2000);
    // make sure redis has more log
    sendMessageToMaster(redisMaster, 1);
    xslaveof(backupKeeper.getIp(), backupKeeper.getPort(), getRedisSlaves());
    sleep(2000);
    setKeeperState(backupKeeper, KeeperState.ACTIVE, redisMaster.getIp(), redisMaster.getPort());
    sleep(2000);
    Set<RedisSlave> slaves = redisKeeperServer.slaves();
    Assert.assertEquals(PARTIAL_STATE.PARTIAL, redisKeeperServer.getRedisMaster().partialState());
    slaves.forEach(redisSlave -> Assert.assertEquals(PARTIAL_STATE.PARTIAL, redisSlave.partialState()));
    Assert.assertEquals(slaves.size(), redisKeeperServer.getKeeperMonitor().getKeeperStats().getWaitOffsetSucceed());
    Assert.assertEquals(0, redisKeeperServer.getKeeperMonitor().getKeeperStats().getWaitOffsetFail());
    sendMessageToMasterAndTestSlaveRedis();
}
Also used : RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) RedisSlave(com.ctrip.xpipe.redis.keeper.RedisSlave) Test(org.junit.Test)

Example 2 with RedisSlave

use of com.ctrip.xpipe.redis.keeper.RedisSlave in project x-pipe by ctripcorp.

the class BadKeeperWrongCommands method giveWrongCommands.

@SuppressWarnings("unused")
@Test
public void giveWrongCommands() throws IOException {
    String[] commands = createCommands();
    RedisKeeperServer active = getRedisKeeperServer(activeKeeper);
    RedisKeeperServer backup = getRedisKeeperServer(backupKeeper);
    replicationStore = active.getReplicationStore();
    Set<RedisSlave> slaves = active.slaves();
    replicationStore.appendCommands(Unpooled.wrappedBuffer("*3\r\n$3\r\nset\r\n$1\r\na\r\n$5\r\n12 34\r\n\r\n".getBytes()));
    logger.info(remarkableMessage("stop redis master"));
    waitForAnyKey();
    stopRedisMaster();
    printSlaves(slaves);
    logger.info(remarkableMessage("give part commands:" + commands[0].length()));
    waitForAnyKey();
    replicationStore.appendCommands(Unpooled.wrappedBuffer(commands[0].getBytes()));
    logger.info(remarkableMessage("close client connection"));
    waitForAnyKey();
    for (RedisSlave slave : active.slaves()) {
        slave.close();
    }
    System.out.println("give other commands");
    waitForAnyKey();
    for (int i = 1; i < commands.length; i++) {
        replicationStore.appendCommands(Unpooled.wrappedBuffer(commands[i].getBytes()));
    }
    waitForAnyKeyToExit();
}
Also used : RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) RedisSlave(com.ctrip.xpipe.redis.keeper.RedisSlave) Test(org.junit.Test)

Example 3 with RedisSlave

use of com.ctrip.xpipe.redis.keeper.RedisSlave in project x-pipe by ctripcorp.

the class KeeperFailoverTest method testKeeperFailOver.

@Test
public void testKeeperFailOver() throws Exception {
    RedisKeeperServer activeRedisKeeperServer = getRedisKeeperServerActive();
    logger.info("[testKeeperFailOver][active]{}", activeRedisKeeperServer);
    sleep(6000);
    logger.info(remarkableMessage("[testKeeperFailOver][stop activeKeeper]"), activeRedisKeeperServer);
    remove(activeRedisKeeperServer);
    sleep(6000);
    RedisKeeperServer activeNow = getRedisKeeperServerActive();
    logger.info("[testKeeperFailOver][active new]{}", activeNow);
    Assert.assertNotNull(activeNow);
    Assert.assertNotEquals(activeRedisKeeperServer, activeNow);
    Assert.assertEquals(PARTIAL_STATE.PARTIAL, activeNow.getRedisMaster().partialState());
    for (RedisSlave slave : activeNow.slaves()) {
        Assert.assertEquals(PARTIAL_STATE.PARTIAL, slave.partialState());
    }
    sendMessageToMasterAndTestSlaveRedis();
}
Also used : RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) RedisSlave(com.ctrip.xpipe.redis.keeper.RedisSlave) Test(org.junit.Test)

Example 4 with RedisSlave

use of com.ctrip.xpipe.redis.keeper.RedisSlave in project x-pipe by ctripcorp.

the class PsyncHandlerTest method beforePsyncHandlerTest.

@Before
public void beforePsyncHandlerTest() {
    when(redisSlave.getRedisKeeperServer()).thenReturn(redisKeeperServer);
    when(redisKeeperServer.getKeeperRepl()).thenReturn(keeperRepl);
    when(redisKeeperServer.getKeeperMonitor()).thenReturn(new DefaultKeeperMonitor(redisKeeperServer));
    when(redisKeeperServer.getKeeperConfig()).thenReturn(KeeperConfig);
    when(keeperRepl.getBeginOffset()).thenReturn(begin);
    when(keeperRepl.getEndOffset()).thenReturn(end);
    when(keeperRepl.replId()).thenReturn(replId);
    when(keeperRepl.replId2()).thenReturn(replId2);
    when(keeperRepl.secondReplIdOffset()).thenReturn(secondReplOffset);
    when(KeeperConfig.getReplicationStoreMaxCommandsToTransferBeforeCreateRdb()).thenReturn(maxToTransfer);
    psyncHandler = new PsyncHandler() {

        @Override
        protected void doPartialSync(RedisSlave redisSlave, String replId, Long offset) {
            function = PARTIAL;
        }

        @Override
        protected void doFullSync(RedisSlave redisSlave) {
            function = FULL;
        }

        @Override
        protected void waitForoffset(String[] args, RedisSlave redisSlave, String replId, Long offsetRequest) {
            function = WAIT;
        }
    };
}
Also used : DefaultKeeperMonitor(com.ctrip.xpipe.redis.keeper.monitor.impl.DefaultKeeperMonitor) RedisSlave(com.ctrip.xpipe.redis.keeper.RedisSlave) Before(org.junit.Before)

Example 5 with RedisSlave

use of com.ctrip.xpipe.redis.keeper.RedisSlave in project x-pipe by ctripcorp.

the class RedisPromotor method findSlave.

private RedisSlave findSlave(RedisKeeperServer keeper, String actualIp, int actualPort) {
    Set<RedisSlave> slaves = keeper.slaves();
    for (RedisSlave redisSlave : slaves) {
        InetSocketAddress slaveAddr = (InetSocketAddress) redisSlave.channel().remoteAddress();
        String expectedIp = slaveAddr.getAddress().getHostAddress();
        int expectedPort = redisSlave.getSlaveListeningPort();
        if (expectedIp.equals(actualIp) && expectedPort == actualPort) {
            return redisSlave;
        }
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) RedisSlave(com.ctrip.xpipe.redis.keeper.RedisSlave) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint)

Aggregations

RedisSlave (com.ctrip.xpipe.redis.keeper.RedisSlave)14 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)6 Test (org.junit.Test)4 IOException (java.io.IOException)3 XSlaveofJob (com.ctrip.xpipe.redis.meta.server.job.XSlaveofJob)2 PARTIAL_STATE (com.ctrip.xpipe.api.server.PARTIAL_STATE)1 DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)1 NoMasterlinkRedisError (com.ctrip.xpipe.redis.core.protocal.error.NoMasterlinkRedisError)1 RedisErrorParser (com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)1 RedisSlavePromotionException (com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException)1 DefaultKeeperMonitor (com.ctrip.xpipe.redis.keeper.monitor.impl.DefaultKeeperMonitor)1 SlaveofJob (com.ctrip.xpipe.redis.meta.server.job.SlaveofJob)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutionException (java.util.concurrent.ExecutionException)1 Before (org.junit.Before)1