Java Code Examples for redis.clients.jedis.JedisSentinelPool#getResource()

The following examples show how to use redis.clients.jedis.JedisSentinelPool#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testClosingTracedJedisClosesUnderlyingJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis resource = pool.getResource();
  assertEquals(1, pool.getNumActive());

  resource.close();
  assertEquals(0, pool.getNumActive());
  assertEquals(1, pool.getNumIdle());

  // ensure that resource is reused
  Jedis nextResource = pool.getResource();
  assertEquals(1, pool.getNumActive());
  assertEquals(0, pool.getNumIdle());
  nextResource.close();

  pool.destroy();
}
 
Example 2
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testClosingTracedJedisClosesUnderlyingJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis resource = pool.getResource();
  assertEquals(1, pool.getNumActive());

  resource.close();
  assertEquals(0, pool.getNumActive());
  assertEquals(1, pool.getNumIdle());

  // ensure that resource is reused
  Jedis nextResource = pool.getResource();
  assertEquals(1, pool.getNumActive());
  assertEquals(0, pool.getNumIdle());
  nextResource.close();

  pool.destroy();
}
 
Example 3
Source File: RedisSentinelTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkResourceIsCloseable() {
  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();
  try {
    jedis.set("hello", "jedis");
  } finally {
    jedis.close();
  }

  Jedis jedis2 = pool.getResource();
  try {
    assertEquals(jedis, jedis2);
  } finally {
    jedis2.close();
  }
}
 
Example 5
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 6
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
private void forceFailover(JedisSentinelPool pool) throws InterruptedException {
  HostAndPort oldMaster = pool.getCurrentHostMaster();

  // jedis connection should be master
  Jedis beforeFailoverJedis = pool.getResource();
  assertEquals("PONG", beforeFailoverJedis.ping());

  waitForFailover(pool, oldMaster);

  Jedis afterFailoverJedis = pool.getResource();
  assertEquals("PONG", afterFailoverJedis.ping());
  assertEquals("foobared", afterFailoverJedis.configGet("requirepass").get(1));
  assertEquals(2, afterFailoverJedis.getDB());

  // returning both connections to the pool should not throw
  beforeFailoverJedis.close();
  afterFailoverJedis.close();
}
 
Example 7
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelPoolReturnsTracedJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis jedis = pool.getResource();
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  jedis.close();
  pool.destroy();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 8
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelPoolReturnsTracedJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis jedis = pool.getResource();
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  jedis.close();
  pool.destroy();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 9
Source File: RedisSentinelTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
    public void testSentinelExample() {
        JedisSentinelPool sentinelPool = null;

        // 使用默认配置
//        sentinelPool = ClientBuilder.redisSentinel(appId).build();

        /**
         * 自定义配置
         */
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 3);
        poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE * 2);
        poolConfig.setJmxEnabled(true);
        poolConfig.setMaxWaitMillis(3000);

        sentinelPool = ClientBuilder.redisSentinel(appId)
                .setPoolConfig(poolConfig)
                .setConnectionTimeout(2000)
                .setSoTimeout(1000)
                .build();

        Jedis jedis = sentinelPool.getResource();
        jedis.set("key1", "1");
        assertEquals("2", jedis.incr("key1"));
        jedis.close();
    }
 
Example 10
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 11
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@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();
  }
}
 
Example 12
Source File: RedisDeployCenterImpl.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@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(appDesc, sentinelHost, masterName, masterHost, masterPort);
    if (!isRun) {
        return false;
    }
    return true;
}
 
Example 13
Source File: GenericRedisDAOImpl.java    From OpenIoE with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
   	CommunicationFacade communicationFacade = new CommunicationFacadeBean();
	Set<String> sentinels = communicationFacade.requestRedisSentinelsHosts();
   	version = (String) communicationFacade.requestNodeConfigs().get(CommunicationKeysConstants.KEY_VERSION);
	JedisPoolConfig poolConfig = new JedisPoolConfig();
       poolConfig.setMaxTotal(1);
       pool = new JedisSentinelPool(CryptoIntegrationConstants.REDIS_CLUSTER_MASTER_NAME, sentinels, poolConfig);
       jedis = pool.getResource();
   }
 
Example 14
Source File: RedisSentinelJobStoreTest.java    From quartz-redis-jobstore with Apache License 2.0 3 votes vote down vote up
@Before
public void setUpRedis() throws IOException, SchedulerConfigException {
    final List<Integer> sentinels = Arrays.asList(getPort(), getPort());
    final List<Integer> group1 = Arrays.asList(getPort(), getPort());
    final List<Integer> group2 = Arrays.asList(getPort(), getPort());
    //creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
    redisCluster = RedisCluster.builder().sentinelPorts(sentinels).quorumSize(2)
        .serverPorts(group1).replicationGroup("master1", 1)
        .serverPorts(group2).replicationGroup("master2", 1)
        .ephemeralServers().replicationGroup("master3", 1)
        .build();
    redisCluster.start();


    Set<String> jedisSentinelHosts = JedisUtil.sentinelHosts(redisCluster);

    joinedHosts = Joiner.on(",").join(jedisSentinelHosts);

    final short database = 1;
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPoolConfig.setTestOnCreate(true);
    jedisPoolConfig.setTestOnReturn(true);
    jedisPoolConfig.setMaxWaitMillis(2000);
    jedisPoolConfig.setMaxTotal(20);
    jedisPool = new JedisSentinelPool("master1", jedisSentinelHosts, jedisPoolConfig);
    jobStore = new RedisJobStore();
    jobStore.setHost(joinedHosts);
    jobStore.setJedisPool(jedisSentinelPool);
    jobStore.setLockTimeout(2000);
    jobStore.setMasterGroupName("master1");
    jobStore.setRedisSentinel(true);
    jobStore.setInstanceId("testJobStore1");
    jobStore.setDatabase(database);
    mockScheduleSignaler = mock(SchedulerSignaler.class);
    jobStore.initialize(null, mockScheduleSignaler);
    schema = new RedisJobStoreSchema();

    jedis = jedisPool.getResource();
    jedis.flushDB();

}