From 700553ab7358beef833db34ff6d61758e07e6a2d Mon Sep 17 00:00:00 2001 From: sayedppqq Date: Wed, 27 Sep 2023 16:16:45 +0600 Subject: [PATCH] unlock and sync for all secondaries Signed-off-by: sayedppqq --- pkg/backup.go | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/pkg/backup.go b/pkg/backup.go index 5015efd01..5ddcdb874 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -442,7 +442,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic if parameters.ConfigServer != "" { // sharded cluster. so disable the balancer first. then perform the 'usual' tasks. - primary, secondary, err := getPrimaryNSecondaryMember(parameters.ConfigServer) + primary, secondary, secondaryMembers, err := getPrimaryNSecondaryMember(parameters.ConfigServer) if err != nil { return nil, err } @@ -466,8 +466,10 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic // Check if secondary is already locked before locking it. // If yes, unlock it and sync with primary - if err := checkIfSecondaryLockedAndSync(secondary); err != nil { - return nil, err + for _, secondary := range secondaryMembers { + if err := checkIfSecondaryLockedAndSync(secondary); err != nil { + return nil, err + } } if err := setupConfigServer(parameters.ConfigServer, secondary); err != nil { @@ -488,7 +490,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic for key, host := range parameters.ReplicaSets { // do the task - primary, secondary, err := getPrimaryNSecondaryMember(host) + primary, secondary, secondaryMembers, err := getPrimaryNSecondaryMember(host) if err != nil { klog.Errorf("error while getting primary and secondary member of %v. error: %v", host, err) return nil, err @@ -503,8 +505,10 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic // Check if secondary is already locked before locking it. // If yes, unlock it and sync with primary - if err := checkIfSecondaryLockedAndSync(secondary); err != nil { - return nil, err + for _, secondary := range secondaryMembers { + if err := checkIfSecondaryLockedAndSync(secondary); err != nil { + return nil, err + } } err = lockSecondaryMember(secondary) @@ -588,11 +592,10 @@ func getSSLUser(path string) (string, error) { return strings.TrimSpace(user), nil } -func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, err error) { +func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, secondaryMembers []string, err error) { klog.Infoln("finding primary and secondary instances of", mongoDSN) v := make(map[string]interface{}) - // stop balancer args := append([]interface{}{ "config", "--host", mongoDSN, @@ -601,33 +604,35 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, err }, 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 { - return "", "", err + return "", "", secondaryMembers, err } primary, ok := v["primary"].(string) if !ok || primary == "" { - return "", "", fmt.Errorf("unable to get primary instance using rs.isMaster(). got response: %v", v) + return "", "", secondaryMembers, fmt.Errorf("unable to get primary instance using rs.isMaster(). got response: %v", v) } hosts, ok := v["hosts"].([]interface{}) if !ok { - return "", "", fmt.Errorf("unable to get hosts using rs.isMaster(). got response: %v", v) + return "", "", secondaryMembers, fmt.Errorf("unable to get hosts using rs.isMaster(). got response: %v", v) } for _, host := range hosts { - secHost, ok := host.(string) - if !ok || secHost == "" { + curHost, ok := host.(string) + + if !ok || curHost == "" { err = fmt.Errorf("unable to get secondary instance using rs.isMaster(). got response: %v", v) continue } - - if secHost != primary { - klog.Infof("Primary %s & Secondary %s found for mongoDSN %s \n", primary, secHost, mongoDSN) - return primary, secHost, nil + if curHost != primary { + secondaryMembers = append(secondaryMembers, curHost) } } + if len(secondaryMembers) > 0 { + return primary, secondaryMembers[0], secondaryMembers, err + } - return primary, "", err + return primary, "", secondaryMembers, err } // run from mongos instance