Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(minReadySeconds): support minReadySeconds for PD (#5827) #5831

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pkg/manager/member/pd_upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package member

import (
"fmt"
"strconv"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
Expand All @@ -23,13 +24,19 @@ import (

"github.com/pingcap/advanced-statefulset/client/apis/apps/v1/helper"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
)

const (
// set this PD clustre annotation to true to fail cluster upgrade if PD loose the quorum during one pod restart
annoKeyPDPeersCheck = "tidb.pingcap.com/pd-check-quorum-before-upgrade"

// TODO: change to use minReadySeconds in sts spec
// See https://kubernetes.io/blog/2021/08/27/minreadyseconds-statefulsets/
annoKeyPDMinReadySeconds = "tidb.pingcap.com/pd-min-ready-seconds"
)

type pdUpgrader struct {
Expand Down Expand Up @@ -79,6 +86,17 @@ func (u *pdUpgrader) gracefulUpgrade(tc *v1alpha1.TidbCluster, oldSet *apps.Stat
return nil
}

minReadySeconds := 0
s, ok := tc.Annotations[annoKeyPDMinReadySeconds]
if ok {
i, err := strconv.Atoi(s)
if err != nil {
klog.Warningf("tidbcluster: [%s/%s] annotation %s should be an integer: %v", ns, tcName, annoKeyPDMinReadySeconds, err)
} else {
minReadySeconds = i
}
}

mngerutils.SetUpgradePartition(newSet, *oldSet.Spec.UpdateStrategy.RollingUpdate.Partition)
podOrdinals := helper.GetPodOrdinals(*oldSet.Spec.Replicas, oldSet).List()
for _i := len(podOrdinals) - 1; _i >= 0; _i-- {
Expand All @@ -95,8 +113,13 @@ func (u *pdUpgrader) gracefulUpgrade(tc *v1alpha1.TidbCluster, oldSet *apps.Stat
}

if revision == tc.Status.PD.StatefulSet.UpdateRevision {
if !podutil.IsPodReady(pod) {
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s upgraded pd pod: [%s] is not ready", ns, tcName, podName)
if !podutil.IsPodAvailable(pod, int32(minReadySeconds), metav1.Now()) {
readyCond := podutil.GetPodReadyCondition(pod.Status)
if readyCond == nil || readyCond.Status != corev1.ConditionTrue {
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s upgraded pd pod: [%s] is not ready", ns, tcName, podName)

}
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s upgraded pd pod: [%s] is not available, last transition time is %v", ns, tcName, podName, readyCond.LastTransitionTime)
}
if member, exist := tc.Status.PD.Members[PdName(tc.Name, i, tc.Namespace, tc.Spec.ClusterDomain, tc.Spec.AcrossK8s)]; !exist || !member.Health {
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s pd upgraded pod: [%s] is not health", ns, tcName, podName)
Expand Down
24 changes: 24 additions & 0 deletions pkg/manager/member/pd_upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,30 @@ func TestPDUpgraderUpgrade(t *testing.T) {
g.Expect(newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(pointer.Int32Ptr(1)))
},
},
{
name: "upgraded pod is ready but not available",
changeFn: func(tc *v1alpha1.TidbCluster) {
tc.Status.PD.Synced = true
if tc.Annotations == nil {
tc.Annotations = map[string]string{}
}
// 5min is enough for unit test
tc.Annotations[annoKeyTiDBMinReadySeconds] = "300"
},
changePods: func(pods []*corev1.Pod) {
pods[1].Status.Conditions[0].LastTransitionTime = metav1.Now()
},
changeOldSet: nil,
transferLeaderErr: false,
pdPeersAreUnstable: true,
errExpectFn: func(g *GomegaWithT, err error) {
g.Expect(err).NotTo(HaveOccurred())
},
expectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster, newSet *apps.StatefulSet) {
g.Expect(tc.Status.PD.Phase).To(Equal(v1alpha1.UpgradePhase))
g.Expect(newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(pointer.Int32Ptr(1)))
},
},
}

for i := range tests {
Expand Down
Loading