Skip to content

Commit 035f2ef

Browse files
committed
remove switch
1 parent e026993 commit 035f2ef

File tree

1 file changed

+106
-49
lines changed

1 file changed

+106
-49
lines changed

server/src/main/java/org/elasticsearch/indices/ShardLimitValidator.java

Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,94 @@ static String errorMessageFrom(Result result) {
249249
}
250250

251251
public enum LimitGroup {
252-
NORMAL(NORMAL_GROUP),
253-
FROZEN(FROZEN_GROUP),
254-
INDEX("index"),
255-
SEARCH("search");
252+
NORMAL(NORMAL_GROUP) {
253+
@Override
254+
public int numberOfNodes(DiscoveryNodes discoveryNodes) {
255+
return nodeCount(discoveryNodes, ShardLimitValidator::hasNonFrozen);
256+
}
257+
258+
@Override
259+
public int countShards(IndexMetadata indexMetadata) {
260+
return isOpenIndex(indexMetadata) && matchesIndexSettingGroup(indexMetadata, LimitGroup.NORMAL.groupName())
261+
? indexMetadata.getTotalNumberOfShards()
262+
: 0;
263+
}
264+
265+
@Override
266+
public int newShardsTotal(int shards, int replicas) {
267+
return shards * (1 + replicas);
268+
}
269+
270+
@Override
271+
protected int newReplicaShards(boolean isFrozenIndex, int shards, int replicaIncrease) {
272+
return isFrozenIndex ? 0 : shards * replicaIncrease;
273+
}
274+
},
275+
FROZEN(FROZEN_GROUP) {
276+
@Override
277+
public int numberOfNodes(DiscoveryNodes discoveryNodes) {
278+
return nodeCount(discoveryNodes, ShardLimitValidator::hasFrozen);
279+
}
280+
281+
@Override
282+
public int countShards(IndexMetadata indexMetadata) {
283+
return isOpenIndex(indexMetadata) && matchesIndexSettingGroup(indexMetadata, LimitGroup.FROZEN.groupName())
284+
? indexMetadata.getTotalNumberOfShards()
285+
: 0;
286+
}
287+
288+
@Override
289+
public int newShardsTotal(int shards, int replicas) {
290+
return shards * (1 + replicas);
291+
}
292+
293+
@Override
294+
protected int newReplicaShards(boolean isFrozenIndex, int shards, int replicaIncrease) {
295+
return isFrozenIndex ? shards * replicaIncrease : 0;
296+
}
297+
},
298+
INDEX("index") {
299+
@Override
300+
public int numberOfNodes(DiscoveryNodes discoveryNodes) {
301+
return nodeCount(discoveryNodes, node -> node.hasRole(DiscoveryNodeRole.INDEX_ROLE.roleName()));
302+
}
303+
304+
@Override
305+
public int countShards(IndexMetadata indexMetadata) {
306+
return isOpenIndex(indexMetadata) ? indexMetadata.getNumberOfShards() : 0;
307+
}
308+
309+
@Override
310+
public int newShardsTotal(int shards, int replicas) {
311+
return shards;
312+
}
313+
314+
@Override
315+
protected int newReplicaShards(boolean isFrozenIndex, int shards, int replicaIncrease) {
316+
return 0;
317+
}
318+
},
319+
SEARCH("search") {
320+
@Override
321+
public int numberOfNodes(DiscoveryNodes discoveryNodes) {
322+
return nodeCount(discoveryNodes, node -> node.hasRole(DiscoveryNodeRole.SEARCH_ROLE.roleName()));
323+
}
324+
325+
@Override
326+
public int countShards(IndexMetadata indexMetadata) {
327+
return isOpenIndex(indexMetadata) ? indexMetadata.getNumberOfShards() * indexMetadata.getNumberOfReplicas() : 0;
328+
}
329+
330+
@Override
331+
public int newShardsTotal(int shards, int replicas) {
332+
return shards * replicas;
333+
}
334+
335+
@Override
336+
protected int newReplicaShards(boolean isFrozenIndex, int shards, int replicaIncrease) {
337+
return shards * replicaIncrease;
338+
}
339+
};
256340

257341
private final String groupName;
258342

@@ -269,27 +353,20 @@ public String toString() {
269353
return groupName;
270354
}
271355

272-
public int numberOfNodes(DiscoveryNodes discoveryNodes) {
273-
return switch (this) {
274-
case NORMAL -> nodeCount(discoveryNodes, ShardLimitValidator::hasNonFrozen);
275-
case FROZEN -> nodeCount(discoveryNodes, ShardLimitValidator::hasFrozen);
276-
case INDEX -> nodeCount(discoveryNodes, node -> node.hasRole(DiscoveryNodeRole.INDEX_ROLE.roleName()));
277-
case SEARCH -> nodeCount(discoveryNodes, node -> node.hasRole(DiscoveryNodeRole.SEARCH_ROLE.roleName()));
278-
};
279-
}
356+
public abstract int numberOfNodes(DiscoveryNodes discoveryNodes);
280357

281-
public int countShards(IndexMetadata indexMetadata) {
282-
return switch (this) {
283-
case NORMAL -> isOpenIndex(indexMetadata) && matchesIndexSettingGroup(indexMetadata, LimitGroup.NORMAL.groupName())
284-
? indexMetadata.getTotalNumberOfShards()
285-
: 0;
286-
case FROZEN -> isOpenIndex(indexMetadata) && matchesIndexSettingGroup(indexMetadata, LimitGroup.FROZEN.groupName())
287-
? indexMetadata.getTotalNumberOfShards()
288-
: 0;
289-
case INDEX -> isOpenIndex(indexMetadata) ? indexMetadata.getNumberOfShards() : 0;
290-
case SEARCH -> isOpenIndex(indexMetadata) ? indexMetadata.getNumberOfShards() * indexMetadata.getNumberOfReplicas() : 0;
291-
};
292-
}
358+
public abstract int countShards(IndexMetadata indexMetadata);
359+
360+
/**
361+
* Compute the total number of new shards including both primaries and replicas that would be created for the given
362+
* number of shards and replicas in this group.
363+
* @param shards Number of primary shards
364+
* @param replicas Number of replica shards per primary
365+
* @return Number of total new shards to be created for the group.
366+
*/
367+
public abstract int newShardsTotal(int shards, int replicas);
368+
369+
protected abstract int newReplicaShards(boolean isFrozenIndex, int shards, int replicaIncrease);
293370

294371
/**
295372
* Compute the total number of new shards including both primaries and replicas that would be created for an index with the
@@ -298,9 +375,9 @@ public int countShards(IndexMetadata indexMetadata) {
298375
* @return The total number of new shards to be created for this group.
299376
*/
300377
public int newShardsTotal(Settings indexSettings) {
301-
final boolean isFrozenLimitGroup = FROZEN_GROUP.equals(INDEX_SETTING_SHARD_LIMIT_GROUP.get(indexSettings));
302-
final int numberOfShards = (isFrozenLimitGroup == (this == FROZEN)) ? INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettings) : 0;
303-
final int numberOfReplicas = (isFrozenLimitGroup == (this == FROZEN))
378+
final boolean isFrozenIndex = FROZEN_GROUP.equals(INDEX_SETTING_SHARD_LIMIT_GROUP.get(indexSettings));
379+
final int numberOfShards = (isFrozenIndex == (this == FROZEN)) ? INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettings) : 0;
380+
final int numberOfReplicas = (isFrozenIndex == (this == FROZEN))
304381
? IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(indexSettings)
305382
: 0;
306383
return newShardsTotal(numberOfShards, numberOfReplicas);
@@ -313,31 +390,11 @@ public int newShardsTotal(Settings indexSettings) {
313390
* @return The number of new replica shards to be created for this group.
314391
*/
315392
public int newShardsTotal(Settings indexSettings, int updatedReplicas) {
316-
final boolean isFrozenLimitGroup = FROZEN_GROUP.equals(INDEX_SETTING_SHARD_LIMIT_GROUP.get(indexSettings));
393+
final boolean isFrozenIndex = FROZEN_GROUP.equals(INDEX_SETTING_SHARD_LIMIT_GROUP.get(indexSettings));
317394
final int shards = INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettings);
318395
final int replicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(indexSettings);
319396
final int replicaIncrease = updatedReplicas - replicas;
320-
return switch (this) {
321-
case NORMAL -> isFrozenLimitGroup ? 0 : shards * replicaIncrease;
322-
case FROZEN -> isFrozenLimitGroup ? shards * replicaIncrease : 0;
323-
case INDEX -> 0;
324-
case SEARCH -> shards * replicaIncrease;
325-
};
326-
}
327-
328-
/**
329-
* Compute the total number of new shards including both primaries and replicas that would be created for the given
330-
* number of shards and replicas in this group.
331-
* @param shards Number of primary shards
332-
* @param replicas Number of replica shards per primary
333-
* @return Number of total new shards to be created for the group.
334-
*/
335-
public int newShardsTotal(int shards, int replicas) {
336-
return switch (this) {
337-
case NORMAL, FROZEN -> shards * (1 + replicas);
338-
case INDEX -> shards;
339-
case SEARCH -> shards * replicas;
340-
};
397+
return newReplicaShards(isFrozenIndex, shards, replicaIncrease);
341398
}
342399

343400
/**

0 commit comments

Comments
 (0)