Skip to content

Commit

Permalink
share key func between cache and consistent hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ecordell committed Nov 15, 2021
1 parent 8fafecc commit 298a9a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
15 changes: 3 additions & 12 deletions internal/dispatch/caching/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/authzed/spicedb/internal/dispatch"
v1 "github.com/authzed/spicedb/internal/proto/dispatch/v1"
"github.com/authzed/spicedb/pkg/tuple"
)

const (
Expand Down Expand Up @@ -146,7 +145,7 @@ func registerMetricsFunc(name string, subsystem string, metricsFunc func() uint6
// DispatchCheck implements dispatch.Check interface
func (cd *cachingDispatcher) DispatchCheck(ctx context.Context, req *v1.DispatchCheckRequest) (*v1.DispatchCheckResponse, error) {
cd.checkTotalCounter.Inc()
requestKey := checkRequestToKey(req)
requestKey := dispatch.CheckRequestToKey(req)

if cachedResultRaw, found := cd.c.Get(requestKey); found {
cachedResult := cachedResultRaw.(checkResultEntry)
Expand Down Expand Up @@ -178,7 +177,7 @@ func (cd *cachingDispatcher) DispatchExpand(ctx context.Context, req *v1.Dispatc
// DispatchLookup implements dispatch.Lookup interface and does not do any caching yet.
func (cd *cachingDispatcher) DispatchLookup(ctx context.Context, req *v1.DispatchLookupRequest) (*v1.DispatchLookupResponse, error) {
cd.lookupTotalCounter.Inc()
requestKey := lookupRequestToKey(req)
requestKey := dispatch.LookupRequestToKey(req)
if cachedResultRaw, found := cd.c.Get(requestKey); found {
cachedResult := cachedResultRaw.(lookupResultEntry)
if req.Metadata.DepthRemaining >= cachedResult.depthRequired {
Expand All @@ -191,7 +190,7 @@ func (cd *cachingDispatcher) DispatchLookup(ctx context.Context, req *v1.Dispatc

// We only want to cache the result if there was no error
if err == nil {
requestKey := lookupRequestToKey(req)
requestKey := dispatch.LookupRequestToKey(req)
toCache := lookupResultEntry{computed, computed.Metadata.DepthRequired}
toCache.result.Metadata.DispatchCount = 0

Expand All @@ -216,11 +215,3 @@ func (cd *cachingDispatcher) Close() error {

return nil
}

func checkRequestToKey(req *v1.DispatchCheckRequest) string {
return fmt.Sprintf("check//%s@%s@%s", tuple.StringONR(req.ObjectAndRelation), tuple.StringONR(req.Subject), req.Metadata.AtRevision)
}

func lookupRequestToKey(req *v1.DispatchLookupRequest) string {
return fmt.Sprintf("lookup//%s#%s@%s@%s", req.ObjectRelation.Namespace, req.ObjectRelation.Relation, tuple.StringONR(req.Subject), req.Metadata.AtRevision)
}
17 changes: 17 additions & 0 deletions internal/dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"

"github.com/authzed/spicedb/pkg/tuple"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -63,3 +65,18 @@ func CheckDepth(ctx context.Context, req HasMetadata) error {

return nil
}

// CheckRequestToKey converts a check request into a cache key
func CheckRequestToKey(req *v1.DispatchCheckRequest) string {
return fmt.Sprintf("check//%s@%s@%s", tuple.StringONR(req.ObjectAndRelation), tuple.StringONR(req.Subject), req.Metadata.AtRevision)
}

// LookupRequestToKey converts a lookup request into a cache key
func LookupRequestToKey(req *v1.DispatchLookupRequest) string {
return fmt.Sprintf("lookup//%s#%s@%s@%s", req.ObjectRelation.Namespace, req.ObjectRelation.Relation, tuple.StringONR(req.Subject), req.Metadata.AtRevision)
}

// ExpandRequestToKey converts an expand request into a cache key
func ExpandRequestToKey(req *v1.DispatchExpandRequest) string {
return fmt.Sprintf("expand//%s@%s", tuple.StringONR(req.ObjectAndRelation), req.Metadata.AtRevision)
}
38 changes: 3 additions & 35 deletions internal/dispatch/remote/cluster.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package remote

import (
"bytes"
"context"

"google.golang.org/grpc"
Expand Down Expand Up @@ -32,12 +31,7 @@ func (cr *clusterDispatcher) DispatchCheck(ctx context.Context, req *v1.Dispatch
if err != nil {
return &v1.DispatchCheckResponse{Metadata: emptyMetadata}, err
}
requestKey := bytes.Join([][]byte{
[]byte(req.ObjectAndRelation.String()),
[]byte(req.Subject.String()),
[]byte(req.Metadata.AtRevision),
}, []byte("-"))
ctx = context.WithValue(ctx, balancer.CtxKey, requestKey)
ctx = context.WithValue(ctx, balancer.CtxKey, []byte(dispatch.CheckRequestToKey(req)))
resp, err := cr.clusterClient.DispatchCheck(ctx, req)
if err != nil {
return &v1.DispatchCheckResponse{Metadata: requestFailureMetadata}, err
Expand All @@ -51,13 +45,7 @@ func (cr *clusterDispatcher) DispatchExpand(ctx context.Context, req *v1.Dispatc
if err != nil {
return &v1.DispatchExpandResponse{Metadata: emptyMetadata}, err
}

requestKey := bytes.Join([][]byte{
[]byte(req.ObjectAndRelation.String()),
[]byte(req.ExpansionMode.String()),
[]byte(req.Metadata.AtRevision),
}, []byte("-"))
ctx = context.WithValue(ctx, balancer.CtxKey, requestKey)
ctx = context.WithValue(ctx, balancer.CtxKey, dispatch.ExpandRequestToKey(req))
resp, err := cr.clusterClient.DispatchExpand(ctx, req)
if err != nil {
return &v1.DispatchExpandResponse{Metadata: requestFailureMetadata}, err
Expand All @@ -71,27 +59,7 @@ func (cr *clusterDispatcher) DispatchLookup(ctx context.Context, req *v1.Dispatc
if err != nil {
return &v1.DispatchLookupResponse{Metadata: emptyMetadata}, err
}

requestKey := bytes.Join([][]byte{
[]byte(req.ObjectRelation.String()),
[]byte(req.Subject.String()),
[]byte(req.Metadata.AtRevision),
}, []byte("-"))
for _, d := range req.DirectStack {
requestKey = bytes.Join([][]byte{
requestKey,
[]byte("d"),
[]byte(d.String()),
}, []byte("-"))
}
for _, t := range req.TtuStack {
requestKey = bytes.Join([][]byte{
requestKey,
[]byte("t"),
[]byte(t.String()),
}, []byte("-"))
}
ctx = context.WithValue(ctx, balancer.CtxKey, requestKey)
ctx = context.WithValue(ctx, balancer.CtxKey, []byte(dispatch.LookupRequestToKey(req)))
resp, err := cr.clusterClient.DispatchLookup(ctx, req)
if err != nil {
return &v1.DispatchLookupResponse{Metadata: requestFailureMetadata}, err
Expand Down

0 comments on commit 298a9a9

Please sign in to comment.