Search in sources :

Example 11 with JedisCluster

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

the class ClusterScriptingCommandsTest method setUp.

@Before
public void setUp() throws InterruptedException {
    node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
    node1.auth("cluster");
    node1.flushAll();
    node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
    node2.auth("cluster");
    node2.flushAll();
    node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
    node3.auth("cluster");
    node3.flushAll();
    // ---- configure cluster
    // add nodes to cluster
    node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
    node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
    // split available slots across the three nodes
    int slotsPerNode = JedisCluster.HASHSLOTS / 3;
    int[] node1Slots = new int[slotsPerNode];
    int[] node2Slots = new int[slotsPerNode + 1];
    int[] node3Slots = new int[slotsPerNode];
    for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) {
        if (i < slotsPerNode) {
            node1Slots[slot1++] = i;
        } else if (i > slotsPerNode * 2) {
            node3Slots[slot3++] = i;
        } else {
            node2Slots[slot2++] = i;
        }
    }
    node1.clusterAddSlots(node1Slots);
    node2.clusterAddSlots(node2Slots);
    node3.clusterAddSlots(node3Slots);
    waitForClusterReady();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    jedisCluster = new JedisCluster(jedisClusterNode, 2000, 2000, 5, "cluster", new JedisPoolConfig());
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Before(org.junit.Before)

Example 12 with JedisCluster

use of redis.clients.jedis.JedisCluster in project storm by apache.

the class TestRedisDataSourcesProvider method testRedisClusterSink.

@SuppressWarnings("unchecked")
@Test
public void testRedisClusterSink() throws IOException {
    ISqlTridentDataSource ds = DataSourcesRegistry.constructTridentDataSource(URI.create("redis://localhost:6380"), null, null, CLUSTER_TBL_PROPERTIES, FIELDS);
    Assert.assertNotNull(ds);
    ISqlTridentDataSource.SqlTridentConsumer consumer = ds.getConsumer();
    Assert.assertEquals(RedisClusterState.Factory.class, consumer.getStateFactory().getClass());
    Assert.assertEquals(RedisClusterStateUpdater.class, consumer.getStateUpdater().getClass());
    RedisClusterState state = (RedisClusterState) consumer.getStateFactory().makeState(Collections.emptyMap(), null, 0, 1);
    StateUpdater stateUpdater = consumer.getStateUpdater();
    JedisCluster mockJedisCluster = mock(JedisCluster.class);
    Whitebox.setInternalState(state, "jedisCluster", mockJedisCluster);
    List<TridentTuple> tupleList = mockTupleList();
    stateUpdater.updateState(state, tupleList, null);
    for (TridentTuple t : tupleList) {
        // PK goes to the key
        String id = String.valueOf(t.getValueByField("ID"));
        String serializedValue = new String(SERIALIZER.write(t.getValues(), null).array());
        verify(mockJedisCluster).hset(eq(ADDITIONAL_KEY), eq(id), eq(serializedValue));
    }
    verify(mockJedisCluster, never()).close();
}
Also used : RedisClusterState(org.apache.storm.redis.trident.state.RedisClusterState) JedisCluster(redis.clients.jedis.JedisCluster) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) StateUpdater(org.apache.storm.trident.state.StateUpdater) RedisStateUpdater(org.apache.storm.redis.trident.state.RedisStateUpdater) RedisClusterStateUpdater(org.apache.storm.redis.trident.state.RedisClusterStateUpdater) TridentTuple(org.apache.storm.trident.tuple.TridentTuple) Test(org.junit.Test)

Example 13 with JedisCluster

use of redis.clients.jedis.JedisCluster in project storm by apache.

the class RedisClusterStateQuerier method retrieveValuesFromRedis.

/**
     * {@inheritDoc}
     */
@Override
protected List<String> retrieveValuesFromRedis(RedisClusterState state, List<String> keys) {
    JedisCluster jedisCluster = null;
    try {
        jedisCluster = state.getJedisCluster();
        List<String> redisVals = new ArrayList<String>();
        for (String key : keys) {
            switch(dataType) {
                case STRING:
                    redisVals.add(jedisCluster.get(key));
                    break;
                case HASH:
                    redisVals.add(jedisCluster.hget(additionalKey, key));
                    break;
                default:
                    throw new IllegalArgumentException("Cannot process such data type: " + dataType);
            }
        }
        return redisVals;
    } finally {
        if (jedisCluster != null) {
            state.returnJedisCluster(jedisCluster);
        }
    }
}
Also used : JedisCluster(redis.clients.jedis.JedisCluster) ArrayList(java.util.ArrayList)

Example 14 with JedisCluster

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

the class RedisClusterBuilder method build.

