Search in sources :

Example 1 with BulkStringParser

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

the class SlaveOfCommandHandler method handleSelf.

@SuppressWarnings("unused")
private void handleSelf(String[] args, RedisClient redisClient) {
    if (args[0].equalsIgnoreCase(NO)) {
        // 
        logger.info("[handleSelf][promote self to master]");
        redisClient.getRedisKeeperServer().stopAndDisposeMaster();
    } else {
        logger.info("[handleSelf][slaveof]{}", StringUtil.join(" ", args));
        redisClient.getRedisKeeperServer().getRedisKeeperServerState().setMasterAddress(new InetSocketAddress(args[0], Integer.parseInt(args[1])));
    }
    redisClient.sendMessage(new BulkStringParser(RedisProtocol.OK).format());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BulkStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser)

Example 2 with BulkStringParser

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

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

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

the class FakeRedisServerAction method handleFullSync.

private void handleFullSync(final OutputStream ous) throws IOException, InterruptedException {
    logger.info("[handleFullSync]{}", getSocket());
    fakeRedisServer.reGenerateRdb();
    fakeRedisServer.addCommandsListener(this);
    try {
        Thread.sleep(fakeRedisServer.getSleepBeforeSendFullSyncInfo());
    } catch (InterruptedException e) {
    }
    String info = String.format("+%s %s %d\r\n", AbstractPsync.FULL_SYNC, fakeRedisServer.getRunId(), fakeRedisServer.getRdbOffset());
    ous.write(info.getBytes());
    ous.flush();
    ScheduledFuture<?> future = null;
    if (fakeRedisServer.isSendLFBeforeSendRdb()) {
        future = sendLfToSlave(ous);
    }
    try {
        Thread.sleep(fakeRedisServer.getSleepBeforeSendRdb());
    } catch (InterruptedException e) {
    }
    if (future != null) {
        future.cancel(true);
    }
    byte[] rdb = null;
    int rdbStartPos = 0;
    String rdbContent = fakeRedisServer.getRdbContent();
    if (fakeRedisServer.isEof()) {
        String mark = RunidGenerator.DEFAULT.generateRunid();
        String content = "$EOF:" + mark + "\r\n";
        rdbStartPos = content.length();
        content += rdbContent + mark;
        rdb = content.getBytes();
        waitAckToSendCommands = true;
    } else {
        BulkStringParser bulkStringParser = new BulkStringParser(rdbContent);
        ByteBuf byteBuf = bulkStringParser.format();
        rdb = ByteBufUtils.readToBytes(byteBuf);
        rdbStartPos = 3 + String.valueOf(rdbContent.length()).length();
        waitAckToSendCommands = false;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("[handleFullSync]{}, {}", getSocket(), new String(rdb));
    }
    if (fakeRedisServer.getAndDecreaseSendHalfRdbAndCloseConnectionCount() > 0) {
        ous.write(rdb, 0, rdbStartPos + rdbContent.length() / 2);
        ous.flush();
        ous.close();
    } else {
        ous.write(rdb);
        ous.flush();
    }
    if (!waitAckToSendCommands) {
        writeCommands(ous);
    }
}
Also used : BulkStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with BulkStringParser

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

the class AbstractReplicationStorePsync method createRdbReader.

@Override
protected BulkStringParser createRdbReader() {
    inOutPayloadReplicationStore = new InOutPayloadReplicationStore();
    BulkStringParser rdbReader = new BulkStringParser(inOutPayloadReplicationStore);
    return rdbReader;
}
Also used : BulkStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser)

Aggregations

BulkStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringParser)5 RedisErrorParser (com.ctrip.xpipe.redis.core.protocal.protocal.RedisErrorParser)1 RedisKeeperServer (com.ctrip.xpipe.redis.keeper.RedisKeeperServer)1 RedisSlavePromotionException (com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException)1 ByteBuf (io.netty.buffer.ByteBuf)1 InetSocketAddress (java.net.InetSocketAddress)1