-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
srv_keyspace.go
162 lines (136 loc) · 4.71 KB
/
srv_keyspace.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
/*
Copyright 2017 Google Inc.
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 agreedto 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 (
"fmt"
"path"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
// This file contains the utility methods to manage SrvKeyspace objects.
func srvKeyspaceFileName(keyspace string) string {
return path.Join(KeyspacesPath, keyspace, SrvKeyspaceFile)
}
// WatchSrvKeyspaceData is returned / streamed by WatchSrvKeyspace.
// The WatchSrvKeyspace API guarantees exactly one of Value or Err will be set.
type WatchSrvKeyspaceData struct {
Value *topodatapb.SrvKeyspace
Err error
}
// WatchSrvKeyspace will set a watch on the SrvKeyspace object.
// It has the same contract as Conn.Watch, but it also unpacks the
// contents into a SrvKeyspace object.
func (ts *Server) WatchSrvKeyspace(ctx context.Context, cell, keyspace string) (*WatchSrvKeyspaceData, <-chan *WatchSrvKeyspaceData, CancelFunc) {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return &WatchSrvKeyspaceData{Err: err}, nil, nil
}
filePath := srvKeyspaceFileName(keyspace)
current, wdChannel, cancel := conn.Watch(ctx, filePath)
if current.Err != nil {
return &WatchSrvKeyspaceData{Err: current.Err}, nil, nil
}
value := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(current.Contents, value); err != nil {
// Cancel the watch, drain channel.
cancel()
for range wdChannel {
}
return &WatchSrvKeyspaceData{Err: fmt.Errorf("error unpacking initial SrvKeyspace object: %v", err)}, nil, nil
}
changes := make(chan *WatchSrvKeyspaceData, 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 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 <- &WatchSrvKeyspaceData{Err: wd.Err}
return
}
value := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(wd.Contents, value); err != nil {
cancel()
for range wdChannel {
}
changes <- &WatchSrvKeyspaceData{Err: fmt.Errorf("error unpacking SrvKeyspace object: %v", err)}
return
}
changes <- &WatchSrvKeyspaceData{Value: value}
}
}()
return &WatchSrvKeyspaceData{Value: value}, changes, cancel
}
// GetSrvKeyspaceNames returns the SrvKeyspace objects for a cell.
func (ts *Server) GetSrvKeyspaceNames(ctx context.Context, cell string) ([]string, error) {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return nil, err
}
children, err := conn.ListDir(ctx, KeyspacesPath, false /*full*/)
switch {
case err == nil:
return DirEntriesToStringArray(children), nil
case IsErrType(err, NoNode):
return nil, nil
default:
return nil, err
}
}
// UpdateSrvKeyspace saves a new SrvKeyspace. It is a blind write.
func (ts *Server) UpdateSrvKeyspace(ctx context.Context, cell, keyspace string, srvKeyspace *topodatapb.SrvKeyspace) error {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return err
}
nodePath := srvKeyspaceFileName(keyspace)
data, err := proto.Marshal(srvKeyspace)
if err != nil {
return err
}
_, err = conn.Update(ctx, nodePath, data, nil)
return err
}
// DeleteSrvKeyspace deletes a SrvKeyspace.
func (ts *Server) DeleteSrvKeyspace(ctx context.Context, cell, keyspace string) error {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return err
}
nodePath := srvKeyspaceFileName(keyspace)
return conn.Delete(ctx, nodePath, nil)
}
// GetSrvKeyspace returns the SrvKeyspace for a cell/keyspace.
func (ts *Server) GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error) {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return nil, err
}
nodePath := srvKeyspaceFileName(keyspace)
data, _, err := conn.Get(ctx, nodePath)
if err != nil {
return nil, err
}
srvKeyspace := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(data, srvKeyspace); err != nil {
return nil, fmt.Errorf("SrvKeyspace unmarshal failed: %v %v", data, err)
}
return srvKeyspace, nil
}