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());
}
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();
}
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);
}
}
}
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;
}
}
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());
}
Aggregations