18
18
import java .util .HashMap ;
19
19
import java .util .List ;
20
20
import java .util .Map ;
21
- import java .util .OptionalInt ;
22
21
import java .util .function .Supplier ;
23
22
24
23
import static org .elasticsearch .cluster .metadata .MetadataIndexStateService .isIndexVerifiedBeforeClosed ;
@@ -101,7 +100,7 @@ public boolean expandToAllNodes() {
101
100
return maxReplicas == Integer .MAX_VALUE ;
102
101
}
103
102
104
- public OptionalInt getDesiredNumberOfReplicas (IndexMetadata indexMetadata , RoutingAllocation allocation ) {
103
+ public int getDesiredNumberOfReplicas (IndexMetadata indexMetadata , RoutingAllocation allocation ) {
105
104
assert enabled : "should only be called when enabled" ;
106
105
int numMatchingDataNodes = 0 ;
107
106
for (DiscoveryNode discoveryNode : allocation .nodes ().getDataNodes ().values ()) {
@@ -110,20 +109,21 @@ public OptionalInt getDesiredNumberOfReplicas(IndexMetadata indexMetadata, Routi
110
109
numMatchingDataNodes ++;
111
110
}
112
111
}
112
+ return calculateDesiredNumberOfReplicas (numMatchingDataNodes );
113
+ }
113
114
115
+ // Package private only for testing purposes!
116
+ int calculateDesiredNumberOfReplicas (int numMatchingDataNodes ) {
114
117
final int min = minReplicas ();
115
118
final int max = getMaxReplicas (numMatchingDataNodes );
116
119
int numberOfReplicas = numMatchingDataNodes - 1 ;
120
+ // Make sure number of replicas is always between min and max
117
121
if (numberOfReplicas < min ) {
118
122
numberOfReplicas = min ;
119
123
} else if (numberOfReplicas > max ) {
120
124
numberOfReplicas = max ;
121
125
}
122
-
123
- if (numberOfReplicas >= min && numberOfReplicas <= max ) {
124
- return OptionalInt .of (numberOfReplicas );
125
- }
126
- return OptionalInt .empty ();
126
+ return numberOfReplicas ;
127
127
}
128
128
129
129
@ Override
@@ -153,11 +153,10 @@ public static Map<Integer, List<String>> getAutoExpandReplicaChanges(
153
153
if (allocation == null ) {
154
154
allocation = allocationSupplier .get ();
155
155
}
156
- autoExpandReplicas .getDesiredNumberOfReplicas (indexMetadata , allocation ).ifPresent (numberOfReplicas -> {
157
- if (numberOfReplicas != indexMetadata .getNumberOfReplicas ()) {
158
- nrReplicasChanged .computeIfAbsent (numberOfReplicas , ArrayList ::new ).add (indexMetadata .getIndex ().getName ());
159
- }
160
- });
156
+ int numberOfReplicas = autoExpandReplicas .getDesiredNumberOfReplicas (indexMetadata , allocation );
157
+ if (numberOfReplicas != indexMetadata .getNumberOfReplicas ()) {
158
+ nrReplicasChanged .computeIfAbsent (numberOfReplicas , ArrayList ::new ).add (indexMetadata .getIndex ().getName ());
159
+ }
161
160
}
162
161
}
163
162
return nrReplicasChanged ;
0 commit comments