redis.clients.jedis.JedisCluster Java Examples

The following examples show how to use redis.clients.jedis.JedisCluster. 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: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
public RedisShardBackplane(
    RedisShardBackplaneConfig config,
    String source,
    Function<Operation, Operation> onPublish,
    Function<Operation, Operation> onComplete,
    Predicate<Operation> isPrequeued,
    Predicate<Operation> isDispatched,
    Supplier<JedisCluster> jedisClusterFactory) {
  this.config = config;
  this.source = source;
  this.onPublish = onPublish;
  this.onComplete = onComplete;
  this.isPrequeued = isPrequeued;
  this.isDispatched = isDispatched;
  this.jedisClusterFactory = jedisClusterFactory;
}
 
Example #2
Source File: RedisLimit.java    From Taroco with Apache License 2.0 6 votes vote down vote up
private Object limitRequest(Object connection) {
    Object result;
    String key = String.valueOf(System.currentTimeMillis() / 1000);
    if (connection instanceof Jedis) {
        result = ((Jedis) connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
        ((Jedis) connection).close();
    } else {
        result = ((JedisCluster) connection).eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(limit)));
        try {
            ((JedisCluster) connection).close();
        } catch (IOException e) {
            log.error("limit IOException", e);
        }
    }
    return result;
}
 
Example #3
Source File: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
private Instant getExpiresAt(JedisCluster jedis, String key, Instant now) {
  String value = jedis.get(key);
  if (value != null) {
    try {
      return Instant.ofEpochMilli(Long.parseLong(value));
    } catch (NumberFormatException e) {
      logger.log(Level.SEVERE, format("invalid expiration %s for %s", value, key));
    }
  }

  Instant expiresAt = now.plusMillis(config.getProcessingTimeoutMillis());
  jedis.setex(
      key,
      /* expire=*/ (config.getProcessingTimeoutMillis() * 2) / 1000,
      String.format("%d", expiresAt.toEpochMilli()));
  return expiresAt;
}
 
Example #4
Source File: RedisLock.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private Object eval(RedisConnection connection,String lua,List<String> keys,List<String> args){
    Object nativeConnection = connection.getNativeConnection();
    if (nativeConnection instanceof JedisCluster) {
        return (Long)((JedisCluster)nativeConnection).eval(lua, keys, args);
    } else if (nativeConnection instanceof Jedis) {
        return (Long)((Jedis)nativeConnection).eval(lua, keys, args);
    }
    return 0L;

}
 
Example #5
Source File: OperationQueue.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public boolean removeFromDequeue(JedisCluster jedis, String val) {
  for (ProvisionedRedisQueue provisionedQueue : queues) {
    if (provisionedQueue.queue().removeFromDequeue(jedis, val)) {
      return true;
    }
  }
  return false;
}
 
Example #6
Source File: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private boolean removeWorkerAndPublish(JedisCluster jedis, String name, String changeJson) {
  if (jedis.hdel(config.getWorkersHashName(), name) == 1) {
    jedis.publish(config.getWorkerChannel(), changeJson);
    return true;
  }
  return false;
}
 
Example #7
Source File: BalancedRedisQueue.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public boolean isEvenlyDistributed(JedisCluster jedis) {
  long size = queues.get(0).size(jedis);
  for (RedisQueue queue : partialIterationQueueOrder()) {
    if (queue.size(jedis) != size) {
      return false;
    }
  }
  return true;
}
 
Example #8
Source File: JedisClusterConfig.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@Bean
public JedisCluster jedisClusterFactory() {
    String[] serverArray = redisProperties.getNodes().split(",");
    Set<HostAndPort> nodes = new HashSet<>();
    for (String ipPort: serverArray) {
        String[] ipPortPair = ipPort.split(":");
        nodes.add(new HostAndPort(ipPortPair[0].trim(),Integer.valueOf(ipPortPair[1].trim())));
    }
    return new JedisCluster(nodes, redisProperties.getCommandTimeout());
}
 
Example #9
Source File: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private void queue(
    JedisCluster jedis,
    String operationName,
    List<Platform.Property> provisions,
    String queueEntryJson) {
  if (jedis.hdel(config.getDispatchedOperationsHashName(), operationName) == 1) {
    logger.log(Level.WARNING, format("removed dispatched operation %s", operationName));
  }
  operationQueue.push(jedis, provisions, queueEntryJson);
}
 
