Search in sources :

Example 21 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class RedisPromotor method promoteSlaveToMaster.

private void promoteSlaveToMaster(RedisSlave redisSlave) throws Exception {
    SimpleObjectPool<NettyClient> fsyncPool = null;
    SimpleObjectPool<NettyClient> clientPool = null;
    try {
        fsyncPool = NettyPoolUtil.createNettyPool(new InetSocketAddress(promoteServerIp, promoteServerPort));
        clientPool = NettyPoolUtil.createNettyPool(new InetSocketAddress(promoteServerIp, promoteServerPort));
        waitUntilSlaveSync(redisSlave, this.promoteServerIp, this.promoteServerPort, waitTimeoutMilli);
        try {
            redisKeeperServer.getRedisKeeperServerState().setPromotionState(PROMOTION_STATE.BEGIN_PROMOTE_SLAVE);
            Fsync fsyncCmd = new Fsync(fsyncPool, scheduled);
            String fsyncResult = fsyncCmd.execute().get();
            logger.info("[promoteSlaveToMaster][fsync done]{}, {},{}", fsyncResult, promoteServerIp, promoteServerPort);
            redisModified(redisSlave, clientPool);
        } catch (ExecutionException e) {
            logger.error("[promoteSlaveToMaster]" + redisSlave, e.getCause());
            if (e.getCause() instanceof RedisError) {
                logger.info("[promoteSlaveToMaster][fsync not supported, raw redis]{}", redisSlave);
                redisNotModified(redisSlave, clientPool);
            } else {
                logger.error("[promoteSlaveToMaster][fail]" + redisSlave);
            }
        }
    } finally {
        if (fsyncPool != null) {
            fsyncPool.clear();
        }
        if (clientPool != null) {
            clientPool.clear();
        }
    }
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) Fsync(com.ctrip.xpipe.redis.core.protocal.cmd.Fsync) RedisError(com.ctrip.xpipe.redis.core.protocal.error.RedisError) InetSocketAddress(java.net.InetSocketAddress) ExecutionException(java.util.concurrent.ExecutionException)

Example 22 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class DefaultSentinelManager method getRealSentinels.

@VisibleForTesting
protected List<Sentinel> getRealSentinels(List<InetSocketAddress> sentinels, String sentinelMonitorName) {
    List<Sentinel> realSentinels = null;
    for (InetSocketAddress sentinelAddress : sentinels) {
        SimpleObjectPool<NettyClient> clientPool = keyedClientPool.getKeyPool(sentinelAddress);
        AbstractSentinelCommand.Sentinels sentinelsCommand = new AbstractSentinelCommand.Sentinels(clientPool, sentinelMonitorName, scheduled);
        try {
            realSentinels = sentinelsCommand.execute().get();
            logger.info("[getRealSentinels]get sentinels from {} : {}", sentinelAddress, realSentinels);
            if (realSentinels.size() > 0) {
                realSentinels.add(new Sentinel(sentinelAddress.toString(), sentinelAddress.getHostString(), sentinelAddress.getPort()));
                logger.info("[getRealSentinels]sentinels for monitor {}, list as: {}", sentinelMonitorName, realSentinels);
                break;
            }
        } catch (Exception e) {
            logger.warn("[getRealSentinels]get sentinels from " + sentinelAddress, e);
        }
    }
    return realSentinels;
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) Sentinel(com.ctrip.xpipe.redis.core.protocal.pojo.Sentinel) InetSocketAddress(java.net.InetSocketAddress) AbstractSentinelCommand(com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand) VisibleForTesting(com.ctrip.xpipe.utils.VisibleForTesting)

Example 23 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class DefaultSentinelManager method infoSentinel.

@Override
public String infoSentinel(Sentinel sentinel) {
    SimpleObjectPool<NettyClient> clientPool = keyedClientPool.getKeyPool(new InetSocketAddress(sentinel.getIp(), sentinel.getPort()));
    InfoCommand infoCommand = new InfoCommand(clientPool, InfoCommand.INFO_TYPE.SENTINEL, scheduled);
    infoCommand.logResponse(false);
    try {
        return infoCommand.execute().get();
    } catch (Exception e) {
        logger.error("[infoSentinel] " + sentinel, e);
    }
    return null;
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) InetSocketAddress(java.net.InetSocketAddress) InfoCommand(com.ctrip.xpipe.redis.core.protocal.cmd.InfoCommand)

Example 24 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class DefaultSentinelManager method addSentinel.

