/
memorytopo.go
371 lines (323 loc) · 9.33 KB
/
memorytopo.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
/*
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 memorytopo contains an implementation of the topo.Factory /
// topo.Conn interfaces based on an in-memory tree of data.
// It is constructed with an immutable set of cells.
package memorytopo
import (
"context"
"errors"
"math/rand"
"strings"
"sync"
"sync/atomic"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
const (
// Path components
electionsPath = "elections"
)
var ErrConnectionClosed = errors.New("connection closed")
const (
// UnreachableServerAddr is a sentinel value for CellInfo.ServerAddr.
// If a memorytopo topo.Conn is created with this serverAddr then every
// method on that Conn which takes a context will simply block until the
// context finishes, and return ctx.Err(), in order to simulate an
// unreachable local cell for testing.
UnreachableServerAddr = "unreachable"
)
// Factory is a memory-based implementation of topo.Factory. It
// takes a file-system like approach, with directories at each level
// being an actual directory node. This is meant to be closer to
// file-system like servers, like ZooKeeper or Chubby. etcd or Consul
// implementations would be closer to a node-based implementation.
//
// It contains a single tree of nodes. Each cell topo.Conn will use
// a sub-directory in that tree.
type Factory struct {
// mu protects the following fields.
mu sync.Mutex
// cells is the toplevel map that has one entry per cell.
cells map[string]*node
// generation is used to generate unique incrementing version
// numbers. We want a global counter so when creating a file,
// then deleting it, then re-creating it, we don't restart the
// version at 1. It is initialized with a random number,
// so if we have two implementations, the numbers won't match.
generation uint64
// err is used for testing purposes to force queries / watches
// to return the given error
err error
// listErr is used for testing purposed to fake errors from
// calls to List.
listErr error
// callstats allows us to keep track of how many topo.Conn calls
// we make (Create, Get, Update, Delete, List, ListDir, etc).
callstats *stats.CountersWithMultiLabels
}
// HasGlobalReadOnlyCell is part of the topo.Factory interface.
func (f *Factory) HasGlobalReadOnlyCell(serverAddr, root string) bool {
return false
}
// Create is part of the topo.Factory interface.
func (f *Factory) Create(cell, serverAddr, root string) (topo.Conn, error) {
f.mu.Lock()
defer f.mu.Unlock()
if _, ok := f.cells[cell]; !ok {
return nil, topo.NewError(topo.NoNode, cell)
}
return &Conn{
factory: f,
cell: cell,
serverAddr: serverAddr,
}, nil
}
// SetError forces the given error to be returned from all calls and propagates
// the error to all active watches.
func (f *Factory) SetError(err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.err = err
if err != nil {
for _, node := range f.cells {
node.PropagateWatchError(err)
}
}
}
func (f *Factory) GetCallStats() *stats.CountersWithMultiLabels {
return f.callstats
}
// Lock blocks all requests to the topo and is exposed to allow tests to
// simulate an unresponsive topo server
func (f *Factory) Lock() {
f.mu.Lock()
}
// Unlock unblocks all requests to the topo and is exposed to allow tests to
// simulate an unresponsive topo server
func (f *Factory) Unlock() {
f.mu.Unlock()
}
// Conn implements the topo.Conn interface. It remembers the cell and serverAddr,
// and points at the Factory that has all the data.
type Conn struct {
factory *Factory
cell string
serverAddr string
closed atomic.Bool
}
// dial returns immediately, unless the Conn points to the sentinel
// UnreachableServerAddr, in which case it will block until the context expires.
func (c *Conn) dial(ctx context.Context) error {
if c.closed.Load() {
return ErrConnectionClosed
}
if c.serverAddr == UnreachableServerAddr {
<-ctx.Done()
}
return ctx.Err()
}
// Close is part of the topo.Conn interface.
func (c *Conn) Close() {
c.factory.callstats.Add([]string{"Close"}, 1)
c.closed.Store(true)
}
type watch struct {
contents chan *topo.WatchData
recursive chan *topo.WatchDataRecursive
lock chan string
}
// node contains a directory or a file entry.
// Exactly one of contents or children is not nil.
type node struct {
name string
version uint64
contents []byte
children map[string]*node
// parent is a pointer to the parent node.
// It is set to nil in toplevel and cell node.
parent *node
// watches is a map of all watches for this node.
watches map[int]watch
// lock is nil when the node is not locked.
// otherwise it has a channel that is closed by unlock.
lock chan struct{}
// lockContents is the contents of the locks.
// For regular locks, it has the contents that was passed in.
// For primary election, it has the id of the election leader.
lockContents string
}
func (n *node) isDirectory() bool {
return n.children != nil
}
func (n *node) recurseContents(callback func(n *node)) {
if n.isDirectory() {
for _, child := range n.children {
child.recurseContents(callback)
}
} else {
callback(n)
}
}
func (n *node) propagateRecursiveWatch(ev *topo.WatchDataRecursive) {
for parent := n.parent; parent != nil; parent = parent.parent {
for _, w := range parent.watches {
if w.recursive != nil {
w.recursive <- ev
}
}
}
}
var (
nextWatchIndex = 0
nextWatchIndexMu sync.Mutex
)
func (n *node) addWatch(w watch) int {
nextWatchIndexMu.Lock()
defer nextWatchIndexMu.Unlock()
watchIndex := nextWatchIndex
nextWatchIndex++
n.watches[watchIndex] = w
return watchIndex
}
// PropagateWatchError propagates the given error to all watches on this node
// and recursively applies to all children
func (n *node) PropagateWatchError(err error) {
for _, ch := range n.watches {
if ch.contents == nil {
continue
}
ch.contents <- &topo.WatchData{
Err: err,
}
}
for _, c := range n.children {
c.PropagateWatchError(err)
}
}
// NewServerAndFactory returns a new MemoryTopo and the backing factory for all
// the cells. It will create one cell for each parameter passed in. It will log.Exit out
// in case of a problem.
func NewServerAndFactory(ctx context.Context, cells ...string) (*topo.Server, *Factory) {
f := &Factory{
cells: make(map[string]*node),
generation: uint64(rand.Int63n(1 << 60)),
callstats: stats.NewCountersWithMultiLabels("", "", []string{"Call"}),
}
f.cells[topo.GlobalCell] = f.newDirectory(topo.GlobalCell, nil)
ts, err := topo.NewWithFactory(f, "" /*serverAddress*/, "" /*root*/)
if err != nil {
log.Exitf("topo.NewWithFactory() failed: %v", err)
}
for _, cell := range cells {
f.cells[cell] = f.newDirectory(cell, nil)
if err := ts.CreateCellInfo(ctx, cell, &topodatapb.CellInfo{}); err != nil {
log.Exitf("ts.CreateCellInfo(%v) failed: %v", cell, err)
}
}
return ts, f
}
// NewServer returns the new server
func NewServer(ctx context.Context, cells ...string) *topo.Server {
server, _ := NewServerAndFactory(ctx, cells...)
return server
}
func (f *Factory) getNextVersion() uint64 {
f.generation++
return f.generation
}
func (f *Factory) newFile(name string, contents []byte, parent *node) *node {
return &node{
name: name,
version: f.getNextVersion(),
contents: contents,
parent: parent,
watches: make(map[int]watch),
}
}
func (f *Factory) newDirectory(name string, parent *node) *node {
return &node{
name: name,
version: f.getNextVersion(),
children: make(map[string]*node),
parent: parent,
watches: make(map[int]watch),
}
}
func (f *Factory) nodeByPath(cell, filePath string) *node {
n, ok := f.cells[cell]
if !ok {
return nil
}
parts := strings.Split(filePath, "/")
for _, part := range parts {
if part == "" {
// Skip empty parts, usually happens at the end.
continue
}
if n.children == nil {
// This is a file.
return nil
}
child, ok := n.children[part]
if !ok {
// Path doesn't exist.
return nil
}
n = child
}
return n
}
func (f *Factory) getOrCreatePath(cell, filePath string) *node {
n, ok := f.cells[cell]
if !ok {
return nil
}
parts := strings.Split(filePath, "/")
for _, part := range parts {
if part == "" {
// Skip empty parts, usually happens at the end.
continue
}
if n.children == nil {
// This is a file.
return nil
}
child, ok := n.children[part]
if !ok {
// Path doesn't exist, create it.
child = f.newDirectory(part, n)
n.children[part] = child
}
n = child
}
return n
}
// recursiveDelete deletes a node and its parent directory if empty.
func (f *Factory) recursiveDelete(n *node) {
parent := n.parent
if parent == nil {
return
}
delete(parent.children, n.name)
if len(parent.children) == 0 {
f.recursiveDelete(parent)
}
}
func (f *Factory) SetListError(err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.listErr = err
}