30
30
import org .elasticsearch .common .util .set .Sets ;
31
31
32
32
import java .util .Collection ;
33
- import java .util .Comparator ;
33
+ import java .util .Collections ;
34
34
import java .util .Set ;
35
35
import java .util .TreeSet ;
36
36
import java .util .stream .Collectors ;
@@ -106,7 +106,8 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
106
106
* There are three true/false properties of each node in play: live/non-live, retired/non-retired and in-config/not-in-config.
107
107
* Firstly we divide the nodes into disjoint sets based on these properties:
108
108
*
109
- * - nonRetiredInConfigNotLiveIds
109
+ * - nonRetiredMaster
110
+ * - nonRetiredNotMasterInConfigNotLiveIds
110
111
* - nonRetiredInConfigLiveIds
111
112
* - nonRetiredLiveNotInConfigIds
112
113
*
@@ -127,9 +128,20 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
127
128
final Set <String > nonRetiredInConfigNotLiveIds = new TreeSet <>(inConfigNotLiveIds );
128
129
nonRetiredInConfigNotLiveIds .removeAll (retiredNodeIds );
129
130
130
- final Set <String > nonRetiredInConfigLiveIds = masterFirstTreeSet (liveInConfigIds , currentMaster );
131
+ final Set <String > nonRetiredInConfigLiveIds = new TreeSet <> (liveInConfigIds );
131
132
nonRetiredInConfigLiveIds .removeAll (retiredNodeIds );
132
133
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
+
133
145
final Set <String > nonRetiredLiveNotInConfigIds = Sets .sortedDifference (liveNodeIds , currentConfig .getNodeIds ());
134
146
nonRetiredLiveNotInConfigIds .removeAll (retiredNodeIds );
135
147
@@ -156,9 +168,9 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
156
168
* The new configuration is formed by taking this many nodes in the following preference order:
157
169
*/
158
170
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 ()));
162
174
163
175
if (newConfig .hasQuorum (liveNodeIds )) {
164
176
return newConfig ;
@@ -167,12 +179,4 @@ public VotingConfiguration reconfigure(Set<DiscoveryNode> liveNodes, Set<String>
167
179
return currentConfig ;
168
180
}
169
181
}
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
- }
178
182
}
0 commit comments