forked from matttproud/visor
/
snapshot.go
261 lines (215 loc) · 6.41 KB
/
snapshot.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
// Copyright (c) 2012, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/soundcloud/visor
package visor
import (
"fmt"
"github.com/soundcloud/doozer"
"path"
"regexp"
)
// Snapshot represents a specific point in time
// within the coordinator state. It is used by
// all time-aware interfaces to the coordinator.
type Snapshot struct {
Rev int64
conn *conn
}
// regular expressions to validate paths
const charPat = `[-.[:alnum:]]`
var pathRe = regexp.MustCompile(`^/$|^(/` + charPat + `+)+$`)
// Snapshotable is implemented by any type which
// is time-aware, and can be moved forward in time
// by calling createSnapshot with a new revision.
type snapshotable interface {
createSnapshot(rev int64) snapshotable
}
// Dial calls doozer.Dial and returns a Snapshot of the coordinator
// at the latest revision.
func Dial(addr string, root string) (s Snapshot, err error) {
dconn, err := doozer.Dial(addr)
if err != nil {
return
}
rev, err := dconn.Rev()
if err != nil {
return
}
s = Snapshot{rev, &conn{addr, root, dconn}}
return
}
// DialUri calls doozer.DialUri and returns a Snapshot of the coordinator cluster
// at the latest revision.
func DialUri(uri string, root string) (s Snapshot, err error) {
dconn, err := doozer.DialUri(uri, "")
if err != nil {
return
}
rev, err := dconn.Rev()
if err != nil {
return
}
s = Snapshot{rev, &conn{uri, root, dconn}}
return
}
// GetScale returns the scale of an app:pty@rev tuple. If the scale isn't found, 0 is returned.
func (s Snapshot) GetScale(app string, revid string, pty string) (scale int, rev int64, err error) {
path := ptyInstancesPath(app, revid, pty)
count, rev, err := s.conn.Stat(path, &s.Rev)
// File doesn't exist, assume scale = 0
if IsErrNoEnt(err) {
return 0, rev, nil
}
if err != nil {
return -1, rev, err
}
return count, rev, nil
}
// GetProxies gets the list of bazooka-proxy service IPs
func (s Snapshot) GetProxies() ([]string, error) {
return s.getdir(proxyDir)
}
// GetPms gets the list of bazooka-pm service IPs
func (s Snapshot) GetPms() ([]string, error) {
return s.getdir(pmDir)
}
func (s Snapshot) RegisterPm(host, version string) (Snapshot, error) {
return s.set(path.Join(pmDir, host), timestamp()+" "+version)
}
func (s Snapshot) UnregisterPm(host string) error {
return s.del(path.Join(pmDir, host))
}
func (s Snapshot) RegisterProxy(host string) (Snapshot, error) {
return s.set(path.Join(proxyDir, host), timestamp())
}
func (s Snapshot) UnregisterProxy(host string) error {
return s.del(path.Join(proxyDir, host))
}
func (s Snapshot) ResetCoordinator() error {
err := s.del("/")
if IsErrNoEnt(err) {
return nil
}
return err
}
// Getuid returns a unique ID from the coordinator
func Getuid(s Snapshot) (int64, error) {
return s.conn.Set(uidPath, -1, []byte{})
}
// exists checks if the specified path exists at this snapshot's revision
func (s Snapshot) exists(path string) (bool, int64, error) {
return s.conn.ExistsRev(path, &s.Rev)
}
// get returns the value at the specified path, at this snapshot's revision
func (s Snapshot) get(path string) (string, int64, error) {
val, rev, err := s.getBytes(path)
return string(val), rev, err
}
// getFile returns the value at the specified path as a file, at this snapshot's revision
func (s Snapshot) getFile(path string, codec codec) (f *file, err error) {
bytes, rev, err := s.getBytes(path)
if err != nil {
return
}
value, err := codec.Decode(bytes)
if err != nil {
return
}
f = &file{dir: path, Value: value, FileRev: rev, codec: codec, Snapshot: s}
return
}
// getBytes returns the value at the specified path, at this snapshot's revision
func (s Snapshot) getBytes(path string) ([]byte, int64, error) {
return s.conn.Get(path, &s.Rev)
}
// getdir returns the list of files in the specified directory, at this snapshot's revision
func (s Snapshot) getdir(path string) ([]string, error) {
return s.conn.Getdir(path, s.Rev)
}
// set sets the specfied path's body to the passed value, at this snapshot's revision
func (s Snapshot) set(path string, val string) (Snapshot, error) {
return s.setBytes(path, []byte(val))
}
// setBytes sets the specfied path's body to the passed value, at this snapshot's revision
func (s Snapshot) setBytes(path string, val []byte) (Snapshot, error) {
rev, err := s.conn.Set(path, s.Rev, val)
if err != nil {
return s, err
}
return s.FastForward(rev), err
}
// del deletes the file at the specified path, at this snapshot's revision
func (s Snapshot) del(path string) error {
return s.conn.Del(path, s.Rev)
}
// update checks if the specified path exists, and if so, does a (*Snapshot).Set with the passed value.
func (s Snapshot) update(path string, val string) (Snapshot, error) {
exists, _, err := s.exists(path)
if err != nil {
return s, err
}
if !exists {
return s, NewError(ErrNoEnt, fmt.Sprintf("path '%s' does not exist at %d", path, s.Rev))
}
return s.set(path, val)
}
func (s Snapshot) createSnapshot(rev int64) snapshotable {
return Snapshot{rev, s.conn}
}
func (s Snapshot) FastForward(rev int64) (ns Snapshot) {
return s.fastForward(s, rev).(Snapshot)
}
// fastForward either calls *createSnapshot* on *obj* or returns *obj* if it
// can't advance the object in time. Note that fastForward can never fail.
func (s *Snapshot) fastForward(obj snapshotable, rev int64) snapshotable {
var err error
if rev == -1 {
rev, err = s.conn.Rev()
if err != nil {
return obj
}
} else if rev < s.Rev {
return obj
}
return obj.createSnapshot(rev)
}
// getLatest returns the latest value for the given path
func getLatest(s Snapshot, path string, codec codec) (f *file, err error) {
evalue, rev, err := s.conn.Get(path, nil)
if err != nil {
return
}
value, err := codec.Decode(evalue)
if err != nil {
return
}
f = &file{dir: path, Value: value, codec: codec, Snapshot: s.FastForward(rev)}
return
}
func getSnapshotables(list []string, fn func(string) (snapshotable, error)) (result []snapshotable, err error) {
type resp struct {
value snapshotable
err error
}
ch := make(chan resp, len(list))
for _, item := range list {
go func(i string) {
r, err := fn(i)
if err != nil {
ch <- resp{err: err}
} else {
ch <- resp{value: r}
}
}(item)
}
for i := 0; i < len(list); i++ {
s := <-ch
if s.err != nil {
return nil, s.err
} else {
result = append(result, s.value)
}
}
return
}