-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vtexplain_topo.go
119 lines (94 loc) · 3.19 KB
/
vtexplain_topo.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
/*
Copyright 2019 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 vtexplain
import (
"context"
"fmt"
"sync"
"vitess.io/vitess/go/vt/topo"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
)
// ExplainTopo satisfies the srvtopo.Server interface.
// Modeled after the vtgate test sandboxTopo
type ExplainTopo struct {
// Map of keyspace name to vschema
Keyspaces map[string]*vschemapb.Keyspace
// Map of ks/shard to test tablet connection
TabletConns map[string]*explainTablet
KeyspaceShards map[string]map[string]*topodatapb.ShardReference
// Synchronization lock
Lock sync.Mutex
// Number of shards for sharded keyspaces
NumShards int
TopoServer *topo.Server
}
func (et *ExplainTopo) getSrvVSchema() *vschemapb.SrvVSchema {
et.Lock.Lock()
defer et.Lock.Unlock()
return &vschemapb.SrvVSchema{
Keyspaces: et.Keyspaces,
}
}
// GetTopoServer is part of the srvtopo.Server interface
func (et *ExplainTopo) GetTopoServer() (*topo.Server, error) {
return et.TopoServer, nil
}
// GetSrvKeyspaceNames is part of the srvtopo.Server interface.
func (et *ExplainTopo) GetSrvKeyspaceNames(ctx context.Context, cell string, staleOK bool) ([]string, error) {
et.Lock.Lock()
defer et.Lock.Unlock()
keyspaces := make([]string, 0, 1)
for k := range et.Keyspaces {
keyspaces = append(keyspaces, k)
}
return keyspaces, nil
}
// GetSrvKeyspace is part of the srvtopo.Server interface.
func (et *ExplainTopo) GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error) {
et.Lock.Lock()
defer et.Lock.Unlock()
vschema := et.Keyspaces[keyspace]
if vschema == nil {
return nil, fmt.Errorf("no vschema for keyspace %s", keyspace)
}
shards := make([]*topodatapb.ShardReference, 0, len(et.KeyspaceShards[keyspace]))
for _, shard := range et.KeyspaceShards[keyspace] {
shards = append(shards, shard)
}
srvKeyspace := &topodatapb.SrvKeyspace{
Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
{
ServedType: topodatapb.TabletType_PRIMARY,
ShardReferences: shards,
},
{
ServedType: topodatapb.TabletType_REPLICA,
ShardReferences: shards,
},
{
ServedType: topodatapb.TabletType_RDONLY,
ShardReferences: shards,
},
},
}
return srvKeyspace, nil
}
func (et *ExplainTopo) WatchSrvKeyspace(ctx context.Context, cell, keyspace string, callback func(*topodatapb.SrvKeyspace, error) bool) {
ks, err := et.GetSrvKeyspace(ctx, cell, keyspace)
callback(ks, err)
}
// WatchSrvVSchema is part of the srvtopo.Server interface.
func (et *ExplainTopo) WatchSrvVSchema(ctx context.Context, cell string, callback func(*vschemapb.SrvVSchema, error) bool) {
callback(et.getSrvVSchema(), nil)
}