-
Notifications
You must be signed in to change notification settings - Fork 2k
/
refresh.go
360 lines (321 loc) · 11.8 KB
/
refresh.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright 2021 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"
"vitess.io/vitess/go/vt/topo/topoproto"
"golang.org/x/net/context"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/logutil"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtgr/config"
"vitess.io/vitess/go/vt/vtgr/db"
"vitess.io/vitess/go/vt/vtgr/inst"
"vitess.io/vitess/go/vt/vtgr/log"
)
var (
lockShardTimingsMs = stats.NewMultiTimings("lockShard", "time vtgr takes to lock the shard", []string{"operation", "success"})
)
// grInstance represents an instance that's running MySQL GR
// it wraps a InstanceKey plus some tablet related information
type grInstance struct {
instanceKey *inst.InstanceKey
tablet *topodatapb.Tablet
primaryTimeStamp time.Time
alias string
}
// GRTopo is VTGR wrapper for topo server
type GRTopo interface {
GetShardNames(ctx context.Context, keyspace string) ([]string, error)
GetShard(ctx context.Context, keyspace, shard string) (*topo.ShardInfo, error)
GetTabletMapForShardByCell(ctx context.Context, keyspace, shard string, cells []string) (map[string]*topo.TabletInfo, error)
LockShard(ctx context.Context, keyspace, shard, action string) (context.Context, func(*error), error)
}
// GRTmcClient is VTGR wrapper for tmc client
type GRTmcClient interface {
ChangeType(ctx context.Context, tablet *topodatapb.Tablet, dbType topodatapb.TabletType, semiSync bool) error
Ping(ctx context.Context, tablet *topodatapb.Tablet) error
}
// GRShard stores the information about a Vitess shard that's running MySQL GR
type GRShard struct {
KeyspaceShard *topo.KeyspaceShard
cells []string
instances []*grInstance
primaryAlias string
shardStatusCollector *shardStatusCollector
sqlGroup *SQLGroup
ts GRTopo
tmc GRTmcClient
dbAgent db.Agent
// Every GRShard tracks a unlock function after it grab a topo lock for the shard
// VTGR needs to release the topo lock before gracefully shutdown
unlock func(*error)
// mutex to protect unlock function access
unlockMu sync.Mutex
// configuration
minNumReplicas int
localDbPort int
disableReadOnlyProtection bool
transientErrorWaitTime time.Duration
bootstrapWaitTime time.Duration
lastDiagnoseResult DiagnoseType
lastDiagnoseSince time.Time
isActive atomic.Bool
logger *log.Logger
// lock prevents multiple go routine fights with each other
sync.Mutex
}
// shardStatusCollector is used for collecting shard status
type shardStatusCollector struct {
status *ShardStatus
sync.Mutex
}
// ShardStatus is used for debugging purpose to get current status of a shard
type ShardStatus struct {
Keyspace string
Shard string
Instances []string
Unreachables []string
Problematics []string
Primary string
DiagnoseResult DiagnoseType
}
func newShardStatusCollector(keyspace, shard string) *shardStatusCollector {
return &shardStatusCollector{
status: &ShardStatus{Keyspace: keyspace, Shard: shard},
}
}
// NewGRShard creates a new GRShard
func NewGRShard(
keyspace, shard string,
cells []string,
tmc GRTmcClient,
ts GRTopo,
dbAgent db.Agent,
config *config.VTGRConfig,
localDbPort int,
isActive bool) *GRShard {
grShard := &GRShard{
KeyspaceShard: &topo.KeyspaceShard{Keyspace: keyspace, Shard: shard},
cells: cells,
shardStatusCollector: newShardStatusCollector(keyspace, shard),
tmc: tmc,
ts: ts,
dbAgent: dbAgent,
unlock: nil,
sqlGroup: NewSQLGroup(config.BootstrapGroupSize, true, keyspace, shard),
minNumReplicas: config.MinNumReplica,
disableReadOnlyProtection: config.DisableReadOnlyProtection,
localDbPort: localDbPort,
logger: log.NewVTGRLogger(keyspace, shard),
transientErrorWaitTime: time.Duration(config.BackoffErrorWaitTimeSeconds) * time.Second,
bootstrapWaitTime: time.Duration(config.BootstrapWaitTimeSeconds) * time.Second,
}
grShard.isActive.Store(isActive)
return grShard
}
// refreshTabletsInShardLocked is called by repair to get a fresh view of the shard
// The caller is responsible to make sure the lock on GRShard
func (shard *GRShard) refreshTabletsInShardLocked(ctx context.Context) {
instances, err := shard.refreshTabletsInShardInternal(ctx)
if err == nil {
shard.instances = instances
}
primary, err := shard.refreshPrimaryShard(ctx)
if err == nil {
shard.primaryAlias = primary
return
}
// If we failed to refreshPrimaryShard, use primary from local tablets
shard.primaryAlias = shard.findPrimaryFromLocalCell()
}
// UpdateTabletsInShardWithLock updates the shard instances with a lock
func (shard *GRShard) UpdateTabletsInShardWithLock(ctx context.Context) {
instances, err := shard.refreshTabletsInShardInternal(ctx)
if err == nil {
// Take a per shard lock here when we actually refresh the data to avoid
// race conditions bewteen controller and repair tasks
shard.Lock()
shard.instances = instances
shard.Unlock()
}
primary, err := shard.refreshPrimaryShard(ctx)
// We set primary separately from instances so that if global topo is not available
// VTGR can still discover the new tablets from local cell
shard.Lock()
defer shard.Unlock()
if err == nil {
shard.primaryAlias = primary
return
}
shard.primaryAlias = shard.findPrimaryFromLocalCell()
}
func (shard *GRShard) refreshTabletsInShardInternal(ctx context.Context) ([]*grInstance, error) {
keyspace, shardName := shard.KeyspaceShard.Keyspace, shard.KeyspaceShard.Shard
tablets, err := shard.ts.GetTabletMapForShardByCell(ctx, keyspace, shardName, shard.cells)
if err != nil {
shard.logger.Errorf("Error fetching tablets for keyspace/shardName %v/%v: %v", keyspace, shardName, err)
return nil, err
}
return parseTabletInfos(tablets), nil
}
func (shard *GRShard) refreshPrimaryShard(ctx context.Context) (string, error) {
keyspace, shardName := shard.KeyspaceShard.Keyspace, shard.KeyspaceShard.Shard
si, err := shard.ts.GetShard(ctx, keyspace, shardName)
if err != nil {
shard.logger.Errorf("Error calling GetShard: %v", err)
return "", err
}
return topoproto.TabletAliasString(si.PrimaryAlias), nil
}
// findPrimaryFromLocalCell iterates through the replicas stored in grShard and returns
// the one that's marked as primary
func (shard *GRShard) findPrimaryFromLocalCell() string {
var latestPrimaryTimestamp time.Time
var primaryInstance *grInstance
for _, instance := range shard.instances {
if instance.tablet.Type == topodatapb.TabletType_PRIMARY {
// It is possible that there are more than one master in topo server
// we should compare timestamp to pick the latest one
if latestPrimaryTimestamp.Before(instance.primaryTimeStamp) {
latestPrimaryTimestamp = instance.primaryTimeStamp
primaryInstance = instance
}
}
}
if primaryInstance != nil {
return primaryInstance.alias
}
return ""
}
// parseTabletInfos replaces the replica reports for the shard key
// Note: this is not thread-safe
func parseTabletInfos(tablets map[string]*topo.TabletInfo) []*grInstance {
// collect all replicas
var newReplicas []*grInstance
for alias, tabletInfo := range tablets {
tablet := tabletInfo.Tablet
// Only monitor primary, replica and ronly tablet types
switch tablet.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY:
// mysql hostname and port might be empty here if tablet is not running
// we will treat them as unreachable
instanceKey := inst.InstanceKey{
Hostname: tablet.MysqlHostname,
Port: int(tablet.MysqlPort),
}
grInstance := grInstance{
instanceKey: &instanceKey,
tablet: tablet,
primaryTimeStamp: logutil.ProtoToTime(tablet.PrimaryTermStartTime),
alias: alias,
}
newReplicas = append(newReplicas, &grInstance)
}
}
return newReplicas
}
// LockShard locks the keyspace-shard on topo server to prevent others from executing conflicting actions.
func (shard *GRShard) LockShard(ctx context.Context, action string) (context.Context, error) {
if shard.KeyspaceShard.Keyspace == "" || shard.KeyspaceShard.Shard == "" {
return nil, fmt.Errorf("try to grab lock with incomplete information: %v/%v", shard.KeyspaceShard.Keyspace, shard.KeyspaceShard.Shard)
}
shard.unlockMu.Lock()
defer shard.unlockMu.Unlock()
if shard.unlock != nil {
return nil, fmt.Errorf("try to grab lock for %s/%s while the shard holds an unlock function", shard.KeyspaceShard.Keyspace, shard.KeyspaceShard.Shard)
}
start := time.Now()
ctx, unlock, err := shard.ts.LockShard(ctx, shard.KeyspaceShard.Keyspace, shard.KeyspaceShard.Shard, fmt.Sprintf("VTGR repairing %s", action))
lockShardTimingsMs.Record([]string{action, strconv.FormatBool(err == nil)}, start)
if err != nil {
return nil, err
}
shard.unlock = unlock
return ctx, nil
}
// UnlockShard unlocks the keyspace-shard on topo server
// and set the unlock function to nil in the container
func (shard *GRShard) UnlockShard() {
shard.unlockMu.Lock()
defer shard.unlockMu.Unlock()
if shard.unlock == nil {
shard.logger.Warningf("Shard %s/%s does not hold a lock", shard.KeyspaceShard.Keyspace, shard.KeyspaceShard.Shard)
return
}
var err error
shard.unlock(&err)
shard.unlock = nil
}
func (shard *GRShard) findTabletByHostAndPort(host string, port int) *grInstance {
for _, instance := range shard.instances {
if instance.instanceKey.Hostname == host && instance.instanceKey.Port == port {
return instance
}
}
return nil
}
func (shard *GRShard) populateVTGRStatusLocked() {
var instanceList []string
for _, instance := range shard.instances {
instanceList = append(instanceList, instance.alias)
}
shard.shardStatusCollector.status.Instances = instanceList
if primary := shard.findShardPrimaryTablet(); primary != nil {
shard.shardStatusCollector.status.Primary = primary.alias
}
}
// GetCurrentShardStatuses returns the status collector has
func (shard *GRShard) GetCurrentShardStatuses() ShardStatus {
shard.Lock()
collector := shard.shardStatusCollector
// dereference status so that we return a copy of the struct
status := *collector.status
shard.Unlock()
return status
}
// OverrideRebootstrapGroupSize force override the group expectedBootstrapSize used in safety check for rebootstrap
func (shard *GRShard) OverrideRebootstrapGroupSize(groupSize int) error {
shard.Lock()
defer shard.Unlock()
shard.logger.Infof("Override rebootstrap group size=%v", groupSize)
shard.sqlGroup.rebootstrapSize = groupSize
return nil
}
// GetUnlock returns the unlock function for the shard for testing
func (shard *GRShard) GetUnlock() func(*error) {
shard.unlockMu.Lock()
defer shard.unlockMu.Unlock()
return shard.unlock
}
// SetIsActive sets isActive for the shard
func (shard *GRShard) SetIsActive(isActive bool) {
shard.logger.Infof("Setting is active to %v", isActive)
shard.isActive.Store(isActive)
}
func (collector *shardStatusCollector) isUnreachable(instance *grInstance) bool {
if instance.instanceKey == nil || instance.instanceKey.Hostname == "" {
return true
}
for _, alias := range collector.status.Unreachables {
if instance.alias == alias {
return true
}
}
return false
}