Search in sources :

Example 1 with KeeperState

use of com.ctrip.xpipe.redis.core.meta.KeeperState in project x-pipe by ctripcorp.

the class DefaultRedisKeeperServer method initKeeperServerState.

private RedisKeeperServerState initKeeperServerState() {
    try {
        ReplicationStore replicationStore = replicationStoreManager.getCurrent();
        if (replicationStore == null) {
            return new RedisKeeperServerStateUnknown(this);
        }
        KeeperState keeperState = replicationStore.getMetaStore().dupReplicationStoreMeta().getKeeperState();
        if (keeperState == null) {
            logger.warn("[initKeeperServerState][keeperState null]");
            return new RedisKeeperServerStateUnknown(this);
        }
        RedisKeeperServerState redisKeeperServerState = null;
        switch(keeperState) {
            case ACTIVE:
                redisKeeperServerState = new RedisKeeperServerStatePreActive(this);
                break;
            case BACKUP:
                redisKeeperServerState = new RedisKeeperServerStatePreBackup(this);
                break;
            case UNKNOWN:
                redisKeeperServerState = new RedisKeeperServerStateUnknown(this);
                break;
            // wrong store state
            case PRE_ACTIVE:
            case PRE_BACKUP:
            default:
                logger.warn("[initKeeperServerState][error state]{}", keeperState);
                redisKeeperServerState = new RedisKeeperServerStateUnknown(this);
                break;
        }
        return redisKeeperServerState;
    } catch (Exception e) {
        logger.error("[initKeeperServerState]" + this, e);
    }
    return new RedisKeeperServerStateUnknown(this);
}
Also used : ReplicationStore(com.ctrip.xpipe.redis.core.store.ReplicationStore) KeeperState(com.ctrip.xpipe.redis.core.meta.KeeperState) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) RedisSlavePromotionException(com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException) IOException(java.io.IOException)

Example 2 with KeeperState

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

use of com.ctrip.xpipe.redis.core.meta.KeeperState in project x-pipe by ctripcorp.

the class DefaultRedisKeeperServer method initReplicationStore.

public synchronized void initReplicationStore(ReplicationStore replicationStore) {
    logger.info("[initReplicationStore]{}", replicationStore);
    RedisKeeperServerState redisKeeperServerState = getRedisKeeperServerState();
    if (redisKeeperServerState != null) {
        KeeperState keeperState = redisKeeperServerState.keeperState();
        try {
            if (keeperState.isActive()) {
                replicationStore.getMetaStore().becomeActive();
            } else if (keeperState.isBackup()) {
                replicationStore.getMetaStore().becomeBackup();
            } else {
                logger.warn("[initReplicationStore][not active and not backup]{}, {}", keeperState, replicationStore);
            }
        } catch (IOException e) {
            logger.error("[initReplicationStore]" + replicationStore, e);
        }
    }
}
Also used : IOException(java.io.IOException) KeeperState(com.ctrip.xpipe.redis.core.meta.KeeperState)

Aggregations

KeeperState (com.ctrip.xpipe.redis.core.meta.KeeperState)3 IOException (java.io.IOException)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 SimpleStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)1 ReplicationStore (com.ctrip.xpipe.redis.core.store.ReplicationStore)1 RedisSlavePromotionException (com.ctrip.xpipe.redis.keeper.exception.RedisSlavePromotionException)1 InetSocketAddress (java.net.InetSocketAddress)1