Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command and tickers updated #1993

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
}

if err := setupConfigServer(parameters.ConfigServer, secondary); err != nil {
klog.Errorf("error while setup config server. error: %v", err)
return nil, err
}

Expand Down
34 changes: 18 additions & 16 deletions pkg/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,59 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
klog.Warningln("locking configserver is skipped. secondary host is empty")
return nil
}
v := make(map[string]interface{})
x := make(map[string]interface{})
// findAndModify BackupControlDocument. skip single quote inside single quote: https://stackoverflow.com/a/28786747/4628962
args := append([]interface{}{
"config",
"--host", configSVRDSN,
"--quiet",
"--eval", "db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }});",
"--eval", `JSON.stringify(db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } } , new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }}));`,
}, mongoCreds...)

output, err := sh.Command(MongoCMD, args...).Output()
if err != nil {
klog.Errorf("Error while running findAndModify to setup configServer : %s ; output : %s \n", err.Error(), output)
return err
}

err = json.Unmarshal(output, &v)
err = json.Unmarshal(output, &x)
if err != nil {
klog.Errorf("Unmarshal error while running findAndModify to setup configServer : %s \n", err.Error())
return err
}
val, ok := v["counter"].(float64)

val, ok := x["counter"].(float64)
if !ok || int(val) == 0 {
return fmt.Errorf("unable to modify BackupControlDocument. got response: %v", v)
return fmt.Errorf("unable to modify BackupControlDocument. got response: %v", x)
}
val2 := float64(0)
timer := 0 // wait approximately 5 minutes.
v2 := make([]map[string]interface{}, 0)
for timer < 60 && (int(val2) == 0 || int(val) != int(val2)) {
timer++
// find backupDocument from secondary configServer
args = append([]interface{}{
"config",
"--host", secondaryHost,
"--quiet",
"--eval", "rs.secondaryOk(); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');",
"--eval", `"db.getMongo().setReadPref('secondary'); JSON.stringify(db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority').toArray());"`,
}, mongoCreds...)

if err := sh.Command(MongoCMD, args...).UnmarshalJSON(&v); err != nil {
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v2); err != nil {
return err
}

val2, ok = v["counter"].(float64)
if !ok {
return fmt.Errorf("unable to get BackupControlDocument. got response: %v", v)
if len(v2) > 0 {
val2, ok = v2[0]["counter"].(float64)
if !ok {
return fmt.Errorf("unable to get BackupControlDocument. got response: %v", x)
}
}
if int(val) != int(val2) {
klog.V(5).Infof("BackupDocument counter in secondary %v is not same. Expected %v, but got %v. Full response: %v", secondaryHost, val, val2, v)
klog.V(5).Infof("BackupDocument counter in secondary is not same. Expected %v, but got %v . Full response: %v", val, val2, x)
time.Sleep(time.Second * 5)
}
}
if timer >= 60 {
return fmt.Errorf("timeout while waiting for BackupDocument counter in secondary %v to be same as primary. Expected %v, but got %v. Full response: %v", secondaryHost, val, val2, v)
return fmt.Errorf("timeout while waiting for BackupDocument counter in secondary to be same as primary. Expected %v, but got %v. Full response: %v", val, val2, x)
}

return nil
Expand Down Expand Up @@ -129,10 +131,10 @@ func checkIfSecondaryLockedAndSync(mongohost string) error {

x := make(map[string]interface{})
args := append([]interface{}{
"config",
"admin",
"--host", mongohost,
"--quiet",
"--eval", "JSON.stringify(db.currentOp())",
"--eval", "db.getMongo().setReadPref('secondary'); JSON.stringify(db.runCommand({currentOp:1}))",
}, mongoCreds...)
output, err := sh.Command(MongoCMD, args...).Output()
if err != nil {
Expand Down
Loading