Skip to content

Commit 6cff547

Browse files
authored
Add ShardSizeProvider interface for ClusterInfo (#135751)
So that we can pass a pared down implementation instead of the full ClusterInfo which can be expensive sometimes. Relates: ES-12723
1 parent bd0d1ac commit 6cff547

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

server/src/main/java/org/elasticsearch/cluster/ClusterInfo.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.TransportVersion;
1313
import org.elasticsearch.TransportVersions;
14+
import org.elasticsearch.cluster.routing.ExpectedShardSizeEstimator;
1415
import org.elasticsearch.cluster.routing.RecoverySource;
1516
import org.elasticsearch.cluster.routing.ShardRouting;
1617
import org.elasticsearch.cluster.routing.UnassignedInfo;
@@ -45,7 +46,7 @@
4546
* <code>InternalClusterInfoService.shardIdentifierFromRouting(String)</code>
4647
* for the key used in the shardSizes map
4748
*/
48-
public class ClusterInfo implements ChunkedToXContent, Writeable {
49+
public class ClusterInfo implements ChunkedToXContent, Writeable, ExpectedShardSizeEstimator.ShardSizeProvider {
4950

5051
public static final ClusterInfo EMPTY = new ClusterInfo();
5152

@@ -289,33 +290,11 @@ public Map<ShardId, Double> getShardWriteLoads() {
289290
/**
290291
* Returns the shard size for the given shardId or <code>null</code> if that metric is not available.
291292
*/
293+
@Override
292294
public Long getShardSize(ShardId shardId, boolean primary) {
293295
return shardSizes.get(shardIdentifierFromRouting(shardId, primary));
294296
}
295297

296-
/**
297-
* Returns the shard size for the given shard routing or <code>null</code> if that metric is not available.
298-
*/
299-
public Long getShardSize(ShardRouting shardRouting) {
300-
return getShardSize(shardRouting.shardId(), shardRouting.primary());
301-
}
302-
303-
/**
304-
* Returns the shard size for the given shard routing or <code>defaultValue</code> it that metric is not available.
305-
*/
306-
public long getShardSize(ShardRouting shardRouting, long defaultValue) {
307-
Long shardSize = getShardSize(shardRouting);
308-
return shardSize == null ? defaultValue : shardSize;
309-
}
310-
311-
/**
312-
* Returns the shard size for the given shard routing or <code>defaultValue</code> it that metric is not available.
313-
*/
314-
public long getShardSize(ShardId shardId, boolean primary, long defaultValue) {
315-
Long shardSize = getShardSize(shardId, primary);
316-
return shardSize == null ? defaultValue : shardSize;
317-
}
318-
319298
/**
320299
* Returns the nodes absolute data-path the given shard is allocated on or <code>null</code> if the information is not available.
321300
*/

server/src/main/java/org/elasticsearch/cluster/ClusterInfoSimulator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void simulateShardStarted(ShardRouting shard) {
101101
var size = getExpectedShardSize(
102102
shard,
103103
shard.getExpectedShardSize(),
104-
getClusterInfo(),
104+
(shardId, primary) -> shardSizes.get(shardIdentifierFromRouting(shardId, primary)),
105105
allocation.snapshotShardSizeInfo(),
106106
project,
107107
allocation.routingTable(project.id())

server/src/main/java/org/elasticsearch/cluster/routing/ExpectedShardSizeEstimator.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
package org.elasticsearch.cluster.routing;
1111

12-
import org.elasticsearch.cluster.ClusterInfo;
1312
import org.elasticsearch.cluster.metadata.IndexMetadata;
1413
import org.elasticsearch.cluster.metadata.Metadata;
1514
import org.elasticsearch.cluster.metadata.ProjectMetadata;
@@ -22,6 +21,36 @@
2221

2322
public class ExpectedShardSizeEstimator {
2423

24+
public interface ShardSizeProvider {
25+
/**
26+
* Returns the shard size for the given shardId or <code>null</code> if that metric is not available.
27+
*/
28+
Long getShardSize(ShardId shardId, boolean primary);
29+
30+
/**
31+
* Returns the shard size for the given shard routing or <code>null</code> if that metric is not available.
32+
*/
33+
default Long getShardSize(ShardRouting shardRouting) {
34+
return getShardSize(shardRouting.shardId(), shardRouting.primary());
35+
}
36+
37+
/**
38+
* Returns the shard size for the given shard routing or <code>defaultValue</code> it that metric is not available.
39+
*/
40+
default long getShardSize(ShardRouting shardRouting, long defaultValue) {
41+
final var shardSize = getShardSize(shardRouting);
42+
return shardSize == null ? defaultValue : shardSize;
43+
}
44+
45+
/**
46+
* Returns the shard size for the given shard routing or <code>defaultValue</code> it that metric is not available.
47+
*/
48+
default long getShardSize(ShardId shardId, boolean primary, long defaultValue) {
49+
final var shardSize = getShardSize(shardId, primary);
50+
return shardSize == null ? defaultValue : shardSize;
51+
}
52+
}
53+
2554
public static boolean shouldReserveSpaceForInitializingShard(ShardRouting shard, RoutingAllocation allocation) {
2655
return shouldReserveSpaceForInitializingShard(shard, allocation.metadata());
2756
}
@@ -69,7 +98,7 @@ public static boolean shouldReserveSpaceForInitializingShard(ShardRouting shard,
6998
public static long getExpectedShardSize(
7099
ShardRouting shard,
71100
long defaultValue,
72-
ClusterInfo clusterInfo,
101+
ShardSizeProvider shardSizeProvider,
73102
SnapshotShardSizeInfo snapshotShardSizeInfo,
74103
ProjectMetadata projectMetadata,
75104
RoutingTable routingTable
@@ -79,15 +108,15 @@ public static long getExpectedShardSize(
79108
&& shard.active() == false
80109
&& shard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
81110
assert shard.primary() : "All replica shards are recovering from " + RecoverySource.Type.PEER;
82-
return getExpectedSizeOfResizedShard(shard, defaultValue, indexMetadata, clusterInfo, projectMetadata, routingTable);
111+
return getExpectedSizeOfResizedShard(shard, defaultValue, indexMetadata, shardSizeProvider, projectMetadata, routingTable);
83112
} else if (shard.active() == false && shard.recoverySource().getType() == RecoverySource.Type.SNAPSHOT) {
84113
assert shard.primary() : "All replica shards are recovering from " + RecoverySource.Type.PEER;
85114
return snapshotShardSizeInfo.getShardSize(shard, defaultValue);
86115
} else {
87-
var shardSize = clusterInfo.getShardSize(shard.shardId(), shard.primary());
116+
var shardSize = shardSizeProvider.getShardSize(shard.shardId(), shard.primary());
88117
if (shardSize == null && shard.primary() == false) {
89118
// derive replica size from corresponding primary
90-
shardSize = clusterInfo.getShardSize(shard.shardId(), true);
119+
shardSize = shardSizeProvider.getShardSize(shard.shardId(), true);
91120
}
92121
return shardSize == null ? defaultValue : shardSize;
93122
}
@@ -97,7 +126,7 @@ private static long getExpectedSizeOfResizedShard(
97126
ShardRouting shard,
98127
long defaultValue,
99128
IndexMetadata indexMetadata,
100-
ClusterInfo clusterInfo,
129+
ShardSizeProvider shardSizeProvider,
101130
ProjectMetadata projectMetadata,
102131
RoutingTable routingTable
103132
) {
@@ -120,7 +149,7 @@ private static long getExpectedSizeOfResizedShard(
120149
for (int i = 0; i < indexRoutingTable.size(); i++) {
121150
IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(i);
122151
if (shardIds.contains(shardRoutingTable.shardId())) {
123-
targetShardSize += clusterInfo.getShardSize(shardRoutingTable.primaryShard(), 0);
152+
targetShardSize += shardSizeProvider.getShardSize(shardRoutingTable.primaryShard(), 0);
124153
}
125154
}
126155
}

0 commit comments

Comments
 (0)