-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tablet_discovery.go
327 lines (303 loc) · 10 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
/*
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"
"flag"
"fmt"
"strings"
"sync"
"time"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"vitess.io/vitess/go/vt/orchestrator/config"
"vitess.io/vitess/go/vt/orchestrator/db"
"vitess.io/vitess/go/vt/orchestrator/external/golib/log"
"vitess.io/vitess/go/vt/orchestrator/external/golib/sqlutils"
"vitess.io/vitess/go/vt/orchestrator/inst"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vttablet/tmclient"
)
var (
ts *topo.Server
clustersToWatch = flag.String("clusters_to_watch", "", "Comma-separated list of keyspaces or keyspace/shards that this instance will monitor and repair. Defaults to all clusters in the topology. Example: \"ks1,ks2/-80\"")
)
// OpenTabletDiscovery opens the vitess topo if enables and returns a ticker
// channel for polling.
func OpenTabletDiscovery() <-chan time.Time {
// TODO(sougou): If there's a shutdown signal, we have to close the topo.
ts = topo.Open()
// TODO(sougou): remove ts and push some functions into inst.
inst.TopoServ = ts
// Clear existing cache and perform a new refresh.
if _, err := db.ExecOrchestrator("delete from vitess_tablet"); err != nil {
log.Errore(err)
}
refreshTabletsUsing(func(instanceKey *inst.InstanceKey) {
_ = inst.InjectSeed(instanceKey)
})
// TODO(sougou): parameterize poll interval.
return time.Tick(15 * time.Second) //nolint SA1015: using time.Tick leaks the underlying ticker
}
// RefreshTablets reloads the tablets from topo.
func RefreshTablets() {
refreshTabletsUsing(func(instanceKey *inst.InstanceKey) {
DiscoverInstance(*instanceKey)
})
}
func refreshTabletsUsing(loader func(instanceKey *inst.InstanceKey)) {
if !IsLeaderOrActive() {
return
}
if *clustersToWatch == "" { // all known clusters
ctx, cancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer cancel()
cells, err := ts.GetKnownCells(ctx)
if err != nil {
log.Errore(err)
return
}
refreshCtx, refreshCancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer refreshCancel()
var wg sync.WaitGroup
for _, cell := range cells {
wg.Add(1)
go func(cell string) {
defer wg.Done()
refreshTabletsInCell(refreshCtx, cell, loader)
}(cell)
}
wg.Wait()
} else {
// Parse input and build list of keyspaces / shards
var keyspaceShards []*topo.KeyspaceShard
inputs := strings.Split(*clustersToWatch, ",")
for _, ks := range inputs {
if strings.Contains(ks, "/") {
// This is a keyspace/shard specification
input := strings.Split(ks, "/")
keyspaceShards = append(keyspaceShards, &topo.KeyspaceShard{Keyspace: input[0], Shard: input[1]})
} else {
// Assume this is a keyspace and find all shards in keyspace
ctx, cancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer cancel()
shards, err := ts.GetShardNames(ctx, ks)
if err != nil {
// Log the errr and continue
log.Errorf("Error fetching shards for keyspace: %v", ks)
continue
}
if len(shards) == 0 {
log.Errorf("Topo has no shards for ks: %v", ks)
continue
}
for _, s := range shards {
keyspaceShards = append(keyspaceShards, &topo.KeyspaceShard{Keyspace: ks, Shard: s})
}
}
}
if len(keyspaceShards) == 0 {
log.Errorf("Found no keyspaceShards for input: %v", *clustersToWatch)
return
}
refreshCtx, refreshCancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer refreshCancel()
var wg sync.WaitGroup
for _, ks := range keyspaceShards {
wg.Add(1)
go func(ks *topo.KeyspaceShard) {
defer wg.Done()
refreshTabletsInKeyspaceShard(refreshCtx, ks.Keyspace, ks.Shard, loader)
}(ks)
}
wg.Wait()
}
}
func refreshTabletsInCell(ctx context.Context, cell string, loader func(instanceKey *inst.InstanceKey)) {
tablets, err := topotools.GetTabletMapForCell(ctx, ts, cell)
if err != nil {
log.Errorf("Error fetching topo info for cell %v: %v", cell, err)
return
}
query := "select hostname, port, info from vitess_tablet where cell = ?"
args := sqlutils.Args(cell)
refreshTablets(tablets, query, args, loader)
}
func refreshTabletsInKeyspaceShard(ctx context.Context, keyspace, shard string, loader func(instanceKey *inst.InstanceKey)) {
tablets, err := ts.GetTabletMapForShard(ctx, keyspace, shard)
if err != nil {
log.Errorf("Error fetching tablets for keyspace/shard %v/%v: %v", keyspace, shard, err)
return
}
query := "select hostname, port, info from vitess_tablet where keyspace = ? and shard = ?"
args := sqlutils.Args(keyspace, shard)
refreshTablets(tablets, query, args, loader)
}
func refreshTablets(tablets map[string]*topo.TabletInfo, query string, args []interface{}, loader func(instanceKey *inst.InstanceKey)) {
// Discover new tablets.
// TODO(sougou): enhance this to work with multi-schema,
// where each instanceKey can have multiple tablets.
latestInstances := make(map[inst.InstanceKey]bool)
for _, tabletInfo := range tablets {
tablet := tabletInfo.Tablet
if tablet.MysqlHostname == "" {
continue
}
if tablet.Type != topodatapb.TabletType_MASTER && !topo.IsReplicaType(tablet.Type) {
continue
}
instanceKey := inst.InstanceKey{
Hostname: tablet.MysqlHostname,
Port: int(tablet.MysqlPort),
}
latestInstances[instanceKey] = true
old, err := inst.ReadTablet(instanceKey)
if err != nil && err != inst.ErrTabletAliasNil {
log.Errore(err)
continue
}
if proto.Equal(tablet, old) {
continue
}
if err := inst.SaveTablet(tablet); err != nil {
log.Errore(err)
continue
}
loader(&instanceKey)
log.Infof("Discovered: %v", tablet)
}
// Forget tablets that were removed.
toForget := make(map[inst.InstanceKey]*topodatapb.Tablet)
err := db.QueryOrchestrator(query, args, func(row sqlutils.RowMap) error {
curKey := inst.InstanceKey{
Hostname: row.GetString("hostname"),
Port: row.GetInt("port"),
}
if !latestInstances[curKey] {
tablet := &topodatapb.Tablet{}
if err := prototext.Unmarshal([]byte(row.GetString("info")), tablet); err != nil {
log.Errore(err)
return nil
}
toForget[curKey] = tablet
}
return nil
})
if err != nil {
log.Errore(err)
}
for instanceKey, tablet := range toForget {
log.Infof("Forgetting: %v", tablet)
_, err := db.ExecOrchestrator(`
delete
from vitess_tablet
where
hostname=? and port=?`,
instanceKey.Hostname,
instanceKey.Port,
)
if err != nil {
log.Errore(err)
}
if err := inst.ForgetInstance(&instanceKey); err != nil {
log.Errore(err)
}
}
}
// LockShard locks the keyspace-shard preventing others from performing conflicting actions.
func LockShard(instanceKey inst.InstanceKey) (func(*error), error) {
if instanceKey.Hostname == "" {
return nil, errors.New("Can't lock shard: instance is unspecified")
}
tablet, err := inst.ReadTablet(instanceKey)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.Config.LockShardTimeoutSeconds)*time.Second)
defer cancel()
_, unlock, err := ts.LockShard(ctx, tablet.Keyspace, tablet.Shard, "Orc Recovery")
return unlock, err
}
// TabletRefresh refreshes the tablet info.
func TabletRefresh(instanceKey inst.InstanceKey) (*topodatapb.Tablet, error) {
tablet, err := inst.ReadTablet(instanceKey)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer cancel()
ti, err := ts.GetTablet(ctx, tablet.Alias)
if err != nil {
return nil, err
}
if err := inst.SaveTablet(ti.Tablet); err != nil {
return nil, err
}
return ti.Tablet, nil
}
// TabletDemoteMaster requests the master tablet to stop accepting transactions.
func TabletDemoteMaster(instanceKey inst.InstanceKey) error {
return tabletDemoteMaster(instanceKey, true)
}
// TabletUndoDemoteMaster requests the master tablet to undo the demote.
func TabletUndoDemoteMaster(instanceKey inst.InstanceKey) error {
return tabletDemoteMaster(instanceKey, false)
}
func tabletDemoteMaster(instanceKey inst.InstanceKey, forward bool) error {
if instanceKey.Hostname == "" {
return errors.New("Can't demote/undo master: instance is unspecified")
}
tablet, err := inst.ReadTablet(instanceKey)
if err != nil {
return err
}
tmc := tmclient.NewTabletManagerClient()
// TODO(sougou): this should be controllable because we may want
// to give a longer timeout for a graceful takeover.
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
if forward {
_, err = tmc.DemoteMaster(ctx, tablet)
} else {
err = tmc.UndoDemoteMaster(ctx, tablet)
}
return err
}
func ShardMaster(instanceKey *inst.InstanceKey) (masterKey *inst.InstanceKey, err error) {
tablet, err := inst.ReadTablet(*instanceKey)
if err != nil {
return nil, err
}
sCtx, sCancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer sCancel()
si, err := ts.GetShard(sCtx, tablet.Keyspace, tablet.Shard)
if err != nil {
return nil, err
}
if !si.HasMaster() {
return nil, fmt.Errorf("no master tablet for shard %v/%v", tablet.Keyspace, tablet.Shard)
}
tCtx, tCancel := context.WithTimeout(context.Background(), *topo.RemoteOperationTimeout)
defer tCancel()
master, err := ts.GetTablet(tCtx, si.MasterAlias)
if err != nil {
return nil, err
}
return &inst.InstanceKey{
Hostname: master.MysqlHostname,
Port: int(master.MysqlPort),
}, nil
}