@@ -92,15 +92,16 @@ type PhysRestore struct {
9292 startTS int64
9393 secOpts * topo.MongodOptsSec
9494
95- name string
96- opid string
97- nodeInfo * topo.NodeInfo
98- stg storage.Storage
99- bcpStg storage.Storage
100- bcp * backup.BackupMeta
101- files []files
102- bcpSizeRS int64 // total uncompressed size of the backup for RS (including all increments)
103- restoreTS bson.Timestamp
95+ name string
96+ opid string
97+ nodeInfo * topo.NodeInfo
98+ stg storage.Storage
99+ bcpStg storage.Storage
100+ bcp * backup.BackupMeta
101+ files []files
102+ bcpSizeRS int64 // total uncompressed size of the backup for RS (including all increments)
103+ restoreTS bson.Timestamp
104+ newStorage func () (storage.Storage , error )
104105
105106 confOpts * config.RestoreConf
106107
@@ -120,7 +121,6 @@ type PhysRestore struct {
120121
121122 stopHB chan struct {}
122123 stopCleanupHB chan struct {}
123- hbWG sync.WaitGroup
124124
125125 log log.LogEvent
126126 logBuff * logBuff
@@ -213,9 +213,7 @@ func NewPhysical(
213213 }, nil
214214}
215215
216- func (r * PhysRestore ) closeStoragesAfterHeartbeats () {
217- r .hbWG .Wait ()
218-
216+ func (r * PhysRestore ) closeStorages () {
219217 storage .Close (r .bcpStg , r .log )
220218 r .bcpStg = nil
221219
@@ -289,7 +287,7 @@ func (r *PhysRestore) close(noerr bool, progress nodeStatus) (err error) {
289287 if r .stopCleanupHB != nil {
290288 close (r .stopCleanupHB )
291289 }
292- r .closeStoragesAfterHeartbeats ()
290+ r .closeStorages ()
293291 }()
294292
295293 // resolve and exec cleanup
@@ -1209,6 +1207,7 @@ func (r *PhysRestore) Snapshot(
12091207 defer func () {
12101208 if cmd .Exit && err == nil {
12111209 // nothing to cleanup in case of successful ext restore with exit
1210+ r .closeStorages ()
12121211 return
12131212 }
12141213 if err != nil && ! errors .Is (err , ErrNoDataForShard ) {
@@ -2414,7 +2413,12 @@ func (r *PhysRestore) init(ctx context.Context, name string, opid ctrl.OPID, l l
24142413 return errors .Wrap (err , "get pbm config" )
24152414 }
24162415
2417- r .stg , err = util .StorageFromConfig (& cfg .Storage , r .nodeInfo .Me , l )
2416+ storageConf := cfg .Storage
2417+ r .newStorage = func () (storage.Storage , error ) {
2418+ return util .StorageFromConfig (& storageConf , r .nodeInfo .Me , l )
2419+ }
2420+
2421+ r .stg , err = r .newStorage ()
24182422 if err != nil {
24192423 return errors .Wrap (err , "get storage" )
24202424 }
@@ -2468,26 +2472,31 @@ func (r *PhysRestore) init(ctx context.Context, name string, opid ctrl.OPID, l l
24682472
24692473// startHB starts heartbeats in separate go routine.
24702474func (r * PhysRestore ) startHB (l log.LogEvent ) {
2471- err := r .hb ()
2475+ stg , err := r .newStorage ()
2476+ if err != nil {
2477+ l .Error ("get heartbeat storage: %v" , err )
2478+ return
2479+ }
2480+
2481+ err = r .hb (stg )
24722482 if err != nil {
24732483 l .Error ("send init heartbeat: %v" , err )
24742484 }
24752485
24762486 r .stopHB = make (chan struct {})
2477- r .hbWG .Add (1 )
24782487 go func () {
24792488 tk := time .NewTicker (time .Second * hbFrameSec )
24802489 defer func () {
2481- r .hbWG .Done ()
24822490 tk .Stop ()
2491+ storage .Close (stg , l )
24832492 r .stopHB = nil
24842493 l .Debug ("heartbeats stopped" )
24852494 }()
24862495
24872496 for {
24882497 select {
24892498 case <- tk .C :
2490- err := r .hb ()
2499+ err := r .hb (stg )
24912500 if err != nil {
24922501 l .Warning ("send heartbeat: %v" , err )
24932502 }
@@ -2498,31 +2507,31 @@ func (r *PhysRestore) startHB(l log.LogEvent) {
24982507 }()
24992508}
25002509
2501- func (r * PhysRestore ) hb () error {
2510+ func (r * PhysRestore ) hb (stg storage. Storage ) error {
25022511 now := []byte (strconv .FormatInt (time .Now ().Unix (), 10 ))
25032512
2504- err := storage .RetryableWrite (r . stg , r .syncPathNode + "." + syncHbSuffix , now )
2513+ err := storage .RetryableWrite (stg , r .syncPathNode + "." + syncHbSuffix , now )
25052514 if err != nil {
25062515 return errors .Wrap (err , "write node hb" )
25072516 }
25082517
2509- err = storage .RetryableWrite (r . stg , r .syncPathRS + "." + syncHbSuffix , now )
2518+ err = storage .RetryableWrite (stg , r .syncPathRS + "." + syncHbSuffix , now )
25102519 if err != nil {
25112520 return errors .Wrap (err , "write rs hb" )
25122521 }
25132522
2514- err = storage .RetryableWrite (r . stg , r .syncPathCluster + "." + syncHbSuffix , now )
2523+ err = storage .RetryableWrite (stg , r .syncPathCluster + "." + syncHbSuffix , now )
25152524 if err != nil {
25162525 return errors .Wrap (err , "write cluster hb" )
25172526 }
25182527
25192528 return nil
25202529}
25212530
2522- func (r * PhysRestore ) hbCleanup () error {
2531+ func (r * PhysRestore ) hbCleanup (stg storage. Storage ) error {
25232532 now := []byte (strconv .FormatInt (time .Now ().Unix (), 10 ))
25242533
2525- err := storage .RetryableWrite (r . stg , r .syncPathCluster + "." + syncHbCleanupSuffix , now )
2534+ err := storage .RetryableWrite (stg , r .syncPathCluster + "." + syncHbCleanupSuffix , now )
25262535 if err != nil {
25272536 return errors .Wrap (err , "write cleanup hb" )
25282537 }
@@ -2533,26 +2542,31 @@ func (r *PhysRestore) hbCleanup() error {
25332542// startCleanupHb generates cleanup hb for the purpose of detecting physical restore
25342543// cleanup activity.
25352544func (r * PhysRestore ) startCleanupHb () {
2536- err := r .hbCleanup ()
2545+ stg , err := r .newStorage ()
2546+ if err != nil {
2547+ r .log .Warning ("get cleanup heartbeat storage: %v" , err )
2548+ return
2549+ }
2550+
2551+ err = r .hbCleanup (stg )
25372552 if err != nil {
25382553 r .log .Warning ("send init cleanup heartbeat: %v" , err )
25392554 }
25402555
25412556 r .stopCleanupHB = make (chan struct {})
2542- r .hbWG .Add (1 )
25432557 go func () {
25442558 tk := time .NewTicker (hbCleanupFrame )
25452559 defer func () {
2546- r .hbWG .Done ()
25472560 tk .Stop ()
2561+ storage .Close (stg , r .log )
25482562 r .stopCleanupHB = nil
25492563 r .log .Debug ("cleanup heartbeats stopped" )
25502564 }()
25512565
25522566 for {
25532567 select {
25542568 case <- tk .C :
2555- err := r .hbCleanup ()
2569+ err := r .hbCleanup (stg )
25562570 if err != nil {
25572571 r .log .Warning ("send cleanup heartbeat: %v" , err )
25582572 }
@@ -3186,12 +3200,13 @@ func PhysRestoreFinish(l log.LogEvent, cmd *ExtFinishCmd) error {
31863200 return errors .Wrap (err , "creating restore object from storage dump" )
31873201 }
31883202
3203+ defer r .closeStorages ()
3204+
31893205 r .startHB (l )
31903206 defer func () {
31913207 if r .stopHB != nil {
31923208 close (r .stopHB )
31933209 }
3194- r .closeStoragesAfterHeartbeats ()
31953210 }()
31963211
31973212 excfg , err := r .prepareExtRestore (l )
@@ -3295,6 +3310,9 @@ func physRestoreFromExtDump(
32953310 physRestore := PhysRestore {
32963311 stg : stg ,
32973312 log : l ,
3313+ newStorage : func () (storage.Storage , error ) {
3314+ return GetRestoreMetaStg (cmd .CfgPath , cmd .Node )
3315+ },
32983316
32993317 dbpath : extDump .DBpath ,
33003318 tmpPort : extDump .TmpPort ,
0 commit comments