-
Notifications
You must be signed in to change notification settings - Fork 245
/
crdb.go
303 lines (246 loc) · 8.65 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
package crdb
import (
"context"
"fmt"
"regexp"
"strconv"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/alecthomas/units"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/log/zerologadapter"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/rs/zerolog/log"
"github.com/shopspring/decimal"
"go.opentelemetry.io/otel"
"github.com/authzed/spicedb/internal/datastore"
"github.com/authzed/spicedb/internal/datastore/crdb/migrations"
)
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 (
tableNamespace = "namespace_config"
tableTuple = "relation_tuple"
tableTransactions = "transactions"
colNamespace = "namespace"
colConfig = "serialized_config"
colTimestamp = "timestamp"
colTransactionKey = "key"
colObjectID = "object_id"
colRelation = "relation"
colUsersetNamespace = "userset_namespace"
colUsersetObjectID = "userset_object_id"
colUsersetRelation = "userset_relation"
errUnableToInstantiate = "unable to instantiate datastore: %w"
errRevision = "unable to find revision: %w"
querySelectNow = "SELECT cluster_logical_timestamp()"
queryReturningTimestamp = "RETURNING cluster_logical_timestamp()"
queryShowZoneConfig = "SHOW ZONE CONFIGURATION FOR RANGE default;"
)
// NewCRDBDatastore initializes a SpiceDB datastore that uses a CockroachDB
// database while leveraging its AOST functionality.
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)
}
if config.maxOpenConns != nil {
poolConfig.MaxConns = int32(*config.maxOpenConns)
}
if config.minOpenConns != nil {
poolConfig.MinConns = int32(*config.minOpenConns)
}
if config.connMaxIdleTime != nil {
poolConfig.MaxConnIdleTime = *config.connMaxIdleTime
}
if config.connMaxLifetime != nil {
poolConfig.MaxConnLifetime = *config.connMaxLifetime
}
poolConfig.ConnConfig.Logger = zerologadapter.NewLogger(log.Logger)
conn, err := pgxpool.ConnectConfig(context.Background(), poolConfig)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
gcWindowNanos := config.gcWindow.Nanoseconds()
clusterTTLNanos, err := readClusterTTLNanos(conn)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}
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
return &crdbDatastore{
dburl: url,
conn: conn,
watchBufferLength: config.watchBufferLength,
quantizationNanos: config.revisionQuantization.Nanoseconds(),
maxRevisionStaleness: maxRevisionStaleness,
gcWindowNanos: gcWindowNanos,
splitAtEstimatedQuerySize: config.splitAtEstimatedQuerySize,
execute: executeWithMaxRetries(config.maxRetries),
overlapKeyer: keyer,
}, nil
}
type crdbDatastore struct {
dburl string
conn *pgxpool.Pool
watchBufferLength uint16
quantizationNanos int64
maxRevisionStaleness time.Duration
gcWindowNanos int64
splitAtEstimatedQuerySize units.Base2Bytes
execute executeTxRetryFunc
overlapKeyer overlapKeyer
lastQuantizedRevision decimal.Decimal
revisionValidThrough time.Time
}
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.Dispose()
version, err := currentRevision.Version()
if err != nil {
return false, err
}
return version == headMigration, nil
}
func (cds *crdbDatastore) Close() error {
cds.conn.Close()
return nil
}
func (cds *crdbDatastore) Revision(ctx context.Context) (datastore.Revision, error) {
ctx, span := tracer.Start(ctx, "Revision")
defer span.End()
localNow := time.Now()
if localNow.Before(cds.revisionValidThrough) {
log.Ctx(ctx).Debug().Time("now", localNow).Time("valid", cds.revisionValidThrough).Msg("returning cached revision")
return cds.lastQuantizedRevision, nil
}
log.Ctx(ctx).Debug().Time("now", localNow).Time("valid", cds.revisionValidThrough).Msg("computing new revision")
nowHLC, err := cds.SyncRevision(ctx)
if err != nil {
return datastore.NoRevision, err
}
// Round the revision down to the nearest quantization
crdbNow := nowHLC.IntPart()
quantized := crdbNow
if cds.quantizationNanos > 0 {
quantized -= (crdbNow % cds.quantizationNanos)
}
validForNanos := (quantized + cds.quantizationNanos) - crdbNow
cds.revisionValidThrough = localNow.
Add(time.Duration(validForNanos) * time.Nanosecond).
Add(cds.maxRevisionStaleness)
log.Ctx(ctx).Debug().Time("now", localNow).Time("valid", cds.revisionValidThrough).Int64("validForNanos", validForNanos).Msg("setting valid through")
cds.lastQuantizedRevision = decimal.NewFromInt(quantized)
return cds.lastQuantizedRevision, nil
}
func (cds *crdbDatastore) SyncRevision(ctx context.Context) (datastore.Revision, error) {
ctx, span := tracer.Start(datastore.SeparateContextWithTracing(ctx), "SyncRevision")
defer span.End()
tx, err := cds.conn.BeginTx(ctx, pgx.TxOptions{AccessMode: pgx.ReadOnly})
if err != nil {
return datastore.NoRevision, fmt.Errorf(errRevision, err)
}
defer tx.Rollback(ctx)
hlcNow, err := readCRDBNow(ctx, tx)
if err != nil {
return datastore.NoRevision, fmt.Errorf(errRevision, err)
}
return hlcNow, nil
}
func (cds *crdbDatastore) CheckRevision(ctx context.Context, revision datastore.Revision) error {
ctx, span := tracer.Start(ctx, "CheckRevision")
defer span.End()
// Make sure the system time indicated is within the software GC window
now, err := cds.SyncRevision(ctx)
if err != nil {
return err
}
nowNanos := now.IntPart()
revisionNanos := revision.IntPart()
staleRevision := revisionNanos < (nowNanos - cds.gcWindowNanos)
if staleRevision {
log.Ctx(ctx).Debug().Stringer("now", now).Stringer("revision", revision).Msg("stale revision")
return datastore.NewInvalidRevisionErr(revision, datastore.RevisionStale)
}
futureRevision := revisionNanos > nowNanos
if futureRevision {
log.Ctx(ctx).Debug().Stringer("now", now).Stringer("revision", revision).Msg("future revision")
return datastore.NewInvalidRevisionErr(revision, datastore.RevisionInFuture)
}
return nil
}
func (cds *crdbDatastore) AddOverlapKey(keySet map[string]struct{}, namespace string) {
cds.overlapKeyer.AddKey(keySet, namespace)
}
func readCRDBNow(ctx context.Context, tx pgx.Tx) (decimal.Decimal, error) {
ctx, span := tracer.Start(ctx, "readCRDBNow")
defer span.End()
var hlcNow decimal.Decimal
if err := tx.QueryRow(
datastore.SeparateContextWithTracing(ctx),
querySelectNow,
).Scan(&hlcNow); err != nil {
return decimal.Decimal{}, fmt.Errorf("unable to read timestamp: %w", err)
}
return hlcNow, nil
}
func readClusterTTLNanos(conn *pgxpool.Pool) (int64, error) {
var target, configSQL string
if err := conn.
QueryRow(context.Background(), 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) datastore.Revision {
return decimal.NewFromInt(t.UnixNano())
}