-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtablet_discovery.go
391 lines (355 loc) · 13.4 KB
/
tablet_discovery.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright 2020 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 logic
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"vitess.io/vitess/go/vt/external/golib/sqlutils"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vtorc/config"
"vitess.io/vitess/go/vt/vtorc/db"
"vitess.io/vitess/go/vt/vtorc/inst"
"vitess.io/vitess/go/vt/vttablet/tmclient"
)
var (
ts *topo.Server
tmc tmclient.TabletManagerClient
clustersToWatch []string
shutdownWaitTime = 30 * time.Second
shardsLockCounter int32
// shardsToWatch is a map storing the shards for a given keyspace that need to be watched.
// We store the key range for all the shards that we want to watch.
// This is populated by parsing `--clusters_to_watch` flag.
shardsToWatch map[string][]*topodatapb.KeyRange
// ErrNoPrimaryTablet is a fixed error message.
ErrNoPrimaryTablet = errors.New("no primary tablet found")
)
// RegisterFlags registers the flags required by VTOrc
func RegisterFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&clustersToWatch, "clusters_to_watch", clustersToWatch, "Comma-separated list of keyspaces or keyspace/keyranges that this instance will monitor and repair. Defaults to all clusters in the topology. Example: \"ks1,ks2/-80\"")
fs.DurationVar(&shutdownWaitTime, "shutdown_wait_time", shutdownWaitTime, "Maximum time to wait for VTOrc to release all the locks that it is holding before shutting down on SIGTERM")
}
// initializeShardsToWatch parses the --clusters_to_watch flag-value
// into a map of keyspace/shards.
func initializeShardsToWatch() error {
shardsToWatch = make(map[string][]*topodatapb.KeyRange)
if len(clustersToWatch) == 0 {
return nil
}
for _, ks := range clustersToWatch {
if strings.Contains(ks, "/") && !strings.HasSuffix(ks, "/") {
// Validate keyspace/shard parses.
k, s, err := topoproto.ParseKeyspaceShard(ks)
if err != nil {
log.Errorf("Could not parse keyspace/shard %q: %+v", ks, err)
continue
}
if !key.IsValidKeyRange(s) {
return fmt.Errorf("invalid key range %q while parsing clusters to watch", s)
}
// Parse the shard name into key range value.
keyRanges, err := key.ParseShardingSpec(s)
if err != nil {
return fmt.Errorf("could not parse shard name %q: %+v", s, err)
}
shardsToWatch[k] = append(shardsToWatch[k], keyRanges...)
} else {
// Remove trailing slash if exists.
ks = strings.TrimSuffix(ks, "/")
// We store the entire range of key range if nothing is specified.
shardsToWatch[ks] = []*topodatapb.KeyRange{key.NewCompleteKeyRange()}
}
}
if len(shardsToWatch) == 0 {
log.Error("No keyspace/shards to watch, watching all keyspaces")
}
return nil
}
// shouldWatchTablet checks if the given tablet is part of the watch list.
func shouldWatchTablet(tablet *topodatapb.Tablet) bool {
// If we are watching all keyspaces, then we want to watch this tablet too.
if len(shardsToWatch) == 0 {
return true
}
shardRanges, ok := shardsToWatch[tablet.GetKeyspace()]
// If we don't have the keyspace in our map, then this tablet
// doesn't need to be watched.
if !ok {
return false
}
// Get the tablet's key range, and check if
// it is part of the shard ranges we are watching.
kr := tablet.GetKeyRange()
for _, shardRange := range shardRanges {
if key.KeyRangeContainsKeyRange(shardRange, kr) {
return true
}
}
return false
}
// OpenTabletDiscovery opens the vitess topo if enables and returns a ticker
// channel for polling.
func OpenTabletDiscovery() <-chan time.Time {
ts = topo.Open()
tmc = inst.InitializeTMC()
// Clear existing cache and perform a new refresh.
if _, err := db.ExecVTOrc("DELETE FROM vitess_tablet"); err != nil {
log.Error(err)
}
// Parse --clusters_to_watch into a filter.
err := initializeShardsToWatch()
if err != nil {
log.Fatalf("Error parsing --clusters-to-watch: %v", err)
}
// We refresh all information from the topo once before we start the ticks to do
// it on a timer.
ctx, cancel := context.WithTimeout(context.Background(), topo.RemoteOperationTimeout)
defer cancel()
if err := refreshAllInformation(ctx); err != nil {
log.Errorf("failed to initialize topo information: %+v", err)
}
return time.Tick(config.GetTopoInformationRefreshDuration()) //nolint SA1015: using time.Tick leaks the underlying ticker
}
// getAllTablets gets all tablets from all cells using a goroutine per cell.
func getAllTablets(ctx context.Context, cells []string) []*topo.TabletInfo {
var tabletsMu sync.Mutex
tablets := make([]*topo.TabletInfo, 0)
eg, ctx := errgroup.WithContext(ctx)
for _, cell := range cells {
eg.Go(func() error {
t, err := ts.GetTabletsByCell(ctx, cell, nil)
if err != nil {
log.Errorf("Failed to load tablets from cell %s: %+v", cell, err)
return nil
}
tabletsMu.Lock()
defer tabletsMu.Unlock()
tablets = append(tablets, t...)
return nil
})
}
_ = eg.Wait() // always nil
return tablets
}
// refreshAllTablets reloads the tablets from topo and discovers the ones which haven't been refreshed in a while
func refreshAllTablets(ctx context.Context) error {
return refreshTabletsUsing(ctx, func(tabletAlias string) {
DiscoverInstance(tabletAlias, false /* forceDiscovery */)
}, false /* forceRefresh */)
}
// refreshTabletsUsing refreshes tablets using a provided loader.
func refreshTabletsUsing(ctx context.Context, loader func(tabletAlias string), forceRefresh bool) error {
// Get all cells.
ctx, cancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer cancel()
cells, err := ts.GetKnownCells(ctx)
if err != nil {
return err
}
// Get all tablets from all cells.
getTabletsCtx, getTabletsCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer getTabletsCancel()
tablets := getAllTablets(getTabletsCtx, cells)
if len(tablets) == 0 {
log.Error("Found no tablets")
return nil
}
// Filter tablets that should not be watched using shardsToWatch map.
matchedTablets := make([]*topo.TabletInfo, 0, len(tablets))
func() {
for _, t := range tablets {
if shouldWatchTablet(t.Tablet) {
matchedTablets = append(matchedTablets, t)
}
}
}()
// Refresh the filtered tablets.
query := "select alias from vitess_tablet"
refreshTablets(matchedTablets, query, nil, loader, forceRefresh, nil)
return nil
}
// forceRefreshAllTabletsInShard is used to refresh all the tablet's information (both MySQL information and topo records)
// for a given shard. This function is meant to be called before or after a cluster-wide operation that we know will
// change the replication information for the entire cluster drastically enough to warrant a full forceful refresh
func forceRefreshAllTabletsInShard(ctx context.Context, keyspace, shard string, tabletsToIgnore []string) {
refreshCtx, refreshCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer refreshCancel()
refreshTabletsInKeyspaceShard(refreshCtx, keyspace, shard, func(tabletAlias string) {
DiscoverInstance(tabletAlias, true)
}, true, tabletsToIgnore)
}
// refreshTabletInfoOfShard only refreshes the tablet records from the topo-server for all the tablets
// of the given keyspace-shard.
func refreshTabletInfoOfShard(ctx context.Context, keyspace, shard string) {
log.Infof("refresh of tablet records of shard - %v/%v", keyspace, shard)
refreshTabletsInKeyspaceShard(ctx, keyspace, shard, func(tabletAlias string) {
// No-op
// We only want to refresh the tablet information for the given shard
}, false, nil)
}
func refreshTabletsInKeyspaceShard(ctx context.Context, keyspace, shard string, loader func(tabletAlias string), forceRefresh bool, tabletsToIgnore []string) {
tablets, err := ts.GetTabletsByShard(ctx, keyspace, shard)
if err != nil {
log.Errorf("Error fetching tablets for keyspace/shard %v/%v: %v", keyspace, shard, err)
return
}
query := "select alias from vitess_tablet where keyspace = ? and shard = ?"
args := sqlutils.Args(keyspace, shard)
refreshTablets(tablets, query, args, loader, forceRefresh, tabletsToIgnore)
}
func refreshTablets(tablets []*topo.TabletInfo, query string, args []any, loader func(tabletAlias string), forceRefresh bool, tabletsToIgnore []string) {
// Discover new tablets.
latestInstances := make(map[string]bool)
var wg sync.WaitGroup
for _, tabletInfo := range tablets {
tablet := tabletInfo.Tablet
tabletAliasString := topoproto.TabletAliasString(tablet.Alias)
latestInstances[tabletAliasString] = true
old, err := inst.ReadTablet(tabletAliasString)
if err != nil && err != inst.ErrTabletAliasNil {
log.Error(err)
continue
}
if !forceRefresh && proto.Equal(tablet, old) {
continue
}
if err := inst.SaveTablet(tablet); err != nil {
log.Error(err)
continue
}
wg.Add(1)
go func() {
defer wg.Done()
if slices.Contains(tabletsToIgnore, topoproto.TabletAliasString(tablet.Alias)) {
return
}
loader(tabletAliasString)
}()
log.Infof("Discovered: %v", tablet)
}
wg.Wait()
// Forget tablets that were removed.
var toForget []string
err := db.QueryVTOrc(query, args, func(row sqlutils.RowMap) error {
tabletAlias := row.GetString("alias")
if !latestInstances[tabletAlias] {
toForget = append(toForget, tabletAlias)
}
return nil
})
if err != nil {
log.Error(err)
}
for _, tabletAlias := range toForget {
if err := inst.ForgetInstance(tabletAlias); err != nil {
log.Error(err)
}
}
}
func getLockAction(analysedInstance string, code inst.AnalysisCode) string {
return fmt.Sprintf("VTOrc Recovery for %v on %v", code, analysedInstance)
}
// LockShard locks the keyspace-shard preventing others from performing conflicting actions.
func LockShard(ctx context.Context, tabletAlias string, lockAction string) (context.Context, func(*error), error) {
if tabletAlias == "" {
return nil, nil, errors.New("can't lock shard: instance is unspecified")
}
val := atomic.LoadInt32(&hasReceivedSIGTERM)
if val > 0 {
return nil, nil, errors.New("can't lock shard: SIGTERM received")
}
tablet, err := inst.ReadTablet(tabletAlias)
if err != nil {
return nil, nil, err
}
atomic.AddInt32(&shardsLockCounter, 1)
ctx, unlock, err := ts.TryLockShard(ctx, tablet.Keyspace, tablet.Shard, lockAction)
if err != nil {
atomic.AddInt32(&shardsLockCounter, -1)
return nil, nil, err
}
return ctx, func(e *error) {
defer atomic.AddInt32(&shardsLockCounter, -1)
unlock(e)
}, nil
}
// tabletUndoDemotePrimary calls the said RPC for the given tablet.
func tabletUndoDemotePrimary(ctx context.Context, tablet *topodatapb.Tablet, semiSync bool) error {
tmcCtx, tmcCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer tmcCancel()
return tmc.UndoDemotePrimary(tmcCtx, tablet, semiSync)
}
// setReadOnly calls the said RPC for the given tablet
func setReadOnly(ctx context.Context, tablet *topodatapb.Tablet) error {
tmcCtx, tmcCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer tmcCancel()
return tmc.SetReadOnly(tmcCtx, tablet)
}
// changeTabletType calls the said RPC for the given tablet with the given parameters.
func changeTabletType(ctx context.Context, tablet *topodatapb.Tablet, tabletType topodatapb.TabletType, semiSync bool) error {
tmcCtx, tmcCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer tmcCancel()
return tmc.ChangeType(tmcCtx, tablet, tabletType, semiSync)
}
// resetReplicationParameters resets the replication parameters on the given tablet.
func resetReplicationParameters(ctx context.Context, tablet *topodatapb.Tablet) error {
tmcCtx, tmcCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer tmcCancel()
return tmc.ResetReplicationParameters(tmcCtx, tablet)
}
// setReplicationSource calls the said RPC with the parameters provided
func setReplicationSource(ctx context.Context, replica *topodatapb.Tablet, primary *topodatapb.Tablet, semiSync bool, heartbeatInterval float64) error {
tmcCtx, tmcCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer tmcCancel()
return tmc.SetReplicationSource(tmcCtx, replica, primary.Alias, 0, "", true, semiSync, heartbeatInterval)
}
// shardPrimary finds the primary of the given keyspace-shard by reading the vtorc backend
func shardPrimary(keyspace string, shard string) (primary *topodatapb.Tablet, err error) {
query := `SELECT
info
FROM
vitess_tablet
WHERE
keyspace = ? AND shard = ?
AND tablet_type = ?
ORDER BY
primary_timestamp DESC
LIMIT 1
`
err = db.Db.QueryVTOrc(query, sqlutils.Args(keyspace, shard, topodatapb.TabletType_PRIMARY), func(m sqlutils.RowMap) error {
if primary == nil {
primary = &topodatapb.Tablet{}
opts := prototext.UnmarshalOptions{DiscardUnknown: true}
return opts.Unmarshal([]byte(m.GetString("info")), primary)
}
return nil
})
if primary == nil && err == nil {
err = ErrNoPrimaryTablet
}
return primary, err
}