Skip to content

Commit a544306

Browse files
1gtmanisurrahman75
andauthored
[cherry-pick] Extract json before unmarshal in mongodb commands (#2340) (#2347)
* 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> * fix fmt 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 f002dab commit a544306

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
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: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package pkg
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"strings"
2322
"time"
2423

2524
"gomodules.xyz/go-sh"
@@ -48,12 +47,11 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
4847
return err
4948
}
5049

51-
s := fmt.Sprintf(`/bin/echo '%s' | /usr/bin/tail -1`, strings.TrimSuffix(string(output), "\n"))
52-
output, err = sh.Command("/bin/sh", "-c", s).Output()
50+
output, err = extractJSON(string(output))
5351
if err != nil {
54-
klog.Errorf("Error while running tail in findAndModify to lock configServer : %s ; output : %s \n", err.Error(), output)
5552
return err
5653
}
54+
5755
err = json.Unmarshal(output, &v)
5856
if err != nil {
5957
klog.Errorf("Unmarshal error while running findAndModify to lock configServer : %s \n", err.Error())
@@ -75,7 +73,18 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
7573
"--eval", "rs.slaveOk(); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');",
7674
}, mongoCreds...)
7775

78-
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 {
7988
return err
8089
}
8190

@@ -118,6 +127,11 @@ func lockSecondaryMember(mongohost string) error {
118127
return err
119128
}
120129

130+
output, err = extractJSON(string(output))
131+
if err != nil {
132+
return err
133+
}
134+
121135
err = json.Unmarshal(output, &v)
122136
if err != nil {
123137
klog.Errorf("Unmarshal error while running fsyncLock on secondary : %s \n", err.Error())
@@ -146,6 +160,12 @@ func checkIfSecondaryLockedAndSync(mongohost string) error {
146160
klog.Errorf("Error while running currentOp on secondary : %s ; output : %s \n", err.Error(), output)
147161
return err
148162
}
163+
164+
output, err = extractJSON(string(output))
165+
if err != nil {
166+
return err
167+
}
168+
149169
err = json.Unmarshal(output, &x)
150170
if err != nil {
151171
klog.Errorf("Unmarshal error while running currentOp on secondary : %s \n", err.Error())
@@ -178,8 +198,18 @@ func waitForSecondarySync(mongohost string) error {
178198
"--eval", "JSON.stringify(rs.status())",
179199
}, mongoCreds...)
180200

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())
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 {
183213
return err
184214
}
185215

@@ -268,6 +298,12 @@ func unlockSecondaryMember(mongohost string) error {
268298
klog.Errorf("Error while running fsyncUnlock on secondary : %s ; output : %s \n", err.Error(), output)
269299
return err
270300
}
301+
302+
output, err = extractJSON(string(output))
303+
if err != nil {
304+
return err
305+
}
306+
271307
err = json.Unmarshal(output, &v)
272308
if err != nil {
273309
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)