-
Notifications
You must be signed in to change notification settings - Fork 246
/
crdb.go
493 lines (419 loc) · 14.6 KB
/
crdb.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package crdb
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"time"
"github.com/IBM/pgxpoolprometheus"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/prometheus/client_golang/prometheus"
"github.com/shopspring/decimal"
"go.opentelemetry.io/otel"
"golang.org/x/sync/errgroup"
"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/internal/datastore/common/revisions"
"github.com/authzed/spicedb/internal/datastore/crdb/migrations"
"github.com/authzed/spicedb/internal/datastore/crdb/pool"
pgxcommon "github.com/authzed/spicedb/internal/datastore/postgres/common"
"github.com/authzed/spicedb/internal/datastore/proxy"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/options"
"github.com/authzed/spicedb/pkg/datastore/revision"
)
func init() {
datastore.Engines = append(datastore.Engines, Engine)
}
var (
psql = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
gcTTLRegex = regexp.MustCompile(`gc\.ttlseconds\s*=\s*([1-9][0-9]+)`)
tracer = otel.Tracer("spicedb/internal/datastore/crdb")
)
const (
Engine = "cockroachdb"
tableNamespace = "namespace_config"
tableTuple = "relation_tuple"
tableTransactions = "transactions"
tableCaveat = "caveat"
colNamespace = "namespace"
colConfig = "serialized_config"
colTimestamp = "timestamp"
colTransactionKey = "key"
colObjectID = "object_id"
colRelation = "relation"
colUsersetNamespace = "userset_namespace"
colUsersetObjectID = "userset_object_id"
colUsersetRelation = "userset_relation"
colCaveatName = "name"
colCaveatDefinition = "definition"
colCaveatContextName = "caveat_name"
colCaveatContext = "caveat_context"
errUnableToInstantiate = "unable to instantiate datastore: %w"
errRevision = "unable to find revision: %w"
querySelectNow = "SELECT cluster_logical_timestamp()"
queryShowZoneConfig = "SHOW ZONE CONFIGURATION FOR RANGE default;"
livingTupleConstraint = "pk_relation_tuple"
)
func newCRDBDatastore(url string, options ...Option) (datastore.Datastore, error) {
config, err := generateConfig(options)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
readPoolConfig, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
config.readPoolOpts.ConfigurePgx(readPoolConfig)
writePoolConfig, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
config.writePoolOpts.ConfigurePgx(writePoolConfig)
initCtx, initCancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer initCancel()
healthChecker, err := pool.NewNodeHealthChecker(url)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
// The initPool is a 1-connection pool that is only used for setup tasks.
// The actual pools are not given the initCtx, since cancellation can
// interfere with pool setup.
initPoolConfig := readPoolConfig.Copy()
initPoolConfig.MinConns = 1
initPool, err := pool.NewRetryPool(initCtx, "init", initPoolConfig, healthChecker, config.maxRetries, config.connectRate)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
defer initPool.Close()
var version crdbVersion
if err := queryServerVersion(initCtx, initPool, &version); err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
changefeedQuery := queryChangefeed
if version.Major < 22 {
log.Info().Object("version", version).Msg("using changefeed query for CRDB version < 22")
changefeedQuery = queryChangefeedPreV22
}
clusterTTLNanos, err := readClusterTTLNanos(initCtx, initPool)
if err != nil {
return nil, fmt.Errorf("unable to read cluster gc window: %w", err)
}
gcWindowNanos := config.gcWindow.Nanoseconds()
if clusterTTLNanos < gcWindowNanos {
log.Warn().
Int64("cockroach_cluster_gc_window_nanos", clusterTTLNanos).
Int64("spicedb_gc_window_nanos", gcWindowNanos).
Msg("configured CockroachDB cluster gc window is less than configured SpiceDB gc window, falling back to CRDB value - see https://spicedb.dev/d/crdb-gc-window-warning")
config.gcWindow = time.Duration(clusterTTLNanos) * time.Nanosecond
}
keySetInit := newKeySet
var keyer overlapKeyer
switch config.overlapStrategy {
case overlapStrategyStatic:
if len(config.overlapKey) == 0 {
return nil, fmt.Errorf(
errUnableToInstantiate,
fmt.Errorf("static tx overlap strategy specified without an overlap key"),
)
}
keyer = appendStaticKey(config.overlapKey)
case overlapStrategyPrefix:
keyer = prefixKeyer
case overlapStrategyRequest:
// overlap keys are computed over requests and not data
keyer = noOverlapKeyer
keySetInit = overlapKeysFromContext
case overlapStrategyInsecure:
log.Warn().Str("strategy", overlapStrategyInsecure).
Msg("running in this mode is only safe when replicas == nodes")
keyer = noOverlapKeyer
}
maxRevisionStaleness := time.Duration(float64(config.revisionQuantization.Nanoseconds())*
config.maxRevisionStalenessPercent) * time.Nanosecond
ds := &crdbDatastore{
RemoteClockRevisions: revisions.NewRemoteClockRevisions(
config.gcWindow,
maxRevisionStaleness,
config.followerReadDelay,
config.revisionQuantization,
),
DecimalDecoder: revision.DecimalDecoder{},
dburl: url,
watchBufferLength: config.watchBufferLength,
writeOverlapKeyer: keyer,
overlapKeyInit: keySetInit,
usersetBatchSize: config.splitAtUsersetCount,
disableStats: config.disableStats,
beginChangefeedQuery: changefeedQuery,
}
ds.RemoteClockRevisions.SetNowFunc(ds.headRevisionInternal)
// this ctx and cancel is tied to the lifetime of the datastore
ds.ctx, ds.cancel = context.WithCancel(context.Background())
ds.writePool, err = pool.NewRetryPool(ds.ctx, "write", writePoolConfig, healthChecker, config.maxRetries, config.connectRate)
if err != nil {
ds.cancel()
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
ds.readPool, err = pool.NewRetryPool(ds.ctx, "read", readPoolConfig, healthChecker, config.maxRetries, config.connectRate)
if err != nil {
ds.cancel()
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
if config.enablePrometheusStats {
if err := prometheus.Register(pgxpoolprometheus.NewCollector(ds.writePool, map[string]string{
"db_name": "spicedb",
"pool_usage": "write",
})); err != nil {
ds.cancel()
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
if err := prometheus.Register(pgxpoolprometheus.NewCollector(ds.readPool, map[string]string{
"db_name": "spicedb",
"pool_usage": "read",
})); err != nil {
ds.cancel()
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
}
// TODO: this (and the GC startup that it's based on for mysql/pg) should
// be removed and have the lifetimes tied to server start/stop.
// Start goroutines for pruning
if config.enableConnectionBalancing {
log.Ctx(initCtx).Info().Msg("starting cockroach connection balancer")
ds.pruneGroup, ds.ctx = errgroup.WithContext(ds.ctx)
writePoolBalancer := pool.NewNodeConnectionBalancer(ds.writePool, healthChecker, 5*time.Second)
readPoolBalancer := pool.NewNodeConnectionBalancer(ds.readPool, healthChecker, 5*time.Second)
ds.pruneGroup.Go(func() error {
writePoolBalancer.Prune(ds.ctx)
return nil
})
ds.pruneGroup.Go(func() error {
readPoolBalancer.Prune(ds.ctx)
return nil
})
ds.pruneGroup.Go(func() error {
healthChecker.Poll(ds.ctx, 5*time.Second)
return nil
})
}
return ds, nil
}
// NewCRDBDatastore initializes a SpiceDB datastore that uses a CockroachDB
// database while leveraging its AOST functionality.
func NewCRDBDatastore(url string, options ...Option) (datastore.Datastore, error) {
ds, err := newCRDBDatastore(url, options...)
if err != nil {
return nil, err
}
return proxy.NewSeparatingContextDatastoreProxy(ds), nil
}
type crdbDatastore struct {
*revisions.RemoteClockRevisions
revision.DecimalDecoder
dburl string
readPool, writePool *pool.RetryPool
watchBufferLength uint16
writeOverlapKeyer overlapKeyer
overlapKeyInit func(ctx context.Context) keySet
usersetBatchSize uint16
disableStats bool
beginChangefeedQuery string
pruneGroup *errgroup.Group
ctx context.Context
cancel context.CancelFunc
}
func (cds *crdbDatastore) SnapshotReader(rev datastore.Revision) datastore.Reader {
querySplitter := common.TupleQuerySplitter{
Executor: pgxcommon.NewPGXExecutor(cds.readPool),
UsersetBatchSize: cds.usersetBatchSize,
}
fromBuilder := func(query sq.SelectBuilder, fromStr string) sq.SelectBuilder {
return query.From(fromStr + " AS OF SYSTEM TIME " + rev.String())
}
return &crdbReader{cds.readPool, querySplitter, noOverlapKeyer, nil, fromBuilder}
}
func (cds *crdbDatastore) ReadWriteTx(
ctx context.Context,
f datastore.TxUserFunc,
opts ...options.RWTOptionsOption,
) (datastore.Revision, error) {
var commitTimestamp revision.Decimal
config := options.NewRWTOptionsWithOptions(opts...)
if config.DisableRetries {
ctx = context.WithValue(ctx, pool.CtxDisableRetries, true)
}
err := cds.writePool.BeginFunc(ctx, func(tx pgx.Tx) error {
querier := pgxcommon.QuerierFuncsFor(tx)
querySplitter := common.TupleQuerySplitter{
Executor: pgxcommon.NewPGXExecutor(querier),
UsersetBatchSize: cds.usersetBatchSize,
}
rwt := &crdbReadWriteTXN{
&crdbReader{
querier,
querySplitter,
cds.writeOverlapKeyer,
cds.overlapKeyInit(ctx),
func(query sq.SelectBuilder, fromStr string) sq.SelectBuilder {
return query.From(fromStr)
},
},
tx,
0,
}
if err := f(rwt); err != nil {
return err
}
// Touching the transaction key happens last so that the "write intent" for
// the transaction as a whole lands in a range for the affected tuples.
for k := range rwt.overlapKeySet {
if _, err := tx.Exec(ctx, queryTouchTransaction, k); err != nil {
return fmt.Errorf("error writing overlapping keys: %w", err)
}
}
if cds.disableStats {
var err error
commitTimestamp, err = readCRDBNow(ctx, querier)
if err != nil {
return fmt.Errorf("error getting commit timestamp: %w", err)
}
return nil
}
var err error
commitTimestamp, err = updateCounter(ctx, tx, rwt.relCountChange)
if err != nil {
return fmt.Errorf("error updating relationship counter: %w", err)
}
return nil
})
if err != nil {
return datastore.NoRevision, err
}
return commitTimestamp, nil
}
func (cds *crdbDatastore) ReadyState(ctx context.Context) (datastore.ReadyState, error) {
headMigration, err := migrations.CRDBMigrations.HeadRevision()
if err != nil {
return datastore.ReadyState{}, fmt.Errorf("invalid head migration found for cockroach: %w", err)
}
currentRevision, err := migrations.NewCRDBDriver(cds.dburl)
if err != nil {
return datastore.ReadyState{}, err
}
defer currentRevision.Close(ctx)
version, err := currentRevision.Version(ctx)
if err != nil {
return datastore.ReadyState{}, err
}
if version != headMigration {
return datastore.ReadyState{
Message: fmt.Sprintf(
"datastore is not migrated: currently at revision `%s`, but requires `%s`. Please run `spicedb migrate`.",
version,
headMigration,
),
IsReady: false,
}, nil
}
readMin := cds.readPool.MinConns()
if readMin > 0 {
readMin--
}
writeMin := cds.writePool.MinConns()
if writeMin > 0 {
writeMin--
}
writeTotal := uint32(cds.writePool.Stat().TotalConns())
readTotal := uint32(cds.readPool.Stat().TotalConns())
if writeTotal < writeMin || readTotal < readMin {
return datastore.ReadyState{
Message: fmt.Sprintf(
"spicedb does not have the required minimum connection count to the datastore. Read: %d/%d, Write: %d/%d",
readTotal,
readMin,
writeTotal,
writeMin,
),
IsReady: false,
}, nil
}
return datastore.ReadyState{IsReady: true}, nil
}
func (cds *crdbDatastore) Close() error {
cds.cancel()
cds.readPool.Close()
cds.writePool.Close()
return nil
}
func (cds *crdbDatastore) HeadRevision(ctx context.Context) (datastore.Revision, error) {
return cds.headRevisionInternal(ctx)
}
func (cds *crdbDatastore) headRevisionInternal(ctx context.Context) (revision.Decimal, error) {
var hlcNow revision.Decimal
var fnErr error
hlcNow, fnErr = readCRDBNow(ctx, cds.readPool)
if fnErr != nil {
return revision.NoRevision, fmt.Errorf(errRevision, fnErr)
}
return hlcNow, fnErr
}
func (cds *crdbDatastore) Features(ctx context.Context) (*datastore.Features, error) {
var features datastore.Features
head, err := cds.HeadRevision(ctx)
if err != nil {
return nil, err
}
// streams don't return at all if they succeed, so the only way to know
// it was created successfully is to wait a bit and then cancel
streamCtx, cancel := context.WithCancel(ctx)
defer cancel()
time.AfterFunc(1*time.Second, cancel)
_ = cds.writePool.ExecFunc(streamCtx, func(ctx context.Context, tag pgconn.CommandTag, err error) error {
if err != nil && errors.Is(err, context.Canceled) {
features.Watch.Enabled = true
features.Watch.Reason = ""
} else if err != nil {
features.Watch.Enabled = false
features.Watch.Reason = fmt.Sprintf("Range feeds must be enabled in CockroachDB and the user must have permission to create them in order to enable the Watch API: %s", err.Error())
}
return nil
}, fmt.Sprintf(cds.beginChangefeedQuery, tableTuple, head))
<-streamCtx.Done()
return &features, nil
}
func readCRDBNow(ctx context.Context, reader pgxcommon.DBFuncQuerier) (revision.Decimal, error) {
ctx, span := tracer.Start(ctx, "readCRDBNow")
defer span.End()
var hlcNow decimal.Decimal
if err := reader.QueryRowFunc(ctx, func(ctx context.Context, row pgx.Row) error {
return row.Scan(&hlcNow)
}, querySelectNow); err != nil {
return revision.NoRevision, fmt.Errorf("unable to read timestamp: %w", err)
}
return revision.NewFromDecimal(hlcNow), nil
}
func readClusterTTLNanos(ctx context.Context, conn pgxcommon.DBFuncQuerier) (int64, error) {
var target, configSQL string
if err := conn.QueryRowFunc(ctx, func(ctx context.Context, row pgx.Row) error {
return row.Scan(&target, &configSQL)
}, queryShowZoneConfig); err != nil {
return 0, err
}
groups := gcTTLRegex.FindStringSubmatch(configSQL)
if groups == nil || len(groups) != 2 {
return 0, fmt.Errorf("CRDB zone config unexpected format")
}
gcSeconds, err := strconv.ParseInt(groups[1], 10, 64)
if err != nil {
return 0, err
}
return gcSeconds * 1_000_000_000, nil
}
func revisionFromTimestamp(t time.Time) revision.Decimal {
return revision.NewFromDecimal(decimal.NewFromInt(t.UnixNano()))
}