99
1010package  org .elasticsearch .cluster .routing ;
1111
12- import  org .elasticsearch .cluster .ClusterInfo ;
1312import  org .elasticsearch .cluster .metadata .IndexMetadata ;
1413import  org .elasticsearch .cluster .metadata .Metadata ;
1514import  org .elasticsearch .cluster .metadata .ProjectMetadata ;
2221
2322public  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