|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package scale |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/onsi/ginkgo/v2" |
| 22 | + "k8s.io/utils/ptr" |
| 23 | + |
| 24 | + "github.com/pingcap/tidb-operator/pkg/client" |
| 25 | + "github.com/pingcap/tidb-operator/pkg/runtime" |
| 26 | + "github.com/pingcap/tidb-operator/pkg/runtime/scope" |
| 27 | + "github.com/pingcap/tidb-operator/tests/e2e/data" |
| 28 | + "github.com/pingcap/tidb-operator/tests/e2e/framework" |
| 29 | + "github.com/pingcap/tidb-operator/tests/e2e/label" |
| 30 | + "github.com/pingcap/tidb-operator/tests/e2e/utils/waiter" |
| 31 | +) |
| 32 | + |
| 33 | +var _ = ginkgo.Describe("Scale TiKV", label.TiKV, label.MultipleAZ, label.P0, label.Scale, func() { |
| 34 | + f := framework.New() |
| 35 | + f.Setup() |
| 36 | + |
| 37 | + ginkgo.It("support scale out from 3 to 4", func(ctx context.Context) { |
| 38 | + pdg := f.MustCreatePD(ctx) |
| 39 | + kvg := f.MustCreateTiKV(ctx, |
| 40 | + data.WithReplicas[scope.TiKVGroup](3), |
| 41 | + data.WithTiKVEvenlySpreadPolicy(), |
| 42 | + ) |
| 43 | + |
| 44 | + f.WaitForPDGroupReady(ctx, pdg) |
| 45 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 46 | + |
| 47 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 48 | + |
| 49 | + patch := client.MergeFrom(kvg.DeepCopy()) |
| 50 | + kvg.Spec.Replicas = ptr.To[int32](4) |
| 51 | + |
| 52 | + f.Must(f.Client.Patch(ctx, kvg, patch)) |
| 53 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 54 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 55 | + }) |
| 56 | + |
| 57 | + ginkgo.It("support scale in from 4 to 3", func(ctx context.Context) { |
| 58 | + pdg := f.MustCreatePD(ctx) |
| 59 | + kvg := f.MustCreateTiKV(ctx, |
| 60 | + data.WithReplicas[scope.TiKVGroup](4), |
| 61 | + data.WithTiKVEvenlySpreadPolicy(), |
| 62 | + ) |
| 63 | + |
| 64 | + f.WaitForPDGroupReady(ctx, pdg) |
| 65 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 66 | + |
| 67 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 68 | + |
| 69 | + patch := client.MergeFrom(kvg.DeepCopy()) |
| 70 | + kvg.Spec.Replicas = ptr.To[int32](3) |
| 71 | + |
| 72 | + f.Must(f.Client.Patch(ctx, kvg, patch)) |
| 73 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 74 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 75 | + }) |
| 76 | + |
| 77 | + ginkgo.It("support scale in from 4 to 3 with a pending pod", func(ctx context.Context) { |
| 78 | + pdg := f.MustCreatePD(ctx) |
| 79 | + kvg := f.MustCreateTiKV(ctx, |
| 80 | + data.WithReplicas[scope.TiKVGroup](3), |
| 81 | + data.WithTiKVEvenlySpreadPolicy(), |
| 82 | + data.WithTiKVPodAntiAffinity(), |
| 83 | + ) |
| 84 | + |
| 85 | + f.WaitForPDGroupReady(ctx, pdg) |
| 86 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 87 | + |
| 88 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 89 | + |
| 90 | + patch := client.MergeFrom(kvg.DeepCopy()) |
| 91 | + kvg.Spec.Replicas = ptr.To[int32](4) |
| 92 | + f.Must(f.Client.Patch(ctx, kvg, patch)) |
| 93 | + |
| 94 | + // just wait a few seconds |
| 95 | + time.Sleep(time.Second * 15) |
| 96 | + |
| 97 | + patch2 := client.MergeFrom(kvg.DeepCopy()) |
| 98 | + kvg.Spec.Replicas = ptr.To[int32](3) |
| 99 | + |
| 100 | + f.Must(f.Client.Patch(ctx, kvg, patch2)) |
| 101 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 102 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 103 | + }) |
| 104 | + |
| 105 | + ginkgo.It("support scale from 3 to 6 and rolling update at same time", ginkgo.Serial, label.Update, func(ctx context.Context) { |
| 106 | + pdg := f.MustCreatePD(ctx) |
| 107 | + kvg := f.MustCreateTiKV(ctx, |
| 108 | + data.WithReplicas[scope.TiKVGroup](3), |
| 109 | + data.WithTiKVEvenlySpreadPolicy(), |
| 110 | + ) |
| 111 | + |
| 112 | + f.WaitForPDGroupReady(ctx, pdg) |
| 113 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 114 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 115 | + |
| 116 | + patch := client.MergeFrom(kvg.DeepCopy()) |
| 117 | + kvg.Spec.Replicas = ptr.To[int32](6) |
| 118 | + kvg.Spec.Template.Annotations = map[string]string{ |
| 119 | + "test": "test", |
| 120 | + } |
| 121 | + |
| 122 | + nctx, cancel := context.WithCancel(ctx) |
| 123 | + ch := make(chan struct{}) |
| 124 | + go func() { |
| 125 | + defer close(ch) |
| 126 | + defer ginkgo.GinkgoRecover() |
| 127 | + f.Must(waiter.WaitPodsRollingUpdateOnce(nctx, f.Client, runtime.FromTiKVGroup(kvg), 3, 0, waiter.LongTaskTimeout)) |
| 128 | + }() |
| 129 | + |
| 130 | + changeTime, err := waiter.MaxPodsCreateTimestamp(ctx, f.Client, runtime.FromTiKVGroup(kvg)) |
| 131 | + f.Must(err) |
| 132 | + |
| 133 | + ginkgo.By("Change config and replicas of the TiKVGroup") |
| 134 | + f.Must(f.Client.Patch(ctx, kvg, patch)) |
| 135 | + f.Must(waiter.WaitForPodsRecreated(ctx, f.Client, runtime.FromTiKVGroup(kvg), *changeTime, waiter.LongTaskTimeout)) |
| 136 | + f.WaitForTiKVGroupReady(ctx, kvg) |
| 137 | + cancel() |
| 138 | + <-ch |
| 139 | + |
| 140 | + f.MustEvenlySpreadTiKV(ctx, kvg) |
| 141 | + }) |
| 142 | +}) |
0 commit comments