@@ -607,6 +607,7 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, err
607
607
}
608
608
609
609
if secHost != primary {
610
+ klog .Infof ("Primary %s & Secondary %s found for mongoDSN %s \n " , primary , secHost , mongoDSN )
610
611
return primary , secHost , nil
611
612
}
612
613
}
@@ -623,10 +624,18 @@ func disabelBalancer(mongosHost string) error {
623
624
"config" ,
624
625
"--host" , mongosHost ,
625
626
"--quiet" ,
626
- "--eval" , "JSON.stringify(sh.stopBalancer())" ,
627
+ "--eval" , "JSON.stringify(sh.stopBalancer(600000,1000 ))" ,
627
628
}, mongoCreds ... )
628
629
// disable balancer
629
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
630
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
631
+ if err != nil {
632
+ klog .Errorf ("Error while stopping balancer : %s ; output : %s \n " , err .Error (), output )
633
+ return err
634
+ }
635
+
636
+ err = json .Unmarshal (output , & v )
637
+ if err != nil {
638
+ klog .Errorf ("Unmarshal error while stopping balancer : %s ; output = %s \n " , err .Error (), output )
630
639
return err
631
640
}
632
641
@@ -642,8 +651,10 @@ func disabelBalancer(mongosHost string) error {
642
651
"--eval" , "while(sh.isBalancerRunning()){ print('waiting for balancer to stop...'); sleep(1000);}" ,
643
652
}, mongoCreds ... )
644
653
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Run (); err != nil {
654
+ klog .Errorf ("Error while waiting for the balancer to stop : %s \n " , err .Error ())
645
655
return err
646
656
}
657
+ klog .Info ("Balancer successfully Disabled." )
647
658
return nil
648
659
}
649
660
@@ -659,14 +670,33 @@ func enableBalancer(mongosHost string) error {
659
670
"--quiet" ,
660
671
"--eval" , "JSON.stringify(sh.setBalancerState(true))" ,
661
672
}, mongoCreds ... )
662
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
673
+
674
+ var (
675
+ output []byte
676
+ err error
677
+ )
678
+ cmd := sh .Command (MongoCMD , args ... )
679
+ for i := 0 ; i < 10 ; i ++ {
680
+ output , err = cmd .Output ()
681
+ if err != nil {
682
+ klog .Errorf ("Try #%d : Error on setBalancerState command : %s, output : %s .\n " , i , err .Error (), output )
683
+ time .Sleep (time .Second )
684
+ } else {
685
+ break
686
+ }
687
+ }
688
+
689
+ err = json .Unmarshal (output , & v )
690
+ if err != nil {
691
+ klog .Errorf ("Unmarshal error while enabling balancer : %+v , output : %s \n " , err .Error (), output )
663
692
return err
664
693
}
665
694
666
695
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
667
- return fmt .Errorf ("unable to disable balancer. got response: %v" , v )
696
+ return fmt .Errorf ("unable to enable balancer. got response: %v" , v )
668
697
}
669
698
699
+ klog .Info ("Balancer successfully re-enabled." )
670
700
return nil
671
701
}
672
702
@@ -685,7 +715,22 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
685
715
"--quiet" ,
686
716
"--eval" , "db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }});" ,
687
717
}, mongoCreds ... )
688
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
718
+
719
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
720
+ if err != nil {
721
+ klog .Errorf ("Error while running findAndModify to lock configServer : %s ; output : %s \n " , err .Error (), output )
722
+ return err
723
+ }
724
+ s := fmt .Sprintf (`/bin/echo '%s' | /usr/bin/tail -1` , strings .TrimSuffix (string (output ), "\n " ))
725
+ output , err = sh .Command ("/bin/sh" , "-c" , s ).Output ()
726
+ if err != nil {
727
+ klog .Errorf ("Error while running tail in findAndModify to lock configServer : %s ; output : %s \n " , err .Error (), output )
728
+ return err
729
+ }
730
+
731
+ err = json .Unmarshal (output , & v )
732
+ if err != nil {
733
+ klog .Errorf ("Unmarshal error while running findAndModify to lock configServer : %s \n " , err .Error ())
689
734
return err
690
735
}
691
736
val , ok := v ["counter" ].(float64 )
@@ -739,14 +784,23 @@ func lockSecondaryMember(mongohost string) error {
739
784
"--quiet" ,
740
785
"--eval" , "JSON.stringify(db.fsyncLock())" ,
741
786
}, mongoCreds ... )
742
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
787
+
788
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
789
+ if err != nil {
790
+ klog .Errorf ("Error while running fsyncLock on secondary : %s ; output : %s \n " , err .Error (), output )
791
+ return err
792
+ }
793
+
794
+ err = json .Unmarshal (output , & v )
795
+ if err != nil {
796
+ klog .Errorf ("Unmarshal error while running fsyncLock on secondary : %s \n " , err .Error ())
743
797
return err
744
798
}
745
799
746
800
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
747
801
return fmt .Errorf ("unable to lock the secondary host. got response: %v" , v )
748
802
}
749
-
803
+ klog . Infof ( "secondary %s locked." , mongohost )
750
804
return nil
751
805
}
752
806
@@ -765,13 +819,21 @@ func unlockSecondaryMember(mongohost string) error {
765
819
"--quiet" ,
766
820
"--eval" , "JSON.stringify(db.fsyncUnlock())" ,
767
821
}, mongoCreds ... )
768
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
822
+
823
+ output , err := sh .Command (MongoCMD , args ... ).Output ()
824
+ if err != nil {
825
+ klog .Errorf ("Error while running fsyncUnlock on secondary : %s ; output : %s \n " , err .Error (), output )
826
+ return err
827
+ }
828
+ err = json .Unmarshal (output , & v )
829
+ if err != nil {
830
+ klog .Errorf ("Unmarshal error while running fsyncUnlock on secondary : %s \n " , err .Error ())
769
831
return err
770
832
}
771
833
772
834
if val , ok := v ["ok" ].(float64 ); ! ok || int (val ) != 1 {
773
835
return fmt .Errorf ("unable to lock the secondary host. got response: %v" , v )
774
836
}
775
-
837
+ klog . Infof ( "secondary %s unlocked." , mongohost )
776
838
return nil
777
839
}
0 commit comments