/
caching.go
151 lines (126 loc) · 3.92 KB
/
caching.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
package proxy
import (
"context"
"errors"
"fmt"
"sync"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog/log"
"github.com/shopspring/decimal"
"golang.org/x/sync/singleflight"
"google.golang.org/protobuf/proto"
"github.com/authzed/spicedb/pkg/cache"
"github.com/authzed/spicedb/pkg/datastore"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
// NewCachingDatastoreProxy creates a new datastore proxy which caches namespace definitions that
// are loaded at specific datastore revisions.
func NewCachingDatastoreProxy(
delegate datastore.Datastore,
cacheConfig *cache.Config,
) (datastore.Datastore, error) {
if cacheConfig == nil {
cacheConfig = &cache.Config{
NumCounters: 1e4, // number of keys to track frequency of (10k).
MaxCost: 1 << 24, // maximum cost of cache (16MB).
BufferItems: 64, // number of keys per Get buffer.
}
} else {
log.Info().Int64("numCounters", cacheConfig.NumCounters).Str("maxCost", humanize.Bytes(uint64(cacheConfig.MaxCost))).Msg("configured caching namespace manager")
}
cache, err := cache.NewCache(cacheConfig)
if err != nil {
return nil, fmt.Errorf("unable to create cache: %w", err)
}
return &nsCachingProxy{
Datastore: delegate,
c: cache,
}, nil
}
type nsCachingProxy struct {
datastore.Datastore
c cache.Cache
readNsGroup singleflight.Group
}
func (p *nsCachingProxy) SnapshotReader(rev datastore.Revision) datastore.Reader {
delegateReader := p.Datastore.SnapshotReader(rev)
return &nsCachingReader{delegateReader, sync.Mutex{}, rev, p}
}
func (p *nsCachingProxy) ReadWriteTx(
ctx context.Context,
f datastore.TxUserFunc,
) (decimal.Decimal, error) {
return p.Datastore.ReadWriteTx(ctx, func(ctx context.Context, delegateRWT datastore.ReadWriteTransaction) error {
rwt := &nsCachingRWT{delegateRWT, &sync.Map{}}
return f(ctx, rwt)
})
}
type nsCachingReader struct {
datastore.Reader
sync.Mutex
rev datastore.Revision
p *nsCachingProxy
}
func (r *nsCachingReader) ReadNamespace(
ctx context.Context,
nsName string,
) (*core.NamespaceDefinition, datastore.Revision, error) {
// Check the nsCache.
nsRevisionKey := fmt.Sprintf("%s@%s", nsName, r.rev)
loadedRaw, found := r.p.c.Get(nsRevisionKey)
if !found {
// We couldn't use the cached entry, load one
var err error
loadedRaw, err, _ = r.p.readNsGroup.Do(nsRevisionKey, func() (interface{}, error) {
loaded, updatedRev, err := r.Reader.ReadNamespace(ctx, nsName)
if err != nil && !errors.Is(err, &datastore.ErrNamespaceNotFound{}) {
// Propagate this error to the caller
return nil, err
}
entry := &cacheEntry{loaded, updatedRev, err}
// Save it to the nsCache
r.p.c.Set(nsRevisionKey, entry, int64(proto.Size(loaded)))
// We have to call wait here or else Ristretto may not have the key available to a
// subsequent caller.
r.p.c.Wait()
return entry, nil
})
if err != nil {
return nil, datastore.NoRevision, err
}
}
loaded := loadedRaw.(*cacheEntry)
return loaded.def, loaded.updated, loaded.notFound
}
type nsCachingRWT struct {
datastore.ReadWriteTransaction
namespaceCache *sync.Map
}
func (rwt *nsCachingRWT) ReadNamespace(
ctx context.Context,
nsName string,
) (*core.NamespaceDefinition, datastore.Revision, error) {
untypedEntry, ok := rwt.namespaceCache.Load(nsName)
var entry cacheEntry
if ok {
entry = untypedEntry.(cacheEntry)
} else {
loaded, updatedRev, err := rwt.ReadWriteTransaction.ReadNamespace(ctx, nsName)
if err != nil && !errors.Is(err, &datastore.ErrNamespaceNotFound{}) {
// Propagate this error to the caller
return nil, datastore.NoRevision, err
}
entry = cacheEntry{loaded, updatedRev, err}
rwt.namespaceCache.Store(nsName, entry)
}
return entry.def, entry.updated, entry.notFound
}
type cacheEntry struct {
def *core.NamespaceDefinition
updated datastore.Revision
notFound error
}
var (
_ datastore.Datastore = &nsCachingProxy{}
_ datastore.Reader = &nsCachingReader{}
)