Skip to content

Commit 7ab7fbb

Browse files
committed
syncer: Set the role tag into the corresponding raft role. #330
1 parent a5d6d9f commit 7ab7fbb

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

mysqlcluster/syncer/follower_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewFollowerSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) synce
4848
service.Spec.Type = "ClusterIP"
4949
}
5050
service.Spec.Selector = c.GetSelectorLabels()
51-
service.Spec.Selector["role"] = "follower"
51+
service.Spec.Selector["role"] = string(utils.Follower)
5252
service.Spec.Selector["healthy"] = "yes"
5353

5454
if len(service.Spec.Ports) != 2 {

mysqlcluster/syncer/leader_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewLeaderSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.
4848
service.Spec.Type = "ClusterIP"
4949
}
5050
service.Spec.Selector = c.GetSelectorLabels()
51-
service.Spec.Selector["role"] = "leader"
51+
service.Spec.Selector["role"] = string(utils.Leader)
5252

5353
if len(service.Spec.Ports) != 2 {
5454
service.Spec.Ports = make([]corev1.ServicePort, 2)

mysqlcluster/syncer/statefulset.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,12 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
332332
return err
333333
}
334334
}
335-
// Update the leader.
336-
if err := s.applyNWait(ctx, &leaderPod); err != nil {
337-
return err
335+
// There may be a case where Leader does not exist during the update process.
336+
if leaderPod.Name != "" {
337+
// Update the leader.
338+
if err := s.applyNWait(ctx, &leaderPod); err != nil {
339+
return err
340+
}
338341
}
339342
return nil
340343
}

mysqlcluster/syncer/status.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ func (s *StatusSyncer) updateNodeStatus(ctx context.Context, cli client.Client,
232232
// update apiv1alpha1.NodeConditionReadOnly.
233233
s.updateNodeCondition(node, int(apiv1alpha1.IndexReadOnly), isReadOnly)
234234

235-
if err = s.setPodHealthy(ctx, &pod, node); err != nil {
236-
log.Error(err, "cannot update pod", "name", podName, "namespace", pod.Namespace)
235+
if err = s.updatePodLabel(ctx, &pod, node); err != nil {
236+
log.Error(err, "failed to update labels", "pod", pod.Name, "namespace", pod.Namespace)
237237
}
238238
}
239239

@@ -358,9 +358,10 @@ func (s *StatusSyncer) addNodesInXenon(host string, toAdd []string) error {
358358
return nil
359359
}
360360

361-
// setPodHealthy set the pod lable healthy.
362-
func (s *StatusSyncer) setPodHealthy(ctx context.Context, pod *corev1.Pod, node *apiv1alpha1.NodeStatus) error {
361+
// updatePodLabel update the pod lables.
362+
func (s *StatusSyncer) updatePodLabel(ctx context.Context, pod *corev1.Pod, node *apiv1alpha1.NodeStatus) error {
363363
healthy := "no"
364+
isPodLabelsUpdated := false
364365
if node.Conditions[apiv1alpha1.IndexLagged].Status == corev1.ConditionFalse {
365366
if node.Conditions[apiv1alpha1.IndexLeader].Status == corev1.ConditionFalse &&
366367
node.Conditions[apiv1alpha1.IndexReadOnly].Status == corev1.ConditionTrue &&
@@ -375,6 +376,13 @@ func (s *StatusSyncer) setPodHealthy(ctx context.Context, pod *corev1.Pod, node
375376

376377
if pod.Labels["healthy"] != healthy {
377378
pod.Labels["healthy"] = healthy
379+
isPodLabelsUpdated = true
380+
}
381+
if pod.Labels["role"] != node.RaftStatus.Role {
382+
pod.Labels["role"] = node.RaftStatus.Role
383+
isPodLabelsUpdated = true
384+
}
385+
if isPodLabelsUpdated {
378386
if err := s.cli.Update(ctx, pod); client.IgnoreNotFound(err) != nil {
379387
return err
380388
}

0 commit comments

Comments
 (0)