-
Notifications
You must be signed in to change notification settings - Fork 342
/
store.go
434 lines (390 loc) · 11.6 KB
/
store.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 postgres
import (
"bytes"
"context"
"errors"
"fmt"
"hash/fnv"
"strconv"
"github.com/IBM/pgxpoolprometheus"
"github.com/georgysavva/scany/v2/pgxscan"
"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/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/kv/kvparams"
)
type Driver struct{}
type Store struct {
Pool *pgxpool.Pool
Params *Params
TableSanitized string
collector prometheus.Collector
}
type EntriesIterator struct {
ctx context.Context
partitionKey []byte
startKey []byte
includeStart bool
store *Store
entries []kv.Entry
limit int
currEntryIdx int
err error
}
const (
DriverName = "postgres"
DefaultTableName = "kv"
paramTableName = "lakefskv_table"
// DefaultPartitions Changing the below value means repartitioning and probably a migration.
// Change it only if you really know what you're doing.
DefaultPartitions = 100
DefaultScanPageSize = 1000
)
//nolint:gochecknoinits
func init() {
kv.Register(DriverName, &Driver{})
}
func (d *Driver) Open(ctx context.Context, kvParams kvparams.Config) (kv.Store, error) {
if kvParams.Postgres == nil {
return nil, fmt.Errorf("missing %s settings: %w", DriverName, kv.ErrDriverConfiguration)
}
config, err := newPgxpoolConfig(kvParams)
if err != nil {
return nil, err
}
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return nil, fmt.Errorf("%w: %s", kv.ErrConnectFailed, err)
}
defer func() {
// if we return before store uses the pool, free it
if pool != nil {
pool.Close()
}
}()
// acquire connection and make sure we reach the database
conn, err := pool.Acquire(ctx)
if err != nil {
return nil, fmt.Errorf("%w: %s", kv.ErrConnectFailed, err)
}
defer conn.Release()
err = conn.Conn().Ping(ctx)
if err != nil {
return nil, fmt.Errorf("%w: %s", kv.ErrConnectFailed, err)
}
params := parseStoreConfig(config.ConnConfig.RuntimeParams, kvParams.Postgres)
err = setupKeyValueDatabase(ctx, conn, params.TableName, params.PartitionsAmount)
if err != nil {
return nil, fmt.Errorf("%w: %s", kv.ErrSetupFailed, err)
}
// register collector to publish pgx's pool stats as metrics
var collector prometheus.Collector
if params.Metrics {
collector = pgxpoolprometheus.NewCollector(pool, map[string]string{"db_name": params.TableName})
err := prometheus.Register(collector)
if err != nil {
return nil, err
}
}
store := &Store{
Pool: pool,
Params: params,
TableSanitized: pgx.Identifier{params.TableName}.Sanitize(),
collector: collector,
}
pool = nil
return store, nil
}
func newPgxpoolConfig(kvParams kvparams.Config) (*pgxpool.Config, error) {
config, err := pgxpool.ParseConfig(kvParams.Postgres.ConnectionString)
if err != nil {
return nil, fmt.Errorf("%w: %s", kv.ErrDriverConfiguration, err)
}
if kvParams.Postgres.MaxOpenConnections > 0 {
config.MaxConns = kvParams.Postgres.MaxOpenConnections
}
if kvParams.Postgres.MaxIdleConnections > 0 {
config.MinConns = kvParams.Postgres.MaxIdleConnections
}
if kvParams.Postgres.ConnectionMaxLifetime > 0 {
config.MaxConnLifetime = kvParams.Postgres.ConnectionMaxLifetime
}
return config, err
}
type Params struct {
TableName string
SanitizedTableName string
PartitionsAmount int
ScanPageSize int
Metrics bool
}
func parseStoreConfig(runtimeParams map[string]string, pgParams *kvparams.Postgres) *Params {
p := &Params{
TableName: DefaultTableName,
PartitionsAmount: DefaultPartitions,
ScanPageSize: DefaultScanPageSize,
Metrics: pgParams.Metrics,
}
if tableName, ok := runtimeParams[paramTableName]; ok {
p.TableName = tableName
}
p.SanitizedTableName = pgx.Identifier{p.TableName}.Sanitize()
if pgParams.ScanPageSize > 0 {
p.ScanPageSize = pgParams.ScanPageSize
}
return p
}
// setupKeyValueDatabase setup everything required to enable kv over postgres
func setupKeyValueDatabase(ctx context.Context, conn *pgxpool.Conn, table string, partitionsAmount int) (err error) {
var aid string
aid, err = generateAdvisoryLockID("lakefs:" + table)
if err != nil {
return err
}
// This will wait indefinitely until the lock can be acquired.
_, err = conn.Exec(ctx, `SELECT pg_advisory_lock($1)`, aid)
if err != nil {
return fmt.Errorf("try lock failed: %w", err)
}
defer func(ctx context.Context) {
_, unlockErr := conn.Exec(ctx, `SELECT pg_advisory_unlock($1)`, aid)
// prefer the last error over unlock error
if err == nil {
err = unlockErr
}
}(ctx)
// main kv table
tableSanitize := pgx.Identifier{table}.Sanitize()
_, err = conn.Exec(ctx, `CREATE TABLE IF NOT EXISTS `+tableSanitize+` (
partition_key BYTEA NOT NULL,
key BYTEA NOT NULL,
value BYTEA NOT NULL,
PRIMARY KEY (partition_key, key))
PARTITION BY HASH (partition_key)`)
if err != nil {
return err
}
// partitions
partitions := getTablePartitions(table, partitionsAmount)
for i := 0; i < len(partitions); i++ {
_, err = conn.Exec(ctx, `CREATE TABLE IF NOT EXISTS`+
pgx.Identifier{partitions[i]}.Sanitize()+` PARTITION OF `+
tableSanitize+` FOR VALUES WITH (MODULUS `+strconv.Itoa(partitionsAmount)+
`,REMAINDER `+strconv.Itoa(i)+`)`)
if err != nil {
return err
}
}
// view of kv table to help humans select from table (same as table with _v as suffix)
_, err = conn.Exec(ctx, `CREATE OR REPLACE VIEW `+pgx.Identifier{table + "_v"}.Sanitize()+
` AS SELECT ENCODE(partition_key, 'escape') AS partition_key, ENCODE(key, 'escape') AS key, value FROM `+tableSanitize)
return err
}
func generateAdvisoryLockID(name string) (string, error) {
h := fnv.New32a()
if _, err := h.Write([]byte(name)); err != nil {
return "", err
}
aid := fmt.Sprint(h.Sum32())
return aid, nil
}
func getTablePartitions(tableName string, partitionsAmount int) []string {
res := make([]string, 0, partitionsAmount)
for i := 0; i < partitionsAmount; i++ {
res = append(res, fmt.Sprintf("%s_%d", tableName, i))
}
return res
}
func (s *Store) Get(ctx context.Context, partitionKey, key []byte) (*kv.ValueWithPredicate, error) {
if len(partitionKey) == 0 {
return nil, kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return nil, kv.ErrMissingKey
}
row := s.Pool.QueryRow(ctx, `SELECT value FROM `+s.Params.SanitizedTableName+` WHERE key = $1 AND partition_key = $2`, key, partitionKey)
var val []byte
err := row.Scan(&val)
if errors.Is(err, pgx.ErrNoRows) {
return nil, kv.ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("postgres get: %w", err)
}
return &kv.ValueWithPredicate{
Value: val,
Predicate: kv.Predicate(val),
}, nil
}
func (s *Store) Set(ctx context.Context, partitionKey, key, value []byte) error {
if len(partitionKey) == 0 {
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return kv.ErrMissingKey
}
if value == nil {
return kv.ErrMissingValue
}
_, err := s.Pool.Exec(ctx, `INSERT INTO `+s.Params.SanitizedTableName+`(partition_key,key,value) VALUES($1,$2,$3)
ON CONFLICT (partition_key,key) DO UPDATE SET value = $3`, partitionKey, key, value)
if err != nil {
return fmt.Errorf("postgres set: %w", err)
}
return nil
}
func (s *Store) SetIf(ctx context.Context, partitionKey, key, value []byte, valuePredicate kv.Predicate) error {
if len(partitionKey) == 0 {
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return kv.ErrMissingKey
}
if value == nil {
return kv.ErrMissingValue
}
var (
res pgconn.CommandTag
err error
)
switch valuePredicate {
case nil: // use insert to make sure there was no previous value before
res, err = s.Pool.Exec(ctx, `INSERT INTO `+s.Params.SanitizedTableName+`(partition_key,key,value) VALUES($1,$2,$3) ON CONFLICT DO NOTHING`, partitionKey, key, value)
case kv.PrecondConditionalExists: // update only if exists
res, err = s.Pool.Exec(ctx, `UPDATE `+s.Params.SanitizedTableName+` SET value=$3 WHERE key=$2 AND partition_key=$1`, partitionKey, key, value)
default: // update just in case the previous value was same as predicate value
res, err = s.Pool.Exec(ctx, `UPDATE `+s.Params.SanitizedTableName+` SET value=$3 WHERE key=$2 AND partition_key=$1 AND value=$4`, partitionKey, key, value, valuePredicate.([]byte))
}
if err != nil {
return fmt.Errorf("postgres setIf: %w", err)
}
if res.RowsAffected() != 1 {
return kv.ErrPredicateFailed
}
return nil
}
func (s *Store) Delete(ctx context.Context, partitionKey, key []byte) error {
if len(partitionKey) == 0 {
return kv.ErrMissingPartitionKey
}
if len(key) == 0 {
return kv.ErrMissingKey
}
_, err := s.Pool.Exec(ctx, `DELETE FROM `+s.Params.SanitizedTableName+` WHERE partition_key=$1 AND key=$2`, partitionKey, key)
if err != nil {
return fmt.Errorf("postgres delete: %w", err)
}
return nil
}
func (s *Store) Scan(ctx context.Context, partitionKey []byte, options kv.ScanOptions) (kv.EntriesIterator, error) {
if len(partitionKey) == 0 {
return nil, kv.ErrMissingPartitionKey
}
// limit based on the minimum between ScanPageSize and ScanOptions batch size
limit := s.Params.ScanPageSize
if options.BatchSize != 0 && s.Params.ScanPageSize != 0 && options.BatchSize < s.Params.ScanPageSize {
limit = options.BatchSize
}
it := &EntriesIterator{
ctx: ctx,
partitionKey: partitionKey,
startKey: options.KeyStart,
store: s,
limit: limit,
includeStart: true,
}
it.runQuery()
if it.err != nil {
return nil, it.err
}
return it, nil
}
func (s *Store) Close() {
if s.collector != nil {
prometheus.Unregister(s.collector)
s.collector = nil
}
s.Pool.Close()
}
// Next reads the next key/value.
func (e *EntriesIterator) Next() bool {
if e.err != nil || len(e.entries) == 0 {
return false
}
if e.currEntryIdx+1 == len(e.entries) {
key := e.entries[e.currEntryIdx].Key
e.startKey = key
e.includeStart = false
e.runQuery()
if e.err != nil || len(e.entries) == 0 {
return false
}
}
e.currEntryIdx++
return true
}
func (e *EntriesIterator) SeekGE(key []byte) {
if !e.isInRange(key) {
e.startKey = key
e.includeStart = true
e.runQuery()
return
}
for i := range e.entries {
if bytes.Compare(key, e.entries[i].Key) <= 0 {
e.currEntryIdx = i - 1
return
}
}
}
func (e *EntriesIterator) Entry() *kv.Entry {
if e.entries == nil {
return nil
}
return &e.entries[e.currEntryIdx]
}
// Err return the last scan error or the cursor error
func (e *EntriesIterator) Err() error {
return e.err
}
func (e *EntriesIterator) Close() {
e.entries = nil
e.currEntryIdx = -1
e.err = kv.ErrClosedEntries
}
func (e *EntriesIterator) runQuery() {
var (
rows pgx.Rows
err error
)
if e.startKey == nil {
rows, err = e.store.Pool.Query(e.ctx, `SELECT partition_key,key,value FROM `+e.store.Params.SanitizedTableName+` WHERE partition_key=$1 ORDER BY key LIMIT $2`, e.partitionKey, e.limit)
} else {
compareOp := ">="
if !e.includeStart {
compareOp = ">"
}
rows, err = e.store.Pool.Query(e.ctx, `SELECT partition_key,key,value FROM `+e.store.Params.SanitizedTableName+` WHERE partition_key=$1 AND key `+compareOp+` $2 ORDER BY key LIMIT $3`, e.partitionKey, e.startKey, e.limit)
}
if err != nil {
e.err = fmt.Errorf("postgres scan: %w", err)
return
}
defer rows.Close()
err = pgxscan.ScanAll(&e.entries, rows)
if err != nil {
e.err = fmt.Errorf("scanning all entries: %w", err)
return
}
e.currEntryIdx = -1
}
func (e *EntriesIterator) isInRange(key []byte) bool {
if len(e.entries) == 0 {
return false
}
minKey := e.entries[0].Key
maxKey := e.entries[len(e.entries)-1].Key
return minKey != nil && maxKey != nil && bytes.Compare(key, minKey) >= 0 && bytes.Compare(key, maxKey) <= 0
}