-
Notifications
You must be signed in to change notification settings - Fork 488
/
tidb_cluster_control.go
277 lines (245 loc) · 9.6 KB
/
tidb_cluster_control.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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 tidbcluster
import (
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1/defaulting"
v1alpha1validation "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1/validation"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/manager"
"github.com/pingcap/tidb-operator/pkg/manager/member"
v1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
errorutils "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
)
// ControlInterface implements the control logic for updating TidbClusters and their children StatefulSets.
// It is implemented as an interface to allow for extensions that provide different semantics.
// Currently, there is only one implementation.
type ControlInterface interface {
// UpdateTidbCluster implements the control logic for StatefulSet creation, update, and deletion
UpdateTidbCluster(*v1alpha1.TidbCluster) error
}
// NewDefaultTidbClusterControl returns a new instance of the default implementation TidbClusterControlInterface that
// implements the documented semantics for TidbClusters.
func NewDefaultTidbClusterControl(
tcControl controller.TidbClusterControlInterface,
pdMemberManager manager.Manager,
tikvMemberManager manager.Manager,
tidbMemberManager manager.Manager,
reclaimPolicyManager manager.Manager,
metaManager manager.Manager,
orphanPodsCleaner member.OrphanPodsCleaner,
pvcCleaner member.PVCCleanerInterface,
pvcResizer member.PVCResizerInterface,
pumpMemberManager manager.Manager,
tiflashMemberManager manager.Manager,
ticdcMemberManager manager.Manager,
discoveryManager member.TidbDiscoveryManager,
tidbClusterStatusManager manager.Manager,
conditionUpdater TidbClusterConditionUpdater,
recorder record.EventRecorder) ControlInterface {
return &defaultTidbClusterControl{
tcControl: tcControl,
pdMemberManager: pdMemberManager,
tikvMemberManager: tikvMemberManager,
tidbMemberManager: tidbMemberManager,
reclaimPolicyManager: reclaimPolicyManager,
metaManager: metaManager,
orphanPodsCleaner: orphanPodsCleaner,
pvcCleaner: pvcCleaner,
pvcResizer: pvcResizer,
pumpMemberManager: pumpMemberManager,
tiflashMemberManager: tiflashMemberManager,
ticdcMemberManager: ticdcMemberManager,
discoveryManager: discoveryManager,
tidbClusterStatusManager: tidbClusterStatusManager,
conditionUpdater: conditionUpdater,
recorder: recorder,
}
}
type defaultTidbClusterControl struct {
tcControl controller.TidbClusterControlInterface
pdMemberManager manager.Manager
tikvMemberManager manager.Manager
tidbMemberManager manager.Manager
reclaimPolicyManager manager.Manager
metaManager manager.Manager
orphanPodsCleaner member.OrphanPodsCleaner
pvcCleaner member.PVCCleanerInterface
pvcResizer member.PVCResizerInterface
pumpMemberManager manager.Manager
tiflashMemberManager manager.Manager
ticdcMemberManager manager.Manager
discoveryManager member.TidbDiscoveryManager
tidbClusterStatusManager manager.Manager
conditionUpdater TidbClusterConditionUpdater
recorder record.EventRecorder
}
// UpdateStatefulSet executes the core logic loop for a tidbcluster.
func (c *defaultTidbClusterControl) UpdateTidbCluster(tc *v1alpha1.TidbCluster) error {
c.defaulting(tc)
if !c.validate(tc) {
return nil // fatal error, no need to retry on invalid object
}
var errs []error
oldStatus := tc.Status.DeepCopy()
if err := c.updateTidbCluster(tc); err != nil {
errs = append(errs, err)
}
if err := c.conditionUpdater.Update(tc); err != nil {
errs = append(errs, err)
}
if apiequality.Semantic.DeepEqual(&tc.Status, oldStatus) {
return errorutils.NewAggregate(errs)
}
if _, err := c.tcControl.UpdateTidbCluster(tc.DeepCopy(), &tc.Status, oldStatus); err != nil {
errs = append(errs, err)
}
return errorutils.NewAggregate(errs)
}
func (c *defaultTidbClusterControl) validate(tc *v1alpha1.TidbCluster) bool {
errs := v1alpha1validation.ValidateTidbCluster(tc)
if len(errs) > 0 {
aggregatedErr := errs.ToAggregate()
klog.Errorf("tidb cluster %s/%s is not valid and must be fixed first, aggregated error: %v", tc.GetNamespace(), tc.GetName(), aggregatedErr)
c.recorder.Event(tc, v1.EventTypeWarning, "FailedValidation", aggregatedErr.Error())
return false
}
return true
}
func (c *defaultTidbClusterControl) defaulting(tc *v1alpha1.TidbCluster) {
defaulting.SetTidbClusterDefault(tc)
}
func (c *defaultTidbClusterControl) updateTidbCluster(tc *v1alpha1.TidbCluster) error {
// syncing all PVs managed by operator's reclaim policy to Retain
if err := c.reclaimPolicyManager.Sync(tc); err != nil {
return err
}
// cleaning all orphan pods(pd, tikv or tiflash which don't have a related PVC) managed by operator
// this could be useful when failover run into an undesired situation as described in PD failover function
skipReasons, err := c.orphanPodsCleaner.Clean(tc)
if err != nil {
return err
}
if klog.V(10) {
for podName, reason := range skipReasons {
klog.Infof("pod %s of cluster %s/%s is skipped, reason %q", podName, tc.Namespace, tc.Name, reason)
}
}
// reconcile TiDB discovery service
if err := c.discoveryManager.Reconcile(tc); err != nil {
return err
}
// works that should do to making the pd cluster current state match the desired state:
// - create or update the pd service
// - create or update the pd headless service
// - create the pd statefulset
// - sync pd cluster status from pd to TidbCluster object
// - set two annotations to the first pd member:
// - label.Bootstrapping
// - label.Replicas
// - upgrade the pd cluster
// - scale out/in the pd cluster
// - failover the pd cluster
if err := c.pdMemberManager.Sync(tc); err != nil {
return err
}
// works that should do to making the tikv cluster current state match the desired state:
// - waiting for the pd cluster available(pd cluster is in quorum)
// - create or update tikv headless service
// - create the tikv statefulset
// - sync tikv cluster status from pd to TidbCluster object
// - set scheduler labels to tikv stores
// - upgrade the tikv cluster
// - scale out/in the tikv cluster
// - failover the tikv cluster
if err := c.tikvMemberManager.Sync(tc); err != nil {
return err
}
// syncing the pump cluster
if err := c.pumpMemberManager.Sync(tc); err != nil {
return err
}
// works that should do to making the tidb cluster current state match the desired state:
// - waiting for the tikv cluster available(at least one peer works)
// - create or update tidb headless service
// - create the tidb statefulset
// - sync tidb cluster status from pd to TidbCluster object
// - upgrade the tidb cluster
// - scale out/in the tidb cluster
// - failover the tidb cluster
if err := c.tidbMemberManager.Sync(tc); err != nil {
return err
}
// works that should do to making the tiflash cluster current state match the desired state:
// - waiting for the tidb cluster available
// - create or update tiflash headless service
// - create the tiflash statefulset
// - sync tiflash cluster status from pd to TidbCluster object
// - set scheduler labels to tiflash stores
// - upgrade the tiflash cluster
// - scale out/in the tiflash cluster
// - failover the tiflash cluster
if err := c.tiflashMemberManager.Sync(tc); err != nil {
return err
}
// - waiting for the pd cluster available(pd cluster is in quorum)
// - create or update ticdc deployment
// - sync ticdc cluster status from pd to TidbCluster object
if err := c.ticdcMemberManager.Sync(tc); err != nil {
return err
}
// syncing the labels from Pod to PVC and PV, these labels include:
// - label.StoreIDLabelKey
// - label.MemberIDLabelKey
// - label.NamespaceLabelKey
if err := c.metaManager.Sync(tc); err != nil {
return err
}
// cleaning the pod scheduling annotation for pd and tikv
pvcSkipReasons, err := c.pvcCleaner.Clean(tc)
if err != nil {
return err
}
if klog.V(10) {
for pvcName, reason := range pvcSkipReasons {
klog.Infof("pvc %s of cluster %s/%s is skipped, reason %q", pvcName, tc.Namespace, tc.Name, reason)
}
}
// resize PVC if necessary
if err := c.pvcResizer.Resize(tc); err != nil {
return err
}
// syncing the some tidbcluster status attributes
// - sync tidbmonitor reference
return c.tidbClusterStatusManager.Sync(tc)
}
var _ ControlInterface = &defaultTidbClusterControl{}
type FakeTidbClusterControlInterface struct {
err error
}
func NewFakeTidbClusterControlInterface() *FakeTidbClusterControlInterface {
return &FakeTidbClusterControlInterface{}
}
func (c *FakeTidbClusterControlInterface) SetUpdateTCError(err error) {
c.err = err
}
func (c *FakeTidbClusterControlInterface) UpdateTidbCluster(_ *v1alpha1.TidbCluster) error {
if c.err != nil {
return c.err
}
return nil
}
var _ ControlInterface = &FakeTidbClusterControlInterface{}