-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathcrdb.go
434 lines (360 loc) · 12.1 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
package crdb
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"time"
"github.com/IBM/pgxpoolprometheus"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/prometheus/client_golang/prometheus"
"github.com/shopspring/decimal"
"go.opentelemetry.io/otel"
"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/internal/datastore/common/revisions"
"github.com/authzed/spicedb/internal/datastore/crdb/migrations"
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/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;"
querySetTransactionTime = "SET TRANSACTION AS OF SYSTEM TIME %s"
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)
}
poolConfig, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
configurePool(config, poolConfig)
initCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
pool, err := pgxpool.ConnectConfig(initCtx, poolConfig)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
var version crdbVersion
if err := queryServerVersion(initCtx, pool, &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
}
if config.enablePrometheusStats {
collector := pgxpoolprometheus.NewCollector(pool, map[string]string{"db_name": "spicedb"})
if err := prometheus.Register(collector); err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
if err := common.RegisterGCMetrics(); err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
}
clusterTTLNanos, err := readClusterTTLNanos(initCtx, pool)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
gcWindowNanos := config.gcWindow.Nanoseconds()
if clusterTTLNanos < gcWindowNanos {
return nil, fmt.Errorf(
errUnableToInstantiate,
fmt.Errorf("cluster gc window is less than requested gc window %d < %d",
clusterTTLNanos,
gcWindowNanos,
),
)
}
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 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{
revisions.NewRemoteClockRevisions(
config.gcWindow,
maxRevisionStaleness,
config.followerReadDelay,
config.revisionQuantization,
),
revision.DecimalDecoder{},
url,
pool,
config.watchBufferLength,
keyer,
config.splitAtUsersetCount,
executeWithMaxRetries(config.maxRetries),
config.disableStats,
changefeedQuery,
}
ds.RemoteClockRevisions.SetNowFunc(ds.headRevisionInternal)
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
}
func configurePool(config crdbOptions, pgxConfig *pgxpool.Config) {
if config.maxOpenConns != nil {
pgxConfig.MaxConns = int32(*config.maxOpenConns)
}
if config.minOpenConns != nil {
pgxConfig.MinConns = int32(*config.minOpenConns)
}
if pgxConfig.MaxConns > 0 && pgxConfig.MinConns > 0 && pgxConfig.MaxConns < pgxConfig.MinConns {
log.Warn().Int32("max-connections", pgxConfig.MaxConns).Int32("min-connections", pgxConfig.MinConns).Msg("maximum number of connections configured is less than minimum number of connections; minimum will be used")
}
if config.connMaxIdleTime != nil {
pgxConfig.MaxConnIdleTime = *config.connMaxIdleTime
}
if config.connMaxLifetime != nil {
pgxConfig.MaxConnLifetime = *config.connMaxLifetime
}
if config.connHealthCheckInterval != nil {
pgxConfig.HealthCheckPeriod = *config.connHealthCheckInterval
}
pgxcommon.ConfigurePGXLogger(pgxConfig.ConnConfig)
}
type crdbDatastore struct {
*revisions.RemoteClockRevisions
revision.DecimalDecoder
dburl string
pool *pgxpool.Pool
watchBufferLength uint16
writeOverlapKeyer overlapKeyer
usersetBatchSize uint16
execute executeTxRetryFunc
disableStats bool
beginChangefeedQuery string
}
func (cds *crdbDatastore) SnapshotReader(rev datastore.Revision) datastore.Reader {
createTxFunc := func(ctx context.Context) (pgx.Tx, common.TxCleanupFunc, error) {
tx, err := cds.pool.BeginTx(ctx, pgx.TxOptions{AccessMode: pgx.ReadOnly})
if err != nil {
return nil, nil, err
}
cleanup := func(ctx context.Context) {
if err := tx.Rollback(ctx); err != nil {
log.Ctx(ctx).Err(err).Msg("error rolling back transaction")
}
}
setTxTime := fmt.Sprintf(querySetTransactionTime, rev)
if _, err := tx.Exec(ctx, setTxTime); err != nil {
if err := tx.Rollback(ctx); err != nil {
log.Warn().Err(err).Msg(
"error rolling back transaction after failing to set transaction time",
)
}
return nil, nil, err
}
return tx, cleanup, nil
}
querySplitter := common.TupleQuerySplitter{
Executor: pgxcommon.NewPGXExecutor(createTxFunc),
UsersetBatchSize: cds.usersetBatchSize,
}
return &crdbReader{createTxFunc, querySplitter, noOverlapKeyer, nil, cds.execute}
}
func noCleanup(context.Context) {}
func (cds *crdbDatastore) ReadWriteTx(
ctx context.Context,
f datastore.TxUserFunc,
) (datastore.Revision, error) {
var commitTimestamp revision.Decimal
if err := cds.execute(ctx, func(ctx context.Context) error {
return cds.pool.BeginTxFunc(ctx, pgx.TxOptions{}, func(tx pgx.Tx) error {
longLivedTx := func(context.Context) (pgx.Tx, common.TxCleanupFunc, error) {
return tx, noCleanup, nil
}
querySplitter := common.TupleQuerySplitter{
Executor: pgxcommon.NewPGXExecutor(longLivedTx),
UsersetBatchSize: cds.usersetBatchSize,
}
rwt := &crdbReadWriteTXN{
&crdbReader{
longLivedTx,
querySplitter,
cds.writeOverlapKeyer,
make(keySet),
executeOnce,
},
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, tx)
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
})
}); err != nil {
return datastore.NoRevision, err
}
return commitTimestamp, nil
}
func (cds *crdbDatastore) IsReady(ctx context.Context) (bool, error) {
headMigration, err := migrations.CRDBMigrations.HeadRevision()
if err != nil {
return false, fmt.Errorf("invalid head migration found for postgres: %w", err)
}
currentRevision, err := migrations.NewCRDBDriver(cds.dburl)
if err != nil {
return false, err
}
defer currentRevision.Close(ctx)
version, err := currentRevision.Version(ctx)
if err != nil {
return false, err
}
return version == headMigration, nil
}
func (cds *crdbDatastore) Close() error {
cds.pool.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
err := cds.execute(ctx, func(ctx context.Context) error {
return cds.pool.BeginTxFunc(ctx, pgx.TxOptions{AccessMode: pgx.ReadOnly}, func(tx pgx.Tx) error {
var fnErr error
hlcNow, fnErr = readCRDBNow(ctx, tx)
if fnErr != nil {
hlcNow = revision.NoRevision
return fmt.Errorf(errRevision, fnErr)
}
return nil
})
})
return hlcNow, err
}
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)
_, err = cds.pool.Exec(streamCtx, fmt.Sprintf(cds.beginChangefeedQuery, tableTuple, head))
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())
}
<-streamCtx.Done()
return &features, nil
}
func readCRDBNow(ctx context.Context, tx pgx.Tx) (revision.Decimal, error) {
ctx, span := tracer.Start(ctx, "readCRDBNow")
defer span.End()
var hlcNow decimal.Decimal
if err := tx.QueryRow(ctx, querySelectNow).Scan(&hlcNow); err != nil {
return revision.NoRevision, fmt.Errorf("unable to read timestamp: %w", err)
}
return revision.NewFromDecimal(hlcNow), nil
}
func readClusterTTLNanos(ctx context.Context, conn *pgxpool.Pool) (int64, error) {
var target, configSQL string
if err := conn.
QueryRow(ctx, queryShowZoneConfig).
Scan(&target, &configSQL); 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()))
}