Skip to content

Commit

Permalink
refine defer deleting pvc codes (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
weekface authored and tennix committed Nov 24, 2018
1 parent f06670a commit e5c3cdd
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 22 deletions.
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

0 comments on commit e5c3cdd

Please sign in to comment.