Search in sources :

Example 76 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class JedisSentinelTestUtil method waitForNewPromotedMaster.

public static HostAndPort waitForNewPromotedMaster(final String masterName, final Jedis sentinelJedis, final Jedis commandJedis) throws InterruptedException {
    final AtomicReference<String> newmaster = new AtomicReference<String>("");
    sentinelJedis.psubscribe(new JedisPubSub() {

        @Override
        public void onPMessage(String pattern, String channel, String message) {
            if (channel.equals("+switch-master")) {
                newmaster.set(message);
                punsubscribe();
            } else if (channel.startsWith("-failover-abort")) {
                punsubscribe();
                throw new FailoverAbortedException("Unfortunately sentinel cannot failover... reason(channel) : " + channel + " / message : " + message);
            }
        }

        @Override
        public void onPSubscribe(String pattern, int subscribedChannels) {
            commandJedis.sentinelFailover(masterName);
        }
    }, "*");
    String[] chunks = newmaster.get().split(" ");
    HostAndPort newMaster = new HostAndPort(chunks[3], Integer.parseInt(chunks[4]));
    return newMaster;
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) JedisPubSub(redis.clients.jedis.JedisPubSub)

Example 77 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class AppServiceImpl method getAppInstanceInfo.

@Override
public List<InstanceInfo> getAppInstanceInfo(Long appId) {
    AppDesc appDesc = appDao.getAppDescById(appId);
    String password = appDesc.getPassword();
    List<InstanceInfo> resultList = instanceDao.getInstListByAppId(appId);
    if (resultList != null && resultList.size() > 0) {
        for (InstanceInfo instanceInfo : resultList) {
            int type = instanceInfo.getType();
            if (instanceInfo.getStatus() != InstanceStatusEnum.GOOD_STATUS.getStatus()) {
                continue;
            }
            if (TypeUtil.isRedisType(type)) {
                if (TypeUtil.isRedisSentinel(type)) {
                    continue;
                }
                String host = instanceInfo.getIp();
                int port = instanceInfo.getPort();
                Boolean isMaster = redisCenter.isMaster(appId, host, port);
                instanceInfo.setRoleDesc(isMaster);
                if (isMaster != null && !isMaster) {
                    HostAndPort hap = redisCenter.getMaster(host, port, password);
                    if (hap != null) {
                        instanceInfo.setMasterHost(hap.getHost());
                        instanceInfo.setMasterPort(hap.getPort());
                        for (InstanceInfo innerInfo : resultList) {
                            if (innerInfo.getIp().equals(hap.getHost()) && innerInfo.getPort() == hap.getPort()) {
                                instanceInfo.setMasterInstanceId(innerInfo.getId());
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    return resultList;
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort)

Example 78 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class JedisClusterNodeInformationParserTest method testParseNodeMyself.

@Test
public void testParseNodeMyself() {
    String nodeInfo = "9b0d2ab38ee31482c95fdb2c7847a0d40e88d518 :7379 myself,master - 0 0 1 connected 0-5460";
    HostAndPort current = new HostAndPort("localhost", 7379);
    ClusterNodeInformation clusterNodeInfo = parser.parse(nodeInfo, current);
    assertEquals(clusterNodeInfo.getNode(), current);
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) ClusterNodeInformation(redis.clients.util.ClusterNodeInformation) Test(org.junit.Test)

Example 79 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class JedisSentinelTest method sentinelFailover.

@Test
public void sentinelFailover() throws InterruptedException {
    Jedis j = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort());
    Jedis j2 = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort());
    try {
        List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
        HostAndPort currentMaster = new HostAndPort(masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort.get(1)));
        JedisSentinelTestUtil.waitForNewPromotedMaster(FAILOVER_MASTER_NAME, j, j2);
        masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
        HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort.get(1)));
        assertNotEquals(newMaster, currentMaster);
    } finally {
        j.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) Test(org.junit.Test)

Example 80 with HostAndPort

use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.

the class JedisSentinelTest method sentinel.

@Test
public void sentinel() {
    Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
    try {
        List<Map<String, String>> masters = j.sentinelMasters();
        boolean inMasters = false;
        for (Map<String, String> master : masters) if (MASTER_NAME.equals(master.get("name")))
            inMasters = true;
        assertTrue(inMasters);
        List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(MASTER_NAME);
        HostAndPort masterFromSentinel = new HostAndPort(masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort.get(1)));
        assertEquals(master, masterFromSentinel);
        List<Map<String, String>> slaves = j.sentinelSlaves(MASTER_NAME);
        assertTrue(slaves.size() > 0);
        assertEquals(master.getPort(), Integer.parseInt(slaves.get(0).get("master-port")));
        // DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
        assertEquals(Long.valueOf(1), j.sentinelReset(MASTER_NAME));
        assertEquals(Long.valueOf(0), j.sentinelReset("woof" + MASTER_NAME));
    } finally {
        j.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

HostAndPort (redis.clients.jedis.HostAndPort)102 Test (org.junit.Test)42 HashSet (java.util.HashSet)35 JedisCluster (redis.clients.jedis.JedisCluster)35 Jedis (redis.clients.jedis.Jedis)25 LinkedHashSet (java.util.LinkedHashSet)18 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)12 Before (org.junit.Before)9 JedisPool (redis.clients.jedis.JedisPool)9 ArrayList (java.util.ArrayList)8 ClusterNodeInformation (redis.clients.util.ClusterNodeInformation)6 AppDesc (com.sohu.cache.entity.AppDesc)5 InstanceInfo (com.sohu.cache.entity.InstanceInfo)5 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)5 IOException (java.io.IOException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 JedisPubSub (redis.clients.jedis.JedisPubSub)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Properties (java.util.Properties)3