io.lettuce.core.cluster.api.StatefulRedisClusterConnection Java Examples

The following examples show how to use io.lettuce.core.cluster.api.StatefulRedisClusterConnection. 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: ClusterScaleProcessServiceImpl.java    From cymbal with Apache License 2.0 6 votes vote down vote up
private void migrateSlots(final StatefulRedisClusterConnection redisConnection,
        final List<MigratePlan> migratePlans) {
    for (MigratePlan each : migratePlans) {
        log.debug("Migrating '{}' slots to '{}'.", each.getMigrateSlotsCount(), each.getTargetNode().getUri());

        Iterator<Map.Entry<Integer, RedisClusterNode>> sourceSlotItera = each.getSourceSlotAndNodes().entrySet()
                .iterator();
        while (sourceSlotItera.hasNext()) {
            Map.Entry<Integer, RedisClusterNode> eachSlotAndNode = sourceSlotItera.next();
            RedisClusterNode targetNode = each.getTargetNode();
            RedisClusterNode sourceNode = eachSlotAndNode.getValue();
            int slot = eachSlotAndNode.getKey();

            migrateSlot(redisConnection, sourceNode, targetNode, slot);
        }
    }
}
 
Example #2
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testCluster2() throws Exception {
    if (!checkOS()) {
        return;
    }
    RedisURI node1 = RedisURI.create("127.0.0.1", 7000);
    RedisURI node2 = RedisURI.create("127.0.0.1", 7001);
    RedisURI node3 = RedisURI.create("127.0.0.1", 7002);
    RedisClusterClient client = RedisClusterClient.create(Arrays.asList(node1, node2, node3));
    StatefulRedisClusterConnection con = client.connect(new JetCacheCodec());
    con.setReadFrom(ReadFrom.SLAVE_PREFERRED);
    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .connection(con)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    cache.put("K1", "V1");
    Thread.sleep(100);
    Assert.assertEquals("V1", cache.get("K1"));
}
 
Example #3
Source File: LettuceConnectionManager.java    From jetcache with Apache License 2.0 6 votes vote down vote up
public Object reactiveCommands(AbstractRedisClient redisClient) {
    connection(redisClient);
    LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
    if (lo.reactiveCommands == null) {
        if (lo.connection instanceof StatefulRedisConnection) {
            lo.reactiveCommands = ((StatefulRedisConnection) lo.connection).reactive();
        } else if (lo.connection instanceof StatefulRedisClusterConnection) {
            lo.reactiveCommands = ((StatefulRedisClusterConnection) lo.connection).reactive();
        } else if (lo.connection instanceof StatefulRedisSentinelConnection) {
            lo.reactiveCommands = ((StatefulRedisSentinelConnection) lo.connection).reactive();
        } else {
            throw new CacheConfigException("type " + lo.connection.getClass() + " is not supported");
        }
    }
    return lo.reactiveCommands;
}
 
Example #4
Source File: LettuceConnectionManager.java    From jetcache with Apache License 2.0 6 votes vote down vote up
public Object asyncCommands(AbstractRedisClient redisClient) {
    connection(redisClient);
    LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
    if (lo.asyncCommands == null) {
        if (lo.connection instanceof StatefulRedisConnection) {
            lo.asyncCommands = ((StatefulRedisConnection) lo.connection).async();
        } else if (lo.connection instanceof StatefulRedisClusterConnection) {
            lo.asyncCommands = ((StatefulRedisClusterConnection) lo.connection).async();
        } else if (lo.connection instanceof StatefulRedisSentinelConnection) {
            lo.asyncCommands = ((StatefulRedisSentinelConnection) lo.connection).async();
        } else {
            throw new CacheConfigException("type " + lo.connection.getClass() + " is not supported");
        }
    }
    return lo.asyncCommands;
}
 
Example #5
Source File: LettuceConnectionManager.java    From jetcache with Apache License 2.0 6 votes vote down vote up
public Object commands(AbstractRedisClient redisClient) {
    connection(redisClient);
    LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
    if (lo.commands == null) {
        if (lo.connection instanceof StatefulRedisConnection) {
            lo.commands = ((StatefulRedisConnection) lo.connection).sync();
        } else if (lo.connection instanceof StatefulRedisClusterConnection) {
            lo.commands = ((StatefulRedisClusterConnection) lo.connection).sync();
        } else if (lo.connection instanceof StatefulRedisSentinelConnection) {
            lo.commands = ((StatefulRedisSentinelConnection) lo.connection).sync();
        }else {
            throw new CacheConfigException("type " + lo.connection.getClass() + " is not supported");
        }
    }
    return lo.commands;
}
 
