Skip to content

Commit 484cc32

Browse files
1gtmanisurrahman75
andauthored
[cherry-pick] Extract json before unmarshal in mongodb commands (#2340) (#2350)
* 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 a519484 commit 484cc32

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
@@ -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
@@ -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)