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

refine defer deleting pvc codes #190

Merged
merged 10 commits into from
Nov 24, 2018
2 changes: 1 addition & 1 deletion pkg/manager/member/pd_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (psd *pdScaler) ScaleOut(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSet
return nil
}

err := psd.deleteAllDeferDeletingPVC(tc, oldSet.GetName(), v1alpha1.PDMemberType, *oldSet.Spec.Replicas, *newSet.Spec.Replicas)
_, err := psd.deleteAllDeferDeletingPVC(tc, oldSet.GetName(), v1alpha1.PDMemberType, *oldSet.Spec.Replicas, *newSet.Spec.Replicas)
if err != nil {
resetReplicas(newSet, oldSet)
return err
Expand Down
35 changes: 15 additions & 20 deletions pkg/manager/member/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ package member

import (
"fmt"
"time"

"github.com/golang/glog"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
apps "k8s.io/api/apps/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"
corelisters "k8s.io/client-go/listers/core/v1"
)

const (
skipReasonPVCNotFound = "pvc is not found"
skipReasonAnnIsNil = "pvc annotations is nil"
skipReasonAnnDeferDeletingIsEmpty = "pvc annotations defer deleting is empty"
)

// Scaler implements the logic for scaling out or scaling in the cluster.
type Scaler interface {
// ScaleOut scales out the cluster
Expand All @@ -42,46 +45,38 @@ type generalScaler struct {
}

func (gs *generalScaler) deleteAllDeferDeletingPVC(tc *v1alpha1.TidbCluster,
setName string, memberType v1alpha1.MemberType, from, to int32) error {
setName string, memberType v1alpha1.MemberType, from, to int32) (map[int32]string, error) {
ns := tc.GetNamespace()
// for unit test
skipReason := map[int32]string{}

for i := from; i < to; i++ {
pvcName := ordinalPVCName(memberType, setName, i)
pvc, err := gs.pvcLister.PersistentVolumeClaims(ns).Get(pvcName)
if errors.IsNotFound(err) {
skipReason[i] = skipReasonPVCNotFound
continue
}
if err != nil {
return err
return skipReason, err
}

if pvc.Annotations == nil {
skipReason[i] = skipReasonAnnIsNil
continue
}
if _, ok := pvc.Annotations[label.AnnPVCDeferDeleting]; !ok {
skipReason[i] = skipReasonAnnDeferDeletingIsEmpty
continue
}

err = gs.pvcControl.DeletePVC(tc, pvc)
if err != nil {
return err
}

err = wait.Poll(1*time.Second, 30*time.Second, func() (done bool, err error) {
_, err = gs.pvcLister.PersistentVolumeClaims(ns).Get(pvcName)
if errors.IsNotFound(err) {
return true, nil
}

glog.V(4).Infof("waiting for PVC: %s/%s deleting", ns, pvcName)
return false, nil
})
if err != nil {
return err
return skipReason, err
}
}

return nil
return skipReason, nil
}

func resetReplicas(newSet *apps.StatefulSet, oldSet *apps.StatefulSet) {
Expand Down
Loading