Example #6
Source File: ClusterScaleProcessServiceImpl.java    From cymbal with Apache License 2.0 6 votes vote down vote up
private void setSlotToTargetNode(final StatefulRedisClusterConnection redisConnection,
        final RedisClusterNode targetNode, final int slot) {
    RedisAdvancedClusterCommands commands = redisConnection.sync();
    retryTemplate.execute(retryContext -> {
        NodeSelection masters = commands.masters();
        int succeedMasterCount = 0;
        StringBuffer failMessage = new StringBuffer();
        for (int i = 0; i < masters.size(); i++) {
            String result = commands.getConnection(masters.node(i).getNodeId()).
                    clusterSetSlotNode(slot, targetNode.getNodeId());
            if (Constant.Redis.EXECUTE_RESULT_SUCCESS.equals(result)) {
                succeedMasterCount++;
            } else {
                failMessage.append(masters.node(i).getUri());
                failMessage.append(": ");
                failMessage.append(result);
                failMessage.append("\n");
            }
        }
        if (succeedMasterCount * 2 < masters.size()) {
            throw new ScaleException("Fail to set node of slot '%d', return value is '%s'.", slot,
                    failMessage.toString());
        }
        return Constant.Redis.EXECUTE_RESULT_SUCCESS;
    });
}
 
Example #7
Source File: WrapperTests.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testRedisConnectionClusterImplCallsWrapperFunctions() {
    @SuppressWarnings("unchecked")
    StatefulRedisClusterConnection<String, String> wrapped = (StatefulRedisClusterConnection<String, String>)
            mock(StatefulRedisClusterConnection.class);
    RedisConnection<String> conn = new RedisConnectionClusterImpl<>(wrapped);
    conn.async();
    verify(wrapped).async();
    conn.sync();
    verify(wrapped).sync();
    conn.close();
    verify(wrapped).close();
}
 
Example #8
Source File: RedisClusterOnlineRetriever.java    From feast with Apache License 2.0 5 votes vote down vote up
public static OnlineRetriever create(Map<String, String> config) {
  List<RedisURI> redisURIList =
      Arrays.stream(config.get("connection_string").split(","))
          .map(
              hostPort -> {
                String[] hostPortSplit = hostPort.trim().split(":");
                return RedisURI.create(hostPortSplit[0], Integer.parseInt(hostPortSplit[1]));
              })
          .collect(Collectors.toList());

  StatefulRedisClusterConnection<byte[], byte[]> connection =
      RedisClusterClient.create(redisURIList).connect(new ByteArrayCodec());

  return new RedisClusterOnlineRetriever(connection);
}
 
Example #9
Source File: StringStringSyncTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Prepare(global = true)
public void loadInitialData() {
    Random random = new Random();
    StatefulRedisClusterConnection<String, String> connection = redisClient.connect();
    RedisAdvancedClusterCommands sync = connection.sync();
    // get rid of all data.
    sync.flushall();

    for (int k = 0; k < keyDomain; k++) {
        int r = random.nextInt(valueCount);
        sync.set(Long.toString(k), values[r]);
    }

    //connection.async().set()
}
 
Example #10
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 5 votes vote down vote up
private <K extends Message, V extends Message> RemoteCache<K, V> createRedisRemoteCache(
    String name,
    RedisClusterClient redisClient,
    K keyPrototype,
    V valuePrototype,
    ReadFrom readFrom) {
  StatefulRedisClusterConnection<K, V> connection =
      redisClient.connect(
          new ProtobufRedisCodec<>(
              (name + ":").getBytes(StandardCharsets.UTF_8), keyPrototype, valuePrototype));
  connection.setReadFrom(readFrom);
  return new RedisRemoteCache<>(connection.async(), name, meterRegistry);
}
 
Example #11
Source File: LettuceCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
protected BaseRedisCommands sync(StatefulConnection conn) {
    if(conn instanceof StatefulRedisClusterConnection)
        return ((StatefulRedisClusterConnection)conn).sync();
    else if(conn instanceof StatefulRedisConnection)
        return ((StatefulRedisConnection)conn).sync();
    return null;
}
 
