@@ -682,6 +682,7 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, err
682
682
}
683
683
684
684
if secHost != primary {
685
+ klog .Infof ("Primary %s & Secondary %s found for mongoDSN %s \n " , primary , secHost , mongoDSN )
685
686
return primary , secHost , nil
686
687
}
687
688
}
@@ -698,10 +699,18 @@ func disabelBalancer(mongosHost string) error {
698
699
"config" ,
699
700
"--host" , mongosHost ,
700
701
"--quiet" ,
701
- "--eval" , "JSON.stringify(sh.stopBalancer())" ,
702
+ "--eval" , "JSON.stringify(sh.stopBalancer(600000,1000 ))" ,
702
703
}, mongoCreds ... )
703
704
// disable balancer
704
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
705
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
706
+ if err != nil {
707
+ klog .Errorf ("Error while stopping balancer : %s ; output : %s \n " , err .Error (), output )
708
+ return err
709
+ }
710
+
711
+ err = json .Unmarshal (output , & v )
712
+ if err != nil {
713
+ klog .Errorf ("Unmarshal error while stopping balancer : %s ; output = %s \n " , err .Error (), output )
705
714
return err
706
715
}
707
716
@@ -717,8 +726,10 @@ func disabelBalancer(mongosHost string) error {
717
726
"--eval" , "while(sh.isBalancerRunning()){ print('waiting for balancer to stop...'); sleep(1000);}" ,
718
727
}, mongoCreds ... )
719
728
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Run (); err != nil {
729
+ klog .Errorf ("Error while waiting for the balancer to stop : %s \n " , err .Error ())
720
730
return err
721
731
}
732
+ klog .Info ("Balancer successfully Disabled." )
722
733
return nil
723
734
}
724
735
@@ -734,14 +745,33 @@ func enableBalancer(mongosHost string) error {
734
745
"--quiet" ,
735
746
"--eval" , "JSON.stringify(sh.setBalancerState(true))" ,
736
747
}, mongoCreds ... )
737
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
748
+
749
+ var (
750
+ output []byte
751
+ err error
752
+ )
753
+ cmd := sh .Command (MongoCMD , args ... )
754
+ for i := 0 ; i < 10 ; i ++ {
755
+ output , err = cmd .Output ()
756
+ if err != nil {
757
+ klog .Errorf ("Try #%d : Error on setBalancerState command : %s, output : %s .\n " , i , err .Error (), output )
758
+ time .Sleep (time .Second )
759
+ } else {
760
+ break
761
+ }
762
+ }
763
+
764
+ err = json .Unmarshal (output , & v )
765
+ if err != nil {
766
+ klog .Errorf ("Unmarshal error while enabling balancer : %+v , output : %s \n " , err .Error (), output )
738
767
return err
739
768
}
740
769
741
770
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
742
- return fmt .Errorf ("unable to disable balancer. got response: %v" , v )
771
+ return fmt .Errorf ("unable to enable balancer. got response: %v" , v )
743
772
}
744
773
774
+ klog .Info ("Balancer successfully re-enabled." )
745
775
return nil
746
776
}
747
777
@@ -760,7 +790,16 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
760
790
"--quiet" ,
761
791
"--eval" , "db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }});" ,
762
792
}, mongoCreds ... )
763
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
793
+
794
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
795
+ if err != nil {
796
+ klog .Errorf ("Error while running findAndModify to lock configServer : %s ; output : %s \n " , err .Error (), output )
797
+ return err
798
+ }
799
+
800
+ err = json .Unmarshal (output , & v )
801
+ if err != nil {
802
+ klog .Errorf ("Unmarshal error while running findAndModify to lock configServer : %s \n " , err .Error ())
764
803
return err
765
804
}
766
805
val , ok := v ["counter" ].(float64 )
@@ -814,14 +853,23 @@ func lockSecondaryMember(mongohost string) error {
814
853
"--quiet" ,
815
854
"--eval" , "JSON.stringify(db.fsyncLock())" ,
816
855
}, mongoCreds ... )
817
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
856
+
857
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
858
+ if err != nil {
859
+ klog .Errorf ("Error while running fsyncLock on secondary : %s ; output : %s \n " , err .Error (), output )
860
+ return err
861
+ }
862
+
863
+ err = json .Unmarshal (output , & v )
864
+ if err != nil {
865
+ klog .Errorf ("Unmarshal error while running fsyncLock on secondary : %s \n " , err .Error ())
818
866
return err
819
867
}
820
868
821
869
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
822
870
return fmt .Errorf ("unable to lock the secondary host. got response: %v" , v )
823
871
}
824
-
872
+ klog . Infof ( "secondary %s locked." , mongohost )
825
873
return nil
826
874
}
827
875
@@ -840,14 +888,22 @@ func unlockSecondaryMember(mongohost string) error {
840
888
"--quiet" ,
841
889
"--eval" , "JSON.stringify(db.fsyncUnlock())" ,
842
890
}, mongoCreds ... )
843
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
891
+
892
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
893
+ if err != nil {
894
+ klog .Errorf ("Error while running fsyncUnlock on secondary : %s ; output : %s \n " , err .Error (), output )
895
+ return err
896
+ }
897
+ err = json .Unmarshal (output , & v )
898
+ if err != nil {
899
+ klog .Errorf ("Unmarshal error while running fsyncUnlock on secondary : %s \n " , err .Error ())
844
900
return err
845
901
}
846
902
847
903
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
848
904
return fmt .Errorf ("unable to lock the secondary host. got response: %v" , v )
849
905
}
850
-
906
+ klog . Infof ( "secondary %s unlocked." , mongohost )
851
907
return nil
852
908
}
853
909
0 commit comments