@@ -317,7 +317,6 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
317317 return err
318318 }
319319 var leaderPod corev1.Pod
320- var followerPods []corev1.Pod
321320 for _ , pod := range pods .Items {
322321 // Check if the pod is healthy.
323322 if pod .ObjectMeta .Labels ["healthy" ] != "yes" {
@@ -328,127 +327,18 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
328327 leaderPod = pod
329328 continue
330329 }
331- followerPods = append (followerPods , pod )
332330 // If pod is not leader, direct update.
333331 if err := s .applyNWait (ctx , & pod ); err != nil {
334332 return err
335333 }
336334 }
337- // All followers have been updated now, then update leader.
338- if leaderPod .Name != "" {
339- // When replicas is two (one leader and one follower).
340- if * s .sfs .Spec .Replicas == 2 {
341- if err := s .preUpdate (ctx , leaderPod .Name , followerPods [0 ].Name ); err != nil {
342- return err
343- }
344- }
345- // Update the leader.
346- if err := s .applyNWait (ctx , & leaderPod ); err != nil {
347- return err
348- }
349- }
350-
351- return nil
352- }
353-
354- // preUpdate run before update the leader pod when replicas is 2.
355- // Its main function is manually switch the leader node.
356- // 1. Get secrets (operator-user, operator-password, root-password).
357- // 2. Connect leader mysql.
358- // 3. Set leader read only.
359- // 4. Make sure the leader has sent all binlog to follower.
360- // 5. Check followerHost current role.
361- // 6. If followerHost is not leader, switch it to leader through xenon.
362- func (s * StatefulSetSyncer ) preUpdate (ctx context.Context , leader , follower string ) error {
363- leaderRunner , closeConn , err := s .SQLRunnerFactory (internal .NewConfigFromClusterKey (
364- s .cli , s .MysqlCluster .GetClusterKey (), utils .OperatorUser , utils .LeaderHost ))
365- if err != nil {
335+ // Update the leader.
336+ if err := s .applyNWait (ctx , & leaderPod ); err != nil {
366337 return err
367338 }
368- defer closeConn ()
369-
370- // Status.Replicas indicate the number of Pod has been created.
371- // So sfs.Spec.Replicas is 2, May be sfs.Status.Replicas maybe are 3, 5 ,
372- // because it do not update the pods, so it is still the last status.
373- if * s .sfs .Spec .Replicas != 2 {
374- return nil
375- }
376-
377- // Touch a new preUpdate file ,indicate that preUpdate is going on
378- // remove it when it is finished.
379- // See https://github.com/radondb/radondb-mysql-kubernetes/issues/178
380- utils .TouchUpdateFile ()
381- defer utils .RemoveUpdateFile ()
382- sctName := s .GetNameForResource (utils .Secret )
383- svcName := s .GetNameForResource (utils .HeadlessSVC )
384- nameSpace := s .Namespace
385-
386- // Get secrets.
387- secret := & corev1.Secret {}
388- if err := s .cli .Get (context .TODO (),
389- types.NamespacedName {
390- Namespace : nameSpace ,
391- Name : sctName ,
392- },
393- secret ,
394- ); err != nil {
395- return fmt .Errorf ("failed to get the secret: %s" , sctName )
396- }
397-
398- rootPasswd , ok := secret .Data ["root-password" ]
399- if ! ok {
400- return fmt .Errorf ("failed to get the root password: %s" , rootPasswd )
401- }
402-
403- if err = retry (time .Second * 2 , time .Duration (waitLimit )* time .Second , func () (bool , error ) {
404- // Set leader read only.
405- if err = leaderRunner .QueryExec (internal .NewQuery ("SET GLOBAL super_read_only=on;" )); err != nil {
406- log .Error (err , "failed to set leader read only" , "node" , leader )
407- return false , err
408- }
409-
410- // Make sure the master has sent all binlog to slave.
411- success , err := internal .CheckProcesslist (leaderRunner )
412- if err != nil {
413- return false , err
414- }
415- if success {
416- return true , nil
417- }
418- return false , nil
419- }); err != nil {
420- return err
421- }
422-
423- followerHost := fmt .Sprintf ("%s.%s.%s" , follower , svcName , nameSpace )
424- if err = retry (time .Second * 5 , time .Second * 60 , func () (bool , error ) {
425- for _ , node := range s .Status .Nodes {
426- host := node .Name
427- if host == followerHost && ! s .isLeader (host ) {
428- if err := s .XenonExecutor .RaftTryToLeader (followerHost ); err != nil {
429- return false , err
430- }
431- }
432- }
433- return true , nil
434- }); err != nil {
435- return err
436- }
437-
438339 return nil
439340}
440341
441- func (s * StatefulSetSyncer ) isLeader (host string ) bool {
442- raftStatus , err := s .XenonExecutor .RaftStatus (host )
443- if err != nil {
444- return false
445- }
446- if raftStatus .Role == string (utils .Leader ) {
447- return true
448- }
449- return false
450- }
451-
452342// mutate set the statefulset.
453343func (s * StatefulSetSyncer ) mutate () error {
454344 s .sfs .Spec .ServiceName = s .GetNameForResource (utils .StatefulSet )
0 commit comments