Example #12
Source File: ClusterScaleProcessServiceImpl.java    From cymbal with Apache License 2.0 5 votes vote down vote up
private void migrateKeysInSlot(final StatefulRedisClusterConnection redisConnection,
        final RedisClusterNode sourceNode, final RedisClusterNode targetNode, final int slot) {
    RedisAdvancedClusterCommands commands = redisConnection.sync();
    retryTemplate.execute(retryContext -> {
        boolean migrateDone = false;
        while (!migrateDone) {
            List<String> keys = commands.getConnection(sourceNode.getNodeId())
                    .clusterGetKeysInSlot(slot, KEYS_PER_MIGRATE);
            log.debug("Migrating '{}' keys to '{}'.", keys.size(), targetNode.getUri());

            if (keys.isEmpty()) {
                migrateDone = true;
            } else {
                String[] keysArray = keys.toArray(new String[keys.size()]);

                // do migrate
                String result = commands.migrate(targetNode.getUri().getHost(), targetNode.getUri().getPort(), 0,
                        MIGRATE_TIMEOUT * (retryContext.getRetryCount() + 1),
                        MigrateArgs.Builder.keys(keysArray).copy().replace());

                // if result is OK, del keys from source node
                switch (result) {
                    case Constant.Redis.EXECUTE_RESULT_SUCCESS:
                        commands.del(keysArray);
                        if (keys.size() < KEYS_PER_MIGRATE) {
                            migrateDone = true;
                        }
                        break;
                    case Constant.Redis.EXECUTE_RESULT_NOKEY:
                        migrateDone = true;
                        break;
                    default:
                        throw new ScaleException(String.format("Migrate fail of slot '%d'.", slot));
                }
            }
        }

        return Constant.Redis.EXECUTE_RESULT_SUCCESS;
    });
}
 
Example #13
Source File: ClusterScaleProcessServiceImpl.java    From cymbal with Apache License 2.0 5 votes vote down vote up
private void rebalanceCluster(final Cluster cluster) {
    RedisClusterClient redisClusterClient = null;
    StatefulRedisClusterConnection redisConnection = null;

    try {
        final List<InstanceBO> instanceBOS = instanceProcessService
                .queryInstanceBOsByClusterId(cluster.getClusterId());

        List<RedisURI> redisURIs = new ArrayList(instanceBOS.size());
        for (InstanceBO each : instanceBOS) {
            redisURIs.add(RedisURI.builder().withHost(each.getNode().getIp()).withPort(each.getSelf().getPort())
                    .build());
        }

        redisClusterClient = RedisClusterClient.create(redisURIs);
        redisClusterClient.setDefaultTimeout(Duration.ofSeconds(DEFAULT_TIMEOUT));
        redisConnection = redisClusterClient.connect();

        // assign slot
        List<MigratePlan> plans = getMigratePlans(redisConnection);
        migrateSlots(redisConnection, plans);
    } finally {
        if (redisConnection != null) {
            redisConnection.close();
        }

        if (redisClusterClient != null) {
            redisClusterClient.shutdown();
        }
    }
}
 
Example #14
Source File: RedisClusterRateLimiterFactory.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
private StatefulRedisClusterConnection<String, String> getConnection() {
    // going to ignore race conditions at the cost of having multiple connections
    if (connection == null) {
        connection = client.connect();
    }
    return connection;
}
 
Example #15
Source File: StringStringSyncTest.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
ThreadState() {
    StatefulRedisClusterConnection<String, String> connection = redisClient.connect();
    sync = connection.sync();
}
 
Example #16
Source File: RedisConnectionClusterImpl.java    From sherlock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param connection cluster connection to wrap
 */
protected RedisConnectionClusterImpl(StatefulRedisClusterConnection<K, K> connection) {
    this.connection = connection;
}
 
Example #17
Source File: LettuceRedisClusterCacheManager.java    From AutoLoadCache with Apache License 2.0 4 votes vote down vote up
public LettuceRedisClusterClient(StatefulRedisClusterConnection<byte[], byte[]> connection, AbstractRedisCacheManager cacheManager) {
    this.connection = connection;
    this.cacheManager = cacheManager;
}
 
Example #18
Source File: LettuceRedisClusterCacheManager.java    From AutoLoadCache with Apache License 2.0 4 votes vote down vote up
@Override
protected IRedis getRedis() {
    StatefulRedisClusterConnection<byte[], byte[]> connection = redisClusterClient.connect(ByteArrayCodec.INSTANCE);
    return new LettuceRedisClusterClient(connection, this);
}
 
Example #19
Source File: TracingRedisAdvancedClusterCommands.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public StatefulRedisClusterConnection<K, V> getStatefulConnection() {
  return new TracingStatefulRedisClusterConnection<>(commands.getStatefulConnection(),
      tracingConfiguration);
}
 
Example #20
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param connection redis connection
 * @param tracingConfiguration tracing configuration
 */
public TracingStatefulRedisClusterConnection(StatefulRedisClusterConnection<K, V> connection,
    TracingConfiguration tracingConfiguration) {
  this.connection = connection;
  this.tracingConfiguration = tracingConfiguration;
}
 
Example #21
Source File: TracingRedisAdvancedClusterAsyncCommands.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public StatefulRedisClusterConnection<K, V> getStatefulConnection() {
  return new TracingStatefulRedisClusterConnection<>(commands.getStatefulConnection(),
      tracingConfiguration);
}
 
Example #22
Source File: TracingRedisAdvancedClusterCommands.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public StatefulRedisClusterConnection<K, V> getStatefulConnection() {
  return new TracingStatefulRedisClusterConnection<>(commands.getStatefulConnection(),
      tracingConfiguration);
}
 
