Skip to content

Commit fb53db5

Browse files
1gtmanisurrahman75
andauthored
[cherry-pick] Extract json before unmarshal in mongodb commands (#2340) (#2341)
* Extract json before unmarshal in mongodb commands (#2340) /cherry-pick Signed-off-by: sayedppqq <sayed@appscode.com> Signed-off-by: Anisur Rahman <anisur@appscode.com> * Update CI runners image version Signed-off-by: Anisur Rahman <anisur@appscode.com> --------- Signed-off-by: sayedppqq <sayed@appscode.com> Signed-off-by: Anisur Rahman <anisur@appscode.com> Co-authored-by: Md. Anisur Rahman <54911684+anisurrahman75@users.noreply.github.com> Co-authored-by: Anisur Rahman <anisur@appscode.com>
1 parent f02bb8b commit fb53db5

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
jobs:
1212
build:
1313
name: Build
14-
runs-on: ubuntu-20.04
14+
runs-on: ubuntu-24.04
1515
steps:
1616
- name: Set up Go 1.24
1717
uses: actions/setup-go@v5

pkg/backup.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,20 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, sec
647647
"--eval", "JSON.stringify(rs.isMaster())",
648648
}, mongoCreds...)
649649
// even --quiet doesn't skip replicaset PrimaryConnection log. so take tha last line. issue tracker: https://jira.mongodb.org/browse/SERVER-27159
650-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
650+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
651+
if err != nil {
652+
klog.Errorf("Error while running isMaster : %s ; output : %s \n", err.Error(), output)
653+
return "", "", secondaryMembers, err
654+
}
655+
656+
output, err = extractJSON(string(output))
657+
if err != nil {
658+
return "", "", secondaryMembers, err
659+
}
660+
661+
err = json.Unmarshal(output, &v)
662+
if err != nil {
663+
klog.Errorf("Unmarshal error while running isMaster : %+v , output : %s \n", err.Error(), output)
651664
return "", "", secondaryMembers, err
652665
}
653666

@@ -697,6 +710,11 @@ func disabelBalancer(mongosHost string) error {
697710
return err
698711
}
699712

713+
output, err = extractJSON(string(output))
714+
if err != nil {
715+
return err
716+
}
717+
700718
err = json.Unmarshal(output, &v)
701719
if err != nil {
702720
klog.Errorf("Unmarshal error while stopping balancer : %s ; output = %s \n", err.Error(), output)
@@ -750,6 +768,11 @@ func enableBalancer(mongosHost string) error {
750768
}
751769
}
752770

771+
output, err = extractJSON(string(output))
772+
if err != nil {
773+
return err
774+
}
775+
753776
err = json.Unmarshal(output, &v)
754777
if err != nil {
755778
klog.Errorf("Unmarshal error while enabling balancer : %+v , output : %s \n", err.Error(), output)

pkg/lock.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
5454
return err
5555
}
5656

57+
output, err = extractJSON(string(output))
58+
if err != nil {
59+
return err
60+
}
61+
5762
err = json.Unmarshal(output, &v)
5863
if err != nil {
5964
klog.Errorf("Unmarshal error while running findAndModify to lock configServer : %s \n", err.Error())
@@ -75,7 +80,18 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
7580
"--eval", "rs.slaveOk(); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');",
7681
}, mongoCreds...)
7782

78-
if err := sh.Command(MongoCMD, args...).UnmarshalJSON(&v); err != nil {
83+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
84+
if err != nil {
85+
return err
86+
}
87+
88+
output, err = extractJSON(string(output))
89+
if err != nil {
90+
return err
91+
}
92+
93+
err = json.Unmarshal(output, &v)
94+
if err != nil {
7995
return err
8096
}
8197

@@ -118,6 +134,11 @@ func lockSecondaryMember(mongohost string) error {
118134
return err
119135
}
120136

137+
output, err = extractJSON(string(output))
138+
if err != nil {
139+
return err
140+
}
141+
121142
err = json.Unmarshal(output, &v)
122143
if err != nil {
123144
klog.Errorf("Unmarshal error while running fsyncLock on secondary : %s \n", err.Error())
@@ -146,6 +167,12 @@ func checkIfSecondaryLockedAndSync(mongohost string) error {
146167
klog.Errorf("Error while running currentOp on secondary : %s ; output : %s \n", err.Error(), output)
147168
return err
148169
}
170+
171+
output, err = extractJSON(string(output))
172+
if err != nil {
173+
return err
174+
}
175+
149176
err = json.Unmarshal(output, &x)
150177
if err != nil {
151178
klog.Errorf("Unmarshal error while running currentOp on secondary : %s \n", err.Error())
@@ -178,8 +205,18 @@ func waitForSecondarySync(mongohost string) error {
178205
"--eval", "JSON.stringify(rs.status())",
179206
}, mongoCreds...)
180207

181-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&status); err != nil {
182-
klog.Errorf("Error while running status on secondary : %s ; output : %s \n", mongohost, err.Error())
208+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
209+
if err != nil {
210+
return err
211+
}
212+
213+
output, err = extractJSON(string(output))
214+
if err != nil {
215+
return err
216+
}
217+
218+
err = json.Unmarshal(output, &status)
219+
if err != nil {
183220
return err
184221
}
185222

@@ -268,6 +305,12 @@ func unlockSecondaryMember(mongohost string) error {
268305
klog.Errorf("Error while running fsyncUnlock on secondary : %s ; output : %s \n", err.Error(), output)
269306
return err
270307
}
308+
309+
output, err = extractJSON(string(output))
310+
if err != nil {
311+
return err
312+
}
313+
271314
err = json.Unmarshal(output, &v)
272315
if err != nil {
273316
klog.Errorf("Unmarshal error while running fsyncUnlock on secondary : %s \n", err.Error())

pkg/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net/url"
2222
"os/exec"
23+
"regexp"
2324
"strings"
2425
"time"
2526

@@ -179,3 +180,15 @@ func getBackupDB(mongoArgs string) string {
179180
}
180181
return ""
181182
}
183+
184+
// extractJSON is needed due to ignore unnecessary character like /x1b from output before unmarshal
185+
func extractJSON(input string) ([]byte, error) {
186+
// Regular expression to match JSON objects (assuming JSON starts with `{` and ends with `}`)
187+
re := regexp.MustCompile(`\{.*\}`)
188+
jsonPart := re.FindString(string(input))
189+
if jsonPart == "" {
190+
klog.Infoln("output from MongoDB:", input)
191+
return nil, fmt.Errorf("no JSON part found in the output from MongoDB")
192+
}
193+
return []byte(jsonPart), nil
194+
}

0 commit comments

Comments
 (0)