1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"time"
5
6
7
+ "github.com/percona/percona-backup-mongodb/internal/backup"
6
8
"github.com/percona/percona-backup-mongodb/internal/config"
7
- "github.com/percona/percona-backup-mongodb/internal/context "
9
+ "github.com/percona/percona-backup-mongodb/internal/ctrl "
8
10
"github.com/percona/percona-backup-mongodb/internal/defs"
9
11
"github.com/percona/percona-backup-mongodb/internal/errors"
10
12
"github.com/percona/percona-backup-mongodb/internal/lock"
11
13
"github.com/percona/percona-backup-mongodb/internal/log"
12
- "github.com/percona/percona-backup-mongodb/internal/priority"
13
- "github.com/percona/percona-backup-mongodb/internal/query"
14
14
"github.com/percona/percona-backup-mongodb/internal/storage"
15
15
"github.com/percona/percona-backup-mongodb/internal/topo"
16
- "github.com/percona/percona-backup-mongodb/internal/types"
17
16
"github.com/percona/percona-backup-mongodb/internal/version"
18
- "github.com/percona/percona-backup-mongodb/pbm/backup"
19
17
)
20
18
21
19
type currentBackup struct {
22
- header * types .BackupCmd
20
+ header * ctrl .BackupCmd
23
21
cancel context.CancelFunc
24
22
}
25
23
@@ -52,17 +50,19 @@ func (a *Agent) CancelBackup() {
52
50
}
53
51
54
52
// Backup starts backup
55
- func (a * Agent ) Backup (ctx context.Context , cmd * types.BackupCmd , opid types.OPID , ep config.Epoch ) {
53
+ func (a * Agent ) Backup (ctx context.Context , cmd * ctrl.BackupCmd , opid ctrl.OPID , ep config.Epoch ) {
54
+ logger := log .FromContext (ctx )
55
+
56
56
if cmd == nil {
57
- l := a . log . NewEvent (string (defs .CmdBackup ), "" , opid .String (), ep .TS ())
57
+ l := logger . NewEvent (string (ctrl .CmdBackup ), "" , opid .String (), ep .TS ())
58
58
l .Error ("missed command" )
59
59
return
60
60
}
61
61
62
- l := a . log . NewEvent (string (defs .CmdBackup ), cmd .Name , opid .String (), ep .TS ())
63
- ctx = log .SetLoggerToContext (ctx , a . log )
62
+ l := logger . NewEvent (string (ctrl .CmdBackup ), cmd .Name , opid .String (), ep .TS ())
63
+ ctx = log .SetLoggerToContext (ctx , logger )
64
64
65
- nodeInfo , err := topo .GetNodeInfoExt (ctx , a .node . Session () )
65
+ nodeInfo , err := topo .GetNodeInfoExt (ctx , a .nodeConn )
66
66
if err != nil {
67
67
l .Error ("get node info: %v" , err )
68
68
return
@@ -75,7 +75,7 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
75
75
}
76
76
77
77
isClusterLeader := nodeInfo .IsClusterLeader ()
78
- canRunBackup , err := topo .NodeSuitsExt (ctx , a .node . Session () , nodeInfo , cmd .Type )
78
+ canRunBackup , err := topo .NodeSuitsExt (ctx , a .nodeConn , nodeInfo , cmd .Type )
79
79
if err != nil {
80
80
l .Error ("node check: %v" , err )
81
81
if ! isClusterLeader {
@@ -97,18 +97,18 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
97
97
var bcp * backup.Backup
98
98
switch cmd .Type {
99
99
case defs .PhysicalBackup :
100
- bcp = backup .NewPhysical (a .pbm , a .node )
100
+ bcp = backup .NewPhysical (a .leadConn , a .nodeConn , a . brief )
101
101
case defs .ExternalBackup :
102
- bcp = backup .NewExternal (a .pbm , a .node )
102
+ bcp = backup .NewExternal (a .leadConn , a .nodeConn , a . brief )
103
103
case defs .IncrementalBackup :
104
- bcp = backup .NewIncremental (a .pbm , a .node , cmd .IncrBase )
104
+ bcp = backup .NewIncremental (a .leadConn , a .nodeConn , a . brief , cmd .IncrBase )
105
105
case defs .LogicalBackup :
106
106
fallthrough
107
107
default :
108
- bcp = backup .New (a .pbm , a .node )
108
+ bcp = backup .New (a .leadConn , a .nodeConn , a . brief , a . dumpConns )
109
109
}
110
110
111
- cfg , err := config .GetConfig (ctx , a .pbm . Conn )
111
+ cfg , err := config .GetConfig (ctx , a .leadConn )
112
112
if err != nil {
113
113
l .Error ("unable to get PBM config settings: " + err .Error ())
114
114
return
@@ -122,7 +122,7 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
122
122
if isClusterLeader {
123
123
balancer := topo .BalancerModeOff
124
124
if nodeInfo .IsSharded () {
125
- bs , err := topo .GetBalancerStatus (ctx , a .pbm . Conn )
125
+ bs , err := topo .GetBalancerStatus (ctx , a .leadConn )
126
126
if err != nil {
127
127
l .Error ("get balancer status: %v" , err )
128
128
return
@@ -138,8 +138,8 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
138
138
}
139
139
l .Debug ("init backup meta" )
140
140
141
- if err = topo .CheckTopoForBackup (ctx , a .pbm . Conn , cmd .Type ); err != nil {
142
- ferr := query .ChangeBackupState (a .pbm . Conn , cmd .Name , defs .StatusError , err .Error ())
141
+ if err = topo .CheckTopoForBackup (ctx , a .leadConn , cmd .Type ); err != nil {
142
+ ferr := backup .ChangeBackupState (a .leadConn , cmd .Name , defs .StatusError , err .Error ())
143
143
l .Info ("mark backup as %s `%v`: %v" , defs .StatusError , err , ferr )
144
144
return
145
145
}
@@ -150,7 +150,7 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
150
150
const srcHostMultiplier = 3.0
151
151
var c map [string ]float64
152
152
if cmd .Type == defs .IncrementalBackup && ! cmd .IncrBase {
153
- src , err := query .LastIncrementalBackup (ctx , a .pbm . Conn )
153
+ src , err := backup .LastIncrementalBackup (ctx , a .leadConn )
154
154
if err != nil {
155
155
// try backup anyway
156
156
l .Warning ("define source backup: %v" , err )
@@ -162,7 +162,7 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
162
162
}
163
163
}
164
164
165
- agents , err := topo .ListAgentStatuses (ctx , a .pbm . Conn )
165
+ agents , err := topo .ListAgentStatuses (ctx , a .leadConn )
166
166
if err != nil {
167
167
l .Error ("get agents list: %v" , err )
168
168
return
@@ -177,12 +177,12 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
177
177
validCandidates = append (validCandidates , s )
178
178
}
179
179
180
- nodes , err := priority . BcpNodesPriority (ctx , a .pbm . Conn , c , validCandidates )
180
+ nodes , err := BcpNodesPriority (ctx , a .leadConn , c , validCandidates )
181
181
if err != nil {
182
182
l .Error ("get nodes priority: %v" , err )
183
183
return
184
184
}
185
- shards , err := topo .ClusterMembers (ctx , a .pbm . Conn .MongoClient ())
185
+ shards , err := topo .ClusterMembers (ctx , a .leadConn .MongoClient ())
186
186
if err != nil {
187
187
l .Error ("get cluster members: %v" , err )
188
188
return
@@ -208,8 +208,8 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
208
208
}
209
209
210
210
epoch := ep .TS ()
211
- lck := lock .NewLock (a .pbm . Conn , lock.LockHeader {
212
- Type : defs .CmdBackup ,
211
+ lck := lock .NewLock (a .leadConn , lock.LockHeader {
212
+ Type : ctrl .CmdBackup ,
213
213
Replset : nodeInfo .SetName ,
214
214
Node : nodeInfo .Me ,
215
215
OPID : opid .String (),
@@ -219,8 +219,8 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
219
219
// install a backup lock despite having PITR one
220
220
got , err := a .acquireLock (ctx , lck , l , func (ctx context.Context ) (bool , error ) {
221
221
return lck .Rewrite (ctx , & lock.LockHeader {
222
- Replset : a .node . RS () ,
223
- Type : defs .CmdPITR ,
222
+ Replset : a .brief . SetName ,
223
+ Type : ctrl .CmdPITR ,
224
224
})
225
225
})
226
226
if err != nil {
@@ -232,7 +232,7 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
232
232
return
233
233
}
234
234
235
- err = query .SetRSNomineeACK (ctx , a .pbm . Conn , cmd .Name , nodeInfo .SetName , nodeInfo .Me )
235
+ err = backup .SetRSNomineeACK (ctx , a .leadConn , cmd .Name , nodeInfo .SetName , nodeInfo .Me )
236
236
if err != nil {
237
237
l .Warning ("set nominee ack: %v" , err )
238
238
}
@@ -264,15 +264,15 @@ func (a *Agent) Backup(ctx context.Context, cmd *types.BackupCmd, opid types.OPI
264
264
265
265
const renominationFrame = 5 * time .Second
266
266
267
- func (a * Agent ) nominateRS (ctx context.Context , bcp , rs string , nodes [][]string , l * log.Event ) error {
267
+ func (a * Agent ) nominateRS (ctx context.Context , bcp , rs string , nodes [][]string , l log.LogEvent ) error {
268
268
l .Debug ("nomination list for %s: %v" , rs , nodes )
269
- err := query .SetRSNomination (ctx , a .pbm . Conn , bcp , rs )
269
+ err := backup .SetRSNomination (ctx , a .leadConn , bcp , rs )
270
270
if err != nil {
271
271
return errors .Wrap (err , "set nomination meta" )
272
272
}
273
273
274
274
for _ , n := range nodes {
275
- nms , err := query .GetRSNominees (ctx , a .pbm . Conn , bcp , rs )
275
+ nms , err := backup .GetRSNominees (ctx , a .leadConn , bcp , rs )
276
276
if err != nil && ! errors .Is (err , errors .ErrNotFound ) {
277
277
return errors .Wrap (err , "get nomination meta" )
278
278
}
@@ -281,13 +281,13 @@ func (a *Agent) nominateRS(ctx context.Context, bcp, rs string, nodes [][]string
281
281
return nil
282
282
}
283
283
284
- err = query .SetRSNominees (ctx , a .pbm . Conn , bcp , rs , n )
284
+ err = backup .SetRSNominees (ctx , a .leadConn , bcp , rs , n )
285
285
if err != nil {
286
286
return errors .Wrap (err , "set nominees" )
287
287
}
288
288
l .Debug ("nomination %s, set candidates %v" , rs , n )
289
289
290
- err = query .BackupHB (ctx , a .pbm . Conn , bcp )
290
+ err = backup .BackupHB (ctx , a .leadConn , bcp )
291
291
if err != nil {
292
292
l .Warning ("send heartbeat: %v" , err )
293
293
}
@@ -298,7 +298,7 @@ func (a *Agent) nominateRS(ctx context.Context, bcp, rs string, nodes [][]string
298
298
return nil
299
299
}
300
300
301
- func (a * Agent ) waitNomination (ctx context.Context , bcp , rs , node string , l * log.Event ) (bool , error ) {
301
+ func (a * Agent ) waitNomination (ctx context.Context , bcp , rs , node string , l log.LogEvent ) (bool , error ) {
302
302
tk := time .NewTicker (time .Millisecond * 500 )
303
303
defer tk .Stop ()
304
304
@@ -308,7 +308,7 @@ func (a *Agent) waitNomination(ctx context.Context, bcp, rs, node string, l *log
308
308
for {
309
309
select {
310
310
case <- tk .C :
311
- nm , err := query .GetRSNominees (ctx , a .pbm . Conn , bcp , rs )
311
+ nm , err := backup .GetRSNominees (ctx , a .leadConn , bcp , rs )
312
312
if err != nil {
313
313
if errors .Is (err , errors .ErrNotFound ) {
314
314
continue
0 commit comments