Search in sources :

Example 1 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

the class RedisSentinelBuilder method build.

public JedisSentinelPool build() {
    if (sentinelPool == null) {
        while (true) {
            try {
                LOCK.tryLock(10, TimeUnit.MILLISECONDS);
                if (sentinelPool == null) {
                    /**
                         * http请求返回的结果是空的;
                         */
                    String response = HttpUtils.doGet(String.format(ConstUtils.REDIS_SENTINEL_URL, appId));
                    if (response == null || response.isEmpty()) {
                        logger.warn("get response from remote server error, appId: {}, continue...", appId);
                        continue;
                    }
                    /**
                         * http请求返回的结果是无效的;
                         */
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode heartbeatInfo = null;
                    try {
                        heartbeatInfo = mapper.readTree(response);
                    } catch (Exception e) {
                        logger.error("heartbeat error, appId: {}. continue...", appId, e);
                    }
                    if (heartbeatInfo == null) {
                        logger.error("get sentinel info for appId: {} error. continue...", appId);
                        continue;
                    }
                    /** 检查客户端版本 **/
                    if (heartbeatInfo.get("status").intValue() == ClientStatusEnum.ERROR.getStatus()) {
                        throw new IllegalStateException(heartbeatInfo.get("message").textValue());
                    } else if (heartbeatInfo.get("status").intValue() == ClientStatusEnum.WARN.getStatus()) {
                        logger.warn(heartbeatInfo.get("message").textValue());
                    } else {
                        logger.info(heartbeatInfo.get("message").textValue());
                    }
                    /**
                         * 有效的请求:取出masterName和sentinels,并创建JedisSentinelPool的实例;
                         */
                    String masterName = heartbeatInfo.get("masterName").asText();
                    String sentinels = heartbeatInfo.get("sentinels").asText();
                    Set<String> sentinelSet = new HashSet<String>();
                    for (String sentinelStr : sentinels.split(" ")) {
                        String[] sentinelArr = sentinelStr.split(":");
                        if (sentinelArr.length == 2) {
                            sentinelSet.add(sentinelStr);
                        }
                    }
                    //收集上报数据
                    //                        ClientDataCollectReportExecutor.getInstance();
                    sentinelPool = new JedisSentinelPool(masterName, sentinelSet, poolConfig, connectionTimeout, soTimeout, null, Protocol.DEFAULT_DATABASE);
                    return sentinelPool;
                }
            } catch (Throwable e) {
                //容错
                logger.error("error in build, appId: {}", appId, e);
            } finally {
                LOCK.unlock();
            }
            try {
                //活锁
                TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return sentinelPool;
}
Also used : Random(java.util.Random) JsonNode(com.fasterxml.jackson.databind.JsonNode) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 2 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

the class JedisSentinelPoolTest method initializeWithNotAvailableSentinelsShouldThrowException.

@Test(expected = JedisConnectionException.class)
public void initializeWithNotAvailableSentinelsShouldThrowException() {
    Set<String> wrongSentinels = new HashSet<String>();
    wrongSentinels.add(new HostAndPort("localhost", 65432).toString());
    wrongSentinels.add(new HostAndPort("localhost", 65431).toString());
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
    pool.destroy();
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

the class JedisSentinelPoolTest method customClientName.

@Test
public void customClientName() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 0, "my_shiny_client_name");
    Jedis jedis = pool.getResource();
    try {
        assertEquals("my_shiny_client_name", jedis.clientGetname());
    } finally {
        jedis.close();
        pool.destroy();
    }
    assertTrue(pool.isClosed());
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) Test(org.junit.Test)

Example 4 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

the class JedisSentinelPoolTest method checkCloseableConnections.

@Test
public void checkCloseableConnections() throws Exception {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
    Jedis jedis = pool.getResource();
    jedis.auth("foobared");
    jedis.set("foo", "bar");
    assertEquals("bar", jedis.get("foo"));
    jedis.close();
    pool.close();
    assertTrue(pool.isClosed());
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) Test(org.junit.Test)

Example 5 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

the class JedisSentinelPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
    Jedis jedis = pool.getResource();
    Jedis jedis2 = null;
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
        jedis.close();
        jedis2 = pool.getResource();
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } catch (JedisConnectionException e) {
        if (jedis2 != null) {
            jedis2 = null;
        }
    } finally {
        jedis2.close();
        pool.destroy();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Aggregations

JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)19 Test (org.junit.Test)17 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)12 Jedis (redis.clients.jedis.Jedis)11 HashSet (java.util.HashSet)4 HostAndPort (redis.clients.jedis.HostAndPort)3 BaseTest (com.sohu.tv.test.base.BaseTest)2 Transaction (redis.clients.jedis.Transaction)2 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)2 LoadingCacheTest (com.alicp.jetcache.LoadingCacheTest)1 RefreshCacheTest (com.alicp.jetcache.RefreshCacheTest)1 AbstractExternalCacheTest (com.alicp.jetcache.test.external.AbstractExternalCacheTest)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AppDesc (com.sohu.cache.entity.AppDesc)1 InstanceInfo (com.sohu.cache.entity.InstanceInfo)1 Random (java.util.Random)1