/
srv_vschema.go
225 lines (193 loc) · 5.83 KB
/
srv_vschema.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
/*
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 topo
import (
"context"
"fmt"
"sync"
"google.golang.org/protobuf/proto"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vterrors"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
)
// This file contains the utility methods to manage SrvVSchema objects.
// WatchSrvVSchemaData is returned / streamed by WatchSrvVSchema.
// The WatchSrvVSchema API guarantees exactly one of Value or Err will be set.
type WatchSrvVSchemaData struct {
Value *vschemapb.SrvVSchema
Err error
}
// WatchSrvVSchema will set a watch on the SrvVSchema object.
// It has the same contract as Conn.Watch, but it also unpacks the
// contents into a SrvVSchema object.
func (ts *Server) WatchSrvVSchema(ctx context.Context, cell string) (*WatchSrvVSchemaData, <-chan *WatchSrvVSchemaData, error) {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return nil, nil, err
}
ctx, cancel := context.WithCancel(ctx)
current, wdChannel, err := conn.Watch(ctx, SrvVSchemaFile)
if err != nil {
cancel()
return nil, nil, err
}
value := &vschemapb.SrvVSchema{}
if err := proto.Unmarshal(current.Contents, value); err != nil {
// Cancel the watch, drain channel.
cancel()
for range wdChannel {
}
return nil, nil, vterrors.Wrapf(err, "error unpacking initial SrvVSchema object")
}
changes := make(chan *WatchSrvVSchemaData, 10)
// The background routine reads any event from the watch channel,
// translates it, and sends it to the caller.
// If cancel() is called, the underlying Watch() code will
// send an ErrInterrupted and then close the channel. We'll
// just propagate that back to our caller.
go func() {
defer cancel()
defer close(changes)
for wd := range wdChannel {
if wd.Err != nil {
// Last error value, we're done.
// wdChannel will be closed right after
// this, no need to do anything.
changes <- &WatchSrvVSchemaData{Err: wd.Err}
return
}
value := &vschemapb.SrvVSchema{}
if err := proto.Unmarshal(wd.Contents, value); err != nil {
cancel()
for range wdChannel {
}
changes <- &WatchSrvVSchemaData{Err: vterrors.Wrapf(err, "error unpacking SrvVSchema object")}
return
}
changes <- &WatchSrvVSchemaData{Value: value}
}
}()
return &WatchSrvVSchemaData{Value: value}, changes, nil
}
// UpdateSrvVSchema updates the SrvVSchema file for a cell.
func (ts *Server) UpdateSrvVSchema(ctx context.Context, cell string, srvVSchema *vschemapb.SrvVSchema) error {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return err
}
nodePath := SrvVSchemaFile
data, err := proto.Marshal(srvVSchema)
if err != nil {
return err
}
_, err = conn.Update(ctx, nodePath, data, nil)
return err
}
// GetSrvVSchema returns the SrvVSchema for a cell.
func (ts *Server) GetSrvVSchema(ctx context.Context, cell string) (*vschemapb.SrvVSchema, error) {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return nil, err
}
nodePath := SrvVSchemaFile
data, _, err := conn.Get(ctx, nodePath)
if err != nil {
return nil, err
}
srvVSchema := &vschemapb.SrvVSchema{}
if err := proto.Unmarshal(data, srvVSchema); err != nil {
return nil, vterrors.Wrapf(err, "SrvVSchema unmarshal failed: %v", data)
}
return srvVSchema, nil
}
// DeleteSrvVSchema deletes the SrvVSchema file for a cell.
func (ts *Server) DeleteSrvVSchema(ctx context.Context, cell string) error {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return err
}
nodePath := SrvVSchemaFile
return conn.Delete(ctx, nodePath, nil)
}
// RebuildSrvVSchema rebuilds the SrvVSchema for the provided cell list
// (or all cells if cell list is empty).
func (ts *Server) RebuildSrvVSchema(ctx context.Context, cells []string) error {
// get the actual list of cells
if len(cells) == 0 {
var err error
cells, err = ts.GetKnownCells(ctx)
if err != nil {
return fmt.Errorf("GetKnownCells failed: %v", err)
}
}
// get the keyspaces
keyspaces, err := ts.GetKeyspaces(ctx)
if err != nil {
return fmt.Errorf("GetKeyspaces failed: %v", err)
}
// build the SrvVSchema in parallel, protected by mu
wg := sync.WaitGroup{}
mu := sync.Mutex{}
var finalErr error
srvVSchema := &vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{},
}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
defer wg.Done()
k, err := ts.GetVSchema(ctx, keyspace)
if IsErrType(err, NoNode) {
err = nil
k = &vschemapb.Keyspace{}
}
mu.Lock()
defer mu.Unlock()
if err != nil {
log.Errorf("%v: GetVSchema(%v) failed", err, keyspace)
finalErr = err
return
}
srvVSchema.Keyspaces[keyspace] = k
}(keyspace)
}
wg.Wait()
if finalErr != nil {
return finalErr
}
rr, err := ts.GetRoutingRules(ctx)
if err != nil {
return fmt.Errorf("GetRoutingRules failed: %v", err)
}
srvVSchema.RoutingRules = rr
srr, err := ts.GetShardRoutingRules(ctx)
if err != nil {
return fmt.Errorf("GetShardRoutingRules failed: %v", err)
}
srvVSchema.ShardRoutingRules = srr
// now save the SrvVSchema in all cells in parallel
for _, cell := range cells {
wg.Add(1)
go func(cell string) {
defer wg.Done()
if err := ts.UpdateSrvVSchema(ctx, cell, srvVSchema); err != nil {
log.Errorf("%v: UpdateSrvVSchema(%v) failed", err, cell)
mu.Lock()
finalErr = err
mu.Unlock()
}
}(cell)
}
wg.Wait()
return finalErr
}