9
9
10
10
package org .elasticsearch .cluster .routing ;
11
11
12
- import org .elasticsearch .cluster .ClusterInfo ;
13
12
import org .elasticsearch .cluster .metadata .IndexMetadata ;
14
13
import org .elasticsearch .cluster .metadata .Metadata ;
15
14
import org .elasticsearch .cluster .metadata .ProjectMetadata ;
22
21
23
22
public class ExpectedShardSizeEstimator {
24
23
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
+
25
54
public static boolean shouldReserveSpaceForInitializingShard (ShardRouting shard , RoutingAllocation allocation ) {
26
55
return shouldReserveSpaceForInitializingShard (shard , allocation .metadata ());
27
56
}
@@ -69,7 +98,7 @@ public static boolean shouldReserveSpaceForInitializingShard(ShardRouting shard,
69
98
public static long getExpectedShardSize (
70
99
ShardRouting shard ,
71
100
long defaultValue ,
72
- ClusterInfo clusterInfo ,
101
+ ShardSizeProvider shardSizeProvider ,
73
102
SnapshotShardSizeInfo snapshotShardSizeInfo ,
74
103
ProjectMetadata projectMetadata ,
75
104
RoutingTable routingTable
@@ -79,15 +108,15 @@ public static long getExpectedShardSize(
79
108
&& shard .active () == false
80
109
&& shard .recoverySource ().getType () == RecoverySource .Type .LOCAL_SHARDS ) {
81
110
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 );
83
112
} else if (shard .active () == false && shard .recoverySource ().getType () == RecoverySource .Type .SNAPSHOT ) {
84
113
assert shard .primary () : "All replica shards are recovering from " + RecoverySource .Type .PEER ;
85
114
return snapshotShardSizeInfo .getShardSize (shard , defaultValue );
86
115
} else {
87
- var shardSize = clusterInfo .getShardSize (shard .shardId (), shard .primary ());
116
+ var shardSize = shardSizeProvider .getShardSize (shard .shardId (), shard .primary ());
88
117
if (shardSize == null && shard .primary () == false ) {
89
118
// derive replica size from corresponding primary
90
- shardSize = clusterInfo .getShardSize (shard .shardId (), true );
119
+ shardSize = shardSizeProvider .getShardSize (shard .shardId (), true );
91
120
}
92
121
return shardSize == null ? defaultValue : shardSize ;
93
122
}
@@ -97,7 +126,7 @@ private static long getExpectedSizeOfResizedShard(
97
126
ShardRouting shard ,
98
127
long defaultValue ,
99
128
IndexMetadata indexMetadata ,
100
- ClusterInfo clusterInfo ,
129
+ ShardSizeProvider shardSizeProvider ,
101
130
ProjectMetadata projectMetadata ,
102
131
RoutingTable routingTable
103
132
) {
@@ -120,7 +149,7 @@ private static long getExpectedSizeOfResizedShard(
120
149
for (int i = 0 ; i < indexRoutingTable .size (); i ++) {
121
150
IndexShardRoutingTable shardRoutingTable = indexRoutingTable .shard (i );
122
151
if (shardIds .contains (shardRoutingTable .shardId ())) {
123
- targetShardSize += clusterInfo .getShardSize (shardRoutingTable .primaryShard (), 0 );
152
+ targetShardSize += shardSizeProvider .getShardSize (shardRoutingTable .primaryShard (), 0 );
124
153
}
125
154
}
126
155
}
0 commit comments