Search in sources :

Example 86 with HostAndPort

use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.

the class ExceptionsTest method redirection.

@Test
public void redirection() {
    HostAndPort hap = new HostAndPort("", 0);
    int slot = -1;
    try {
        throw new JedisRedirectionException(MESSAGE, hap, slot);
    } catch (Exception e) {
        assertSame(JedisRedirectionException.class, e.getClass());
        assertEquals(MESSAGE, e.getMessage());
        assertNull(e.getCause());
    }
    try {
        throw new JedisRedirectionException(CAUSE, hap, slot);
    } catch (Exception e) {
        assertSame(JedisRedirectionException.class, e.getClass());
        assertEquals(CAUSE, e.getCause());
        assertEquals(CAUSE.toString(), e.getMessage());
    }
    try {
        throw new JedisRedirectionException(MESSAGE, CAUSE, hap, slot);
    } catch (Exception e) {
        assertSame(JedisRedirectionException.class, e.getClass());
        assertEquals(MESSAGE, e.getMessage());
        assertEquals(CAUSE, e.getCause());
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) Test(org.junit.Test)

Example 87 with HostAndPort

use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.

the class ClusterConnectionProvider method initializeSlotsCache.

private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
    ArrayList<HostAndPort> startNodeList = new ArrayList<>(startNodes);
    Collections.shuffle(startNodeList);
    for (HostAndPort hostAndPort : startNodeList) {
        try (Connection jedis = new Connection(hostAndPort, clientConfig)) {
            cache.discoverClusterNodesAndSlots(jedis);
            return;
        } catch (JedisConnectionException e) {
        // try next nodes
        }
    }
    throw new JedisClusterOperationException("Could not initialize cluster slots cache.");
}
Also used : JedisClusterOperationException(redis.clients.jedis.exceptions.JedisClusterOperationException) HostAndPort(redis.clients.jedis.HostAndPort) ArrayList(java.util.ArrayList) Connection(redis.clients.jedis.Connection) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 88 with HostAndPort

use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.

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) FailoverAbortedException(redis.clients.jedis.exceptions.FailoverAbortedException) JedisPubSub(redis.clients.jedis.JedisPubSub)

Example 89 with HostAndPort

use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.

the class FailoverCommandsTest method failoverForce.

@Test
public void failoverForce() throws InterruptedException {
    assumeFalse(failoverStuck);
    try (Jedis master = new Jedis(masterAddress)) {
        switching = true;
        assertEquals("OK", master.failover(FailoverParams.failoverParams().to(new HostAndPort("127.0.0.1", replicaAddress.getPort())).force().timeout(100)));
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) Test(org.junit.Test)

Example 90 with HostAndPort

use of redis.clients.jedis.HostAndPort in project e3mall by colg-cloud.

the class JedisTest method testJedisCluster.

/**
 * redis 集群
 */
@Test
public void testJedisCluster() {
    // 创建一个JedisCluster对象,有一个参数nodes是一个set类型,set中包含若干个HostAndPort对象
    Set<HostAndPort> nodes = new HashSet<>();
    nodes.add(new HostAndPort("192.168.21.103", 7000));
    nodes.add(new HostAndPort("192.168.21.103", 7001));
    nodes.add(new HostAndPort("192.168.21.103", 7002));
    nodes.add(new HostAndPort("192.168.21.103", 7003));
    nodes.add(new HostAndPort("192.168.21.103", 7004));
    nodes.add(new HostAndPort("192.168.21.103", 7005));
    JedisCluster jedisCluster = new JedisCluster(nodes);
    // 直接使用JedisCluster对象操作redis,操作完不用关闭,自带连接池
    jedisCluster.set("test", "123");
    System.out.println(jedisCluster.get("test"));
    // 系统关闭前,关闭JedisCluster对象
    try {
        jedisCluster.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) IOException(java.io.IOException) HashSet(java.util.HashSet) 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