Search in sources :

Example 6 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 7 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 8 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)

Example 9 with JedisSentinelPool

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

the class RedisDeployCenterImpl method addSentinel.

@Override
public boolean addSentinel(long appId, String sentinelHost) {
    AppDesc appDesc = appDao.getAppDescById(appId);
    JedisSentinelPool jedisSentinelPool = redisCenter.getJedisSentinelPool(appDesc);
    if (jedisSentinelPool == null) {
        return false;
    }
    List<InstanceInfo> instanceInfos = instanceDao.getInstListByAppId(appId);
    String masterName = null;
    for (Iterator<InstanceInfo> i = instanceInfos.iterator(); i.hasNext(); ) {
        InstanceInfo instanceInfo = i.next();
        if (instanceInfo.getType() != ConstUtils.CACHE_REDIS_SENTINEL) {
            i.remove();
            continue;
        }
        if (masterName == null && StringUtils.isNotBlank(instanceInfo.getCmd())) {
            masterName = instanceInfo.getCmd();
        }
    }
    Jedis jedis = null;
    String masterHost = null;
    Integer masterPort = null;
    try {
        jedis = jedisSentinelPool.getResource();
        masterHost = jedis.getClient().getHost();
        masterPort = jedis.getClient().getPort();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        jedis.close();
        jedisSentinelPool.destroy();
    }
    boolean isRun = runSentinel(sentinelHost, masterName, masterHost, masterPort, appId);
    if (!isRun) {
        return false;
    }
    return true;
}
Also used : Jedis(redis.clients.jedis.Jedis) AppDesc(com.sohu.cache.entity.AppDesc) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) InstanceInfo(com.sohu.cache.entity.InstanceInfo)

Example 10 with JedisSentinelPool

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

the class RedisSentinelTest method testSentinel.

@Test
public void testSentinel() {
    JedisSentinelPool sentinelPool = ClientBuilder.redisSentinel(appId).setConnectionTimeout(2000).setSoTimeout(1000).build();
    HostAndPort currentHostMaster = sentinelPool.getCurrentHostMaster();
    logger.info("current master: {}", currentHostMaster.toString());
    Jedis jedis = sentinelPool.getResource();
    for (int i = 0; i < 10; i++) {
        jedis.lpush("mylist", "list-" + i);
    }
    jedis.close();
    sentinelPool.destroy();
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) Jedis(redis.clients.jedis.Jedis) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) BaseTest(com.sohu.tv.test.base.BaseTest) 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