@@ -93,16 +93,30 @@ public class OperationRouting {
93
93
94
94
public static final Setting <Boolean > STRICT_WEIGHTED_SHARD_ROUTING_ENABLED = Setting .boolSetting (
95
95
"cluster.routing.weighted.strict" ,
96
+ true ,
97
+ Setting .Property .Dynamic ,
98
+ Setting .Property .NodeScope
99
+ );
100
+
101
+ public static final Setting <Boolean > IGNORE_WEIGHTED_SHARD_ROUTING = Setting .boolSetting (
102
+ "cluster.routing.ignore_weighted_routing" ,
96
103
false ,
97
104
Setting .Property .Dynamic ,
98
105
Setting .Property .NodeScope
99
106
);
107
+
108
+ private static final List <Preference > WEIGHTED_ROUTING_RESTRICTED_PREFERENCES = Arrays .asList (
109
+ Preference .ONLY_NODES ,
110
+ Preference .PREFER_NODES
111
+ );
112
+
100
113
private volatile List <String > awarenessAttributes ;
101
114
private volatile boolean useAdaptiveReplicaSelection ;
102
115
private volatile boolean ignoreAwarenessAttr ;
103
116
private volatile double weightedRoutingDefaultWeight ;
104
117
private volatile boolean isFailOpenEnabled ;
105
118
private volatile boolean isStrictWeightedShardRouting ;
119
+ private volatile boolean ignoreWeightedRouting ;
106
120
107
121
public OperationRouting (Settings settings , ClusterSettings clusterSettings ) {
108
122
// whether to ignore awareness attributes when routing requests
@@ -116,11 +130,13 @@ public OperationRouting(Settings settings, ClusterSettings clusterSettings) {
116
130
this .weightedRoutingDefaultWeight = WEIGHTED_ROUTING_DEFAULT_WEIGHT .get (settings );
117
131
this .isFailOpenEnabled = WEIGHTED_ROUTING_FAILOPEN_ENABLED .get (settings );
118
132
this .isStrictWeightedShardRouting = STRICT_WEIGHTED_SHARD_ROUTING_ENABLED .get (settings );
133
+ this .ignoreWeightedRouting = IGNORE_WEIGHTED_SHARD_ROUTING .get (settings );
119
134
clusterSettings .addSettingsUpdateConsumer (USE_ADAPTIVE_REPLICA_SELECTION_SETTING , this ::setUseAdaptiveReplicaSelection );
120
135
clusterSettings .addSettingsUpdateConsumer (IGNORE_AWARENESS_ATTRIBUTES_SETTING , this ::setIgnoreAwarenessAttributes );
121
136
clusterSettings .addSettingsUpdateConsumer (WEIGHTED_ROUTING_DEFAULT_WEIGHT , this ::setWeightedRoutingDefaultWeight );
122
137
clusterSettings .addSettingsUpdateConsumer (WEIGHTED_ROUTING_FAILOPEN_ENABLED , this ::setFailOpenEnabled );
123
138
clusterSettings .addSettingsUpdateConsumer (STRICT_WEIGHTED_SHARD_ROUTING_ENABLED , this ::setStrictWeightedShardRouting );
139
+ clusterSettings .addSettingsUpdateConsumer (IGNORE_WEIGHTED_SHARD_ROUTING , this ::setIgnoreWeightedRouting );
124
140
}
125
141
126
142
void setUseAdaptiveReplicaSelection (boolean useAdaptiveReplicaSelection ) {
@@ -143,6 +159,10 @@ void setStrictWeightedShardRouting(boolean strictWeightedShardRouting) {
143
159
this .isStrictWeightedShardRouting = strictWeightedShardRouting ;
144
160
}
145
161
162
+ void setIgnoreWeightedRouting (boolean isWeightedRoundRobinEnabled ) {
163
+ this .ignoreWeightedRouting = isWeightedRoundRobinEnabled ;
164
+ }
165
+
146
166
public boolean isIgnoreAwarenessAttr () {
147
167
return ignoreAwarenessAttr ;
148
168
}
@@ -314,11 +334,7 @@ private ShardIterator preferenceActiveShardIterator(
314
334
}
315
335
}
316
336
preferenceType = Preference .parse (preference );
317
- if (weightedRoutingMetadata != null && weightedRoutingMetadata .getWeightedRouting ().isSet () && isStrictWeightedShardRouting ) {
318
- throw new PreferenceBasedSearchNotAllowedException (
319
- "Preference type based routing not allowed with strict weighted shard routing enabled"
320
- );
321
- }
337
+ checkPreferenceBasedRoutingAllowed (preferenceType , weightedRoutingMetadata );
322
338
switch (preferenceType ) {
323
339
case PREFER_NODES :
324
340
final Set <String > nodesIds = Arrays .stream (preference .substring (Preference .PREFER_NODES .type ().length () + 1 ).split ("," ))
@@ -344,11 +360,16 @@ private ShardIterator preferenceActiveShardIterator(
344
360
// for a different element in the list by also incorporating the
345
361
// shard ID into the hash of the user-supplied preference key.
346
362
routingHash = 31 * routingHash + indexShard .shardId .hashCode ();
347
- if (weightedRoutingMetadata != null && weightedRoutingMetadata .getWeightedRouting ().isSet () && isStrictWeightedShardRouting ) {
348
- return indexShard .activeInitializingShardsSimpleWeightedIt (
363
+ if (WeightedRoutingUtils .shouldPerformStrictWeightedRouting (
364
+ isStrictWeightedShardRouting ,
365
+ ignoreWeightedRouting ,
366
+ weightedRoutingMetadata
367
+ )) {
368
+ return indexShard .activeInitializingShardsWeightedIt (
349
369
weightedRoutingMetadata .getWeightedRouting (),
350
370
nodes ,
351
371
getWeightedRoutingDefaultWeight (),
372
+ isFailOpenEnabled ,
352
373
routingHash
353
374
);
354
375
} else if (ignoreAwarenessAttributes ()) {
@@ -365,12 +386,13 @@ private ShardIterator shardRoutings(
365
386
@ Nullable Map <String , Long > nodeCounts ,
366
387
@ Nullable WeightedRoutingMetadata weightedRoutingMetadata
367
388
) {
368
- if (weightedRoutingMetadata != null && weightedRoutingMetadata . getWeightedRouting (). isSet ( )) {
389
+ if (WeightedRoutingUtils . shouldPerformWeightedRouting ( ignoreWeightedRouting , weightedRoutingMetadata )) {
369
390
return indexShard .activeInitializingShardsWeightedIt (
370
391
weightedRoutingMetadata .getWeightedRouting (),
371
392
nodes ,
372
393
getWeightedRoutingDefaultWeight (),
373
- isFailOpenEnabled
394
+ isFailOpenEnabled ,
395
+ null
374
396
);
375
397
} else if (ignoreAwarenessAttributes ()) {
376
398
if (useAdaptiveReplicaSelection ) {
@@ -438,4 +460,15 @@ private static int calculateScaledShardId(IndexMetadata indexMetadata, String ef
438
460
return Math .floorMod (hash , indexMetadata .getRoutingNumShards ()) / indexMetadata .getRoutingFactor ();
439
461
}
440
462
463
+ private void checkPreferenceBasedRoutingAllowed (Preference preference , @ Nullable WeightedRoutingMetadata weightedRoutingMetadata ) {
464
+ if (WeightedRoutingUtils .shouldPerformStrictWeightedRouting (
465
+ isStrictWeightedShardRouting ,
466
+ ignoreWeightedRouting ,
467
+ weightedRoutingMetadata
468
+ ) && WEIGHTED_ROUTING_RESTRICTED_PREFERENCES .contains (preference )) {
469
+ throw new PreferenceBasedSearchNotAllowedException (
470
+ "Preference type based routing not allowed with strict weighted shard routing enabled"
471
+ );
472
+ }
473
+ }
441
474
}
0 commit comments