forked from vitessio/vitess
/
tablet.go
391 lines (336 loc) · 11.9 KB
/
tablet.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 2013, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package topo
import (
"fmt"
"reflect"
"sync"
"golang.org/x/net/context"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/event"
"github.com/youtube/vitess/go/netutil"
"github.com/youtube/vitess/go/trace"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
"github.com/youtube/vitess/go/vt/topo/events"
"github.com/youtube/vitess/go/vt/topo/topoproto"
)
const (
// According to docs, the tablet uid / (mysql server id) is uint32.
// However, zero appears to be a sufficiently degenerate value to use
// as a marker for not having a parent server id.
// http://dev.mysql.com/doc/refman/5.1/en/replication-options.html
NO_TABLET = 0
// ReplicationLag is the key in the health map to indicate high
// replication lag
ReplicationLag = "replication_lag"
// ReplicationLagHigh is the value in the health map to indicate high
// replication lag
ReplicationLagHigh = "high"
)
// IsTrivialTypeChange returns if this db type be trivially reassigned
// without changes to the replication graph
func IsTrivialTypeChange(oldTabletType, newTabletType topodatapb.TabletType) bool {
switch oldTabletType {
case topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY, topodatapb.TabletType_SPARE, topodatapb.TabletType_BACKUP, topodatapb.TabletType_EXPERIMENTAL, topodatapb.TabletType_WORKER:
switch newTabletType {
case topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY, topodatapb.TabletType_SPARE, topodatapb.TabletType_BACKUP, topodatapb.TabletType_EXPERIMENTAL, topodatapb.TabletType_WORKER:
return true
}
case topodatapb.TabletType_RESTORE:
switch newTabletType {
case topodatapb.TabletType_SPARE:
return true
}
}
return false
}
// IsInServingGraph returns if a tablet appears in the serving graph
func IsInServingGraph(tt topodatapb.TabletType) bool {
switch tt {
case topodatapb.TabletType_MASTER, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY:
return true
}
return false
}
// IsRunningQueryService returns if a tablet is running the query service
func IsRunningQueryService(tt topodatapb.TabletType) bool {
switch tt {
case topodatapb.TabletType_MASTER, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY, topodatapb.TabletType_WORKER:
return true
}
return false
}
// IsRunningUpdateStream returns if a tablet is running the update stream
// RPC service.
func IsRunningUpdateStream(tt topodatapb.TabletType) bool {
switch tt {
case topodatapb.TabletType_MASTER, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY:
return true
}
return false
}
// IsSlaveType returns if this type should be connected to a master db
// and actively replicating?
// MASTER is not obviously (only support one level replication graph)
// BACKUP, RESTORE, WORKER may or may not be, but we don't know for sure
func IsSlaveType(tt topodatapb.TabletType) bool {
switch tt {
case topodatapb.TabletType_MASTER, topodatapb.TabletType_BACKUP, topodatapb.TabletType_RESTORE, topodatapb.TabletType_WORKER:
return false
}
return true
}
// TabletValidatePortMap returns an error if the tablet's portmap doesn't
// contain all the necessary ports for the tablet to be fully
// operational. We only care about vt port now, as mysql may not even
// be running.
func TabletValidatePortMap(tablet *topodatapb.Tablet) error {
if _, ok := tablet.PortMap["vt"]; !ok {
return fmt.Errorf("no vt port available")
}
return nil
}
// TabletEndPoint returns an EndPoint associated with the tablet record
func TabletEndPoint(tablet *topodatapb.Tablet) (*topodatapb.EndPoint, error) {
if err := TabletValidatePortMap(tablet); err != nil {
return nil, err
}
entry := NewEndPoint(tablet.Alias.Uid, tablet.Hostname)
for name, port := range tablet.PortMap {
entry.PortMap[name] = int32(port)
}
if len(tablet.HealthMap) > 0 {
entry.HealthMap = make(map[string]string, len(tablet.HealthMap))
for k, v := range tablet.HealthMap {
entry.HealthMap[k] = v
}
}
return entry, nil
}
// TabletComplete validates and normalizes the tablet. If the shard name
// contains a '-' it is going to try to infer the keyrange from it.
func TabletComplete(tablet *topodatapb.Tablet) error {
shard, kr, err := ValidateShardName(tablet.Shard)
if err != nil {
return err
}
tablet.Shard = shard
tablet.KeyRange = kr
return nil
}
// TabletInfo is the container for a Tablet, read from the topology server.
type TabletInfo struct {
version int64 // node version - used to prevent stomping concurrent writes
*topodatapb.Tablet
}
// String returns a string describing the tablet.
func (ti *TabletInfo) String() string {
return fmt.Sprintf("Tablet{%v}", topoproto.TabletAliasString(ti.Alias))
}
// AliasString returns the string representation of the tablet alias
func (ti *TabletInfo) AliasString() string {
return topoproto.TabletAliasString(ti.Alias)
}
// Addr returns hostname:vt port.
func (ti *TabletInfo) Addr() string {
return netutil.JoinHostPort(ti.Hostname, int32(ti.PortMap["vt"]))
}
// MysqlAddr returns hostname:mysql port.
func (ti *TabletInfo) MysqlAddr() string {
return netutil.JoinHostPort(ti.Hostname, int32(ti.PortMap["mysql"]))
}
// DbName is usually implied by keyspace. Having the shard information in the
// database name complicates mysql replication.
func (ti *TabletInfo) DbName() string {
return topoproto.TabletDbName(ti.Tablet)
}
// Version returns the version of this tablet from last time it was read or
// updated.
func (ti *TabletInfo) Version() int64 {
return ti.version
}
// IsInServingGraph returns if this tablet is in the serving graph
func (ti *TabletInfo) IsInServingGraph() bool {
return IsInServingGraph(ti.Type)
}
// IsSlaveType returns if this tablet's type is a slave
func (ti *TabletInfo) IsSlaveType() bool {
return IsSlaveType(ti.Type)
}
// IsHealthEqual compares the two health maps, and
// returns true if they're equivalent.
func IsHealthEqual(left, right map[string]string) bool {
if len(left) == 0 && len(right) == 0 {
return true
}
return reflect.DeepEqual(left, right)
}
// NewTabletInfo returns a TabletInfo basing on tablet with the
// version set. This function should be only used by Server
// implementations.
func NewTabletInfo(tablet *topodatapb.Tablet, version int64) *TabletInfo {
return &TabletInfo{version: version, Tablet: tablet}
}
// GetTablet is a high level function to read tablet data.
// It generates trace spans.
func (ts Server) GetTablet(ctx context.Context, alias *topodatapb.TabletAlias) (*TabletInfo, error) {
span := trace.NewSpanFromContext(ctx)
span.StartClient("TopoServer.GetTablet")
span.Annotate("tablet", topoproto.TabletAliasString(alias))
defer span.Finish()
value, version, err := ts.Impl.GetTablet(ctx, alias)
if err != nil {
return nil, err
}
return &TabletInfo{
version: version,
Tablet: value,
}, nil
}
// UpdateTablet updates the tablet data only - not associated replication paths.
// It also uses a span, and sends the event.
func (ts Server) UpdateTablet(ctx context.Context, tablet *TabletInfo) error {
span := trace.NewSpanFromContext(ctx)
span.StartClient("TopoServer.UpdateTablet")
span.Annotate("tablet", topoproto.TabletAliasString(tablet.Alias))
defer span.Finish()
var version int64 = -1
if tablet.version != 0 {
version = tablet.version
}
newVersion, err := ts.Impl.UpdateTablet(ctx, tablet.Tablet, version)
if err != nil {
return err
}
tablet.version = newVersion
event.Dispatch(&events.TabletChange{
Tablet: *tablet.Tablet,
Status: "updated",
})
return nil
}
// UpdateTabletFields is a high level wrapper for TopoServer.UpdateTabletFields
// that generates trace spans.
func (ts Server) UpdateTabletFields(ctx context.Context, alias *topodatapb.TabletAlias, update func(*topodatapb.Tablet) error) error {
span := trace.NewSpanFromContext(ctx)
span.StartClient("TopoServer.UpdateTabletFields")
span.Annotate("tablet", topoproto.TabletAliasString(alias))
defer span.Finish()
tablet, err := ts.Impl.UpdateTabletFields(ctx, alias, update)
if err != nil {
return err
}
if tablet != nil {
event.Dispatch(&events.TabletChange{
Tablet: *tablet,
Status: "updated",
})
}
return nil
}
// Validate makes sure a tablet is represented correctly in the topology server.
func Validate(ctx context.Context, ts Server, tabletAlias *topodatapb.TabletAlias) error {
// read the tablet record, make sure it parses
tablet, err := ts.GetTablet(ctx, tabletAlias)
if err != nil {
return err
}
if !topoproto.TabletAliasEqual(tablet.Alias, tabletAlias) {
return fmt.Errorf("bad tablet alias data for tablet %v: %#v", topoproto.TabletAliasString(tabletAlias), tablet.Alias)
}
// Validate the entry in the shard replication nodes
if err = ts.ValidateShard(ctx, tablet.Keyspace, tablet.Shard); err != nil {
return err
}
si, err := ts.GetShardReplication(ctx, tablet.Alias.Cell, tablet.Keyspace, tablet.Shard)
if err != nil {
return err
}
if _, err = si.GetShardReplicationNode(tabletAlias); err != nil {
return fmt.Errorf("tablet %v not found in cell %v shard replication: %v", tabletAlias, tablet.Alias.Cell, err)
}
return nil
}
// CreateTablet creates a new tablet and all associated paths for the
// replication graph.
func (ts Server) CreateTablet(ctx context.Context, tablet *topodatapb.Tablet) error {
// Have the Server create the tablet
err := ts.Impl.CreateTablet(ctx, tablet)
if err != nil {
return err
}
if err := UpdateTabletReplicationData(ctx, ts, tablet); err != nil {
return err
}
event.Dispatch(&events.TabletChange{
Tablet: *tablet,
Status: "created",
})
return nil
}
// DeleteTablet wraps the underlying Impl.DeleteTablet
// and dispatches the event.
func (ts Server) DeleteTablet(ctx context.Context, tabletAlias *topodatapb.TabletAlias) error {
// get the current tablet record, if any, to log the deletion
tablet, _, tErr := ts.Impl.GetTablet(ctx, tabletAlias)
if err := ts.Impl.DeleteTablet(ctx, tabletAlias); err != nil {
return err
}
// Only try to log if we have the required info.
if tErr == nil {
// Only copy the identity info for the tablet. The rest has been deleted.
event.Dispatch(&events.TabletChange{
Tablet: topodatapb.Tablet{
Alias: tabletAlias,
Keyspace: tablet.Keyspace,
Shard: tablet.Shard,
},
Status: "deleted",
})
}
return nil
}
// UpdateTabletReplicationData creates or updates the replication
// graph data for a tablet
func UpdateTabletReplicationData(ctx context.Context, ts Server, tablet *topodatapb.Tablet) error {
return UpdateShardReplicationRecord(ctx, ts, tablet.Keyspace, tablet.Shard, tablet.Alias)
}
// DeleteTabletReplicationData deletes replication data.
func DeleteTabletReplicationData(ctx context.Context, ts Server, tablet *topodatapb.Tablet) error {
return RemoveShardReplicationRecord(ctx, ts, tablet.Alias.Cell, tablet.Keyspace, tablet.Shard, tablet.Alias)
}
// GetTabletMap tries to read all the tablets in the provided list,
// and returns them all in a map.
// If error is ErrPartialResult, the results in the dictionary are
// incomplete, meaning some tablets couldn't be read.
func (ts Server) GetTabletMap(ctx context.Context, tabletAliases []*topodatapb.TabletAlias) (map[topodatapb.TabletAlias]*TabletInfo, error) {
span := trace.NewSpanFromContext(ctx)
span.StartLocal("topo.GetTabletMap")
span.Annotate("num_tablets", len(tabletAliases))
defer span.Finish()
wg := sync.WaitGroup{}
mutex := sync.Mutex{}
tabletMap := make(map[topodatapb.TabletAlias]*TabletInfo)
var someError error
for _, tabletAlias := range tabletAliases {
wg.Add(1)
go func(tabletAlias *topodatapb.TabletAlias) {
defer wg.Done()
tabletInfo, err := ts.GetTablet(ctx, tabletAlias)
mutex.Lock()
if err != nil {
log.Warningf("%v: %v", tabletAlias, err)
// There can be data races removing nodes - ignore them for now.
if err != ErrNoNode {
someError = ErrPartialResult
}
} else {
tabletMap[*tabletAlias] = tabletInfo
}
mutex.Unlock()
}(tabletAlias)
}
wg.Wait()
return tabletMap, someError
}