Search in sources :

Example 21 with RedisKeeperServer

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

the class KinfoCommandHandler method doHandle.

@Override
protected void doHandle(String[] args, RedisClient redisClient) {
    RedisKeeperServer keeper = redisClient.getRedisKeeperServer();
    String result = JSON.toJSONString(keeper.getReplicationStore().getMetaStore().dupReplicationStoreMeta());
    logger.info("[doHandle]{}", result);
    redisClient.sendMessage(new BulkStringParser(result).format());
}
Also used : RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) BulkStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser)

Example 22 with RedisKeeperServer

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

the class KeeperContainerService method stop.

public void stop(String clusterId, String shardId) {
    String keeperServerKey = assembleKeeperServerKey(clusterId, shardId);
    RedisKeeperServer keeperServer = redisKeeperServers.get(keeperServerKey);
    if (keeperServer == null) {
        throw new RedisKeeperRuntimeException(new ErrorMessage<>(KeeperContainerErrorCode.KEEPER_NOT_EXIST, String.format("Stop keeper for cluster %s shard %s failed since keeper doesn't exist", clusterId, shardId)), null);
    }
    if (keeperServer.getLifecycleState().isStopped()) {
        throw new RedisKeeperRuntimeException(new ErrorMessage<>(KeeperContainerErrorCode.KEEPER_ALREADY_STOPPED, String.format("Keeper for cluster %s shard %s already stopped", clusterId, shardId)), null);
    }
    try {
        keeperServer.stop();
    } catch (Throwable ex) {
        throw new RedisKeeperRuntimeException(new ErrorMessage<>(KeeperContainerErrorCode.INTERNAL_EXCEPTION, String.format("Stop keeper failed for cluster %s shard %s", clusterId, shardId)), ex);
    }
}
Also used : DefaultRedisKeeperServer(com.ctrip.xpipe.redis.keeper.impl.DefaultRedisKeeperServer) RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) RedisKeeperRuntimeException(com.ctrip.xpipe.redis.keeper.exception.RedisKeeperRuntimeException) ErrorMessage(com.ctrip.xpipe.exception.ErrorMessage)

Example 23 with RedisKeeperServer

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

the class SlaveOfCommandHandler method handleSlaveOf.

protected void handleSlaveOf(String[] args, RedisClient redisClient) {
    if (args[0].equalsIgnoreCase(NO)) {
        /**
         * if reply OK to slaveof no one, then sentinel is found crash
         * because sentinel thinks the keeper is the new master while it is actually not?
         */
        redisClient.sendMessage(new RedisErrorParser("Keeper not allowed to process slaveof command").format());
    } else {
        RedisKeeperServer redisKeeperServer = redisClient.getRedisKeeperServer();
        String host = args[0];
        int port = Integer.parseInt(args[1]);
        if (redisKeeperServer.getRedisKeeperServerState().handleSlaveOf()) {
            logger.info("[handleSlaveOf][slaveof]{}:{} {}", host, port, redisClient);
            redisClient.getRedisKeeperServer().getRedisKeeperServerState().setMasterAddress(new InetSocketAddress(host, port));
        } else {
            logger.info("[handleSlaveOf][slaveof, ignore]{},{}:{} {}", redisKeeperServer.getRedisKeeperServerState(), host, port, redisClient);
        }
        redisClient.sendMessage(SimpleStringParser.OK);
    }
}
Also used : RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) InetSocketAddress(java.net.InetSocketAddress) RedisErrorParser(com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)

Example 24 with RedisKeeperServer

use of com.ctrip.xpipe.redis.keeper.RedisKeeperServer 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)

Example 25 with RedisKeeperServer

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

the class PsyncHandler method doHandle.

@Override
protected void doHandle(final String[] args, final RedisClient redisClient) throws Exception {
    // in non-psync executor
    final RedisKeeperServer redisKeeperServer = redisClient.getRedisKeeperServer();
    if (redisKeeperServer.rdbDumper() == null && redisKeeperServer.getReplicationStore().isFresh()) {
        redisClient.sendMessage(new RedisErrorParser(new NoMasterlinkRedisError("Can't SYNC while replicationstore fresh")).format());
        return;
    }
    if (!redisKeeperServer.getRedisKeeperServerState().psync(redisClient, args)) {
        return;
    }
    final RedisSlave redisSlave = redisClient.becomeSlave();
    if (redisSlave == null) {
        logger.warn("[doHandle][psync client already slave]" + redisClient);
        try {
            redisClient.close();
        } catch (IOException e) {
            logger.error("[doHandle]" + redisClient, e);
        }
        return;
    }
    // transfer to psync executor, which will do psync dedicatedly
    redisSlave.processPsyncSequentially(new Runnable() {

        @Override
        public void run() {
            try {
                innerDoHandle(args, redisSlave, redisKeeperServer);
            } catch (Throwable th) {
                try {
                    logger.error("[run]" + redisClient, th);
                    if (redisSlave.isOpen()) {
                        redisSlave.close();
                    }
                } catch (IOException e) {
                    logger.error("[run][close]" + redisSlave, th);
                }
            }
        }
    });
}
Also used : RedisKeeperServer(com.ctrip.xpipe.redis.keeper.RedisKeeperServer) RedisSlave(com.ctrip.xpipe.redis.keeper.RedisSlave) IOException(java.io.IOException) RedisErrorParser(com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser) NoMasterlinkRedisError(com.ctrip.xpipe.redis.core.protocal.error.NoMasterlinkRedisError)

Aggregations

RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)44 Test (org.junit.Test)23 DefaultRedisKeeperServer (com.ctrip.xpipe.redis.keeper.impl.DefaultRedisKeeperServer)13 RedisSlave (com.ctrip.xpipe.redis.keeper.RedisSlave)6 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)5 ErrorMessage (com.ctrip.xpipe.exception.ErrorMessage)4 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)4 RedisKeeperRuntimeException (com.ctrip.xpipe.redis.keeper.exception.RedisKeeperRuntimeException)4 DefaultReplicationStore (com.ctrip.xpipe.redis.keeper.store.DefaultReplicationStore)4 InMemoryPsync (com.ctrip.xpipe.redis.core.protocal.cmd.InMemoryPsync)3 RedisErrorParser (com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)3 LinkedList (java.util.LinkedList)3 ReplicationStore (com.ctrip.xpipe.redis.core.store.ReplicationStore)2 XSlaveofJob (com.ctrip.xpipe.redis.meta.server.job.XSlaveofJob)2 IOException (java.io.IOException)2 Endpoint (com.ctrip.xpipe.api.endpoint.Endpoint)1 PARTIAL_STATE (com.ctrip.xpipe.api.server.PARTIAL_STATE)1 SERVER_ROLE (com.ctrip.xpipe.api.server.Server.SERVER_ROLE)1 DcMeta (com.ctrip.xpipe.redis.core.entity.DcMeta)1 NoMasterlinkRedisError (com.ctrip.xpipe.redis.core.protocal.error.NoMasterlinkRedisError)1