Skip to content

Commit 9cd9095

Browse files
Extract json before unmarshal in mongodb commands (#2340) (#2355)
Extract json before unmarshal in mongodb commands (#2340) Signed-off-by: sayedppqq <sayed@appscode.com> Update CI runners image version Signed-off-by: Anisur Rahman <anisur@appscode.com>
1 parent f4c333d commit 9cd9095

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
@@ -722,7 +722,20 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, sec
722722
"--eval", "JSON.stringify(rs.isMaster())",
723723
}, mongoCreds...)
724724
// even --quiet doesn't skip replicaset PrimaryConnection log. so take tha last line. issue tracker: https://jira.mongodb.org/browse/SERVER-27159
725-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
725+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
726+
if err != nil {
727+
klog.Errorf("Error while running isMaster : %s ; output : %s \n", err.Error(), output)
728+
return "", "", secondaryMembers, err
729+
}
730+
731+
output, err = extractJSON(string(output))
732+
if err != nil {
733+
return "", "", secondaryMembers, err
734+
}
735+
736+
err = json.Unmarshal(output, &v)
737+
if err != nil {
738+
klog.Errorf("Unmarshal error while running isMaster : %+v , output : %s \n", err.Error(), output)
726739
return "", "", secondaryMembers, err
727740
}
728741

@@ -772,6 +785,11 @@ func disabelBalancer(mongosHost string) error {
772785
return err
773786
}
774787

788+
output, err = extractJSON(string(output))
789+
if err != nil {
790+
return err
791+
}
792+
775793
err = json.Unmarshal(output, &v)
776794
if err != nil {
777795
klog.Errorf("Unmarshal error while stopping balancer : %s ; output = %s \n", err.Error(), output)
@@ -825,6 +843,11 @@ func enableBalancer(mongosHost string) error {
825843
}
826844
}
827845

846+
output, err = extractJSON(string(output))
847+
if err != nil {
848+
return err
849+
}
850+
828851
err = json.Unmarshal(output, &v)
829852
if err != nil {
830853
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
@@ -47,6 +47,11 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
4747
return err
4848
}
4949

50+
output, err = extractJSON(string(output))
51+
if err != nil {
52+
return err
53+
}
54+
5055
err = json.Unmarshal(output, &v)
5156
if err != nil {
5257
klog.Errorf("Unmarshal error while running findAndModify to setup configServer : %s \n", err.Error())
@@ -68,7 +73,18 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
6873
"--eval", "rs.secondaryOk(); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');",
6974
}, mongoCreds...)
7075

71-
if err := sh.Command(MongoCMD, args...).UnmarshalJSON(&v); err != nil {
76+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
77+
if err != nil {
78+
return err
79+
}
80+
81+
output, err = extractJSON(string(output))
82+
if err != nil {
83+
return err
84+
}
85+
86+
err = json.Unmarshal(output, &v)
87+
if err != nil {
7288
return err
7389
}
7490

@@ -111,6 +127,11 @@ func lockSecondaryMember(mongohost string) error {
111127
return err
112128
}
113129

130+
output, err = extractJSON(string(output))
131+
if err != nil {
132+
return err
133+
}
134+
114135
err = json.Unmarshal(output, &v)
115136
if err != nil {
116137
klog.Errorf("Unmarshal error while running fsyncLock on secondary : %s \n", err.Error())
@@ -139,6 +160,12 @@ func checkIfSecondaryLockedAndSync(mongohost string) error {
139160
klog.Errorf("Error while running currentOp on secondary : %s ; output : %s \n", err.Error(), output)
140161
return err
141162
}
163+
164+
output, err = extractJSON(string(output))
165+
if err != nil {
166+
return err
167+
}
168+
142169
err = json.Unmarshal(output, &x)
143170
if err != nil {
144171
klog.Errorf("Unmarshal error while running currentOp on secondary : %s \n", err.Error())
@@ -171,8 +198,18 @@ func waitForSecondarySync(mongohost string) error {
171198
"--eval", "JSON.stringify(rs.status())",
172199
}, mongoCreds...)
173200

174-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&status); err != nil {
175-
klog.Errorf("Error while running status on secondary : %s ; output : %s \n", mongohost, err.Error())
201+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
202+
if err != nil {
203+
return err
204+
}
205+
206+
output, err = extractJSON(string(output))
207+
if err != nil {
208+
return err
209+
}
210+
211+
err = json.Unmarshal(output, &status)
212+
if err != nil {
176213
return err
177214
}
178215

@@ -261,6 +298,12 @@ func unlockSecondaryMember(mongohost string) error {
261298
klog.Errorf("Error while running fsyncUnlock on secondary : %s ; output : %s \n", err.Error(), output)
262299
return err
263300
}
301+
302+
output, err = extractJSON(string(output))
303+
if err != nil {
304+
return err
305+
}
306+
264307
err = json.Unmarshal(output, &v)
265308
if err != nil {
266309
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
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"net/url"
2323
"os/exec"
24+
"regexp"
2425
"strings"
2526
"time"
2627

@@ -195,3 +196,15 @@ func getBackupDB(mongoArgs string) string {
195196
}
196197
return ""
197198
}
199+
200+
// extractJSON is needed due to ignore unnecessary character like /x1b from output before unmarshal
201+
func extractJSON(input string) ([]byte, error) {
202+
// Regular expression to match JSON objects (assuming JSON starts with `{` and ends with `}`)
203+
re := regexp.MustCompile(`\{.*\}`)
204+
jsonPart := re.FindString(string(input))
205+
if jsonPart == "" {
206+
klog.Infoln("output from MongoDB:", input)
207+
return nil, fmt.Errorf("no JSON part found in the output from MongoDB")
208+
}
209+
return []byte(jsonPart), nil
210+
}

0 commit comments

Comments
 (0)