Example #10
Source File: RedisDistributedLock.java    From seed with Apache License 2.0 5 votes vote down vote up
/**
 * @param jedisCluster   操作Redis3.0集群的对象
 * @param key            RedisCluster全局唯一的key名称
 * @param holdTimeMillis 锁持有时间
 */
public RedisDistributedLock(JedisCluster jedisCluster, String key, long holdTimeMillis) {
    if(holdTimeMillis <= 0){
        throw new IllegalArgumentException("Argument holdTimeMillis must > 0.");
    }
    this.jedisCluster = jedisCluster;
    this.key = key;
    this.holdTimeMillis = holdTimeMillis;
}
 
Example #11
Source File: RedisClusterServiceTest.java    From redis-game-transaction with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(new String[]{"bean/*.xml"});
//        RGTRedisClusterService redisClusterService = (RGTRedisClusterService) classPathXmlApplicationContext.getBean("redisClusterService");
//
//        redisClusterService.setString("test", "200");
//        System.out.println(redisClusterService.getString("test"));
        JedisCluster jedisCluster = (JedisCluster) classPathXmlApplicationContext.getBean("jedisCluster");
        jedisCluster.set("test", "200");
        System.out.println(jedisCluster.get("test"));
    }
 
Example #12
Source File: Jedis3Driver.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Override
public void startVendorInstance() throws Exception {
    Set<HostAndPort> addresses = getAddresses();
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(100);
    if (get("REDIS_CLUSTER_PASSWORD") != null) {
        this.client = new JedisCluster(addresses, 30000, 30000, 3, get("REDIS_CLUSTER_PASSWORD"), poolConfig);
    } else {
        this.client = new JedisCluster(addresses, poolConfig);
    }
}
 
Example #13
Source File: RedisQueue.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public String nonBlockingDequeue(JedisCluster jedis) throws InterruptedException {
  String val = jedis.rpoplpush(name, getDequeueName());
  if (val != null) {
    return val;
  }
  if (Thread.currentThread().isInterrupted()) {
    throw new InterruptedException();
  }
  return null;
}
 
Example #14
Source File: JedisDemo.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 集群方式(尚未实现)。
 */
private static void cluster() {
    // 生成集群节点列表
    Set<HostAndPort> clusterNodes = new HashSet<HostAndPort>();
    clusterNodes.add(new HostAndPort("127.0.0.1", 6379));
    clusterNodes.add(new HostAndPort("192168.56.102", 6379));
    
    // 执行指令
    JedisCluster client = new JedisCluster(clusterNodes);
    String result = client.set("key-string", "Hello, Redis!");
    System.out.println( String.format("set指令执行结果:%s", result) );
    String value = client.get("key-string");
    System.out.println( String.format("get指令执行结果:%s", value) );
}
 
Example #15
Source File: JedisClusterFactory.java    From redis-game-transaction with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    if (jedisClusterNodes == null || jedisClusterNodes.size() == 0) {
        throw new NullPointerException("jedisClusterNodes is null.");
    }
    Set<HostAndPort> haps = new HashSet<HostAndPort>();
    for (String node : jedisClusterNodes) {
        String[] arr = node.split(":");
        if (arr.length != 2) {
            throw new ParseException("node address error !",node.length()-1);
        }
        haps.add(new HostAndPort(arr[0], Integer.valueOf(arr[1])));
    }
    jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, genericObjectPoolConfig);
}
 
Example #16
Source File: RedisShardBackplaneTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
void verifyChangePublished(JedisCluster jedis, String opName) throws IOException {
  ArgumentCaptor<String> changeCaptor = ArgumentCaptor.forClass(String.class);
  verify(jedis, times(1)).publish(eq(backplane.operationChannel(opName)), changeCaptor.capture());
  OperationChange opChange = parseOperationChange(changeCaptor.getValue());
  assertThat(opChange.hasReset()).isTrue();
  assertThat(opChange.getReset().getOperation().getName()).isEqualTo(opName);
}
 
