-
Notifications
You must be signed in to change notification settings - Fork 247
/
cluster.go
429 lines (348 loc) · 12.1 KB
/
cluster.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package remote
import (
"context"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/authzed/consistent"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/protobuf/proto"
"github.com/authzed/spicedb/internal/dispatch"
"github.com/authzed/spicedb/internal/dispatch/keys"
log "github.com/authzed/spicedb/internal/logging"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
)
var dispatchCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "dispatch",
Name: "remote_dispatch_handler_total",
Help: "which dispatcher handled a request",
}, []string{"request_kind", "handler_name"})
func init() {
prometheus.MustRegister(dispatchCounter)
}
type ClusterClient interface {
DispatchCheck(ctx context.Context, req *v1.DispatchCheckRequest, opts ...grpc.CallOption) (*v1.DispatchCheckResponse, error)
DispatchExpand(ctx context.Context, req *v1.DispatchExpandRequest, opts ...grpc.CallOption) (*v1.DispatchExpandResponse, error)
DispatchReachableResources(ctx context.Context, in *v1.DispatchReachableResourcesRequest, opts ...grpc.CallOption) (v1.DispatchService_DispatchReachableResourcesClient, error)
DispatchLookupResources(ctx context.Context, in *v1.DispatchLookupResourcesRequest, opts ...grpc.CallOption) (v1.DispatchService_DispatchLookupResourcesClient, error)
DispatchLookupSubjects(ctx context.Context, in *v1.DispatchLookupSubjectsRequest, opts ...grpc.CallOption) (v1.DispatchService_DispatchLookupSubjectsClient, error)
}
type ClusterDispatcherConfig struct {
// KeyHandler is then handler to use for generating dispatch hash ring keys.
KeyHandler keys.Handler
// DispatchOverallTimeout is the maximum duration of a dispatched request
// before it should timeout.
DispatchOverallTimeout time.Duration
}
// SecondaryDispatch defines a struct holding a client and its name for secondary
// dispatching.
type SecondaryDispatch struct {
Name string
Client ClusterClient
}
// NewClusterDispatcher creates a dispatcher implementation that uses the provided client
// to dispatch requests to peer nodes in the cluster.
func NewClusterDispatcher(client ClusterClient, conn *grpc.ClientConn, config ClusterDispatcherConfig, secondaryDispatch map[string]SecondaryDispatch, secondaryDispatchExprs map[string]*DispatchExpr) dispatch.Dispatcher {
keyHandler := config.KeyHandler
if keyHandler == nil {
keyHandler = &keys.DirectKeyHandler{}
}
dispatchOverallTimeout := config.DispatchOverallTimeout
if dispatchOverallTimeout <= 0 {
dispatchOverallTimeout = 60 * time.Second
}
return &clusterDispatcher{
clusterClient: client,
conn: conn,
keyHandler: keyHandler,
dispatchOverallTimeout: dispatchOverallTimeout,
secondaryDispatch: secondaryDispatch,
secondaryDispatchExprs: secondaryDispatchExprs,
}
}
type clusterDispatcher struct {
clusterClient ClusterClient
conn *grpc.ClientConn
keyHandler keys.Handler
dispatchOverallTimeout time.Duration
secondaryDispatch map[string]SecondaryDispatch
secondaryDispatchExprs map[string]*DispatchExpr
}
func (cr *clusterDispatcher) DispatchCheck(ctx context.Context, req *v1.DispatchCheckRequest) (*v1.DispatchCheckResponse, error) {
if err := dispatch.CheckDepth(ctx, req); err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, err
}
requestKey, err := cr.keyHandler.CheckDispatchKey(ctx, req)
if err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, err
}
ctx = context.WithValue(ctx, consistent.CtxKey, requestKey)
resp, err := dispatchRequest(ctx, cr, "check", req, func(ctx context.Context, client ClusterClient) (*v1.DispatchCheckResponse, error) {
resp, err := client.DispatchCheck(ctx, req)
if err != nil {
return resp, err
}
err = adjustMetadataForDispatch(resp.Metadata)
return resp, err
})
if err != nil {
return &v1.DispatchCheckResponse{Metadata: requestFailureMetadata}, err
}
return resp, err
}
type requestMessage interface {
zerolog.LogObjectMarshaler
GetMetadata() *v1.ResolverMeta
}
type responseMessage interface {
proto.Message
GetMetadata() *v1.ResponseMeta
}
type respTuple[S responseMessage] struct {
resp S
err error
}
type secondaryRespTuple[S responseMessage] struct {
handlerName string
resp S
}
func dispatchRequest[Q requestMessage, S responseMessage](ctx context.Context, cr *clusterDispatcher, reqKey string, req Q, handler func(context.Context, ClusterClient) (S, error)) (S, error) {
withTimeout, cancelFn := context.WithTimeout(ctx, cr.dispatchOverallTimeout)
defer cancelFn()
if len(cr.secondaryDispatchExprs) == 0 || len(cr.secondaryDispatch) == 0 {
return handler(withTimeout, cr.clusterClient)
}
// If no secondary dispatches are defined, just invoke directly.
expr, ok := cr.secondaryDispatchExprs[reqKey]
if !ok {
return handler(withTimeout, cr.clusterClient)
}
// Otherwise invoke in parallel with any secondary matches.
primaryResultChan := make(chan respTuple[S], 1)
secondaryResultChan := make(chan secondaryRespTuple[S], len(cr.secondaryDispatch))
// Run the main dispatch.
go func() {
resp, err := handler(withTimeout, cr.clusterClient)
primaryResultChan <- respTuple[S]{resp, err}
}()
result, err := RunDispatchExpr(expr, req)
if err != nil {
log.Warn().Err(err).Msg("error when trying to evaluate the dispatch expression")
}
log.Trace().Str("secondary-dispatchers", strings.Join(result, ",")).Object("request", req).Msg("running secondary dispatchers")
for _, secondaryDispatchName := range result {
secondary, ok := cr.secondaryDispatch[secondaryDispatchName]
if !ok {
log.Warn().Str("secondary-dispatcher-name", secondaryDispatchName).Msg("received unknown secondary dispatcher")
continue
}
log.Trace().Str("secondary-dispatcher", secondary.Name).Object("request", req).Msg("running secondary dispatcher")
go func() {
resp, err := handler(withTimeout, secondary.Client)
if err != nil {
// For secondary dispatches, ignore any errors, as only the primary will be handled in
// that scenario.
log.Trace().Str("secondary", secondary.Name).Err(err).Msg("got ignored secondary dispatch error")
return
}
secondaryResultChan <- secondaryRespTuple[S]{resp: resp, handlerName: secondary.Name}
}()
}
var foundError error
select {
case <-withTimeout.Done():
return *new(S), fmt.Errorf("check dispatch has timed out")
case r := <-primaryResultChan:
if r.err == nil {
dispatchCounter.WithLabelValues(reqKey, "(primary)").Add(1)
return r.resp, nil
}
// Otherwise, if an error was found, log it and we'll return after *all* the secondaries have run.
// This allows an otherwise error-state to be handled by one of the secondaries.
foundError = r.err
case r := <-secondaryResultChan:
dispatchCounter.WithLabelValues(reqKey, r.handlerName).Add(1)
return r.resp, nil
}
dispatchCounter.WithLabelValues(reqKey, "(primary)").Add(1)
return *new(S), foundError
}
func adjustMetadataForDispatch(metadata *v1.ResponseMeta) error {
if metadata == nil {
return spiceerrors.MustBugf("received a nil metadata")
}
// NOTE: We only add 1 to the dispatch count if it was not already handled by the downstream dispatch,
// which will only be the case in a fully cached or further undispatched call.
if metadata.DispatchCount == 0 {
metadata.DispatchCount++
}
return nil
}
func (cr *clusterDispatcher) DispatchExpand(ctx context.Context, req *v1.DispatchExpandRequest) (*v1.DispatchExpandResponse, error) {
if err := dispatch.CheckDepth(ctx, req); err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}
requestKey, err := cr.keyHandler.ExpandDispatchKey(ctx, req)
if err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}
ctx = context.WithValue(ctx, consistent.CtxKey, requestKey)
withTimeout, cancelFn := context.WithTimeout(ctx, cr.dispatchOverallTimeout)
defer cancelFn()
resp, err := cr.clusterClient.DispatchExpand(withTimeout, req)
if err != nil {
return &v1.DispatchExpandResponse{Metadata: requestFailureMetadata}, err
}
err = adjustMetadataForDispatch(resp.Metadata)
return resp, err
}
func (cr *clusterDispatcher) DispatchReachableResources(
req *v1.DispatchReachableResourcesRequest,
stream dispatch.ReachableResourcesStream,
) error {
requestKey, err := cr.keyHandler.ReachableResourcesDispatchKey(stream.Context(), req)
if err != nil {
return err
}
ctx := context.WithValue(stream.Context(), consistent.CtxKey, requestKey)
stream = dispatch.StreamWithContext(ctx, stream)
if err := dispatch.CheckDepth(ctx, req); err != nil {
return err
}
withTimeout, cancelFn := context.WithTimeout(ctx, cr.dispatchOverallTimeout)
defer cancelFn()
client, err := cr.clusterClient.DispatchReachableResources(withTimeout, req)
if err != nil {
return err
}
for {
select {
case <-withTimeout.Done():
return withTimeout.Err()
default:
result, err := client.Recv()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
merr := adjustMetadataForDispatch(result.Metadata)
if merr != nil {
return merr
}
serr := stream.Publish(result)
if serr != nil {
return serr
}
}
}
}
func (cr *clusterDispatcher) DispatchLookupResources(
req *v1.DispatchLookupResourcesRequest,
stream dispatch.LookupResourcesStream,
) error {
requestKey, err := cr.keyHandler.LookupResourcesDispatchKey(stream.Context(), req)
if err != nil {
return err
}
ctx := context.WithValue(stream.Context(), consistent.CtxKey, requestKey)
stream = dispatch.StreamWithContext(ctx, stream)
if err := dispatch.CheckDepth(ctx, req); err != nil {
return err
}
withTimeout, cancelFn := context.WithTimeout(ctx, cr.dispatchOverallTimeout)
defer cancelFn()
client, err := cr.clusterClient.DispatchLookupResources(withTimeout, req)
if err != nil {
return err
}
for {
select {
case <-withTimeout.Done():
return withTimeout.Err()
default:
result, err := client.Recv()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
merr := adjustMetadataForDispatch(result.Metadata)
if merr != nil {
return merr
}
serr := stream.Publish(result)
if serr != nil {
return serr
}
}
}
}
func (cr *clusterDispatcher) DispatchLookupSubjects(
req *v1.DispatchLookupSubjectsRequest,
stream dispatch.LookupSubjectsStream,
) error {
requestKey, err := cr.keyHandler.LookupSubjectsDispatchKey(stream.Context(), req)
if err != nil {
return err
}
ctx := context.WithValue(stream.Context(), consistent.CtxKey, requestKey)
stream = dispatch.StreamWithContext(ctx, stream)
if err := dispatch.CheckDepth(ctx, req); err != nil {
return err
}
withTimeout, cancelFn := context.WithTimeout(ctx, cr.dispatchOverallTimeout)
defer cancelFn()
client, err := cr.clusterClient.DispatchLookupSubjects(withTimeout, req)
if err != nil {
return err
}
for {
select {
case <-withTimeout.Done():
return withTimeout.Err()
default:
result, err := client.Recv()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
merr := adjustMetadataForDispatch(result.Metadata)
if merr != nil {
return merr
}
serr := stream.Publish(result)
if serr != nil {
return serr
}
}
}
}
func (cr *clusterDispatcher) Close() error {
return nil
}
// ReadyState returns whether the underlying dispatch connection is available
func (cr *clusterDispatcher) ReadyState() dispatch.ReadyState {
state := cr.conn.GetState()
log.Trace().Interface("connection-state", state).Msg("checked if cluster dispatcher is ready")
return dispatch.ReadyState{
IsReady: state == connectivity.Ready || state == connectivity.Idle,
Message: fmt.Sprintf("found expected state when trying to connect to cluster: %v", state),
}
}
// Always verify that we implement the interface
var _ dispatch.Dispatcher = &clusterDispatcher{}
var emptyMetadata = &v1.ResponseMeta{
DispatchCount: 0,
}
var requestFailureMetadata = &v1.ResponseMeta{
DispatchCount: 1,
}