@Override
public void addSentinel(String clusterId, String shardId, HostPort redisMaster, ExecutionLog executionLog) {
    String sentinelMonitorName = dcMetaCache.getSentinelMonitorName(clusterId, shardId);
    String allSentinels = dcMetaCache.getSentinel(clusterId, shardId).getAddress();
    executionLog.info(String.format("[addSentinel]%s,%s,%s, monitorName:%s, master:%s:%d", clusterId, shardId, allSentinels, sentinelMonitorName, redisMaster.getHost(), redisMaster.getPort()));
    if (checkEmpty(sentinelMonitorName, allSentinels, executionLog)) {
        return;
    }
    int quorum = DEFAULT_SENTINEL_QUORUM;
    List<InetSocketAddress> sentinels = IpUtils.parse(allSentinels);
    if (sentinels.size() < quorum) {
        throw new IllegalStateException(String.format("sentinel size < quorum, %d < %d", sentinels.size(), quorum));
    }
    int addSize = Math.min(sentinels.size(), DEFAULT_SENTINEL_ADD_SIZE);
    for (int i = 0; i < addSize; i++) {
        InetSocketAddress sentinelAddress = sentinels.get(i);
        SimpleObjectPool<NettyClient> clientPool = keyedClientPool.getKeyPool(sentinelAddress);
        SentinelAdd command = new SentinelAdd(clientPool, sentinelMonitorName, redisMaster.getHost(), redisMaster.getPort(), quorum, scheduled);
        try {
            String result = command.execute().get();
            executionLog.info(String.format("add to sentinel %s : %s", sentinelAddress, result));
        } catch (InterruptedException | ExecutionException e) {
            throw new AddSentinelException(sentinelAddress, clusterId, shardId, redisMaster.getHost(), redisMaster.getPort(), e);
        }
    }
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) InetSocketAddress(java.net.InetSocketAddress) SentinelAdd(com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelAdd) AddSentinelException(com.ctrip.xpipe.redis.meta.server.dcchange.exception.AddSentinelException) ExecutionException(java.util.concurrent.ExecutionException)

Example 25 with NettyClient

use of com.ctrip.xpipe.netty.commands.NettyClient in project x-pipe by ctripcorp.

the class DefaultSentinelManager method removeSentinel.

@Override
public void removeSentinel(String clusterId, String shardId, ExecutionLog executionLog) {
    String sentinelMonitorName = dcMetaCache.getSentinelMonitorName(clusterId, shardId);
    String allSentinels = dcMetaCache.getSentinel(clusterId, shardId).getAddress();
    executionLog.info(String.format("removeSentinel cluster:%s, shard:%s, masterName:%s, sentinelAddress:%s", clusterId, shardId, sentinelMonitorName, allSentinels));
    if (checkEmpty(sentinelMonitorName, allSentinels, executionLog)) {
        return;
    }
    List<InetSocketAddress> sentinels = IpUtils.parse(allSentinels);
    List<Sentinel> realSentinels = getRealSentinels(sentinels, sentinelMonitorName, executionLog);
    if (realSentinels == null) {
        executionLog.warn("get real sentinels null");
        return;
    }
    executionLog.info(String.format("removeSentinel realSentinels:%s", realSentinels));
    for (Sentinel sentinel : realSentinels) {
        SimpleObjectPool<NettyClient> clientPool = keyedClientPool.getKeyPool(new InetSocketAddress(sentinel.getIp(), sentinel.getPort()));
        SentinelRemove sentinelRemove = new SentinelRemove(clientPool, sentinelMonitorName, scheduled);
        try {
            String result = sentinelRemove.execute().get();
            executionLog.info(String.format("removeSentinel %s from %s : %s", sentinelMonitorName, sentinel, result));
        } catch (InterruptedException | ExecutionException e) {
            executionLog.info(String.format("removeSentinel %s from %s : %s", sentinelMonitorName, sentinel, e.getMessage()));
            logger.warn("[removeSentinel]" + sentinel, e);
        }
    }
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) Sentinel(com.ctrip.xpipe.redis.core.protocal.pojo.Sentinel) InetSocketAddress(java.net.InetSocketAddress) SentinelRemove(com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelRemove) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)37 InetSocketAddress (java.net.InetSocketAddress)31 Test (org.junit.Test)8 ExecutionException (java.util.concurrent.ExecutionException)6 AbstractTest (com.ctrip.xpipe.AbstractTest)4 InfoCommand (com.ctrip.xpipe.redis.core.protocal.cmd.InfoCommand)4 Server (com.ctrip.xpipe.simpleserver.Server)4 IOException (java.io.IOException)4 CommandExecutionException (com.ctrip.xpipe.command.CommandExecutionException)3 SequenceCommandChain (com.ctrip.xpipe.command.SequenceCommandChain)3 FixedObjectPool (com.ctrip.xpipe.pool.FixedObjectPool)3 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)3 AbstractSentinelCommand (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand)3 Replconf (com.ctrip.xpipe.redis.core.protocal.cmd.Replconf)3 DefaultEndPoint (com.ctrip.xpipe.endpoint.DefaultEndPoint)2 XpipeException (com.ctrip.xpipe.exception.XpipeException)2 NettyClientFactory (com.ctrip.xpipe.netty.commands.NettyClientFactory)2 XpipeObjectPoolFromKeyed (com.ctrip.xpipe.pool.XpipeObjectPoolFromKeyed)2 DefaultSlaveOfCommand (com.ctrip.xpipe.redis.core.protocal.cmd.DefaultSlaveOfCommand)2 InMemoryPsync (com.ctrip.xpipe.redis.core.protocal.cmd.InMemoryPsync)2