Skip to content

Commit

Permalink
move lock related fucn to separate file
Browse files Browse the repository at this point in the history
Signed-off-by: sayedppqq <[email protected]>
  • Loading branch information
sayedppqq committed Oct 3, 2023
1 parent 49f11b5 commit 25fa866
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 248 deletions.
248 changes: 0 additions & 248 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,251 +714,3 @@ func enableBalancer(mongosHost string) error {
klog.Info("Balancer successfully re-enabled.")
return nil
}

func setupConfigServer(configSVRDSN, secondaryHost string) error {
klog.Infoln("Attempting to setup configserver", configSVRDSN)

if secondaryHost == "" {
klog.Warningln("locking configserver is skipped. secondary host is empty")
return nil
}
v := 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 }});",
}, 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)
if err != nil {
klog.Errorf("Unmarshal error while running findAndModify to setup configServer : %s \n", err.Error())
return err
}
val, ok := v["counter"].(float64)
if !ok || int(val) == 0 {
return fmt.Errorf("unable to modify BackupControlDocument. got response: %v", v)
}
val2 := float64(0)
timer := 0 // wait approximately 5 minutes.
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');",
}, mongoCreds...)

if err := sh.Command(MongoCMD, args...).UnmarshalJSON(&v); err != nil {
return err
}

val2, ok = v["counter"].(float64)
if !ok {
return fmt.Errorf("unable to get BackupControlDocument. got response: %v", v)
}
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)
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 nil
}

func lockSecondaryMember(mongohost string) error {
klog.Infoln("Attempting to lock secondary member", mongohost)

if mongohost == "" {
klog.Warningln("locking secondary member is skipped. secondary host is empty")
return nil
}

// lock file
v := make(map[string]interface{})
args := append([]interface{}{
"config",
"--host", mongohost,
"--quiet",
"--eval", "JSON.stringify(db.fsyncLock())",
}, mongoCreds...)

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

err = json.Unmarshal(output, &v)
if err != nil {
klog.Errorf("Unmarshal error while running fsyncLock on secondary : %s \n", err.Error())
return err
}

if val, ok := v["ok"].(float64); !ok || int(val) != 1 {
return fmt.Errorf("unable to lock the secondary host. got response: %v", v)
}
klog.Infof("secondary %s locked.", mongohost)
return nil
}

func checkIfSecondaryLockedAndSync(mongohost string) error {
klog.Infoln("Checking if secondary %s is already locked.", mongohost)

x := make(map[string]interface{})
args := append([]interface{}{
"config",
"--host", mongohost,
"--quiet",
"--eval", "JSON.stringify(db.currentOp())",
}, mongoCreds...)
output, err := sh.Command(MongoCMD, args...).Output()
if err != nil {
klog.Errorf("Error while running currentOp on secondary : %s ; output : %s \n", err.Error(), output)
return err
}
err = json.Unmarshal(output, &x)
if err != nil {
klog.Errorf("Unmarshal error while running currentOp on secondary : %s \n", err.Error())
return err
}

val, ok := x["fsyncLock"].(bool)
if ok && val {
klog.Infoln("Found fsyncLock true while locking")
err := unlockSecondaryMember(mongohost)
if err != nil {
return err
}
if err := waitForSecondarySync(mongohost); err != nil {
return err
}
}
return nil
}

func waitForSecondarySync(mongohost string) error {
klog.Infoln("Attempting to sync secondary with primary")

for {
status := make(map[string]interface{})
args := append([]interface{}{
"config",
"--host", mongohost,
"--quiet",
"--eval", "JSON.stringify(rs.status())",
}, mongoCreds...)

if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&status); err != nil {
klog.Errorf("Error while running status on secondary : %s ; output : %s \n", mongohost, err.Error())
return err
}

members, ok := status["members"].([]interface{})
if !ok {
return fmt.Errorf("unable to get members using rs.status(). got response: %v", status)
}

var masterOptimeDate, curOptimeDate time.Time

for _, member := range members {

memberInfo, ok := member.(map[string]interface{})
if !ok {
return fmt.Errorf("unable to get member info of primary using rs.status(). got response: %v", member)
}

if memberInfo["stateStr"] == "PRIMARY" {
optimedate, ok := memberInfo["optimeDate"].(string)
if !ok {
return fmt.Errorf("unable to get optimedate of primary using rs.status(). got response: %v", memberInfo)
}

convTime, err := getTime(optimedate)
if err != nil {
return err
}
masterOptimeDate = convTime
break
}
}
synced := true
for _, member := range members {

memberInfo, ok := member.(map[string]interface{})
if !ok {
return fmt.Errorf("unable to get member info of secondary using rs.status(). got response: %v", member)
}

if memberInfo["stateStr"] == "SECONDARY" && memberInfo["name"] == mongohost {

optimedate, ok := memberInfo["optimeDate"].(string)
if !ok {
return fmt.Errorf("unable to get optimedate of secondary using rs.status(). got response: %v", memberInfo)
}

convTime, err := getTime(optimedate)
if err != nil {
return err
}
curOptimeDate = convTime
if curOptimeDate.Before(masterOptimeDate) {
synced = false
}
break
}
}
if synced {
break
}

klog.Infoln("Waiting... database is not synced yet")
time.Sleep(5 * time.Second)
}
return nil
}

func unlockSecondaryMember(mongohost string) error {
klog.Infoln("Attempting to unlock secondary member", mongohost)
if mongohost == "" {
klog.Warningln("skipped unlocking secondary member. secondary host is empty")
return nil
}
v := make(map[string]interface{})

// unlock file
args := append([]interface{}{
"config",
"--host", mongohost,
"--quiet",
"--eval", "JSON.stringify(db.fsyncUnlock())",
}, mongoCreds...)

output, err := sh.Command(MongoCMD, args...).Output()
if err != nil {
klog.Errorf("Error while running fsyncUnlock on secondary : %s ; output : %s \n", err.Error(), output)
return err
}
err = json.Unmarshal(output, &v)
if err != nil {
klog.Errorf("Unmarshal error while running fsyncUnlock on secondary : %s \n", err.Error())
return err
}
if val, ok := v["ok"].(float64); !ok || int(val) != 1 {
return fmt.Errorf("unable to lock the secondary host. got response: %v", v)
}
klog.Infof("secondary %s unlocked.", mongohost)
return nil
}
Loading

0 comments on commit 25fa866

Please sign in to comment.