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

Fix PD failover #2570

Merged
merged 8 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion pkg/controller/tidbcluster/tidb_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewController(
pdScaler := mm.NewPDScaler(pdControl, pvcInformer.Lister(), pvcControl)
tikvScaler := mm.NewTiKVScaler(pdControl, pvcInformer.Lister(), pvcControl, podInformer.Lister())
tiflashScaler := mm.NewTiFlashScaler(pdControl, pvcInformer.Lister(), pvcControl, podInformer.Lister())
pdFailover := mm.NewPDFailover(cli, pdControl, pdFailoverPeriod, podInformer.Lister(), podControl, pvcInformer.Lister(), pvcControl, pvInformer.Lister(), recorder)
pdFailover := mm.NewPDFailover(cli, pdControl, pdFailoverPeriod, podInformer.Lister(), podControl, pvcInformer.Lister(), pvcControl, pvInformer.Lister(), setInformer.Lister(), recorder)
tikvFailover := mm.NewTiKVFailover(tikvFailoverPeriod, recorder)
tidbFailover := mm.NewTiDBFailover(tidbFailoverPeriod, recorder, podInformer.Lister())
tiflashFailover := mm.NewTiFlashFailover(tiflashFailoverPeriod, recorder)
Expand Down
17 changes: 17 additions & 0 deletions pkg/manager/member/pd_failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strconv"
"time"

"github.com/pingcap/advanced-statefulset/client/apis/apps/v1/helper"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
"github.com/pingcap/tidb-operator/pkg/controller"
Expand All @@ -26,6 +27,8 @@ import (
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
apps "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
Expand All @@ -40,6 +43,7 @@ type pdFailover struct {
pvcLister corelisters.PersistentVolumeClaimLister
pvcControl controller.PVCControlInterface
pvLister corelisters.PersistentVolumeLister
setLister apps.StatefulSetLister
recorder record.EventRecorder
}

Expand All @@ -52,6 +56,7 @@ func NewPDFailover(cli versioned.Interface,
pvcLister corelisters.PersistentVolumeClaimLister,
pvcControl controller.PVCControlInterface,
pvLister corelisters.PersistentVolumeLister,
setLister apps.StatefulSetLister,
recorder record.EventRecorder) Failover {
return &pdFailover{
cli,
Expand All @@ -62,6 +67,7 @@ func NewPDFailover(cli versioned.Interface,
pvcLister,
pvcControl,
pvLister,
setLister,
recorder}
}

Expand All @@ -77,7 +83,18 @@ func (pf *pdFailover) Failover(tc *v1alpha1.TidbCluster) error {
}

healthCount := 0
pdsts, err := pf.setLister.StatefulSets(ns).Get(controller.PDMemberName(tcName))
if err != nil {
return err
}
podNames := sets.String{}
for _, ordinal := range helper.GetPodOrdinals(*pdsts.Spec.Replicas, pdsts).List() {
podNames.Insert(util.GetPodName(tc, v1alpha1.PDMemberType, ordinal))
}
for podName, pdMember := range tc.Status.PD.Members {
if !podNames.Has(podName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should update tryToMarkAPeerAsFailure instead of here, why ignore the non-managed members in the cluster health check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok for me. updated.

continue
}
if pdMember.Health {
healthCount++
} else {
Expand Down
29 changes: 26 additions & 3 deletions pkg/manager/member/pd_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/fake"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/pdapi"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -471,7 +472,7 @@ func TestPDFailoverFailover(t *testing.T) {
tc.Spec.PD.MaxFailoverCount = pointer.Int32Ptr(test.maxFailoverCount)
test.update(tc)

pdFailover, pvcIndexer, podIndexer, fakePDControl, fakePodControl, fakePVCControl := newFakePDFailover()
pdFailover, pvcIndexer, podIndexer, stsIndexer, fakePDControl, fakePodControl, fakePVCControl := newFakePDFailover()
pdClient := controller.NewFakePDClient(fakePDControl, tc)
pdFailover.recorder = recorder

Expand All @@ -482,6 +483,8 @@ func TestPDFailoverFailover(t *testing.T) {
return nil, nil
})

stsIndexer.Add(newStsForTc(tc, v1alpha1.PDMemberType))

if test.hasPVC {
pvc := newPVCForPDFailover(tc, v1alpha1.PDMemberType, 1)
if test.pvcWithDeletionTimestamp {
Expand Down Expand Up @@ -525,7 +528,7 @@ func TestPDFailoverRecovery(t *testing.T) {
tc := newTidbClusterForPD()
test.update(tc)

pdFailover, _, _, _, _, _ := newFakePDFailover()
pdFailover, _, _, _, _, _, _ := newFakePDFailover()
pdFailover.Recover(tc)
test.expectFn(tc)
}
Expand Down Expand Up @@ -614,14 +617,15 @@ func TestPDFailoverRecovery(t *testing.T) {
}
}

func newFakePDFailover() (*pdFailover, cache.Indexer, cache.Indexer, *pdapi.FakePDControl, *controller.FakePodControl, *controller.FakePVCControl) {
func newFakePDFailover() (*pdFailover, cache.Indexer, cache.Indexer, cache.Indexer, *pdapi.FakePDControl, *controller.FakePodControl, *controller.FakePVCControl) {
cli := fake.NewSimpleClientset()
kubeCli := kubefake.NewSimpleClientset()
pdControl := pdapi.NewFakePDControl(kubeCli)
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeCli, 0)
podInformer := kubeInformerFactory.Core().V1().Pods()
pvcInformer := kubeInformerFactory.Core().V1().PersistentVolumeClaims()
pvInformer := kubeInformerFactory.Core().V1().PersistentVolumes()
stsInformer := kubeInformerFactory.Apps().V1().StatefulSets()
podControl := controller.NewFakePodControl(podInformer)
pvcControl := controller.NewFakePVCControl(pvcInformer)

Expand All @@ -634,9 +638,11 @@ func newFakePDFailover() (*pdFailover, cache.Indexer, cache.Indexer, *pdapi.Fake
pvcInformer.Lister(),
pvcControl,
pvInformer.Lister(),
stsInformer.Lister(),
nil},
pvcInformer.Informer().GetIndexer(),
podInformer.Informer().GetIndexer(),
stsInformer.Informer().GetIndexer(),
pdControl, podControl, pvcControl
}

Expand Down Expand Up @@ -756,3 +762,20 @@ func collectEvents(source <-chan string) []string {
}
return events
}

func newStsForTc(tc *v1alpha1.TidbCluster, memberType v1alpha1.MemberType) *apps.StatefulSet {
sts := apps.StatefulSet{}
sts.Name = fmt.Sprintf("%s-%s", tc.Name, memberType.String())
sts.Namespace = tc.Namespace
switch memberType {
case v1alpha1.TiDBMemberType:
sts.Spec.Replicas = &tc.Spec.TiDB.Replicas
case v1alpha1.TiKVMemberType:
sts.Spec.Replicas = &tc.Spec.TiKV.Replicas
case v1alpha1.PDMemberType:
sts.Spec.Replicas = &tc.Spec.PD.Replicas
case v1alpha1.TiFlashMemberType:
sts.Spec.Replicas = &tc.Spec.TiFlash.Replicas
}
return &sts
}