forked from upspin/upspin
/
proxied.go
299 lines (261 loc) · 7.49 KB
/
proxied.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
// Copyright 2016 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dircache
// This file defines structures that keep track of individual target directories.
// It particular it keeps a count of entries from the directory still in the LRU
// and handles refreshing of directory entries.
import (
"sync"
"time"
"github.com/palager/upspin/access"
"github.com/palager/upspin/bind"
"github.com/palager/upspin/errors"
"github.com/palager/upspin/log"
"github.com/palager/upspin/path"
"github.com/palager/upspin/upspin"
"github.com/palager/upspin/user"
)
// proxiedDir contains information about proxied user directories.
type proxiedDir struct {
l *clog
atime time.Time // time of last access
user upspin.UserName
// sequence is the last sequence number seen in a watch. It is only
// set outside the watcher before any watcher starts
// while reading the log files.
sequence int64
// ep is only used outside the watcher and is the
// endpoint of the server being watched.
ep upspin.Endpoint
die chan bool // channel used to tell watcher to die
dying chan bool // channel used to confirm watcher is dying
// For retrying a watch.
retryInterval time.Duration
wake chan bool
watchSupported bool
}
// proxiedDirs is used to translate between a user name and the relevant cached directory.
type proxiedDirs struct {
sync.Mutex
closing bool // when this is true do not allocate any new watchers
l *clog
m map[upspin.UserName]*proxiedDir
}
func newProxiedDirs(l *clog) *proxiedDirs {
return &proxiedDirs{m: make(map[upspin.UserName]*proxiedDir), l: l}
}
// cacheable saves the endpoint and makes sure it is being watched. It returns
// true if the endpoint is cacheable.
func (p *proxiedDirs) cacheable(name upspin.PathName, ep *upspin.Endpoint) bool {
p.Lock()
defer p.Unlock()
if p.closing {
return false
}
parsed, err := path.Parse(name)
if err != nil {
log.Info.Printf("parse error on a cleaned name: %s", name)
return false
}
u := parsed.User()
d := p.m[u]
// If the endpoint changed, kill off the current watcher.
if d != nil && d.ep != *ep {
d.close()
d.ep = *ep
}
if d == nil {
d = &proxiedDir{l: p.l, ep: *ep, user: u}
p.m[u] = d
}
// Remember when we last accessed this proxied directory.
// TODO: Use this time to stop listening to directories we
// haven't looked at in a long time. We will also have to
// forget about cached information for them if we stop
// watching.
d.atime = time.Now()
// Start a watcher if none is running.
if d.die == nil {
// Don't start a watcher for snapshots.
// TODO(p): once snapshots start returning ErrNotSupported for Watch,
// just set d.watchSupported to true and wait for the first Watch to fix it.
_, suffix, _, err := user.Parse(u)
d.watchSupported = err == nil && suffix != "snapshot"
if d.watchSupported {
d.die = make(chan bool)
d.dying = make(chan bool)
d.wake = make(chan bool, 1)
go d.watcher(*ep)
}
}
return d.watchSupported
}
// retryWatch wakes up watcher (if it exists) to try the Watch again.
func (p *proxiedDirs) retryWatch(parsed path.Parsed) {
p.Lock()
defer p.Unlock()
if p.closing {
return
}
d := p.m[parsed.User()]
if d == nil || d.wake == nil {
return
}
select {
case d.wake <- true:
default:
}
}
// setSequence remembers a sequence read from the logfile.
func (p *proxiedDirs) setSequence(name upspin.PathName, sequence int64) {
p.Lock()
defer p.Unlock()
if p.closing {
return
}
parsed, err := path.Parse(name)
if err != nil {
log.Info.Printf("parse error on a cleaned name: %s", name)
return
}
u := parsed.User()
d := p.m[u]
if d == nil {
d = &proxiedDir{l: p.l, user: u}
p.m[u] = d
}
d.sequence = sequence
}
// close terminates the goroutines associated with a proxied dir.
func (d *proxiedDir) close() {
if d.die != nil {
close(d.die)
<-d.dying
d.die = nil
}
}
const (
initialRetryInterval = time.Second
maxRetryInterval = time.Minute
)
// watcher watches a directory and caches any changes to something already in the LRU.
func (d *proxiedDir) watcher(ep upspin.Endpoint) {
log.Debug.Printf("dircache.Watcher %s %s", d.user, ep)
defer close(d.dying)
// If we don't know better, always read in the whole state. It
// is shorter than the history of all operations.
if d.sequence == 0 {
d.sequence = -1
}
d.retryInterval = initialRetryInterval
for {
err := d.watch(ep)
if err == nil {
log.Debug.Printf("dircache.Watcher %s %s exiting", d.user, ep)
// watch() only returns if the watcher has been told to die
// or if there is an error requiring a new Watch.
return
}
if err == upspin.ErrNotSupported {
// Can't survive this.
d.watchSupported = false
log.Debug.Printf("dir/dircache.watcher: %s: %s", d.user, err)
return
}
if errors.Is(errors.Invalid, err) {
// A bad record in the log or a bad sequence number. Reread current state.
log.Info.Printf("dir/dircache.watcher restarting from -1: %s: %s", d.user, err)
d.sequence = -1
} else {
log.Info.Printf("dir/dircache.watcher: %s: %s", d.user, err)
}
select {
case <-time.After(d.retryInterval):
d.retryInterval *= 2
if d.retryInterval > maxRetryInterval {
d.retryInterval = maxRetryInterval
}
case <-d.wake:
}
}
}
// watch loops receiving watch events. It returns nil if told to die.
// Otherwise it returns whatever error was encountered.
func (d *proxiedDir) watch(ep upspin.Endpoint) error {
dir, err := bind.DirServer(d.l.cfg, ep)
if err != nil {
return err
}
name := upspin.PathName(string(d.user) + "/")
done := make(chan struct{})
defer close(done)
event, err := dir.Watch(name, d.sequence, done)
if err != nil {
return err
}
log.Info.Printf("dir/dircache: Watch(%q) started", name)
// If Watch succeeds, go back to the initial interval.
d.retryInterval = initialRetryInterval
// Loop receiving events until we are told to stop or the event stream is closed.
for {
select {
case <-d.die:
return nil
case e, ok := <-event:
if !ok {
return errors.Str("Watch event stream closed")
}
if e.Error != nil {
log.Debug.Printf("dir/dircache: Watch(%q) error: %s", name, e.Error)
} else {
log.Debug.Printf("dir/dircache: Watch(%q) entry: %s (delete=%t)", name, e.Entry.Name, e.Delete)
}
if err := d.handleEvent(&e); err != nil {
return err
}
}
}
}
func (d *proxiedDir) handleEvent(e *upspin.Event) error {
// Something odd happened?
if e.Error != nil {
return e.Error
}
// If we are rereading the current state, wipe what we know.
if d.sequence == -1 {
d.l.wipeLog(d.user)
}
// Is this a file we are watching? We always watch Access files since ones we never
// saw before can affect our cached state.
if !access.IsAccessFile(e.Entry.Name) {
_, ok := d.l.lru.Get(lruKey{name: e.Entry.Name, glob: false})
if !ok {
// Not a file we are watching, how about in a directory we are watching?
dirName := path.DropPath(e.Entry.Name, 1)
if dirName == e.Entry.Name {
return nil
}
_, ok := d.l.lru.Get(lruKey{name: dirName, glob: true})
if !ok {
return nil
}
}
}
// This is an event we care about.
// Ignore old events.
d.l.globalLock.Lock()
if !d.l.inSequence(e.Entry.Name, e.Entry.Sequence) {
d.l.globalLock.Unlock()
return nil
}
d.sequence = e.Entry.Sequence
op := lookupReq
if e.Delete {
op = deleteReq
}
d.l.logRequestWithSequence(op, e.Entry.Name, nil, e.Entry, e.Entry.Sequence)
d.l.globalLock.Unlock()
d.l.flush()
return nil
}