@@ -667,6 +667,7 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, err
667
667
}
668
668
669
669
if secHost != primary {
670
+ klog .Infof ("Primary %s & Secondary %s found for mongoDSN %s \n " , primary , secHost , mongoDSN )
670
671
return primary , secHost , nil
671
672
}
672
673
}
@@ -683,10 +684,18 @@ func disabelBalancer(mongosHost string) error {
683
684
"config" ,
684
685
"--host" , mongosHost ,
685
686
"--quiet" ,
686
- "--eval" , "JSON.stringify(sh.stopBalancer())" ,
687
+ "--eval" , "JSON.stringify(sh.stopBalancer(600000,1000 ))" ,
687
688
}, mongoCreds ... )
688
689
// disable balancer
689
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
690
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
691
+ if err != nil {
692
+ klog .Errorf ("Error while stopping balancer : %s ; output : %s \n " , err .Error (), output )
693
+ return err
694
+ }
695
+
696
+ err = json .Unmarshal (output , & v )
697
+ if err != nil {
698
+ klog .Errorf ("Unmarshal error while stopping balancer : %s ; output = %s \n " , err .Error (), output )
690
699
return err
691
700
}
692
701
@@ -702,8 +711,10 @@ func disabelBalancer(mongosHost string) error {
702
711
"--eval" , "while(sh.isBalancerRunning().mode != 'off'){ print('waiting for balancer to stop...'); sleep(1000);}" ,
703
712
}, mongoCreds ... )
704
713
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Run (); err != nil {
714
+ klog .Errorf ("Error while waiting for the balancer to stop : %s \n " , err .Error ())
705
715
return err
706
716
}
717
+ klog .Info ("Balancer successfully Disabled." )
707
718
return nil
708
719
}
709
720
@@ -719,14 +730,33 @@ func enableBalancer(mongosHost string) error {
719
730
"--quiet" ,
720
731
"--eval" , "JSON.stringify(sh.setBalancerState(true))" ,
721
732
}, mongoCreds ... )
722
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
733
+
734
+ var (
735
+ output []byte
736
+ err error
737
+ )
738
+ cmd := sh .Command (MongoCMD , args ... )
739
+ for i := 0 ; i < 10 ; i ++ {
740
+ output , err = cmd .Output ()
741
+ if err != nil {
742
+ klog .Errorf ("Try #%d : Error on setBalancerState command : %s, output : %s .\n " , i , err .Error (), output )
743
+ time .Sleep (time .Second )
744
+ } else {
745
+ break
746
+ }
747
+ }
748
+
749
+ err = json .Unmarshal (output , & v )
750
+ if err != nil {
751
+ klog .Errorf ("Unmarshal error while enabling balancer : %+v , output : %s \n " , err .Error (), output )
723
752
return err
724
753
}
725
754
726
755
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
727
- return fmt .Errorf ("unable to disable balancer. got response: %v" , v )
756
+ return fmt .Errorf ("unable to enable balancer. got response: %v" , v )
728
757
}
729
758
759
+ klog .Info ("Balancer successfully re-enabled." )
730
760
return nil
731
761
}
732
762
@@ -745,7 +775,16 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
745
775
"--quiet" ,
746
776
"--eval" , "JSON.stringify(db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }}));" ,
747
777
}, mongoCreds ... )
748
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
778
+
779
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
780
+ if err != nil {
781
+ klog .Errorf ("Error while running findAndModify to lock configServer : %s ; output : %s \n " , err .Error (), output )
782
+ return err
783
+ }
784
+
785
+ err = json .Unmarshal (output , & v )
786
+ if err != nil {
787
+ klog .Errorf ("Unmarshal error while running findAndModify to lock configServer : %s \n " , err .Error ())
749
788
return err
750
789
}
751
790
val , ok := v ["counter" ].(float64 )
@@ -801,14 +840,23 @@ func lockSecondaryMember(mongohost string) error {
801
840
"--quiet" ,
802
841
"--eval" , "JSON.stringify(db.fsyncLock())" ,
803
842
}, mongoCreds ... )
804
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
843
+
844
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
845
+ if err != nil {
846
+ klog .Errorf ("Error while running fsyncLock on secondary : %s ; output : %s \n " , err .Error (), output )
847
+ return err
848
+ }
849
+
850
+ err = json .Unmarshal (output , & v )
851
+ if err != nil {
852
+ klog .Errorf ("Unmarshal error while running fsyncLock on secondary : %s \n " , err .Error ())
805
853
return err
806
854
}
807
855
808
856
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
809
857
return fmt .Errorf ("unable to lock the secondary host. got response: %v" , v )
810
858
}
811
-
859
+ klog . Infof ( "secondary %s locked." , mongohost )
812
860
return nil
813
861
}
814
862
@@ -827,14 +875,22 @@ func unlockSecondaryMember(mongohost string) error {
827
875
"--quiet" ,
828
876
"--eval" , "JSON.stringify(db.fsyncUnlock())" ,
829
877
}, mongoCreds ... )
830
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
878
+
879
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
880
+ if err != nil {
881
+ klog .Errorf ("Error while running fsyncUnlock on secondary : %s ; output : %s \n " , err .Error (), output )
882
+ return err
883
+ }
884
+ err = json .Unmarshal (output , & v )
885
+ if err != nil {
886
+ klog .Errorf ("Unmarshal error while running fsyncUnlock on secondary : %s \n " , err .Error ())
831
887
return err
832
888
}
833
889
834
890
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
835
891
return fmt .Errorf ("unable to lock the secondary host. got response: %v" , v )
836
892
}
837
-
893
+ klog . Infof ( "secondary %s unlocked." , mongohost )
838
894
return nil
839
895
}
840
896
0 commit comments