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);
}
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]);
}
}
}
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);
}
}
}
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());
}
}
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();
}
Aggregations