Skip to content

Commit 4d0c253

Browse files
committed
Add "hasAnyIndices" method to Metadata
If all we want to know is whether the cluster has any indices, then this new method is more efficient than using the existing "getTotalNumberOfIndices" method
1 parent 597ab8a commit 4d0c253

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,18 @@ public int getTotalNumberOfIndices() {
550550
return indexCount;
551551
}
552552

553+
/**
554+
* @return {code true} if there are any indices in any project in this cluster
555+
*/
556+
public boolean hasAnyIndices() {
557+
for (ProjectMetadata project : projects().values()) {
558+
if (project.indices().isEmpty() == false) {
559+
return true;
560+
}
561+
}
562+
return false;
563+
}
564+
553565
/**
554566
* @return The oldest {@link IndexVersion} of indices across all projects
555567
*/

server/src/main/java/org/elasticsearch/cluster/routing/allocation/NodeAllocationStatsAndWeightsCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public Map<String, NodeAllocationStatsAndWeight> nodesAllocationStatsAndWeights(
7070
ClusterInfo clusterInfo,
7171
@Nullable DesiredBalance desiredBalance
7272
) {
73-
if (metadata.indices().isEmpty() == false) {
73+
if (metadata.hasAnyIndices()) {
7474
// must not use licensed features when just starting up
7575
writeLoadForecaster.refreshLicense();
7676
}

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public BalancedShardsAllocator(ClusterSettings clusterSettings, WriteLoadForecas
147147

148148
@Override
149149
public void allocate(RoutingAllocation allocation) {
150-
if (allocation.metadata().indices().isEmpty() == false) {
150+
if (allocation.metadata().hasAnyIndices()) {
151151
// must not use licensed features when just starting up
152152
writeLoadForecaster.refreshLicense();
153153
}

0 commit comments

Comments
 (0)