-
Notifications
You must be signed in to change notification settings - Fork 351
/
database.go
329 lines (303 loc) · 8.91 KB
/
database.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
package db
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/georgysavva/scany/pgxscan"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/treeverse/lakefs/pkg/logging"
)
type TxFunc func(tx Tx) (interface{}, error)
type Querier interface {
Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}
type Database interface {
Querier
Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error
Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error
GetPrimitive(ctx context.Context, dest interface{}, query string, args ...interface{}) error
Exec(ctx context.Context, query string, args ...interface{}) (pgconn.CommandTag, error)
Transact(ctx context.Context, fn TxFunc, opts ...TxOpt) (interface{}, error)
Close()
Metadata(ctx context.Context) (map[string]string, error)
Stats() sql.DBStats
Pool() *pgxpool.Pool
}
// Void wraps a procedure with no return value as a TxFunc
func Void(fn func(tx Tx) error) TxFunc {
return func(tx Tx) (interface{}, error) { return nil, fn(tx) }
}
type QueryOptions struct {
logger logging.Logger
}
type PgxDatabase struct {
db *pgxpool.Pool
queryOptions *QueryOptions
}
func NewPgxDatabase(db *pgxpool.Pool) *PgxDatabase {
return &PgxDatabase{db: db}
}
func (d *PgxDatabase) getLogger(ctx context.Context, fields logging.Fields) logging.Logger {
var log logging.Logger
if d.queryOptions != nil {
log = d.queryOptions.logger
} else {
log = logging.Default()
}
return log.WithContext(ctx).WithFields(fields)
}
func (d *PgxDatabase) Close() {
d.db.Close()
}
func (d *PgxDatabase) Pool() *pgxpool.Pool {
return d.db
}
// performAndReport performs fn and logs a "done" report if its duration was long enough.
func (d *PgxDatabase) performAndReport(ctx context.Context, fields logging.Fields, fn func() (interface{}, error)) (interface{}, error) {
start := time.Now()
ret, err := fn()
duration := time.Since(start)
if duration > 100*time.Millisecond {
logger := d.getLogger(ctx, fields).WithField("took", duration)
if err != nil {
logger = logger.WithError(err)
}
logger.Info("database done")
}
return ret, err
}
func (d *PgxDatabase) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
_, err := d.performAndReport(ctx, logging.Fields{
"type": "get",
"query": query,
"args": args,
}, func() (interface{}, error) {
return nil, pgxscan.Get(ctx, d.db, dest, query, args...)
})
return err
}
func (d *PgxDatabase) GetPrimitive(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
row := d.db.QueryRow(ctx, query, args...)
err := row.Scan(dest)
if errors.Is(err, pgx.ErrNoRows) {
return ErrNotFound
}
if err != nil {
return fmt.Errorf("query %s: %w", query, err)
}
return nil
}
func (d *PgxDatabase) Query(ctx context.Context, query string, args ...interface{}) (rows pgx.Rows, err error) {
l := d.getLogger(ctx, logging.Fields{
"type": "start query",
"query": query,
"args": args,
})
start := time.Now()
ret, err := d.db.Query(ctx, query, args...)
if ret == nil {
return nil, err
}
return Logged(ret, start, l), err
}
func (d *PgxDatabase) Select(ctx context.Context, results interface{}, query string, args ...interface{}) error {
rows, err := d.Query(ctx, query, args...)
if err != nil {
return err
}
defer rows.Close()
return pgxscan.ScanAll(results, rows)
}
func (d *PgxDatabase) Exec(ctx context.Context, query string, args ...interface{}) (pgconn.CommandTag, error) {
ret, err := d.performAndReport(ctx, logging.Fields{
"type": "exec",
"query": query,
"args": args,
}, func() (interface{}, error) { return d.db.Exec(ctx, query, args...) })
if err != nil {
return nil, err
}
return ret.(pgconn.CommandTag), nil
}
func (d *PgxDatabase) getTxOptions(ctx context.Context) *TxOptions {
options := DefaultTxOptions(ctx)
if d.queryOptions != nil {
options.logger = d.queryOptions.logger
}
return options
}
func (d *PgxDatabase) Transact(ctx context.Context, fn TxFunc, opts ...TxOpt) (interface{}, error) {
options := d.getTxOptions(ctx)
for _, opt := range opts {
opt(options)
}
var attempt int
var ret interface{}
var err error
var tx pgx.Tx
defer func() {
if p := recover(); p != nil && tx != nil {
_ = tx.Rollback(ctx)
panic(p)
}
}()
for attempt < SerializationRetryMaxAttempts {
if attempt > 0 {
duration := SerializationRetryStartInterval * time.Duration(attempt)
dbRetriesCount.Inc()
options.logger.
WithField("attempt", attempt).
WithField("sleep_interval", duration).
Warn("retrying transaction due to serialization error")
time.Sleep(duration)
}
tx, err = d.db.BeginTx(ctx, pgx.TxOptions{
IsoLevel: options.isolationLevel,
AccessMode: options.accessMode,
})
if err != nil {
return nil, err
}
ret, err = fn(&dbTx{tx: tx, logger: options.logger, ctx: ctx})
if err != nil {
rollbackErr := tx.Rollback(ctx)
if rollbackErr != nil {
// returning the original error and not the rollbackErr
// there are cases we use the return value with specific error, so we capture 'ret'
return ret, err
}
// retry on serialization error
if IsSerializationError(err) {
// retry
attempt++
continue
}
} else {
commitErr := tx.Commit(ctx)
if commitErr != nil {
// retry on serialization error
if IsSerializationError(commitErr) {
attempt++
continue
}
// other commit error
return nil, commitErr
}
// committed successfully, we're done
}
// always return the callback value with or without the error - some cases depends on the data with an error
return ret, err
}
if attempt == SerializationRetryMaxAttempts {
options.logger.
WithField("attempt", attempt).
Warn("transaction failed after max attempts due to serialization error")
}
return nil, ErrSerialization
}
func (d *PgxDatabase) Metadata(ctx context.Context) (map[string]string, error) {
metadata := make(map[string]string)
version, err := d.getVersion(ctx)
if err == nil {
metadata["postgresql_version"] = version
}
auroraVersion, err := d.getAuroraVersion()
if err == nil {
metadata["postgresql_aurora_version"] = auroraVersion
}
m, err := d.Transact(ctx, func(tx Tx) (interface{}, error) {
// select name,setting from pg_settings
// where name in ('data_directory', 'rds.extensions', 'TimeZone', 'work_mem')
type pgSettings struct {
Name string `db:"name"`
Setting string `db:"setting"`
}
rows, err := tx.Query(
`SELECT name, setting FROM pg_settings
WHERE name IN ('data_directory', 'rds.extensions', 'TimeZone', 'work_mem')`)
if err != nil {
return nil, err
}
settings := make(map[string]string)
for rows.Next() {
var setting pgSettings
err = rows.Scan(&setting)
if err != nil {
return nil, err
}
if setting.Name == "data_directory" {
isRDS := strings.HasPrefix(setting.Setting, "/rdsdata")
settings["is_rds"] = strconv.FormatBool(isRDS)
continue
}
settings[setting.Name] = setting.Setting
}
return settings, nil
}, ReadOnly())
if err != nil {
return metadata, nil
}
// set pgs settings under the metadata with key prefix
settings := m.(map[string]string)
for k, v := range settings {
metadata["postgresql_setting_"+k] = v
}
return metadata, nil
}
func (d *PgxDatabase) getVersion(ctx context.Context) (string, error) {
v, err := d.Transact(ctx, func(tx Tx) (interface{}, error) {
type ver struct {
Version string `db:"version"`
}
var v ver
err := tx.Get(&v, "SELECT version()")
if err != nil {
return "", err
}
return v.Version, nil
}, ReadOnly(), WithLogger(logging.Dummy()))
if err != nil {
return "", err
}
return v.(string), err
}
func (d *PgxDatabase) getAuroraVersion() (string, error) {
ctx := context.Background()
v, err := d.Transact(ctx, func(tx Tx) (interface{}, error) {
var v string
err := tx.Get(&v, "SELECT aurora_version()")
if err != nil {
return "", err
}
return v, nil
}, ReadOnly(), WithLogger(logging.Dummy()))
if err != nil {
return "", err
}
return v.(string), err
}
func (d *PgxDatabase) Stats() sql.DBStats {
stat := d.db.Stat()
return sql.DBStats{
MaxOpenConnections: int(stat.MaxConns()),
// Includes conns being constructed, so this violates the invariant documented
// in DBStats that OpenConnections = InUse + Idle.
OpenConnections: int(stat.TotalConns()),
InUse: int(stat.AcquiredConns()),
Idle: int(stat.IdleConns()),
// WaitCount is the number of connections for which sql had to wait.
// EmptyAcquireCount is the number of connections that pgx acquired after
// waiting. The two are close enough (but race each other).
WaitCount: stat.EmptyAcquireCount(),
// Time to acquire is close enough to time spent waiting; fudge.
WaitDuration: stat.AcquireDuration(),
// Not clear that pgx can report MaxIdleClosed, MaxIdleTimeClosed,
// MaxLifetimeClosed.
}
}