@@ -117,7 +117,8 @@ impl ToSql<Text, Pg> for Shard {
117117/// indexers that should index it. The deployment should then be assigned to
118118/// one of the returned indexers.
119119pub trait DeploymentPlacer {
120- fn place ( & self , name : & str , network : & str ) -> Result < Option < ( Shard , Vec < NodeId > ) > , String > ;
120+ fn place ( & self , name : & str , network : & str )
121+ -> Result < Option < ( Vec < Shard > , Vec < NodeId > ) > , String > ;
121122}
122123
123124/// Tools for managing unused deployments
@@ -382,6 +383,31 @@ impl SubgraphStoreInner {
382383 store. find_layout ( site)
383384 }
384385
386+ fn place_on_node (
387+ & self ,
388+ mut nodes : Vec < NodeId > ,
389+ default_node : NodeId ,
390+ ) -> Result < NodeId , StoreError > {
391+ match nodes. len ( ) {
392+ 0 => {
393+ // This is really a configuration error
394+ Ok ( default_node)
395+ }
396+ 1 => Ok ( nodes. pop ( ) . unwrap ( ) ) ,
397+ _ => {
398+ let conn = self . primary_conn ( ) ?;
399+
400+ // unwrap is fine since nodes is not empty
401+ let node = conn. least_assigned_node ( & nodes) ?. unwrap ( ) ;
402+ Ok ( node)
403+ }
404+ }
405+ }
406+
407+ fn place_in_shard ( & self , mut shards : Vec < Shard > ) -> Result < Shard , StoreError > {
408+ Ok ( shards. pop ( ) . unwrap ( ) )
409+ }
410+
385411 fn place (
386412 & self ,
387413 name : & SubgraphName ,
@@ -402,16 +428,10 @@ impl SubgraphStoreInner {
402428
403429 match placement {
404430 None => Ok ( ( PRIMARY_SHARD . clone ( ) , default_node) ) ,
405- Some ( ( _, nodes) ) if nodes. is_empty ( ) => {
406- // This is really a configuration error
407- Ok ( ( PRIMARY_SHARD . clone ( ) , default_node) )
408- }
409- Some ( ( shard, mut nodes) ) if nodes. len ( ) == 1 => Ok ( ( shard, nodes. pop ( ) . unwrap ( ) ) ) ,
410- Some ( ( shard, nodes) ) => {
411- let conn = self . primary_conn ( ) ?;
431+ Some ( ( shards, nodes) ) => {
432+ let node = self . place_on_node ( nodes, default_node) ?;
433+ let shard = self . place_in_shard ( shards) ?;
412434
413- // unwrap is fine since nodes is not empty
414- let node = conn. least_assigned_node ( & nodes) ?. unwrap ( ) ;
415435 Ok ( ( shard, node) )
416436 }
417437 }
0 commit comments