Search in sources :

Example 1 with RedisKeeperServerState

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

the class XRedisPartialTest method createRedisKeeperServer.

@Override
protected RedisKeeperServer createRedisKeeperServer(KeeperMeta keeperMeta, File baseDir, KeeperConfig keeperConfig, MetaServerKeeperService metaService, LeaderElectorManager leaderElectorManager, KeepersMonitorManager keeperMonitorManager) {
    return new DefaultRedisKeeperServer(keeperMeta, keeperConfig, baseDir, metaService, leaderElectorManager, keeperMonitorManager) {

        private int count = 0;

        @Override
        public RedisKeeperServerState getRedisKeeperServerState() {
            RedisKeeperServerState redisKeeperServerState = super.getRedisKeeperServerState();
            return (RedisKeeperServerState) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { RedisKeeperServerState.class }, new InvocationHandler() {

                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getName().equals("psync")) {
                        count++;
                        logger.info("[psync][count]{}, {}", args, count);
                        if (count == 2) {
                            RedisClient redisClient = (RedisClient) args[0];
                            logger.info("[psync][close client]{}, {}", count, redisClient);
                            redisClient.close();
                        }
                    }
                    return method.invoke(redisKeeperServerState, args);
                }
            });
        }
    };
}
Also used : RedisKeeperServerState(com.ctrip.xpipe.redis.keeper.RedisKeeperServerState) RedisClient(com.ctrip.xpipe.redis.keeper.RedisClient) DefaultRedisKeeperServer(com.ctrip.xpipe.redis.keeper.impl.DefaultRedisKeeperServer) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 2 with RedisKeeperServerState

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

the class RedisKeeperServerStateUnknownTest method testActive.

@Test
public void testActive() throws IOException {
    // active
    KeeperMeta keeperMeta = redisKeeperServer.getCurrentKeeperMeta();
    ShardStatus shardStatus = createShardStatus(keeperMeta, null, redisMasterMeta);
    unknown.setShardStatus(shardStatus);
    RedisKeeperServerState newState = redisKeeperServer.getRedisKeeperServerState();
    Assert.assertTrue(newState instanceof RedisKeeperServerStateActive);
    Assert.assertEquals(new InetSocketAddress(redisMasterMeta.getIp(), redisMasterMeta.getPort()), newState.getMaster().getSocketAddress());
}
Also used : RedisKeeperServerState(com.ctrip.xpipe.redis.keeper.RedisKeeperServerState) ShardStatus(com.ctrip.xpipe.redis.core.meta.ShardStatus) InetSocketAddress(java.net.InetSocketAddress) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test)

Example 3 with RedisKeeperServerState

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

the class RedisKeeperServerStateUnknownTest method testBackup.

@Test
public void testBackup() throws IOException {
    // active
    KeeperMeta keeperMeta = SerializationUtils.clone(redisKeeperServer.getCurrentKeeperMeta());
    keeperMeta.setPort(keeperMeta.getPort() + 1);
    ShardStatus shardStatus = createShardStatus(keeperMeta, null, redisMasterMeta);
    unknown.setShardStatus(shardStatus);
    RedisKeeperServerState newState = redisKeeperServer.getRedisKeeperServerState();
    Assert.assertTrue(newState instanceof RedisKeeperServerStateBackup);
    Assert.assertEquals(new InetSocketAddress(keeperMeta.getIp(), keeperMeta.getPort()), newState.getMaster().getSocketAddress());
}
Also used : RedisKeeperServerState(com.ctrip.xpipe.redis.keeper.RedisKeeperServerState) ShardStatus(com.ctrip.xpipe.redis.core.meta.ShardStatus) InetSocketAddress(java.net.InetSocketAddress) KeeperMeta(com.ctrip.xpipe.redis.core.entity.KeeperMeta) Test(org.junit.Test)

Example 4 with RedisKeeperServerState

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

the class KeeperCommandHandler method doSetKeeperState.

private void doSetKeeperState(RedisClient redisClient, KeeperState keeperState, InetSocketAddress masterAddress) {
    RedisKeeperServer redisKeeperServer = redisClient.getRedisKeeperServer();
    RedisKeeperServerState currentState = redisKeeperServer.getRedisKeeperServerState();
    try {
        switch(keeperState) {
            case ACTIVE:
                currentState.becomeActive(masterAddress);
                break;
            case BACKUP:
                currentState.becomeBackup(masterAddress);
                break;
            case UNKNOWN:
                throw new IllegalStateException("state can not change to unknown!");
            default:
                throw new IllegalStateException("unrecognised state:" + keeperState);
        }
        redisClient.sendMessage(new SimpleStringParser(RedisProtocol.OK).format());
    } catch (Exception e) {
        logger.error("[doSetKeeperState]" + String.format("%s, %s, %s", redisClient, keeperState, masterAddress), e);
        redisClient.sendMessage(new RedisErrorParser(e.getMessage()).format());
    }
}
Also used : RedisKeeperServerState(com.ctrip.xpipe.redis.keeper.RedisKeeperServerState) RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) RedisErrorParser(com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)

Aggregations

RedisKeeperServerState (com.ctrip.xpipe.redis.keeper.RedisKeeperServerState)4 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)2 ShardStatus (com.ctrip.xpipe.redis.core.meta.ShardStatus)2 InetSocketAddress (java.net.InetSocketAddress)2 Test (org.junit.Test)2 RedisErrorParser (com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)1 SimpleStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)1 RedisClient (com.ctrip.xpipe.redis.keeper.RedisClient)1 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)1 DefaultRedisKeeperServer (com.ctrip.xpipe.redis.keeper.impl.DefaultRedisKeeperServer)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1