Search in sources :

Example 1 with SentinelAdd

use of com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelAdd 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 2 with SentinelAdd

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

the class SentinelCommandTest method testSentinelMaster.

@Test
public void testSentinelMaster() throws InterruptedException, ExecutionException {
    try {
        String addResult = new SentinelAdd(clientPool, masterName, "127.0.0.1", 6379, 3, scheduled).execute().get();
        logger.info("{}", addResult);
    } catch (Exception ignore) {
    }
    try {
        HostPort master = new AbstractSentinelCommand.SentinelMaster(clientPool, scheduled, masterName).execute().get();
        logger.info("[master]{}", master);
        Assert.assertEquals("127.0.0.1", master.getHost());
        Assert.assertEquals(6379, master.getPort());
    } catch (Exception e) {
        logger.error("[testSentinelMaster]", e);
    }
    try {
        new SentinelRemove(clientPool, masterName, scheduled).execute().get();
    } catch (Exception e) {
        logger.error("[testRemove]", e);
    }
}
Also used : SentinelRemove(com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelRemove) HostPort(com.ctrip.xpipe.endpoint.HostPort) SentinelAdd(com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelAdd) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

SentinelAdd (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelAdd)2 ExecutionException (java.util.concurrent.ExecutionException)2 HostPort (com.ctrip.xpipe.endpoint.HostPort)1 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 SentinelRemove (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractSentinelCommand.SentinelRemove)1 AddSentinelException (com.ctrip.xpipe.redis.meta.server.dcchange.exception.AddSentinelException)1 InetSocketAddress (java.net.InetSocketAddress)1 Test (org.junit.Test)1