/
scale.go
233 lines (191 loc) · 7.16 KB
/
scale.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.package spec
package e2e
import (
"fmt"
"time"
. "github.com/onsi/ginkgo" // revive:disable:dot-imports
. "github.com/onsi/gomega" // revive:disable:dot-imports
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
)
const (
pdScaleOutTo = 5
tikvScaleOutTo = 5
tidbScaleOutTo = 3
pdScaleInTo = 3
tikvScaleInTo = 3
tidbScaleInTo = 2
)
var podUIDsBeforeScale map[string]types.UID
func testScale() {
By(fmt.Sprintf("When scale out TiDB cluster: pd ==> [%d], tikv ==> [%d], tidb ==> [%d]", pdScaleOutTo, tikvScaleOutTo, tidbScaleOutTo))
err := wait.Poll(5*time.Second, 5*time.Minute, scaleOut)
Expect(err).NotTo(HaveOccurred())
By("Then TiDB cluster should scale out successfully")
err = wait.Poll(5*time.Second, 5*time.Minute, scaled)
Expect(err).NotTo(HaveOccurred())
By("And should scaled out correctly")
err = wait.Poll(5*time.Second, 5*time.Minute, scaledCorrectly)
Expect(err).NotTo(HaveOccurred())
By("And the data is correct")
err = wait.Poll(5*time.Second, 5*time.Minute, dataIsCorrect)
Expect(err).NotTo(HaveOccurred())
By(fmt.Sprintf("When scale in TiDB cluster: pd ==> [%d], tikv ==> [%d], tidb ==> [%d]", pdScaleInTo, tikvScaleInTo, tidbScaleInTo))
err = wait.Poll(5*time.Second, 5*time.Minute, scaleIn)
Expect(err).NotTo(HaveOccurred())
By("Then TiDB cluster scale in securely")
err = wait.Poll(5*time.Second, 5*time.Minute, scaleInSafely)
Expect(err).NotTo(HaveOccurred())
By("And should scale in successfully")
err = wait.Poll(5*time.Second, 5*time.Minute, scaled)
Expect(err).NotTo(HaveOccurred())
By("And should be scaled in correctly")
err = wait.Poll(5*time.Second, 5*time.Minute, scaledCorrectly)
Expect(err).NotTo(HaveOccurred())
By("And the data is correct")
err = wait.Poll(5*time.Second, 5*time.Minute, dataIsCorrect)
Expect(err).NotTo(HaveOccurred())
By(fmt.Sprintf("When scale out TiDB cluster one more time: pd ==> [%d], tikv ==> [%d], tidb ==> [%d]", pdScaleOutTo, tikvScaleOutTo, tidbScaleOutTo))
err = wait.Poll(5*time.Second, 5*time.Minute, scaleOut)
Expect(err).NotTo(HaveOccurred())
By("Then TiDB cluster should scale out successfully")
err = wait.Poll(5*time.Second, 5*time.Minute, scaled)
Expect(err).NotTo(HaveOccurred())
By("And should scaled out correctly")
err = wait.Poll(5*time.Second, 5*time.Minute, scaledCorrectly)
Expect(err).NotTo(HaveOccurred())
By("And the data is correct")
err = wait.Poll(5*time.Second, 5*time.Minute, dataIsCorrect)
Expect(err).NotTo(HaveOccurred())
}
func scaleOut() (bool, error) {
tc, err := cli.PingcapV1alpha1().TidbClusters(ns).Get(clusterName, metav1.GetOptions{})
if err != nil {
logf("failed to get tidbcluster when scale out tidbcluster, error: %v", err)
return false, nil
}
podUIDsBeforeScale, err = getPodsUID()
if err != nil {
return false, nil
}
tc.Spec.PD.Replicas = pdScaleOutTo
tc.Spec.TiKV.Replicas = tikvScaleOutTo
tc.Spec.TiDB.Replicas = tidbScaleOutTo
tc, err = cli.PingcapV1alpha1().TidbClusters(ns).Update(tc)
if err != nil {
logf("failed to update tidbcluster when scale out tidbcluster, error: %v", err)
return false, nil
}
logf("Replicas after scaled out: PD: %d , TiKV: %d, TiDB: %d", tc.Spec.PD.Replicas, tc.Spec.TiKV.Replicas, tc.Spec.TiDB.Replicas)
return true, nil
}
func scaled() (bool, error) {
return allMembersRunning()
}
func scaleIn() (bool, error) {
tc, err := cli.PingcapV1alpha1().TidbClusters(ns).Get(clusterName, metav1.GetOptions{})
if err != nil {
logf("failed to get tidbcluster when scale in tidbcluster, error: %v", err)
return false, nil
}
if tc.Spec.PD.Replicas <= pdScaleInTo {
return true, fmt.Errorf("the tidbcluster's pd replicas less then pdScaleInTo: [%d]", pdScaleInTo)
}
if tc.Spec.TiKV.Replicas <= tikvScaleInTo {
return true, fmt.Errorf("the tidbcluster's tikv replicas less then tikvScaleInTo: [%d]", tikvScaleInTo)
}
if tc.Spec.TiDB.Replicas <= tidbScaleInTo {
return true, fmt.Errorf("the tidbcluster's tidb replicas less then tidbScaleInTo: [%d]", tidbScaleInTo)
}
podUIDsBeforeScale, err = getPodsUID()
if err != nil {
return false, nil
}
if err != nil {
return false, nil
}
tc.Spec.PD.Replicas = pdScaleInTo
tc.Spec.TiKV.Replicas = tikvScaleInTo
tc.Spec.TiDB.Replicas = tidbScaleInTo
tc, err = cli.PingcapV1alpha1().TidbClusters(ns).Update(tc)
if err != nil {
logf("failed to update tidbcluster when scale in tidbcluster, error: %v", err)
return false, nil
}
logf("Replicas after scaled in: PD: %d , TiKV: %d, TiDB: %d", tc.Spec.PD.Replicas, tc.Spec.TiKV.Replicas, tc.Spec.TiDB.Replicas)
return true, nil
}
func scaledCorrectly() (bool, error) {
podUIDs, err := getPodsUID()
if err != nil {
logf("failed to get pd pods's uid, error: %v", err)
return false, nil
}
if len(podUIDsBeforeScale) == len(podUIDs) {
return false, fmt.Errorf("the length of pods before scale equals the length of pods after scale")
}
for podName, uidAfter := range podUIDs {
if uidBefore, ok := podUIDsBeforeScale[podName]; ok && uidBefore != uidAfter {
return false, fmt.Errorf("pod: [%s] have be recreated", podName)
}
}
return true, nil
}
// scaleInSafely confirms member scale in safely
func scaleInSafely() (bool, error) {
tc, err := cli.PingcapV1alpha1().TidbClusters(ns).Get(clusterName, metav1.GetOptions{})
if err != nil {
logf("failed to get tidbcluster when scale in tidbcluster, error: %v", err)
return false, nil
}
tikvSetName := controller.TiKVMemberName(clusterName)
tikvSet, err := kubeCli.AppsV1beta1().StatefulSets(ns).Get(tikvSetName, metav1.GetOptions{})
if err != nil {
logf("failed to get tikvSet statefulset: [%s], error: %v", tikvSetName, err)
return false, nil
}
pdClient := controller.NewDefaultPDControl().GetPDClient(tc)
stores, err := pdClient.GetStores()
if err != nil {
logf("pdClient.GetStores failed,error: %v", err)
return false, nil
}
if len(stores.Stores) > int(*tikvSet.Spec.Replicas) {
logf("stores.Stores: %v", stores.Stores)
logf("tikvSet.Spec.Replicas: %d", *tikvSet.Spec.Replicas)
return false, fmt.Errorf("the tikvSet.Spec.Replicas may reduce before tikv complete offline")
}
if *tikvSet.Spec.Replicas == tc.Spec.TiKV.Replicas {
return true, nil
}
return false, nil
}
func getPodsUID() (map[string]types.UID, error) {
result := map[string]types.UID{}
selector, err := label.New().Cluster(clusterName).Selector()
if err != nil {
return nil, err
}
pods, err := kubeCli.CoreV1().Pods(ns).List(metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
for _, pod := range pods.Items {
result[pod.GetName()] = pod.GetUID()
}
return result, nil
}