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