Read From Slaves is not working in 5.1.0.M1 version

132 views
Skip to first unread message

Bhargav D

unread,
Jun 15, 2018, 5:32:35 PM6/15/18
to lettuce-redis-client-users

I have a Redis cluster in AWS with 3 shards. Plan is to do all reads from Slaves. My reads are always going to Primaries in the shards. used monitor command to verify that. Seems something wrong. Can you please suggest is something is wrong in the code below. 

Code is

import io.lettuce.core.RedisFuture;
import io.lettuce.core.dynamic.Commands;
import io.lettuce.core.dynamic.batch.BatchExecutor;
import io.lettuce.core.dynamic.batch.BatchSize;
import io.lettuce.core.dynamic.batch.CommandBatching;


import java.util.List;

@BatchSize(2)
public interface HashCommands extends Commands, BatchExecutor {
RedisFuture<String> get(String key, CommandBatching commandBatching);
RedisFuture<String> hget(String key, String fields, CommandBatching commandBatching);
RedisFuture<List<String>> hmget(String key, String[] fields);
}

import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import io.lettuce.core.codec.ByteArrayCodec;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.dynamic.RedisCommandFactory;
import io.lettuce.core.dynamic.batch.CommandBatching;
import io.lettuce.core.output.KeyValueListOutput;
import io.lettuce.core.output.KeyValueOutput;
import io.lettuce.core.protocol.LettuceCharsets;

import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class LettuceRead  {

    public static void main(String[] args) throws Exception{
        try {
            RedisURI uri = RedisURI.builder()
                    .withHost("localhost")
                    .withPort(7000)
                    .build();
            RedisClusterClient client = RedisClusterClient.create(uri);
            ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
                    .enablePeriodicRefresh(Duration.ofSeconds(60))
                    .enableAllAdaptiveRefreshTriggers()
                    .build();

            client.setOptions(ClusterClientOptions.builder()
                    .topologyRefreshOptions(topologyRefreshOptions)
                    .build());
            StatefulRedisClusterConnection<String, String> connection = client.connect();
            connection.setReadFrom(ReadFrom.SLAVE);
            RedisCommandFactory factory = new RedisCommandFactory(connection);
            HashCommands commands = factory.getCommands(HashCommands.class);
            List<List<String>> results = new ArrayList<>();
            List<RedisFuture<List<String>>> futures = new ArrayList<>();

            String[] fields = new String[2];
            fields[0] = "test:field1";
            fields[1] = "test:field2";
            int KEYS_TO_FETCH = 11;
            for(int i = 0; i < KEYS_TO_FETCH; i++) {
                futures.add(commands.hmget("test:id="+i, fields));
            }
            commands.flush();

            for (RedisFuture<List<String>> future : futures) {
                List<String> keyValues = future.get(10000, TimeUnit.MILLISECONDS);
                for (String keyValue: keyValues) {
                    System.out.println(keyValue);

                }
                results.add(keyValues);
            }

            connection.close();
            client.shutdown();
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }

    }
}

Is there anything wrong in my code?

Mark Paluch

unread,
Jun 16, 2018, 1:47:09 PM6/16/18
to lettuce-redis-client-users, Bhargav D
Hi, 
your code looks well. This looks like a bug. Care to file a bug report at https://github.com/lettuce-io/lettuce-core/issues?

Cheers, 
Mark
--
You received this message because you are subscribed to the Google Groups "lettuce-redis-client-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lettuce-redis-clien...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lettuce-redis-client-users/bb81f598-c4f8-43c0-a307-f1f5531c63c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bhargav D

unread,
Jun 16, 2018, 3:35:48 PM6/16/18
to lettuce-redis-client-users
Sure. I'll create a bug.  This bug exists in older versions as well (5.0.3.RELEASE). 
To unsubscribe from this group and stop receiving emails from it, send an email to lettuce-redis-client-users+unsub...@googlegroups.com.

Bhargav D

unread,
Jun 16, 2018, 3:42:07 PM6/16/18
to lettuce-redis-client-users
Reply all
Reply to author
Forward
0 new messages