Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/main/java/io/lettuce/core/cluster/PartitionsConsensusImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.lettuce.core.cluster;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;

import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.models.partitions.Partitions;
Expand Down Expand Up @@ -31,12 +28,16 @@ Partitions getPartitions(Partitions current, Map<RedisURI, Partitions> topologyV

List<VotedPartitions> votedList = new ArrayList<>();

Map<String, RedisClusterNode> nodes = new HashMap<>(current.size());
for (RedisClusterNode knownNode : current) {
nodes.put(knownNode.getNodeId(), knownNode);
}

for (Partitions partitions : topologyViews.values()) {

int knownNodes = 0;
for (RedisClusterNode knownNode : current) {

if (partitions.getPartitionByNodeId(knownNode.getNodeId()) != null) {
for (RedisClusterNode node : partitions) {
if (nodes.containsKey(node.getNodeId())) {
knownNodes++;
}
}
Expand All @@ -47,6 +48,7 @@ Partitions getPartitions(Partitions current, Map<RedisURI, Partitions> topologyV
Collections.shuffle(votedList);
Collections.sort(votedList, (o1, o2) -> Integer.compare(o2.votes, o1.votes));

votedList.get(0).partitions.updateCache();
return votedList.get(0).partitions;
}

Expand Down Expand Up @@ -87,6 +89,7 @@ Partitions getPartitions(Partitions current, Map<RedisURI, Partitions> topologyV
Collections.shuffle(votedList);
Collections.sort(votedList, (o1, o2) -> Integer.compare(o2.votes, o1.votes));

votedList.get(0).partitions.updateCache();
return votedList.get(0).partitions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,14 @@ private static RedisClusterNode parseNode(List<Object> kvlist, BitSet slots) {
* @return the partitions object.
*/
public static Partitions parse(String nodes) {
return parseByCache(nodes, true);
}

public static Partitions parseByCache(String nodes, boolean updateCache) {

Partitions partitions = new Partitions();

try {

String[] lines = nodes.split(Character.toString(TOKEN_NODE_SEPARATOR));
List<RedisClusterNode> mappedNodes = new ArrayList<>(lines.length);

Expand All @@ -184,7 +187,13 @@ public static Partitions parse(String nodes) {
}
mappedNodes.add(ClusterPartitionParser.parseNode(line));
}
partitions.addAll(mappedNodes);

if (updateCache) {
partitions.addAll(mappedNodes);
} else {
partitions.addAllWithoutCache(mappedNodes);
}

} catch (Exception e) {
throw new RedisException("Cannot parse " + nodes, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ public boolean addAll(Collection<? extends RedisClusterNode> c) {
}
}

// just used in topology refresh, no race condition and lock is removed
public void addAllWithoutCache(Collection<? extends RedisClusterNode> c) {

LettuceAssert.noNullElements(c, "Partitions must not contain null elements");
partitions.addAll(c);
this.nodeReadView = Collections.unmodifiableCollection(c);
}

public void updateReadView() {
this.nodeReadView = Collections.unmodifiableCollection(partitions);
}

/**
* Remove all {@link RedisClusterNode nodes} from the {@link Partitions} using elements from the given collection and update
* the read-view/caches.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ NodeTopologyViews getNodeSpecificViews(Requests requestedTopology, Requests requ
allNodes.addAll(nodeWithStats);

Partitions partitions = new Partitions();
partitions.addAll(nodeWithStats);
partitions.addAllWithoutCache(nodeWithStats);

nodeTopologyView.setPartitions(partitions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private NodeTopologyView(RedisURI redisURI) {
this.available = true;
this.redisURI = redisURI;

this.partitions = ClusterPartitionParser.parse(clusterNodes);
this.partitions = ClusterPartitionParser.parseByCache(clusterNodes, false);
this.connectedClients = getClientCount(info);
this.replicationOffset = getReplicationOffset(info);
this.clusterNodes = clusterNodes;
Expand Down Expand Up @@ -174,7 +174,7 @@ void postProcessPartitions() {
TopologyComparators.SortAction sortAction = TopologyComparators.SortAction.getSortAction();

sortAction.sort(getPartitions());
getPartitions().updateCache();
getPartitions().updateReadView();
}

public boolean canContribute() {
Expand Down