Skip to content

Commit

Permalink
Avoid zero-index when randomizing empty collections #2036
Browse files Browse the repository at this point in the history
We now avoid calling ThreadLocalRandom.nextInt(…) with a zero argument to avoid IllegalArgumentException
  • Loading branch information
mp911de committed Mar 16, 2022
1 parent 2bda738 commit 940e33a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ public RedisFuture<Boolean> msetnx(Map<K, V> map) {
public RedisFuture<K> randomkey() {

Partitions partitions = getStatefulConnection().getPartitions();

if (partitions.isEmpty()) {
return super.randomkey();
}

int index = ThreadLocalRandom.current().nextInt(partitions.size());
RedisClusterNode partition = partitions.getPartition(index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,7 @@

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import io.lettuce.core.AbstractRedisReactiveCommands;
import io.lettuce.core.FlushMode;
import io.lettuce.core.GeoArgs;
import io.lettuce.core.GeoWithin;
import io.lettuce.core.KeyScanCursor;
import io.lettuce.core.KeyValue;
import io.lettuce.core.RedisException;
import io.lettuce.core.RedisURI;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.StreamScanCursor;
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.reactive.RedisKeyReactiveCommands;
import io.lettuce.core.api.reactive.RedisScriptingReactiveCommands;
Expand Down Expand Up @@ -363,6 +353,11 @@ public Mono<String> mset(Map<K, V> map) {
public Mono<K> randomkey() {

Partitions partitions = getStatefulConnection().getPartitions();

if (partitions.isEmpty()) {
return super.randomkey();
}

int index = ThreadLocalRandom.current().nextInt(partitions.size());

Mono<RedisClusterReactiveCommands<K, V>> connection = getConnectionReactive(partitions.getPartition(index).getNodeId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
package io.lettuce.core.masterreplica;

import static io.lettuce.core.masterreplica.ReplicaUtils.findNodeByHostAndPort;

import java.util.*;
import static io.lettuce.core.masterreplica.ReplicaUtils.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -26,7 +32,12 @@

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import io.lettuce.core.*;
import io.lettuce.core.ConnectionFuture;
import io.lettuce.core.OrderingReadFromAccessor;
import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisException;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulConnection;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.cluster.models.partitions.Partitions;
Expand Down Expand Up @@ -144,7 +155,7 @@ public Iterator<RedisNodeDescription> iterator() {
return connections.filter(StatefulConnection::isOpen).next().switchIfEmpty(connections.next()).toFuture();
}

return connections.filter(StatefulConnection::isOpen).collectList().map(it -> {
return connections.filter(StatefulConnection::isOpen).collectList().filter(it -> !it.isEmpty()).map(it -> {
int index = ThreadLocalRandom.current().nextInt(it.size());
return it.get(index);
}).switchIfEmpty(connections.next()).toFuture();
Expand Down

0 comments on commit 940e33a

Please sign in to comment.