Search in sources :

Example 1 with SimpleStringParser

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

the class DefaultFullSyncListener method setRdbFileInfo.

@Override
public void setRdbFileInfo(EofType eofType, long rdbOffset) {
    if (logger.isInfoEnabled()) {
        logger.info("[setRdbFileInfo]eofType:" + eofType + ",rdbFileOffset:" + rdbOffset);
    }
    SimpleStringParser simpleStringParser = new SimpleStringParser(StringUtil.join(" ", DefaultPsync.FULL_SYNC, redisSlave.getRedisKeeperServer().getKeeperRepl().replId(), String.valueOf(rdbOffset)));
    logger.info("[setRdbFileInfo]{},{}", simpleStringParser.getPayload(), redisSlave);
    redisSlave.sendMessage(simpleStringParser.format());
    redisSlave.beginWriteRdb(eofType, rdbOffset);
}
Also used : SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)

Example 2 with SimpleStringParser

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

the class KeeperCommandHandler method doHandle.

@Override
protected void doHandle(String[] args, RedisClient redisClient) {
    if (args.length >= 1) {
        if (args[0].equalsIgnoreCase(AbstractKeeperCommand.GET_STATE)) {
            KeeperState keeperState = redisClient.getRedisKeeperServer().getRedisKeeperServerState().keeperState();
            redisClient.sendMessage(new SimpleStringParser(keeperState.toString()).format());
        } else if (args[0].equalsIgnoreCase(AbstractKeeperCommand.SET_STATE)) {
            if (args.length >= 4) {
                KeeperState keeperState = KeeperState.valueOf(args[1]);
                InetSocketAddress masterAddress = new InetSocketAddress(args[2], Integer.parseInt(args[3]));
                doSetKeeperState(redisClient, keeperState, masterAddress);
            } else {
                throw new IllegalArgumentException("setstate argument error:" + StringUtil.join(" ", args));
            }
        } else {
            throw new IllegalStateException("unknown command:" + args[0]);
        }
    }
}
Also used : SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) InetSocketAddress(java.net.InetSocketAddress) KeeperState(com.ctrip.xpipe.redis.core.meta.KeeperState)

Example 3 with SimpleStringParser

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

the class DefaultRedisClient method readCommands.

@Override
public String[] readCommands(ByteBuf byteBuf) {
    while (true) {
        switch(commandState) {
            case READ_SIGN:
                if (!hasDataRead(byteBuf)) {
                    return null;
                }
                int readIndex = byteBuf.readerIndex();
                byte sign = byteBuf.getByte(readIndex);
                if (sign == RedisClientProtocol.ASTERISK_BYTE) {
                    redisClientProtocol = new ArrayParser();
                } else if (sign == '\n') {
                    byteBuf.readByte();
                    return new String[] { "\n" };
                } else {
                    redisClientProtocol = new SimpleStringParser();
                }
                commandState = COMMAND_STATE.READ_COMMANDS;
            case READ_COMMANDS:
                RedisClientProtocol<?> resultParser = redisClientProtocol.read(byteBuf);
                if (resultParser == null) {
                    return null;
                }
                Object result = resultParser.getPayload();
                if (result == null) {
                    return new String[0];
                }
                commandState = COMMAND_STATE.READ_SIGN;
                String[] ret = null;
                if (result instanceof String) {
                    ret = handleString((String) result);
                } else if (result instanceof Object[]) {
                    ret = handleArray((Object[]) result);
                } else {
                    throw new IllegalStateException("unkonw result array:" + result);
                }
                return ret;
            default:
                throw new IllegalStateException("unkonwn state:" + commandState);
        }
    }
}
Also used : SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) ArrayParser(com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser)

Example 4 with SimpleStringParser

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

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

the class PsyncHandler method doPartialSync.

protected void doPartialSync(RedisSlave redisSlave, String replId, Long offset) {
    if (logger.isInfoEnabled()) {
        logger.info("[doPartialSync]" + redisSlave);
    }
    SimpleStringParser simpleStringParser = null;
    if (!redisSlave.capaOf(CAPA.PSYNC2)) {
        simpleStringParser = new SimpleStringParser(DefaultPsync.PARTIAL_SYNC);
    } else {
        simpleStringParser = new SimpleStringParser(String.format("%s %s", DefaultPsync.PARTIAL_SYNC, replId));
    }
    redisSlave.sendMessage(simpleStringParser.format());
    redisSlave.markPsyncProcessed();
    redisSlave.beginWriteCommands(offset);
    redisSlave.partialSync();
    redisSlave.getRedisKeeperServer().getKeeperMonitor().getKeeperStats().increatePartialSync();
}
Also used : SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)

Aggregations

SimpleStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)5 KeeperState (com.ctrip.xpipe.redis.core.meta.KeeperState)1 ArrayParser (com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser)1 RedisErrorParser (com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)1 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)1 RedisKeeperServerState (com.ctrip.xpipe.redis.keeper.RedisKeeperServerState)1 InetSocketAddress (java.net.InetSocketAddress)1