Example #23
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param connection redis connection
 * @param tracingConfiguration tracing configuration
 */
public TracingStatefulRedisClusterConnection(StatefulRedisClusterConnection<K, V> connection,
    TracingConfiguration tracingConfiguration) {
  this.connection = connection;
  this.tracingConfiguration = tracingConfiguration;
}
 
Example #24
Source File: TracingRedisAdvancedClusterAsyncCommands.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public StatefulRedisClusterConnection<K, V> getStatefulConnection() {
  return new TracingStatefulRedisClusterConnection<>(commands.getStatefulConnection(),
      tracingConfiguration);
}
 
Example #25
Source File: TracingRedisAdvancedClusterCommands.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public StatefulRedisClusterConnection<K, V> getStatefulConnection() {
  return new TracingStatefulRedisClusterConnection<>(commands.getStatefulConnection(),
      tracingConfiguration);
}
 
Example #26
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
/**
 * @param connection redis connection
 * @param tracingConfiguration tracing configuration
 */
public TracingStatefulRedisClusterConnection(StatefulRedisClusterConnection<K, V> connection,
    TracingConfiguration tracingConfiguration) {
  this.connection = connection;
  this.tracingConfiguration = tracingConfiguration;
}
 
Example #27
Source File: TracingRedisAdvancedClusterAsyncCommands.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public StatefulRedisClusterConnection<K, V> getStatefulConnection() {
  return new TracingStatefulRedisClusterConnection<>(commands.getStatefulConnection(),
      tracingConfiguration);
}
 
Example #28
Source File: RedisClusterFeatureSinkTest.java    From feast with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws IOException {
  redisCluster = new RedisCluster(REDIS_CLUSTER_PORT1, REDIS_CLUSTER_PORT2, REDIS_CLUSTER_PORT3);
  redisCluster.start();
  redisClusterClient =
      RedisClusterClient.create(
          Arrays.asList(
              RedisURI.create(REDIS_CLUSTER_HOST, REDIS_CLUSTER_PORT1),
              RedisURI.create(REDIS_CLUSTER_HOST, REDIS_CLUSTER_PORT2),
              RedisURI.create(REDIS_CLUSTER_HOST, REDIS_CLUSTER_PORT3)));
  StatefulRedisClusterConnection<byte[], byte[]> connection =
      redisClusterClient.connect(new ByteArrayCodec());
  redisClusterCommands = connection.sync();
  redisClusterCommands.setTimeout(java.time.Duration.ofMillis(600000));

  FeatureSetSpec spec1 =
      FeatureSetSpec.newBuilder()
          .setName("fs")
          .setProject("myproject")
          .addEntities(EntitySpec.newBuilder().setName("entity").setValueType(Enum.INT64).build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature").setValueType(Enum.STRING).build())
          .build();

  FeatureSetSpec spec2 =
      FeatureSetSpec.newBuilder()
          .setName("feature_set")
          .setProject("myproject")
          .addEntities(
              EntitySpec.newBuilder()
                  .setName("entity_id_primary")
                  .setValueType(Enum.INT32)
                  .build())
          .addEntities(
              EntitySpec.newBuilder()
                  .setName("entity_id_secondary")
                  .setValueType(Enum.STRING)
                  .build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature_1").setValueType(Enum.STRING).build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature_2").setValueType(Enum.INT64).build())
          .build();

  Map<FeatureSetReference, FeatureSetSpec> specMap =
      ImmutableMap.of(
          FeatureSetReference.of("myproject", "fs", 1), spec1,
          FeatureSetReference.of("myproject", "feature_set", 1), spec2);
  RedisClusterConfig redisClusterConfig =
      RedisClusterConfig.newBuilder()
          .setConnectionString(CONNECTION_STRING)
          .setInitialBackoffMs(2000)
          .setMaxRetries(4)
          .build();

  redisClusterFeatureSink =
      RedisFeatureSink.builder().setRedisClusterConfig(redisClusterConfig).build();
  redisClusterFeatureSink.prepareWrite(p.apply("Specs-1", Create.of(specMap)));
}
 
Example #29
Source File: RedisClusterOnlineRetriever.java    From feast with Apache License 2.0 4 votes vote down vote up
public static OnlineRetriever create(StatefulRedisClusterConnection<byte[], byte[]> connection) {
  return new RedisClusterOnlineRetriever(connection);
}
 
Example #30
Source File: RedisClusterOnlineRetriever.java    From feast with Apache License 2.0 4 votes vote down vote up
private RedisClusterOnlineRetriever(StatefulRedisClusterConnection<byte[], byte[]> connection) {
  this.syncCommands = connection.sync();
}