diff --git a/pkg/apis/pingcap/v1alpha1/tidbcluster.go b/pkg/apis/pingcap/v1alpha1/tidbcluster.go index 94f8ecca13..1e4d2a13f5 100644 --- a/pkg/apis/pingcap/v1alpha1/tidbcluster.go +++ b/pkg/apis/pingcap/v1alpha1/tidbcluster.go @@ -199,8 +199,7 @@ func (tc *TidbCluster) TiKVUpgrading() bool { } func (tc *TidbCluster) TiKVScaling() bool { - return tc.Status.TiKV.Phase == ScaleOutPhase || - tc.Status.TiKV.Phase == ScaleInPhase + return tc.Status.TiKV.Phase == ScalePhase } func (tc *TidbCluster) TiDBUpgrading() bool { diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index cb0e065702..d6833abb1d 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -61,10 +61,8 @@ const ( NormalPhase MemberPhase = "Normal" // UpgradePhase represents the upgrade state of TiDB cluster. UpgradePhase MemberPhase = "Upgrade" - // ScaleInPhase represents the scaling in state of TiDB cluster. - ScaleInPhase MemberPhase = "ScaleIn" - // ScaleOutPhase represents the scaling out state of TiDB cluster. - ScaleOutPhase MemberPhase = "ScaleOut" + // ScalePhase represents the scaling state of TiDB cluster. + ScalePhase MemberPhase = "Scale" ) // ConfigUpdateStrategy represents the strategy to update configuration diff --git a/pkg/manager/member/tikv_member_manager.go b/pkg/manager/member/tikv_member_manager.go index 7aef1364ba..12290bec5e 100644 --- a/pkg/manager/member/tikv_member_manager.go +++ b/pkg/manager/member/tikv_member_manager.go @@ -633,7 +633,9 @@ func (tkmm *tikvMemberManager) syncTidbClusterStatus(tc *v1alpha1.TidbCluster, s } if upgrading && tc.Status.PD.Phase != v1alpha1.UpgradePhase { tc.Status.TiKV.Phase = v1alpha1.UpgradePhase - } else if !tc.TiKVScaling() { + } else if tc.TiKVStsDesiredReplicas() != *set.Spec.Replicas { + tc.Status.TiKV.Phase = v1alpha1.ScalePhase + } else { tc.Status.TiKV.Phase = v1alpha1.NormalPhase } diff --git a/pkg/manager/member/tikv_member_manager_test.go b/pkg/manager/member/tikv_member_manager_test.go index 878a71713e..cbf7169868 100644 --- a/pkg/manager/member/tikv_member_manager_test.go +++ b/pkg/manager/member/tikv_member_manager_test.go @@ -799,11 +799,15 @@ func TestTiKVMemberManagerSyncTidbClusterStatus(t *testing.T) { status := apps.StatefulSetStatus{ Replicas: int32(3), } + spec := apps.StatefulSetSpec{ + Replicas: pointer.Int32Ptr(3), + } now := metav1.Time{Time: time.Now()} testFn := func(test *testcase, t *testing.T) { tc := newTidbClusterForPD() tc.Status.PD.Phase = v1alpha1.NormalPhase set := &apps.StatefulSet{ + Spec: spec, Status: status, } if test.updateTC != nil { @@ -913,7 +917,7 @@ func TestTiKVMemberManagerSyncTidbClusterStatus(t *testing.T) { { name: "statefulset is scaling out", updateTC: func(tc *v1alpha1.TidbCluster) { - tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase + tc.Spec.TiKV.Replicas = 4 }, upgradingFn: func(lister corelisters.PodLister, controlInterface pdapi.PDControlInterface, set *apps.StatefulSet, cluster *v1alpha1.TidbCluster) (bool, error) { return false, nil @@ -925,13 +929,13 @@ func TestTiKVMemberManagerSyncTidbClusterStatus(t *testing.T) { errExpectFn: nil, tcExpectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { g.Expect(tc.Status.TiKV.StatefulSet.Replicas).To(Equal(int32(3))) - g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleOutPhase)) + g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScalePhase)) }, }, { name: "statefulset is scaling in", updateTC: func(tc *v1alpha1.TidbCluster) { - tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase + tc.Spec.TiKV.Replicas = 2 }, upgradingFn: func(lister corelisters.PodLister, controlInterface pdapi.PDControlInterface, set *apps.StatefulSet, cluster *v1alpha1.TidbCluster) (bool, error) { return false, nil @@ -943,7 +947,7 @@ func TestTiKVMemberManagerSyncTidbClusterStatus(t *testing.T) { errExpectFn: nil, tcExpectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { g.Expect(tc.Status.TiKV.StatefulSet.Replicas).To(Equal(int32(3))) - g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleInPhase)) + g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScalePhase)) }, }, { diff --git a/pkg/manager/member/tikv_scaler.go b/pkg/manager/member/tikv_scaler.go index db55909dc0..9dc63bc1d6 100644 --- a/pkg/manager/member/tikv_scaler.go +++ b/pkg/manager/member/tikv_scaler.go @@ -49,10 +49,6 @@ func (tsd *tikvScaler) Scale(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSet, return tsd.ScaleOut(tc, oldSet, newSet) } else if scaling < 0 { return tsd.ScaleIn(tc, oldSet, newSet) - } else { - if tc.TiKVScaling() { - tc.Status.TiKV.Phase = v1alpha1.NormalPhase - } } // we only sync auto scaler annotations when we are finishing syncing scaling return tsd.SyncAutoScalerAnn(tc, oldSet) @@ -62,17 +58,7 @@ func (tsd *tikvScaler) ScaleOut(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulS _, ordinal, replicas, deleteSlots := scaleOne(oldSet, newSet) resetReplicas(newSet, oldSet) if tc.TiKVUpgrading() { - klog.Infof("the TidbCluster: [%s/%s]'s tikv is upgrading, can not scale out until the upgrade completed", - tc.Namespace, tc.Name) - return nil - } - // During TidbCluster upgrade, if TiKV is scaled at the same time, since - // TiKV cannot be upgraded during PD upgrade, the TiKV scaling will occur - // before the TiKV upgrade, in this case, the Pump, TiDB, TiFlash, TiCDC, etc. - // will be upgraded before TiKV upgrade. - // To avoid this case, we skip the scaling out during PD upgrade. - if tc.PDUpgrading() { - klog.Infof("the TidbCluster: [%s/%s]'s pd is upgrading, can not scale out until the upgrade completed", + klog.Infof("TidbCluster: [%s/%s]'s tikv is upgrading, can not scale out until the upgrade completed", tc.Namespace, tc.Name) return nil } @@ -83,7 +69,6 @@ func (tsd *tikvScaler) ScaleOut(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulS return err } - tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase setReplicasAndDeleteSlots(newSet, replicas, deleteSlots) return nil } @@ -98,17 +83,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe // tikv can not scale in when it is upgrading if tc.TiKVUpgrading() { - klog.Infof("the TidbCluster: [%s/%s]'s tikv is upgrading,can not scale in until upgrade have completed", - ns, tcName) - return nil - } - // During TidbCluster upgrade, if TiKV is scaled at the same time, since - // TiKV cannot be upgraded during PD upgrade, the TiKV scaling will occur - // before the TiKV upgrade, in this case, the Pump, TiDB, TiFlash, TiCDC, etc. - // will be upgraded before TiKV upgrade. - // To avoid this case, we skip the scaling in during PD upgrade. - if tc.PDUpgrading() { - klog.Infof("the TidbCluster: [%s/%s]'s pd is upgrading, can not scale in until the upgrade completed", + klog.Infof("TidbCluster: [%s/%s]'s tikv is upgrading, can not scale in until upgrade completed", ns, tcName) return nil } @@ -122,7 +97,6 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe } if controller.PodWebhookEnabled { - tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase setReplicasAndDeleteSlots(newSet, replicas, deleteSlots) return nil } @@ -135,7 +109,6 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe return err } if state != v1alpha1.TiKVStateOffline { - tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase if err := controller.GetPDClient(tsd.pdControl, tc).DeleteStore(id); err != nil { klog.Errorf("tikv scale in: failed to delete store %d, %v", id, err) return err @@ -173,7 +146,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe } klog.Infof("tikv scale in: set pvc %s/%s annotation: %s to %s", ns, pvcName, label.AnnPVCDeferDeleting, now) - tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase + setReplicasAndDeleteSlots(newSet, replicas, deleteSlots) return nil } @@ -216,7 +189,6 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe } klog.Infof("pod %s not ready, tikv scale in: set pvc %s/%s annotation: %s to %s", podName, ns, pvcName, label.AnnPVCDeferDeleting, now) - tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase setReplicasAndDeleteSlots(newSet, replicas, deleteSlots) return nil } diff --git a/pkg/manager/member/tikv_scaler_test.go b/pkg/manager/member/tikv_scaler_test.go index 7ccca03288..56e75cc607 100644 --- a/pkg/manager/member/tikv_scaler_test.go +++ b/pkg/manager/member/tikv_scaler_test.go @@ -31,70 +31,17 @@ import ( "k8s.io/client-go/tools/cache" ) -func TestTiKVScalerScale(t *testing.T) { - g := NewGomegaWithT(t) - type testcase struct { - name string - tikvPhase v1alpha1.MemberPhase - expectPhase v1alpha1.MemberPhase - } - - testFn := func(test *testcase, t *testing.T) { - t.Log(test.name) - tc := newTidbClusterForPD() - - tc.Status.TiKV.Phase = test.tikvPhase - - oldSet := newStatefulSetForPDScale() - newSet := oldSet.DeepCopy() - - scaler, _, _, _, _ := newFakeTiKVScaler() - - _ = scaler.Scale(tc, oldSet, newSet) - g.Expect(tc.Status.TiKV.Phase).To(Equal(test.expectPhase)) - } - - tests := []testcase{ - { - name: "normal", - tikvPhase: v1alpha1.NormalPhase, - expectPhase: v1alpha1.NormalPhase, - }, - { - name: "upgrade", - tikvPhase: v1alpha1.UpgradePhase, - expectPhase: v1alpha1.UpgradePhase, - }, - { - name: "scale in", - tikvPhase: v1alpha1.ScaleInPhase, - expectPhase: v1alpha1.NormalPhase, - }, - { - name: "scale out", - tikvPhase: v1alpha1.ScaleOutPhase, - expectPhase: v1alpha1.NormalPhase, - }, - } - - for i := range tests { - testFn(&tests[i], t) - } -} - func TestTiKVScalerScaleOut(t *testing.T) { g := NewGomegaWithT(t) type testcase struct { name string tikvUpgrading bool - pdUpgrading bool hasPVC bool hasDeferAnn bool pvcDeleteErr bool annoIsNil bool errExpectFn func(*GomegaWithT, error) changed bool - tikvPhase v1alpha1.MemberPhase } testFn := func(test *testcase, t *testing.T) { @@ -104,9 +51,6 @@ func TestTiKVScalerScaleOut(t *testing.T) { if test.tikvUpgrading { tc.Status.TiKV.Phase = v1alpha1.UpgradePhase } - if test.pdUpgrading { - tc.Status.PD.Phase = v1alpha1.UpgradePhase - } oldSet := newStatefulSetForPDScale() newSet := oldSet.DeepCopy() @@ -134,7 +78,6 @@ func TestTiKVScalerScaleOut(t *testing.T) { err := scaler.ScaleOut(tc, oldSet, newSet) test.errExpectFn(g, err) - g.Expect(tc.Status.TiKV.Phase).To(Equal(test.tikvPhase)) if test.changed { g.Expect(int(*newSet.Spec.Replicas)).To(Equal(6)) } else { @@ -152,7 +95,6 @@ func TestTiKVScalerScaleOut(t *testing.T) { pvcDeleteErr: false, errExpectFn: errExpectNil, changed: true, - tikvPhase: v1alpha1.ScaleOutPhase, }, { name: "tikv is upgrading", @@ -163,18 +105,6 @@ func TestTiKVScalerScaleOut(t *testing.T) { pvcDeleteErr: false, errExpectFn: errExpectNil, changed: false, - tikvPhase: v1alpha1.UpgradePhase, - }, - { - name: "pd is upgrading", - tikvUpgrading: false, - pdUpgrading: true, - hasPVC: true, - hasDeferAnn: false, - annoIsNil: true, - pvcDeleteErr: false, - errExpectFn: errExpectNil, - changed: false, }, { name: "cache don't have pvc", @@ -185,7 +115,6 @@ func TestTiKVScalerScaleOut(t *testing.T) { pvcDeleteErr: false, errExpectFn: errExpectNil, changed: true, - tikvPhase: v1alpha1.ScaleOutPhase, }, { name: "pvc annotation is not nil but doesn't contain defer deletion annotation", @@ -196,7 +125,6 @@ func TestTiKVScalerScaleOut(t *testing.T) { pvcDeleteErr: false, errExpectFn: errExpectNil, changed: true, - tikvPhase: v1alpha1.ScaleOutPhase, }, { name: "pvc annotations defer deletion is not nil, pvc delete failed", @@ -219,7 +147,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { type testcase struct { name string tikvUpgrading bool - pdUpgrading bool storeFun func(tc *v1alpha1.TidbCluster) delStoreErr bool hasPVC bool @@ -229,7 +156,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr bool errExpectFn func(*GomegaWithT, error) changed bool - tikvPhase v1alpha1.MemberPhase } controller.ResyncDuration = 0 @@ -242,9 +168,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { if test.tikvUpgrading { tc.Status.TiKV.Phase = v1alpha1.UpgradePhase } - if test.pdUpgrading { - tc.Status.PD.Phase = v1alpha1.UpgradePhase - } oldSet := newStatefulSetForPDScale() newSet := oldSet.DeepCopy() @@ -294,7 +217,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { err := scaler.ScaleIn(tc, oldSet, newSet) test.errExpectFn(g, err) - g.Expect(tc.Status.TiKV.Phase).To(Equal(test.tikvPhase)) if test.changed { g.Expect(int(*newSet.Spec.Replicas)).To(Equal(4)) } else { @@ -307,7 +229,7 @@ func TestTiKVScalerScaleIn(t *testing.T) { name: "store is up, delete store failed", tikvUpgrading: false, storeFun: normalStoreFun, - delStoreErr: true, + delStoreErr: false, hasPVC: true, storeIDSynced: true, isPodReady: true, @@ -315,7 +237,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr: false, errExpectFn: errExpectNotNil, changed: false, - tikvPhase: v1alpha1.ScaleInPhase, }, { name: "store state is up, delete store success", @@ -329,7 +250,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr: false, errExpectFn: errExpectRequeue, changed: false, - tikvPhase: v1alpha1.ScaleInPhase, }, { name: "tikv is upgrading", @@ -343,21 +263,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr: false, errExpectFn: errExpectNil, changed: false, - tikvPhase: v1alpha1.UpgradePhase, - }, - { - name: "pd is upgrading", - tikvUpgrading: false, - pdUpgrading: true, - storeFun: normalStoreFun, - delStoreErr: false, - hasPVC: true, - storeIDSynced: true, - isPodReady: true, - hasSynced: true, - pvcUpdateErr: false, - errExpectFn: errExpectNil, - changed: false, }, { name: "status.TiKV.Stores is empty", @@ -403,7 +308,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr: false, errExpectFn: errExpectNil, changed: true, - tikvPhase: v1alpha1.ScaleInPhase, }, { name: "podName not match", @@ -471,7 +375,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr: false, errExpectFn: errExpectNil, changed: true, - tikvPhase: v1alpha1.ScaleInPhase, }, { name: "store state is tombstone and store id not match", @@ -485,7 +388,6 @@ func TestTiKVScalerScaleIn(t *testing.T) { pvcUpdateErr: false, errExpectFn: errExpectNotNil, changed: false, - tikvPhase: v1alpha1.ScaleInPhase, }, { name: "store state is tombstone, id is not integer", diff --git a/pkg/manager/member/tikv_upgrader_test.go b/pkg/manager/member/tikv_upgrader_test.go index 2e1163f899..5962a39d27 100644 --- a/pkg/manager/member/tikv_upgrader_test.go +++ b/pkg/manager/member/tikv_upgrader_test.go @@ -291,10 +291,10 @@ func TestTiKVUpgraderUpgrade(t *testing.T) { }, }, { - name: "tikv can not upgrade when it is scaling in", + name: "tikv can not upgrade when it is scaling", changeFn: func(tc *v1alpha1.TidbCluster) { tc.Status.PD.Phase = v1alpha1.NormalPhase - tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase + tc.Status.TiKV.Phase = v1alpha1.ScalePhase tc.Status.TiKV.Synced = true }, changeOldSet: func(oldSet *apps.StatefulSet) { @@ -308,29 +308,7 @@ func TestTiKVUpgraderUpgrade(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) }, expectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster, newSet *apps.StatefulSet, pods map[string]*corev1.Pod) { - g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleInPhase)) - g.Expect(*newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(int32(3))) - }, - }, - { - name: "tikv can not upgrade when it is scaling out", - changeFn: func(tc *v1alpha1.TidbCluster) { - tc.Status.PD.Phase = v1alpha1.NormalPhase - tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase - tc.Status.TiKV.Synced = true - }, - changeOldSet: func(oldSet *apps.StatefulSet) { - SetStatefulSetLastAppliedConfigAnnotation(oldSet) - }, - changePods: nil, - beginEvictLeaderErr: false, - endEvictLeaderErr: false, - updatePodErr: false, - errExpectFn: func(g *GomegaWithT, err error) { - g.Expect(err).NotTo(HaveOccurred()) - }, - expectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster, newSet *apps.StatefulSet, pods map[string]*corev1.Pod) { - g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleOutPhase)) + g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScalePhase)) g.Expect(*newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(int32(3))) }, },