Skip to content

Commit

Permalink
extract json before unmarshal in mongodb commands
Browse files Browse the repository at this point in the history
Signed-off-by: sayedppqq <sayed@appscode.com>
  • Loading branch information
sayedppqq committed Feb 19, 2025
1 parent 67cff5f commit 3564b74
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
25 changes: 24 additions & 1 deletion pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,20 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, sec
"--eval", "JSON.stringify(rs.isMaster())",
}, mongoCreds...)
// even --quiet doesn't skip replicaset PrimaryConnection log. so take tha last line. issue tracker: https://jira.mongodb.org/browse/SERVER-27159
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
if err != nil {
klog.Errorf("Error while running isMaster : %s ; output : %s \n", err.Error(), output)
return "", "", secondaryMembers, err
}

output, err = extractJSON(string(output))
if err != nil {
return "", "", secondaryMembers, err
}

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

Expand Down Expand Up @@ -757,6 +770,11 @@ func disabelBalancer(mongosHost string) error {
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &v)
if err != nil {
klog.Errorf("Unmarshal error while stopping balancer : %s ; output = %s \n", err.Error(), output)
Expand Down Expand Up @@ -810,6 +828,11 @@ func enableBalancer(mongosHost string) error {
}
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &v)
if err != nil {
klog.Errorf("Unmarshal error while enabling balancer : %+v , output : %s \n", err.Error(), output)
Expand Down
50 changes: 47 additions & 3 deletions pkg/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
klog.Errorf("Error while running findAndModify to setup configServer : %s ; output : %s \n", err.Error(), output)
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &x)
if err != nil {
klog.Errorf("Unmarshal error while running findAndModify to setup configServer : %s \n", err.Error())
Expand All @@ -68,7 +74,18 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
"--quiet",
"--eval", `"db.getMongo().setReadPref('secondary'); JSON.stringify(db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority').toArray());"`,
}, mongoCreds...)
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v2); err != nil {
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
if err != nil {
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &v2)
if err != nil {
return err
}

Expand Down Expand Up @@ -113,6 +130,11 @@ func lockSecondaryMember(mongohost string) error {
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &v)
if err != nil {
klog.Errorf("Unmarshal error while running fsyncLock on secondary : %s \n", err.Error())
Expand Down Expand Up @@ -141,6 +163,12 @@ func checkIfSecondaryLockedAndSync(mongohost string) error {
klog.Errorf("Error while running currentOp on secondary : %s ; output : %s \n", err.Error(), output)
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &x)
if err != nil {
klog.Errorf("Unmarshal error while running currentOp on secondary : %s \n", err.Error())
Expand Down Expand Up @@ -173,8 +201,18 @@ func waitForSecondarySync(mongohost string) error {
"--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())
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
if err != nil {
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &status)
if err != nil {
return err
}

Expand Down Expand Up @@ -263,6 +301,12 @@ func unlockSecondaryMember(mongohost string) error {
klog.Errorf("Error while running fsyncUnlock on secondary : %s ; output : %s \n", err.Error(), output)
return err
}

output, err = extractJSON(string(output))
if err != nil {
return err
}

err = json.Unmarshal(output, &v)
if err != nil {
klog.Errorf("Unmarshal error while running fsyncUnlock on secondary : %s \n", err.Error())
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/url"
"os/exec"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -179,3 +180,15 @@ func getBackupDB(mongoArgs string) string {
}
return ""
}

// extractJSON is needed due to ignore unnecessary character like /x1b from output before unmarshal
func extractJSON(input string) ([]byte, error) {
// Regular expression to match JSON objects (assuming JSON starts with `{` and ends with `}`)
re := regexp.MustCompile(`\{.*\}`)
jsonPart := re.FindString(string(input))
if jsonPart == "" {
klog.Infoln("output from MongoDB:", input)
return nil, fmt.Errorf("no JSON part found in the output from MongoDB")
}
return []byte(jsonPart), nil
}

0 comments on commit 3564b74

Please sign in to comment.