Skip to content

Commit 4e5886c

Browse files
committed
moar sets
1 parent 7300c4b commit 4e5886c

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

server/src/main/java/org/elasticsearch/cluster/coordination/Reconfigurator.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.elasticsearch.common.util.set.Sets;
3131

3232
import java.util.Collection;
33-
import java.util.Comparator;
33+
import java.util.Collections;
3434
import java.util.Set;
3535
import java.util.TreeSet;
3636
import java.util.stream.Collectors;
@@ -106,7 +106,8 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
106106
* There are three true/false properties of each node in play: live/non-live, retired/non-retired and in-config/not-in-config.
107107
* Firstly we divide the nodes into disjoint sets based on these properties:
108108
*
109-
* - nonRetiredInConfigNotLiveIds
109+
* - nonRetiredMaster
110+
* - nonRetiredNotMasterInConfigNotLiveIds
110111
* - nonRetiredInConfigLiveIds
111112
* - nonRetiredLiveNotInConfigIds
112113
*
@@ -127,9 +128,20 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
127128
final Set<String> nonRetiredInConfigNotLiveIds = new TreeSet<>(inConfigNotLiveIds);
128129
nonRetiredInConfigNotLiveIds.removeAll(retiredNodeIds);
129130

130-
final Set<String> nonRetiredInConfigLiveIds = masterFirstTreeSet(liveInConfigIds, currentMaster);
131+
final Set<String> nonRetiredInConfigLiveIds = new TreeSet<>(liveInConfigIds);
131132
nonRetiredInConfigLiveIds.removeAll(retiredNodeIds);
132133

134+
final Set<String> nonRetiredInConfigLiveMasterIds;
135+
final Set<String> nonRetiredInConfigLiveNotMasterIds;
136+
if (nonRetiredInConfigLiveIds.contains(currentMaster.getId())) {
137+
nonRetiredInConfigLiveNotMasterIds = new TreeSet<>(nonRetiredInConfigLiveIds);
138+
nonRetiredInConfigLiveNotMasterIds.remove(currentMaster.getId());
139+
nonRetiredInConfigLiveMasterIds = Collections.singleton(currentMaster.getId());
140+
} else {
141+
nonRetiredInConfigLiveNotMasterIds = nonRetiredInConfigLiveIds;
142+
nonRetiredInConfigLiveMasterIds = Collections.emptySet();
143+
}
144+
133145
final Set<String> nonRetiredLiveNotInConfigIds = Sets.sortedDifference(liveNodeIds, currentConfig.getNodeIds());
134146
nonRetiredLiveNotInConfigIds.removeAll(retiredNodeIds);
135147

@@ -156,9 +168,9 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
156168
* The new configuration is formed by taking this many nodes in the following preference order:
157169
*/
158170
final VotingConfiguration newConfig = new VotingConfiguration(
159-
// live nodes first, preferring the current config, and if we need more then use non-live nodes
160-
Stream.of(nonRetiredInConfigLiveIds, nonRetiredLiveNotInConfigIds, nonRetiredInConfigNotLiveIds)
161-
.flatMap(Collection::stream).limit(targetSize).collect(Collectors.toSet()));
171+
// live master first, then other live nodes, preferring the current config, and if we need more then use non-live nodes
172+
Stream.of(nonRetiredInConfigLiveMasterIds, nonRetiredInConfigLiveNotMasterIds, nonRetiredLiveNotInConfigIds,
173+
nonRetiredInConfigNotLiveIds).flatMap(Collection::stream).limit(targetSize).collect(Collectors.toSet()));
162174

163175
if (newConfig.hasQuorum(liveNodeIds)) {
164176
return newConfig;
@@ -167,12 +179,4 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
167179
return currentConfig;
168180
}
169181
}
170-
171-
private TreeSet<String> masterFirstTreeSet(Collection<? extends String> items, DiscoveryNode masterNode) {
172-
final String masterNodeId = masterNode.getId();
173-
final TreeSet<String> set = new TreeSet<>(Comparator.<String>comparingInt(s -> s.equals(masterNodeId) ? 0 : 1)
174-
.thenComparing(Comparator.naturalOrder()));
175-
set.addAll(items);
176-
return set;
177-
}
178182
}

0 commit comments

Comments
 (0)