Example #17
Source File: JedisClusterHealthTracker.java    From pepper-metrics with Apache License 2.0 5 votes vote down vote up
public static void addJedisCluster(String namespace, JedisCluster jedisCluster) {
    Assert.assertNotNull(namespace);
    Assert.assertFalse("Duplicate namespace error.", UNIQUE_NAME.contains(namespace));
    UNIQUE_NAME.add(namespace);
    JedisClusterHealthStats stats = new JedisClusterHealthStats(MetricsRegistry.getREGISTRY(), namespace, jedisCluster);
    HealthTracker.addStats(stats);
}
 
Example #18
Source File: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
void publish(
    JedisCluster jedis,
    String channel,
    Instant effectiveAt,
    OperationChange.Builder operationChange) {
  try {
    String operationChangeJson =
        printOperationChange(
            operationChange.setEffectiveAt(toTimestamp(effectiveAt)).setSource(source).build());
    jedis.publish(channel, operationChangeJson);
  } catch (InvalidProtocolBufferException e) {
    logger.log(Level.SEVERE, "error printing operation change", e);
    // very unlikely, printer would have to fail
  }
}
 
Example #19
Source File: RedisClusterTest.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    ThunderProperties properties = ThunderPropertiesManager.getProperties();
    ObjectPoolFactory.initialize(properties);
    ThreadPoolFactory.initialize(properties);
    RedisClusterFactory.initialize(properties);

    JedisCluster cluster = RedisClusterFactory.getCluster();
    del(cluster);
    hget(cluster);
}
 
Example #20
Source File: RedisClusterTest.java    From mpush with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000));
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001));
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002));
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003));
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004));
    jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005));
    cluster = new JedisCluster(jedisClusterNodes, new GenericObjectPoolConfig());
}
 
Example #21
Source File: RedisShardBackplaneTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Test
public void workersWithInvalidProtobufAreRemoved() throws IOException {
  RedisShardBackplaneConfig config =
      RedisShardBackplaneConfig.newBuilder()
          .setWorkersHashName("Workers")
          .setWorkerChannel("WorkerChannel")
          .build();
  JedisCluster jedisCluster = mock(JedisCluster.class);
  when(mockJedisClusterFactory.get()).thenReturn(jedisCluster);
  when(jedisCluster.hgetAll(config.getWorkersHashName()))
      .thenReturn(ImmutableMap.of("foo", "foo"));
  when(jedisCluster.hdel(config.getWorkersHashName(), "foo")).thenReturn(1l);
  backplane =
      new RedisShardBackplane(
          config,
          "invalid-protobuf-worker-removed-test",
          (o) -> o,
          (o) -> o,
          (o) -> false,
          (o) -> false,
          mockJedisClusterFactory);
  backplane.start();

  assertThat(backplane.getWorkers()).isEmpty();
  verify(jedisCluster, times(1)).hdel(config.getWorkersHashName(), "foo");
  ArgumentCaptor<String> changeCaptor = ArgumentCaptor.forClass(String.class);
  verify(jedisCluster, times(1)).publish(eq(config.getWorkerChannel()), changeCaptor.capture());
  String json = changeCaptor.getValue();
  WorkerChange.Builder builder = WorkerChange.newBuilder();
  JsonFormat.parser().merge(json, builder);
  WorkerChange workerChange = builder.build();
  assertThat(workerChange.getName()).isEqualTo("foo");
  assertThat(workerChange.getTypeCase()).isEqualTo(WorkerChange.TypeCase.REMOVE);
}
 
Example #22
Source File: RedisClientConfiguration.java    From kork with Apache License 2.0 5 votes vote down vote up
@Bean("jedisCluster")
@ConditionalOnProperty(value = "redis.cluster-enabled")
@Primary
public JedisCluster jedisCluster(
    GenericObjectPoolConfig objectPoolConfig, ClientConfigurationWrapper config) {
  URI cx = URI.create(config.connection);
  return getJedisCluster(objectPoolConfig, config, cx);
}
 
Example #23
Source File: RedisClientTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Test
public void runExceptionSocketTimeoutExceptionIsDeadlineExceeded()
    throws IOException, InterruptedException {
  RedisClient client = new RedisClient(mock(JedisCluster.class));
  Status status = Status.UNKNOWN;
  try {
    client.run(
        jedis -> {
          throw new JedisConnectionException(new SocketTimeoutException());
        });
  } catch (IOException e) {
    status = Status.fromThrowable(e);
  }
  assertThat(status.getCode()).isEqualTo(Code.DEADLINE_EXCEEDED);
}
 
