Search in sources :

Example 1 with RedisErrorParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser in project x-pipe by ctripcorp.

the class CommandHandlerManager method doHandle.

@Override
protected void doHandle(final String[] args, final RedisClient redisClient) {
    if (args.length == 0) {
        logger.error("[doHandle][arg length]{}", redisClient);
        return;
    }
    redisClient.getRedisKeeperServer().processCommandSequentially(new Runnable() {

        @Override
        public void run() {
            try {
                CommandHandler handler = handlers.get(args[0].toLowerCase());
                if (handler == null) {
                    logger.error("[doHandler][no handler found]{}, {}", redisClient, StringUtil.join(" ", args));
                    redisClient.sendMessage(new RedisErrorParser("unsupported command:" + args[0]).format());
                    return;
                }
                innerDoHandle(args, redisClient, handler);
            } catch (Exception e) {
                logger.error("Error process command {} for client {}", Arrays.asList(args), redisClient, e);
                redisClient.sendMessage(new RedisErrorParser(e.getMessage()).format());
            }
        }

        @Override
        public String toString() {
            return String.format("%s, %s", redisClient, StringUtil.join(" ", args));
        }
    });
}
Also used : CommandHandler(com.ctrip.xpipe.redis.keeper.CommandHandler) RedisErrorParser(com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)

Example 2 with RedisErrorParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser 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 3 with RedisErrorParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser in project x-pipe by ctripcorp.

the class SlaveOfCommandHandler method handleForwarding.

private void handleForwarding(String[] args, RedisClient redisClient) {
    if (args[0].equalsIgnoreCase(NO)) {
        String ip = args[2];
        // already validated
        int port = Integer.parseInt(args[3]);
        try {
            redisClient.getRedisKeeperServer().promoteSlave(ip, port);
            redisClient.sendMessage(new BulkStringParser(RedisProtocol.OK).format());
            return;
        } catch (RedisSlavePromotionException e) {
            logger.error("[doHandle]{},{},{}", redisClient, ip, port);
            redisClient.sendMessage(new RedisErrorParser(e.getMessage()).format());
        }
    }
}
Also used : BulkStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser) RedisSlavePromotionException(com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException) RedisErrorParser(com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)

Example 4 with RedisErrorParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser 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 5 with RedisErrorParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser 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

RedisErrorParser (com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)5 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)3 NoMasterlinkRedisError (com.ctrip.xpipe.redis.core.protocal.error.NoMasterlinkRedisError)1 BulkStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser)1 SimpleStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)1 CommandHandler (com.ctrip.xpipe.redis.keeper.CommandHandler)1 RedisKeeperServerState (com.ctrip.xpipe.redis.keeper.RedisKeeperServerState)1 RedisSlave (com.ctrip.xpipe.redis.keeper.RedisSlave)1 RedisSlavePromotionException (com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1