forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 19
/
topology.go
340 lines (303 loc) · 8.97 KB
/
topology.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
package topotools
import (
"fmt"
"net"
"sort"
"strconv"
"strings"
"sync"
"golang.org/x/net/context"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/netutil"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// TabletNode is the representation of a tablet in the db topology.
// It can be constructed from a Tablet object, or from an EndPoint.
type TabletNode struct {
Host string
Alias *pb.TabletAlias
Port int32
}
// ShortName returns a displayable representation of the host name.
// If the host is an IP address instead of a name, it is not shortened.
func (tn *TabletNode) ShortName() string {
if net.ParseIP(tn.Host) != nil {
return netutil.JoinHostPort(tn.Host, tn.Port)
}
hostPart := strings.SplitN(tn.Host, ".", 2)[0]
if tn.Port == 0 {
return hostPart
}
return netutil.JoinHostPort(hostPart, tn.Port)
}
func newTabletNodeFromTabletInfo(ti *topo.TabletInfo) *TabletNode {
if err := topo.TabletValidatePortMap(ti.Tablet); err != nil {
log.Errorf("ValidatePortmap(%v): %v", ti.Alias, err)
}
return &TabletNode{
Host: ti.Hostname,
Port: ti.PortMap["vt"],
Alias: ti.Alias,
}
}
func newTabletNodeFromEndPoint(ep *pb.EndPoint, cell string) *TabletNode {
return &TabletNode{
Host: ep.Host,
Alias: &pb.TabletAlias{
Uid: ep.Uid,
Cell: cell,
},
Port: ep.PortMap[topo.DefaultPortName],
}
}
// TabletNodesByType maps tablet types to slices of tablet nodes.
type TabletNodesByType struct {
TabletType pb.TabletType
Nodes []*TabletNode
}
// ShardNodes represents all tablet nodes for a shard, indexed by tablet type.
type ShardNodes struct {
Name string
TabletNodes []*TabletNodesByType
ServedTypes []pb.TabletType
Tag interface{} // Tag is an arbitrary value manageable by a plugin.
}
type numericShardNodesList []*ShardNodes
// Len is part of sort.Interface
func (nsnl numericShardNodesList) Len() int {
return len(nsnl)
}
// Less is part of sort.Interface
func (nsnl numericShardNodesList) Less(i, j int) bool {
// This panics, so it shouldn't be called unless all shard
// names can be converted to integers.
ii, err := strconv.Atoi(nsnl[i].Name)
if err != nil {
panic("bad numeric shard: " + nsnl[i].Name)
}
jj, err := strconv.Atoi(nsnl[j].Name)
if err != nil {
panic("bad numeric shard" + nsnl[j].Name)
}
return ii < jj
}
// Swap is part of sort.Interface
func (nsnl numericShardNodesList) Swap(i, j int) {
nsnl[i], nsnl[j] = nsnl[j], nsnl[i]
}
type rangeShardNodesList []*ShardNodes
// Len is part of sort.Interface
func (rsnl rangeShardNodesList) Len() int {
return len(rsnl)
}
// Less is part of sort.Interface
func (rsnl rangeShardNodesList) Less(i, j int) bool {
return rsnl[i].Name < rsnl[j].Name
}
// Swap is part of sort.Interface
func (rsnl rangeShardNodesList) Swap(i, j int) {
rsnl[i], rsnl[j] = rsnl[j], rsnl[i]
}
// KeyspaceNodes represents all tablet nodes in a keyspace.
type KeyspaceNodes struct {
ShardNodes []*ShardNodes // sorted by shard name
ServedFrom map[topo.TabletType]string
}
func newKeyspaceNodes() *KeyspaceNodes {
return &KeyspaceNodes{
ServedFrom: make(map[topo.TabletType]string),
}
}
func (ks *KeyspaceNodes) hasOnlyNumericShardNames() bool {
for _, shardNodes := range ks.ShardNodes {
if _, err := strconv.Atoi(shardNodes.Name); err != nil {
return false
}
}
return true
}
// TabletTypes returns a slice of tablet type names this ks
// contains.
func (ks KeyspaceNodes) TabletTypes() []pb.TabletType {
var contained []pb.TabletType
for _, t := range topoproto.AllTabletTypes {
if ks.HasType(t) {
contained = append(contained, t)
}
}
return contained
}
// HasType returns true if ks has any tablets with the named type.
func (ks KeyspaceNodes) HasType(tabletType pb.TabletType) bool {
for _, shardNodes := range ks.ShardNodes {
for _, tabletNodes := range shardNodes.TabletNodes {
if tabletNodes.TabletType == tabletType {
return true
}
}
}
return false
}
// Topology is the entire set of tablets in the topology.
type Topology struct {
Assigned map[string]*KeyspaceNodes // indexed by keyspace name
Idle []*TabletNode
Scrap []*TabletNode
Partial bool
}
// DbTopology returns the Topology for the topo server.
func DbTopology(ctx context.Context, ts topo.Server) (*Topology, error) {
topology := &Topology{
Assigned: make(map[string]*KeyspaceNodes),
Idle: make([]*TabletNode, 0),
Scrap: make([]*TabletNode, 0),
Partial: false,
}
tabletInfos, err := GetAllTabletsAcrossCells(ctx, ts)
switch err {
case nil:
// we're good, no error
case topo.ErrPartialResult:
// we got a partial result
topology.Partial = true
default:
// we got no result at all
return nil, err
}
assigned := make(map[string]map[string][]*TabletNodesByType)
for _, ti := range tabletInfos {
tablet := newTabletNodeFromTabletInfo(ti)
switch ti.Type {
case pb.TabletType_IDLE:
topology.Idle = append(topology.Idle, tablet)
case pb.TabletType_SCRAP:
topology.Scrap = append(topology.Scrap, tablet)
default:
if _, ok := assigned[ti.Keyspace]; !ok {
assigned[ti.Keyspace] = make(map[string][]*TabletNodesByType)
}
var tabletNode *TabletNodesByType
for _, tabletNodes := range assigned[ti.Keyspace][ti.Shard] {
if tabletNodes.TabletType == ti.Type {
tabletNode = tabletNodes
break
}
}
if tabletNode == nil {
tabletNode = &TabletNodesByType{
TabletType: ti.Type,
}
assigned[ti.Keyspace][ti.Shard] = append(assigned[ti.Keyspace][ti.Shard], tabletNode)
}
tabletNode.Nodes = append(tabletNode.Nodes, tablet)
}
}
for keyspace, shardMap := range assigned {
kn := newKeyspaceNodes()
for shard, nodes := range shardMap {
kn.ShardNodes = append(kn.ShardNodes, &ShardNodes{
Name: shard,
TabletNodes: nodes,
})
}
if kn.hasOnlyNumericShardNames() {
sort.Sort(numericShardNodesList(kn.ShardNodes))
} else {
sort.Sort(rangeShardNodesList(kn.ShardNodes))
}
topology.Assigned[keyspace] = kn
}
return topology, nil
}
// ServingGraph contains the representation of the serving graph
// for a given cell.
type ServingGraph struct {
Cell string
Keyspaces map[string]*KeyspaceNodes // indexed by keyspace name
Errors []string // collected during creation
}
// DbServingGraph returns the ServingGraph for the given cell.
func DbServingGraph(ctx context.Context, ts topo.Server, cell string) (servingGraph *ServingGraph) {
servingGraph = &ServingGraph{
Cell: cell,
Keyspaces: make(map[string]*KeyspaceNodes),
}
rec := concurrency.AllErrorRecorder{}
keyspaces, err := ts.GetSrvKeyspaceNames(ctx, cell)
if err != nil {
servingGraph.Errors = append(servingGraph.Errors, fmt.Sprintf("GetSrvKeyspaceNames failed: %v", err))
return
}
wg := sync.WaitGroup{}
servingTypes := []topo.TabletType{topo.TYPE_MASTER, topo.TYPE_REPLICA, topo.TYPE_RDONLY}
for _, keyspace := range keyspaces {
kn := newKeyspaceNodes()
servingGraph.Keyspaces[keyspace] = kn
wg.Add(1)
go func(keyspace string, kn *KeyspaceNodes) {
defer wg.Done()
ks, err := ts.GetSrvKeyspace(ctx, cell, keyspace)
if err != nil {
rec.RecordError(fmt.Errorf("GetSrvKeyspace(%v, %v) failed: %v", cell, keyspace, err))
return
}
kn.ServedFrom = ks.ServedFrom
displayedShards := make(map[string]bool)
for _, partitionTabletType := range servingTypes {
kp, ok := ks.Partitions[partitionTabletType]
if !ok {
continue
}
for _, srvShard := range kp.ShardReferences {
shard := srvShard.Name
if displayedShards[shard] {
continue
}
displayedShards[shard] = true
sn := &ShardNodes{
Name: shard,
}
kn.ShardNodes = append(kn.ShardNodes, sn)
wg.Add(1)
go func(shard string, sn *ShardNodes) {
defer wg.Done()
tabletTypes, err := ts.GetSrvTabletTypesPerShard(ctx, cell, keyspace, shard)
if err != nil {
rec.RecordError(fmt.Errorf("GetSrvTabletTypesPerShard(%v, %v, %v) failed: %v", cell, keyspace, shard, err))
return
}
for _, tabletType := range tabletTypes {
endPoints, _, err := ts.GetEndPoints(ctx, cell, keyspace, shard, tabletType)
if err != nil {
rec.RecordError(fmt.Errorf("GetEndPoints(%v, %v, %v, %v) failed: %v", cell, keyspace, shard, tabletType, err))
continue
}
for _, endPoint := range endPoints.Entries {
var tabletNode *TabletNodesByType
for _, t := range sn.TabletNodes {
if t.TabletType == tabletType {
tabletNode = t
break
}
}
if tabletNode == nil {
tabletNode = &TabletNodesByType{
TabletType: tabletType,
}
sn.TabletNodes = append(sn.TabletNodes, tabletNode)
}
tabletNode.Nodes = append(tabletNode.Nodes, newTabletNodeFromEndPoint(endPoint, cell))
}
}
}(shard, sn)
}
}
}(keyspace, kn)
}
wg.Wait()
servingGraph.Errors = rec.ErrorStrings()
return
}