Scanning keys in a cluster (RedisCommandExecutionException: MOVED)

1,173 views
Skip to first unread message

Daniel López

unread,
Feb 15, 2016, 4:39:00 AM2/15/16
to lettuce-redis-client-users
Hey there,
I'm trying to scan the keys inside a hash in a redis cluster and I'm getting RedisCommandExecutionExceptions. A similar approach works without a cluster so I'm not sure if I'm not doing it correctly in a cluster.

Let's assume the cluster is defined as x.y.z.110:6379,x.y.z.111:6379,x.y.z.112:6379, with slaves running from 113 to 115.

My code looks like this:
--------------------------------------
        try (RedisAdvancedClusterCommands<String, Cacheable> redisConnection = redisClient.connect(codec).sync()) {
            NodeSelection<String, Cacheable> masters = redisConnection.masters();
            Set<Object> keys = Collections.synchronizedSet(new HashSet<>());
            ScanArgs scanArgs = new ScanArgs();
            scanArgs.match("*").limit(1000);
            masters.asMap().values().forEach(commands -> {
                MapScanCursor<String, Cacheable> cursor;
                try {
                    cursor = commands.hscan(preffix, scanArgs);
                    keys.addAll(cursor.getMap().keySet());
                    while (!cursor.isFinished()) {
                        cursor = commands.hscan(preffix, ScanCursor.of(cursor.getCursor()), scanArgs);
                        keys.addAll(cursor.getMap().keySet());
                    }
                } catch (Exception e) {
                    log.error("Error accessing redis cluster to scan keys", e);
                }
            });
            return keys;
        }
--------------------------------------
And the exception I get is this one:
--------------------------------------
Caused by: com.lambdaworks.redis.RedisCommandExecutionException: MOVED 9316 x.y.z.111:6379
at com.lambdaworks.redis.protocol.AsyncCommand.completeResult(AsyncCommand.java:86)
at com.lambdaworks.redis.protocol.AsyncCommand.complete(AsyncCommand.java:76)
at com.lambdaworks.redis.protocol.CommandHandler.decode(CommandHandler.java:170)
at com.lambdaworks.redis.protocol.CommandHandler.channelRead(CommandHandler.java:141)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:307)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:293)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:307)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:293)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:307)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:293)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:307)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:293)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:840)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
... 1 more
--------------------------------------
Note that the exception does not always happen, and I do get some results. But it is always launching that exception.

I have also tried with an async version, but the error is basically the same, inside other Lettuce classes.

What am I doing wrong?
D.
Reply all
Reply to author
Forward
0 new messages