Example #24
Source File: ClusterBinaryJedisCommandsTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
  // clear all slots
  int[] slotsToDelete = new int[JedisCluster.HASHSLOTS];
  for (int i = 0; i < JedisCluster.HASHSLOTS; i++) {
    slotsToDelete[i] = i;
  }
  node1.clusterDelSlots(slotsToDelete);
  node2.clusterDelSlots(slotsToDelete);
  node3.clusterDelSlots(slotsToDelete);
}
 
Example #25
Source File: RedisCacheStore.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
protected JedisCluster redis() {
    if (REDIS == null) {
        synchronized (RedisCacheStore.class) {
            if (REDIS != null) {
                return REDIS;
            }
            initRedis();
            return REDIS;
        }
    }
    return REDIS;
}
 
Example #26
Source File: RedisClient.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public void run(Consumer<JedisCluster> withJedis) throws IOException {
  call(
      new JedisContext<Void>() {
        @Override
        public Void run(JedisCluster jedis) throws JedisException {
          withJedis.accept(jedis);
          return null;
        }
      });
}
 
Example #27
Source File: RedisClientConfiguration.java    From kork with Apache License 2.0 5 votes vote down vote up
private JedisCluster getJedisCluster(
    GenericObjectPoolConfig objectPoolConfig, ClientConfigurationWrapper config, URI cx) {
  int port = cx.getPort() == -1 ? Protocol.DEFAULT_PORT : cx.getPort();
  String password =
      (cx.getUserInfo() != null && cx.getUserInfo().contains(":"))
          ? cx.getUserInfo().substring(cx.getUserInfo().indexOf(":"))
          : null;

  return new JedisCluster(
      new HostAndPort(cx.getHost(), port),
      config.getTimeoutMs(),
      config.getTimeoutMs(),
      config.getMaxAttempts(),
      objectPoolConfig);
}
 
Example #28
Source File: JedisIamSessionDAO.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public ScanCursor<IamSession> getAccessSessions(final CursorWrapper cursor, int limit) {
	isTrue(limit > 0, "accessSessions batchSize must >0");
	byte[] match = (cacheManager.getIamCache(CACHE_SESSION).getCacheName() + "*").getBytes(UTF_8);
	ScanParams params = new ScanParams().count(limit).match(match);
	JedisCluster jedisCluster = ((JedisIamCacheManager) cacheManager).getJedisCluster();
	return new ScanCursor<IamSession>(jedisCluster, cursor, IamSession.class, params) {
		@Override
		public synchronized IamSession next() {
			IamSession s = super.next();
			awareRelationCache(s);
			return s;
		}
	}.open();
}
 
Example #29
Source File: RedisShardBackplaneTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteOperationDeletesAndPublishes() throws IOException {
  RedisShardBackplaneConfig config =
      RedisShardBackplaneConfig.newBuilder()
          .setDispatchedOperationsHashName("DispatchedOperations")
          .setOperationPrefix("Operation")
          .setOperationChannelPrefix("OperationChannel")
          .build();
  JedisCluster jedisCluster = mock(JedisCluster.class);
  when(mockJedisClusterFactory.get()).thenReturn(jedisCluster);
  backplane =
      new RedisShardBackplane(
          config,
          "delete-operation-test",
          (o) -> o,
          (o) -> o,
          (o) -> false,
          (o) -> false,
          mockJedisClusterFactory);
  backplane.start();

  final String opName = "op";

  backplane.deleteOperation(opName);

  verify(mockJedisClusterFactory, times(1)).get();
  verify(jedisCluster, times(1)).hdel(config.getDispatchedOperationsHashName(), opName);
  verify(jedisCluster, times(1)).del(backplane.operationKey(opName));
  verifyChangePublished(jedisCluster, opName);
}
 
Example #30
Source File: JedisIamCacheManager.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
public JedisIamCacheManager(String prefix, JedisCluster jedisCluster) {
	notNullOf(prefix, "prefix");
	notNullOf(jedisCluster, "jedisCluster");
	// e.g: iam-server => iam_server
	this.prefix = keyFormat(prefix, '_');
	this.jedisCluster = jedisCluster;
}