-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
453 lines (407 loc) · 11 KB
/
Copy pathdriver.go
File metadata and controls
453 lines (407 loc) · 11 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package entcache
import (
"context"
stdsql "database/sql"
"database/sql/driver"
"errors"
"fmt"
"strings"
"sync/atomic"
"time"
_ "unsafe"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"github.com/mitchellh/hashstructure/v2"
)
type (
// Options wraps the basic configuration cache options.
Options struct {
// TTL defines the period of time that an Entry
// is valid in the cache (used for hash-addressed queries).
TTL time.Duration
// KeyTTL defines the period of time that a key-addressed Entry
// (e.g. Get-by-ID queries) is valid in the cache. Key-addressed
// queries can have a longer TTL because they are precisely
// invalidated via the ChangeSet. If zero, TTL is used.
KeyTTL time.Duration
// Cache defines the GetAddDeleter (cache implementation)
// for holding the cache entries.
Cache AddGetDeleter
// Hash defines an optional Hash function for converting
// a query and its arguments to a cache key. If no Hash
// function was provided, the DefaultHash is used.
Hash func(query string, args []any) (Key, error)
// ChangeSet holds the mutation change tracker. When set,
// the Driver checks whether cached entries have been
// invalidated by mutations before returning them.
ChangeSet *ChangeSet
// Logf function. If provided, the Driver will call it with
// errors that can not be handled.
Log func(...any)
}
// Option allows configuring the cache
// driver using functional options.
Option func(*Options)
// A Driver is an SQL cached client. Users should use the
// constructor below for creating new driver.
Driver struct {
dialect.Driver
*Options
stats Stats
}
)
// NewDriver returns a new Driver given an existing driver and optional
// configuration functions.
func NewDriver(drv dialect.Driver, opts ...Option) *Driver {
options := &Options{Hash: DefaultHash}
for _, opt := range opts {
opt(options)
}
d := &Driver{
Driver: drv,
Options: options,
}
if inv, ok := d.Cache.(Invalidator); ok {
_ = inv.WatchInvalidations(context.Background(), func(k Key) {
if d.ChangeSet != nil {
d.ChangeSet.Mark(k)
}
})
}
return d
}
// TTL configures the period of time that an Entry is valid in the cache.
func TTL(ttl time.Duration) Option {
return func(o *Options) {
o.TTL = ttl
}
}
// WithKeyTTL configures a separate TTL for key-addressed queries (e.g. Get-by-ID).
func WithKeyTTL(ttl time.Duration) Option {
return func(o *Options) {
o.KeyTTL = ttl
}
}
// Hash configures an optional Hash function for converting query + args to cache key.
func Hash(hash func(query string, args []any) (Key, error)) Option {
return func(o *Options) {
o.Hash = hash
}
}
// Levels configures the Driver to work with the given cache levels.
func Levels(levels ...Cache) Option {
return func(o *Options) {
if len(levels) == 1 {
o.Cache = levels[0]
} else {
o.Cache = &multiLevel{levels: levels}
}
}
}
type contextLevelCache struct{}
func (c *contextLevelCache) Get(ctx context.Context, k Key) (*Entry, error) {
m, ok := FromContext(ctx)
if !ok {
return nil, ErrNotFound
}
return m.Get(ctx, k)
}
func (c *contextLevelCache) Add(ctx context.Context, k Key, e *Entry, ttl time.Duration) error {
m, ok := FromContext(ctx)
if !ok {
return nil
}
return m.Add(ctx, k, e, ttl)
}
func (c *contextLevelCache) Del(ctx context.Context, k Key) error {
m, ok := FromContext(ctx)
if !ok {
return nil
}
return m.Del(ctx, k)
}
// ContextLevel configures the driver to work with context/request level cache.
func ContextLevel() Option {
return func(o *Options) {
o.Cache = &contextLevelCache{}
}
}
// WithChangeSet configures the Driver to use the given ChangeSet for
// mutation-aware cache invalidation.
func WithChangeSet(cs *ChangeSet) Option {
return func(o *Options) {
o.ChangeSet = cs
}
}
// Query implements the Querier interface for the driver.
func (d *Driver) Query(ctx context.Context, query string, args, v any) error {
if d.Cache == nil {
return d.Driver.Query(ctx, query, args, v)
}
if !strings.HasPrefix(query, "SELECT") && !strings.HasPrefix(query, "select") {
return d.Driver.Query(ctx, query, args, v)
}
vr, ok := v.(*sql.Rows)
if !ok {
return fmt.Errorf("entcache: invalid type %T. expect *sql.Rows", v)
}
argv, ok := args.([]any)
if !ok {
return fmt.Errorf("entcache: invalid type %T. expect []interface{} for args", args)
}
opts, err := d.optionsFromContext(ctx, query, argv)
if err != nil {
return d.Driver.Query(ctx, query, args, v)
}
atomic.AddUint64(&d.stats.Gets, 1)
// Check cache, with ChangeSet-aware invalidation.
e, cacheErr := d.Cache.Get(ctx, opts.key)
if cacheErr == nil && d.ChangeSet != nil && opts.ref {
if d.ChangeSet.Changed(opts.key, time.Now().Add(-opts.ttl)) {
_ = d.Cache.Del(ctx, opts.key)
d.ChangeSet.Clear(opts.key)
cacheErr = ErrNotFound
}
}
if cacheErr == nil {
atomic.AddUint64(&d.stats.Hits, 1)
vr.ColumnScanner = &repeater{columns: e.Columns, values: e.Values}
return nil
}
if cacheErr != ErrNotFound {
return d.Driver.Query(ctx, query, args, v)
}
// Cache Miss: execute query with built-in stampede protection.
return d.queryWithStampedeLock(ctx, query, args, vr, opts)
}
func (d *Driver) queryWithStampedeLock(ctx context.Context, query string, args any, vr *sql.Rows, opts ctxOptions) error {
var releaseFunc func(context.Context)
// If backend implements StampedeLocker
if locker, ok := d.Cache.(StampedeLocker); ok {
retry_lock:
won, wait, release, err := locker.LockOrWait(ctx, opts.key)
if err == nil {
if !won {
// Another node/caller is loading — block on wait() until populated
e, err := wait(ctx)
if errors.Is(err, ErrRetryLocker) {
goto retry_lock
}
if err == nil && e != nil {
atomic.AddUint64(&d.stats.Hits, 1)
vr.ColumnScanner = &repeater{columns: e.Columns, values: e.Values}
return nil
}
// Fallback to querying DB if wait failed or key was cleared
} else if release != nil {
releaseFunc = release
}
}
}
if err := d.Driver.Query(ctx, query, args, vr); err != nil {
if releaseFunc != nil {
releaseFunc(context.Background())
}
return err
}
vr.ColumnScanner = &recorder{
ColumnScanner: vr.ColumnScanner,
skipNotFound: opts.skipNotFound,
releaseFunc: releaseFunc,
onClose: func(columns []string, values [][]driver.Value) {
err := d.Cache.Add(ctx, opts.key, &Entry{Columns: columns, Values: values}, opts.ttl)
if err != nil && d.Log != nil {
atomic.AddUint64(&d.stats.Errors, 1)
d.Log(fmt.Sprintf("entcache: failed storing entry %v in cache: %v", opts.key, err))
}
if d.ChangeSet != nil && opts.ref {
d.ChangeSet.Clear(opts.key)
}
},
}
return nil
}
// Stats returns a copy of the cache statistics.
func (d *Driver) Stats() Stats {
return Stats{
Gets: atomic.LoadUint64(&d.stats.Gets),
Hits: atomic.LoadUint64(&d.stats.Hits),
Errors: atomic.LoadUint64(&d.stats.Errors),
}
}
// QueryContext calls QueryContext of underlying driver.
func (d *Driver) QueryContext(ctx context.Context, query string, args ...any) (*stdsql.Rows, error) {
drv, ok := d.Driver.(interface {
QueryContext(context.Context, string, ...any) (*stdsql.Rows, error)
})
if !ok {
return nil, fmt.Errorf("Driver.QueryContext is not supported")
}
return drv.QueryContext(ctx, query, args...)
}
// ExecContext calls ExecContext of underlying driver.
func (d *Driver) ExecContext(ctx context.Context, query string, args ...any) (stdsql.Result, error) {
drv, ok := d.Driver.(interface {
ExecContext(context.Context, string, ...any) (stdsql.Result, error)
})
if !ok {
return nil, fmt.Errorf("Driver.ExecContext is not supported")
}
return drv.ExecContext(ctx, query, args...)
}
var errSkip = errors.New("entcache: skip cache")
func (d *Driver) optionsFromContext(ctx context.Context, query string, args []any) (ctxOptions, error) {
var opts ctxOptions
if c, ok := ctx.Value(ctxOptionsKey).(*ctxOptions); ok {
opts = *c
}
if opts.key == nil {
key, err := d.Hash(query, args)
if err != nil {
return opts, errSkip
}
opts.key = key
}
if opts.ttl == 0 {
if opts.ref && d.KeyTTL > 0 {
opts.ttl = d.KeyTTL
} else {
opts.ttl = d.TTL
}
}
if opts.evict {
if err := d.Cache.Del(ctx, opts.key); err != nil {
return opts, err
}
}
if opts.skip {
return opts, errSkip
}
return opts, nil
}
// DefaultHash provides the default implementation for converting a query + args to a cache key.
func DefaultHash(query string, args []any) (Key, error) {
key, err := hashstructure.Hash(struct {
Q string
A []any
}{
Q: query,
A: args,
}, hashstructure.FormatV2, nil)
if err != nil {
return nil, err
}
return key, nil
}
// Stats represents the cache statistics of the driver.
type Stats struct {
Gets uint64
Hits uint64
Errors uint64
}
type rawCopy struct {
values []driver.Value
}
func (c *rawCopy) Scan(src interface{}) error {
if b, ok := src.([]byte); ok {
b1 := make([]byte, len(b))
copy(b1, b)
src = b1
}
c.values[0] = src
c.values = c.values[1:]
return nil
}
type recorder struct {
sql.ColumnScanner
values [][]driver.Value
columns []string
done bool
skipNotFound bool
onClose func([]string, [][]driver.Value)
releaseFunc func(context.Context)
}
func (r *recorder) Next() bool {
hasNext := r.ColumnScanner.Next()
r.done = !hasNext
return hasNext
}
func (r *recorder) Scan(dest ...any) error {
values := make([]driver.Value, len(dest))
args := make([]any, len(dest))
c := &rawCopy{values: values}
for i := range args {
args[i] = c
}
if err := r.ColumnScanner.Scan(args...); err != nil {
return err
}
for i := range values {
if err := convertAssign(dest[i], values[i]); err != nil {
return err
}
}
r.values = append(r.values, values)
return nil
}
func (r *recorder) Columns() ([]string, error) {
columns, err := r.ColumnScanner.Columns()
if err != nil {
return nil, err
}
r.columns = columns
return columns, nil
}
func (r *recorder) Close() error {
if r.releaseFunc != nil {
release := r.releaseFunc
r.releaseFunc = nil
defer release(context.Background())
}
if err := r.ColumnScanner.Close(); err != nil {
return err
}
if err := r.ColumnScanner.Err(); err == nil || r.done {
if r.skipNotFound && len(r.values) == 0 {
return nil
}
r.onClose(r.columns, r.values)
}
return nil
}
type repeater struct {
columns []string
values [][]driver.Value
}
func (*repeater) Close() error {
return nil
}
func (*repeater) ColumnTypes() ([]*stdsql.ColumnType, error) {
return nil, fmt.Errorf("entcache.ColumnTypes is not supported")
}
func (r *repeater) Columns() ([]string, error) {
return r.columns, nil
}
func (*repeater) Err() error {
return nil
}
func (r *repeater) Next() bool {
return len(r.values) > 0
}
func (r *repeater) NextResultSet() bool {
return len(r.values) > 0
}
func (r *repeater) Scan(dest ...any) error {
if !r.Next() {
return stdsql.ErrNoRows
}
for i, src := range r.values[0] {
if err := convertAssign(dest[i], src); err != nil {
return err
}
}
r.values = r.values[1:]
return nil
}
//go:linkname convertAssign database/sql.convertAssign
func convertAssign(dest, src any) error