public JedisCluster build() {
    if (jedisCluster == null) {
        while (true) {
            try {
                lock.tryLock(10, TimeUnit.SECONDS);
                if (jedisCluster != null) {
                    return jedisCluster;
                }
                String url = String.format(ConstUtils.REDIS_CLUSTER_URL, String.valueOf(appId));
                String response = HttpUtils.doGet(url);
                ObjectMapper objectMapper = new ObjectMapper();
                HeartbeatInfo heartbeatInfo = null;
                try {
                    heartbeatInfo = objectMapper.readValue(response, HeartbeatInfo.class);
                } catch (IOException e) {
                    logger.error("remote build error, appId: {}", appId, e);
                }
                if (heartbeatInfo == null) {
                    continue;
                }
                /** 检查客户端版本 **/
                if (heartbeatInfo.getStatus() == ClientStatusEnum.ERROR.getStatus()) {
                    throw new IllegalStateException(heartbeatInfo.getMessage());
                } else if (heartbeatInfo.getStatus() == ClientStatusEnum.WARN.getStatus()) {
                    logger.warn(heartbeatInfo.getMessage());
                } else {
                    logger.info(heartbeatInfo.getMessage());
                }
                Set<HostAndPort> nodeList = new HashSet<HostAndPort>();
                //形如 ip1:port1,ip2:port2,ip3:port3
                String nodeInfo = heartbeatInfo.getShardInfo();
                //为了兼容,如果允许直接nodeInfo.split(" ")
                nodeInfo = nodeInfo.replace(" ", ",");
                String[] nodeArray = nodeInfo.split(",");
                for (String node : nodeArray) {
                    String[] ipAndPort = node.split(":");
                    if (ipAndPort.length < 2) {
                        continue;
                    }
                    String ip = ipAndPort[0];
                    int port = Integer.parseInt(ipAndPort[1]);
                    nodeList.add(new HostAndPort(ip, port));
                }
                //收集上报数据
                //                    ClientDataCollectReportExecutor.getInstance();
                jedisCluster = new JedisCluster(nodeList, connectionTimeout, soTimeout, maxRedirections, jedisPoolConfig);
                return jedisCluster;
            } catch (Throwable e) {
                logger.error(e.getMessage(), e);
            } finally {
                lock.unlock();
            }
            try {
                //活锁
                TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
            }
        }
    } else {
        return jedisCluster;
    }
}
Also used : HeartbeatInfo(com.sohu.tv.cachecloud.client.basic.heartbeat.HeartbeatInfo) IOException(java.io.IOException) HostAndPort(redis.clients.jedis.HostAndPort) Random(java.util.Random) JedisCluster(redis.clients.jedis.JedisCluster) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 15 with JedisCluster

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

the class ClusterBinaryJedisCommandsTest method setUp.

@Before
public void setUp() throws InterruptedException {
    node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
    node1.auth("cluster");
    node1.flushAll();
    node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
    node2.auth("cluster");
    node2.flushAll();
    node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
    node3.auth("cluster");
    node3.flushAll();
    // ---- configure cluster
    // add nodes to cluster
    node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
    node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
    // split available slots across the three nodes
    int slotsPerNode = JedisCluster.HASHSLOTS / 3;
    int[] node1Slots = new int[slotsPerNode];
    int[] node2Slots = new int[slotsPerNode + 1];
    int[] node3Slots = new int[slotsPerNode];
    for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) {
        if (i < slotsPerNode) {
            node1Slots[slot1++] = i;
        } else if (i > slotsPerNode * 2) {
            node3Slots[slot3++] = i;
        } else {
            node2Slots[slot2++] = i;
        }
    }
    node1.clusterAddSlots(node1Slots);
    node2.clusterAddSlots(node2Slots);
    node3.clusterAddSlots(node3Slots);
    waitForClusterReady();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    jedisCluster = new JedisCluster(jedisClusterNode, 2000, 2000, 5, "cluster", new JedisPoolConfig());
}
Also used : Jedis(redis.clients.jedis.Jedis) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Before(org.junit.Before)

Aggregations

JedisCluster (redis.clients.jedis.JedisCluster)26 HostAndPort (redis.clients.jedis.HostAndPort)23 Test (org.junit.Test)19 HashSet (java.util.HashSet)18 LinkedHashSet (java.util.LinkedHashSet)18 Jedis (redis.clients.jedis.Jedis)7 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)6 JedisPool (redis.clients.jedis.JedisPool)5 Before (org.junit.Before)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HeartbeatInfo (com.sohu.tv.cachecloud.client.basic.heartbeat.HeartbeatInfo)1 Map (java.util.Map)1 Random (java.util.Random)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 RedisClusterState (org.apache.storm.redis.trident.state.RedisClusterState)1