@@ -33,8 +33,9 @@ use std::{collections::HashMap, sync::RwLock};
3333
3434use postgres:: config:: { Config , Host } ;
3535
36+ use crate :: advisory_lock:: with_migration_lock;
37+ use crate :: catalog;
3638use crate :: primary:: { self , NAMESPACE_PUBLIC } ;
37- use crate :: { advisory_lock, catalog} ;
3839use crate :: { Shard , PRIMARY_SHARD } ;
3940
4041/// Tables that we map from the primary into `primary_public` in each shard
@@ -1113,8 +1114,6 @@ impl PoolInner {
11131114
11141115 let start = Instant :: now ( ) ;
11151116
1116- advisory_lock:: lock_migration ( & mut conn)
1117- . unwrap_or_else ( |err| die ( & pool. logger , "failed to get migration lock" , & err) ) ;
11181117 // This code can cause a race in database setup: if pool A has had
11191118 // schema changes and pool B then tries to map tables from pool A,
11201119 // but does so before the concurrent thread running this code for
@@ -1128,13 +1127,10 @@ impl PoolInner {
11281127 // the schema but before finishing remapping in all shards.
11291128 // Addressing that would require keeping track of the need to remap
11301129 // in the database instead of just in memory
1131- let result = pool
1132- . configure_fdw ( coord. servers . as_ref ( ) )
1133- . and_then ( |( ) | pool. drop_cross_shard_views ( ) )
1134- . and_then ( |( ) | migrate_schema ( & pool. logger , & mut conn) ) ;
1135- debug ! ( & pool. logger, "Release migration lock" ) ;
1136- advisory_lock:: unlock_migration ( & mut conn) . unwrap_or_else ( |err| {
1137- die ( & pool. logger , "failed to release migration lock" , & err) ;
1130+ let result = with_migration_lock ( & mut conn, |conn| {
1131+ pool. configure_fdw ( coord. servers . as_ref ( ) )
1132+ . and_then ( |( ) | pool. drop_cross_shard_views ( ) )
1133+ . and_then ( |( ) | migrate_schema ( & pool. logger , conn) )
11381134 } ) ;
11391135 let result = result
11401136 . and_then ( |count| coord. propagate ( & pool, count) )
@@ -1451,12 +1447,7 @@ impl PoolCoordinator {
14511447 let server = self . server ( & pool. shard ) ?;
14521448 for pool in self . pools . lock ( ) . unwrap ( ) . values ( ) {
14531449 let mut conn = pool. get ( ) ?;
1454- let remap_res = {
1455- advisory_lock:: lock_migration ( & mut conn) ?;
1456- let res = pool. remap ( server) ;
1457- advisory_lock:: unlock_migration ( & mut conn) ?;
1458- res
1459- } ;
1450+ let remap_res = { with_migration_lock ( & mut conn, |_| pool. remap ( server) ) } ;
14601451 if let Err ( e) = remap_res {
14611452 error ! ( pool. logger, "Failed to map imports from {}" , server. shard; "error" => e. to_string( ) ) ;
14621453 return Err ( e) ;
0 commit comments