forked from pingcap/tidb-operator
/
tikv_upgrader.go
219 lines (193 loc) · 6.39 KB
/
tikv_upgrader.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
// 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 member
import (
"fmt"
"strconv"
"time"
"github.com/golang/glog"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
apps "k8s.io/api/apps/v1beta1"
corev1 "k8s.io/api/core/v1"
corelisters "k8s.io/client-go/listers/core/v1"
)
const (
// EvictLeaderBeginTime is the key of evict Leader begin time
EvictLeaderBeginTime = "evictLeaderBeginTime"
// EvictLeaderTimeout is the timeout limit of evict leader
EvictLeaderTimeout = 3 * time.Minute
)
type tikvUpgrader struct {
pdControl controller.PDControlInterface
podControl controller.PodControlInterface
podLister corelisters.PodLister
}
// NewTiKVUpgrader returns a tikv Upgrader
func NewTiKVUpgrader(pdControl controller.PDControlInterface,
podControl controller.PodControlInterface,
podLister corelisters.PodLister) Upgrader {
return &tikvUpgrader{
pdControl: pdControl,
podControl: podControl,
podLister: podLister,
}
}
func (tku *tikvUpgrader) Upgrade(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSet, newSet *apps.StatefulSet) error {
ns := tc.GetNamespace()
tcName := tc.GetName()
if tc.Status.PD.Phase == v1alpha1.UpgradePhase {
_, podSpec, err := GetLastAppliedConfig(oldSet)
if err != nil {
return err
}
newSet.Spec.Template.Spec = *podSpec
return nil
}
if !tc.Status.TiKV.Synced {
return fmt.Errorf("Tidbcluster: [%s/%s]'s tikv status sync failed, can not to be upgraded", ns, tcName)
}
tc.Status.TiKV.Phase = v1alpha1.UpgradePhase
if !templateEqual(newSet.Spec.Template, oldSet.Spec.Template) {
return nil
}
setUpgradePartition(newSet, *oldSet.Spec.UpdateStrategy.RollingUpdate.Partition)
for i := tc.Status.TiKV.StatefulSet.Replicas - 1; i >= 0; i-- {
store := tku.getStoreByOrdinal(tc, i)
if store == nil {
continue
}
podName := tikvPodName(tcName, i)
pod, err := tku.podLister.Pods(ns).Get(podName)
if err != nil {
return err
}
revision, exist := pod.Labels[apps.ControllerRevisionHashLabelKey]
if !exist {
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s tikv pod: [%s] has no label: %s", ns, tcName, podName, apps.ControllerRevisionHashLabelKey)
}
if revision == tc.Status.TiKV.StatefulSet.UpdateRevision {
if pod.Status.Phase != corev1.PodRunning {
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s upgraded tikv pod: [%s] is not running", ns, tcName, podName)
}
if store.State != v1alpha1.TiKVStateUp {
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s upgraded tikv pod: [%s] is not all ready", ns, tcName, podName)
}
err := tku.endEvictLeader(tc, i)
if err != nil {
return err
}
continue
}
return tku.upgradeTiKVPod(tc, i, newSet)
}
return nil
}
func (tku *tikvUpgrader) upgradeTiKVPod(tc *v1alpha1.TidbCluster, ordinal int32, newSet *apps.StatefulSet) error {
ns := tc.GetNamespace()
tcName := tc.GetName()
upgradePodName := tikvPodName(tcName, ordinal)
upgradePod, err := tku.podLister.Pods(ns).Get(upgradePodName)
if err != nil {
return err
}
for _, store := range tc.Status.TiKV.Stores {
if store.PodName == upgradePodName {
storeID, err := strconv.ParseUint(store.ID, 10, 64)
if err != nil {
return err
}
_, evicting := upgradePod.Annotations[EvictLeaderBeginTime]
if tku.readyToUpgrade(upgradePod, store) {
setUpgradePartition(newSet, ordinal)
return nil
}
if !evicting {
return tku.beginEvictLeader(tc, storeID, upgradePod)
}
return controller.RequeueErrorf("tidbcluster: [%s/%s]'s tikv pod: [%s] is evicting leader", ns, tcName, upgradePodName)
}
}
return controller.RequeueErrorf("tidbcluster: [%s/%s] no store status found for tikv pod: [%s]", ns, tcName, upgradePodName)
}
func (tku *tikvUpgrader) readyToUpgrade(upgradePod *corev1.Pod, store v1alpha1.TiKVStore) bool {
if store.LeaderCount == 0 {
return true
}
if evictLeaderBeginTimeStr, evicting := upgradePod.Annotations[EvictLeaderBeginTime]; evicting {
evictLeaderBeginTime, err := time.Parse(time.RFC3339, evictLeaderBeginTimeStr)
if err != nil {
glog.Errorf("parse annotation:[%s] to time failed.", EvictLeaderBeginTime)
return false
}
if time.Now().After(evictLeaderBeginTime.Add(EvictLeaderTimeout)) {
return true
}
}
return false
}
func (tku *tikvUpgrader) beginEvictLeader(tc *v1alpha1.TidbCluster, storeID uint64, pod *corev1.Pod) error {
err := tku.pdControl.GetPDClient(tc).BeginEvictLeader(storeID)
if err != nil {
return err
}
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
pod.Annotations[EvictLeaderBeginTime] = time.Now().Format(time.RFC3339)
_, err = tku.podControl.UpdatePod(tc, pod)
return err
}
func (tku *tikvUpgrader) endEvictLeader(tc *v1alpha1.TidbCluster, ordinal int32) error {
store := tku.getStoreByOrdinal(tc, ordinal)
storeID, err := strconv.ParseUint(store.ID, 10, 64)
if err != nil {
return err
}
upgradedPodName := tikvPodName(tc.GetName(), ordinal)
upgradedPod, err := tku.podLister.Pods(tc.GetNamespace()).Get(upgradedPodName)
if err != nil {
return err
}
_, evicting := upgradedPod.Annotations[EvictLeaderBeginTime]
if evicting {
delete(upgradedPod.Annotations, EvictLeaderBeginTime)
_, err = tku.podControl.UpdatePod(tc, upgradedPod)
if err != nil {
return err
}
}
err = tku.pdControl.GetPDClient(tc).EndEvictLeader(storeID)
if err != nil {
return err
}
return nil
}
func (tku *tikvUpgrader) getStoreByOrdinal(tc *v1alpha1.TidbCluster, ordinal int32) *v1alpha1.TiKVStore {
podName := tikvPodName(tc.GetName(), ordinal)
for _, store := range tc.Status.TiKV.Stores {
if store.PodName == podName {
return &store
}
}
return nil
}
type fakeTiKVUpgrader struct{}
// NewFakeTiKVUpgrader returns a fake tikv upgrader
func NewFakeTiKVUpgrader() Upgrader {
return &fakeTiKVUpgrader{}
}
func (tku *fakeTiKVUpgrader) Upgrade(tc *v1alpha1.TidbCluster, _ *apps.StatefulSet, _ *apps.StatefulSet) error {
tc.Status.TiKV.Phase = v1alpha1.UpgradePhase
return nil
}