forked from open-policy-agent/opa
/
watch.go
343 lines (285 loc) · 8.67 KB
/
watch.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
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package watch
import (
"context"
"errors"
"fmt"
"sync"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/dependencies"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/topdown"
)
// Watcher allows for watches to be registed on queries.
type Watcher struct {
store storage.Store
compiler *ast.Compiler
ctx context.Context
trigger storage.TriggerHandle
handles map[*Handle]struct{}
dataWatch map[string]map[signal]struct{} // map from path -> set of signals
closed bool
l sync.Mutex // The lock around creating and ending watches.
}
// Handle allows a user to listen to and end a watch on a query.
type Handle struct {
C <-chan Event
query string // the original query, used for migration
out chan Event // out is the same channel as C, but without directional constraints
notify signal // channel to recieve new data change alerts on.
done signal // closed by the watcher to signal the sending goroutine to stop sending query results.
ack signal // closed by the sending goroutine to tell the watcher it has stopped sending query results.
watcher *Watcher
l sync.Mutex
}
// Event represents a change to a query. Query is the query in question and Value is the
// JSON encoded results of the new query evaluation. Error will be populated if
// evaluating the new query results encountered an error for any reason. If Error is not
// nil, the contents of Value are undefined.
//
// Metrics and Tracer represent the metrics and trace from the evaluation of the query.
type Event struct {
Query string `json:"query"`
Value rego.ResultSet `json:"value"`
Error error `json:"error,omitempty"`
Metrics metrics.Metrics `json:"-"`
Tracer topdown.BufferTracer `json:"-"`
}
type signal chan struct{}
// New creates and returns a new Watcher on the store using the compiler provided.
// Once a compiler is provided to create a Watcher, it must not be modified, or else
// the results produced by the Watcher are undefined.
func New(ctx context.Context, s storage.Store, c *ast.Compiler, txn storage.Transaction) (w *Watcher, err error) {
w = create(ctx, s, c)
w.trigger, err = s.Register(ctx, txn, storage.TriggerConfig{OnCommit: w.notify})
return w, err
}
// Query registers a watch on the provided Rego query. Whenever changes are made to a
// base or virtual document that the query depends on, an Event describing the new result
// of the query will be sent through the Handle.
//
// Query will return an error if registering the watch fails for any reason.
func (w *Watcher) Query(query string) (*Handle, error) {
out := make(chan Event)
h := &Handle{
C: out,
query: query,
out: out,
notify: make(signal, 1),
done: make(signal),
ack: make(signal),
watcher: w,
}
if err := w.registerHandle(h); err != nil {
return nil, err
}
go h.deliver()
return h, nil
}
// Stop ends the watch on the query associated with the Handle. It will close the channel
// that was delivering notifications through the Handle. This may happen before or after
// Stop returns.
func (h *Handle) Stop() {
h.l.Lock()
defer h.l.Unlock()
h.watcher.endQuery(h)
}
// Close ends the watches on all queries this Watcher has.
//
// Further attempts to register or end watches will result in an error after Close() is
// called.
func (w *Watcher) Close(txn storage.Transaction) {
w.l.Lock()
defer w.l.Unlock()
if !w.closed {
w.close(txn, false)
}
}
// Migrate creates a new Watcher with the same watches as w, but using the new
// compiler. Like when creating a Watcher with New, the provided compiler must not
// be modified after being passed to Migrate, or else behavior is undefined.
//
// After Migrate returns, the old watcher will be closed, and the new will be ready for
// use. All Handles from the old watcher will still be active, via the returned Watcher,
// with the exception of those Handles who's query is no longer valid with the new
// compiler. Such Handles will be shutdown and a final Event sent along their channel
// indicating the cause of the error.
//
// If an error occurs creating the new Watcher, the state of the old Watcher will not be
// changed.
func (w *Watcher) Migrate(c *ast.Compiler, txn storage.Transaction) (*Watcher, error) {
w.l.Lock()
defer w.l.Unlock()
if w.closed {
return w, errors.New("cannot migrate a closed Watcher")
}
m, err := New(w.ctx, w.store, c, txn)
if err != nil {
return w, err
}
var handles []*Handle
for h := range w.handles {
handles = append(handles, h)
}
w.close(txn, true)
for _, h := range handles {
if err := m.registerHandle(h); err != nil {
h.shutDown(newInvalidatedWatchError(err))
}
}
return m, nil
}
func create(ctx context.Context, s storage.Store, c *ast.Compiler) *Watcher {
return &Watcher{
store: s,
compiler: c,
ctx: ctx,
handles: map[*Handle]struct{}{},
dataWatch: map[string]map[signal]struct{}{},
}
}
func (w *Watcher) registerHandle(h *Handle) error {
h.l.Lock()
w.l.Lock()
defer h.l.Unlock()
defer w.l.Unlock()
if w.closed {
return errors.New("cannot start query watch with closed Watcher")
}
parsed, err := ast.ParseBody(h.query)
if err != nil {
return err
}
compiled, err := w.compiler.QueryCompiler().Compile(parsed)
if err != nil {
return err
}
refs, err := dependencies.Base(w.compiler, compiled)
if err != nil {
panic(err)
}
h.watcher = w
w.handles[h] = struct{}{}
for _, r := range refs {
s := r.String()
if _, ok := w.dataWatch[s]; !ok {
w.dataWatch[s] = map[signal]struct{}{}
}
w.dataWatch[s][h.notify] = struct{}{}
}
// Send one notification when we start (like first query).
select {
case h.notify <- struct{}{}:
default:
}
return nil
}
func (h *Handle) deliver() {
defer close(h.ack)
for {
select {
case <-h.notify:
m := metrics.New()
t := topdown.NewBufferTracer()
h.l.Lock()
r := rego.New(
rego.Compiler(h.watcher.compiler),
rego.Store(h.watcher.store),
rego.Query(h.query),
rego.Metrics(m),
rego.Tracer(t),
)
ctx := h.watcher.ctx
h.l.Unlock()
result, err := r.Eval(ctx)
h.out <- Event{
Query: h.query,
Value: result,
Error: err,
Metrics: m,
Tracer: *t,
}
case <-h.done:
return
}
}
}
func (w *Watcher) endQuery(h *Handle) {
w.l.Lock()
defer w.l.Unlock()
delete(w.handles, h)
for _, notifiers := range w.dataWatch {
delete(notifiers, h.notify)
}
h.shutDown(nil)
}
func (h *Handle) shutDown(err error) {
select {
case _, ok := <-h.done:
if !ok {
return // Handle is already ended.
}
default:
}
close(h.done) // Tell the goroutine relaying query updates to stop sending.
go cleanupQuery(h, err)
}
// close assumes that the Watcher is locked. It will not unlock it.
func (w *Watcher) close(txn storage.Transaction, migrating bool) {
w.trigger.Unregister(w.ctx, txn)
if !migrating {
for h := range w.handles {
h.shutDown(nil)
}
}
w.handles = map[*Handle]struct{}{}
w.dataWatch = map[string]map[signal]struct{}{}
w.closed = true
}
func cleanupQuery(h *Handle, err error) {
<-h.ack // The sending goroutine will close ack.
// The notify channel has been removed from all dataWatches, and the sending
// goroutine is no longer listening to it, it's safe to close it.
close(h.notify)
if err != nil {
h.out <- Event{Query: h.query, Value: nil, Error: err}
}
// The sending goroutine will no longer send to out, it's safe to close it.
close(h.out)
}
func (w *Watcher) notify(ctx context.Context, txn storage.Transaction, event storage.TriggerEvent) {
w.l.Lock()
defer w.l.Unlock()
if w.closed {
panic("not reached")
}
// For each piece of changed data, see if there is a watch that depends on it in
// some way. If there is, send notifications to all of the watches that depend on
// the changed data. If a notification is already enqueued for a watch, another
// is not sent, as there would be no reason to (the current changes will
// passively be included next time the watch is evaluated).
notifySet := map[signal]struct{}{}
for _, d := range event.Data {
r := d.Path.Ref(ast.DefaultRootDocument)
for path, notifiers := range w.dataWatch {
if r.HasPrefix(ast.MustParseRef(path)) {
for notify := range notifiers {
notifySet[notify] = struct{}{}
}
}
}
}
for notify := range notifySet {
select {
case notify <- struct{}{}:
default: // Already a notification in the queue.
}
}
}
func newInvalidatedWatchError(err error) error {
return fmt.Errorf("watch invalidated: %s", err.Error())
}