-
Notifications
You must be signed in to change notification settings - Fork 351
/
db.go
351 lines (305 loc) · 8.81 KB
/
db.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
package testutil
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"testing"
"time"
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/block/gs"
"github.com/treeverse/lakefs/pkg/block/mem"
lakefsS3 "github.com/treeverse/lakefs/pkg/block/s3"
"github.com/treeverse/lakefs/pkg/db"
"github.com/treeverse/lakefs/pkg/db/params"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/version"
)
const (
DBName = "lakefs_db"
DBContainerTimeoutSeconds = 60 * 30 // 30 minutes
EnvKeyUseBlockAdapter = "USE_BLOCK_ADAPTER"
envKeyAwsKeyID = "AWS_ACCESS_KEY_ID"
envKeyAwsSecretKey = "AWS_SECRET_ACCESS_KEY" //nolint:gosec
envKeyAwsRegion = "AWS_DEFAULT_REGION"
testMigrateValue = "This is a test value"
testPartitionKey = "This is a test partition key"
)
var keepDB = flag.Bool("keep-db", false, "keep test DB instance running")
func GetDBInstance(pool *dockertest.Pool) (string, func()) {
// connect using docker container name
containerName := os.Getenv("PG_DB_CONTAINER")
if containerName != "" {
resource, ok := pool.ContainerByName(containerName)
if !ok {
log.Fatalf("Cloud not find DB container (%s)", containerName)
}
uri := formatPostgresResourceURI(resource)
return uri, func() {}
}
// connect using supply address
dbURI := os.Getenv("PG_TEST_URI")
if len(dbURI) > 0 {
// use supplied DB connection for testing
if err := verifyDBConnectionString(dbURI); err != nil {
log.Fatalf("could not connect to postgres: %s", err)
}
return dbURI, func() {}
}
// run new instance and connect
resource, err := pool.Run("postgres", "11", []string{
"POSTGRES_USER=lakefs",
"POSTGRES_PASSWORD=lakefs",
fmt.Sprintf("POSTGRES_DB=%s", DBName),
})
if err != nil {
log.Fatalf("Could not start postgresql: %s", err)
}
// expire the container, just to be on the safe side
if !*keepDB {
err = resource.Expire(DBContainerTimeoutSeconds)
if err != nil {
log.Fatalf("could not expire postgres container")
}
}
// format db uri
uri := formatPostgresResourceURI(resource)
// wait for container to start and connect to db
if err = pool.Retry(func() error {
return verifyDBConnectionString(uri)
}); err != nil {
log.Fatalf("could not connect to postgres: %s", err)
}
// set cleanup
closer := func() {
if *keepDB {
return
}
err := pool.Purge(resource)
if err != nil {
log.Fatalf("could not kill postgres container")
}
}
// return DB address and closer func
return uri, closer
}
func formatPostgresResourceURI(resource *dockertest.Resource) string {
dbParams := map[string]string{
"POSTGRES_DB": DBName,
"POSTGRES_USER": "lakefs",
"POSTGRES_PASSWORD": "lakefs",
"POSTGRES_PORT": resource.GetPort("5432/tcp"),
}
env := resource.Container.Config.Env
for _, entry := range env {
for key := range dbParams {
if strings.HasPrefix(entry, key+"=") {
dbParams[key] = entry[len(key)+1:]
break
}
}
}
uri := fmt.Sprintf("postgres://%s:%s@localhost:%s/%s?sslmode=disable",
dbParams["POSTGRES_USER"],
dbParams["POSTGRES_PASSWORD"],
dbParams["POSTGRES_PORT"],
dbParams["POSTGRES_DB"],
)
return uri
}
func verifyDBConnectionString(uri string) error {
ctx := context.Background()
pool, err := pgxpool.Connect(ctx, uri)
if err != nil {
return err
}
defer pool.Close()
return db.Ping(ctx, pool)
}
type GetDBOptions struct {
ApplyDDL bool
}
type GetDBOption func(options *GetDBOptions)
func WithGetDBApplyDDL(apply bool) GetDBOption {
return func(options *GetDBOptions) {
options.ApplyDDL = apply
}
}
func GetDB(t testing.TB, uri string, opts ...GetDBOption) (db.Database, string) {
ctx := context.Background()
options := &GetDBOptions{
ApplyDDL: true,
}
for _, opt := range opts {
opt(options)
}
// generate uuid as schema name
generatedSchema := fmt.Sprintf("schema_%s",
strings.ReplaceAll(uuid.New().String(), "-", ""))
// create connection
connURI := fmt.Sprintf("%s&search_path=%s,public", uri, generatedSchema)
pool, err := pgxpool.Connect(ctx, connURI)
if err != nil {
t.Fatalf("could not connect to PostgreSQL: %s", err)
}
err = db.Ping(ctx, pool)
if err != nil {
pool.Close()
t.Fatalf("could not ping PostgreSQL: %s", err)
}
t.Cleanup(func() {
pool.Close()
})
database := db.NewPgxDatabase(pool)
_, err = database.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return tx.Exec("CREATE SCHEMA IF NOT EXISTS " + generatedSchema)
})
if err != nil {
t.Fatalf("could not create schema: %v", err)
}
if options.ApplyDDL {
// do the actual migration
err := db.MigrateUp(params.Database{ConnectionString: connURI})
if err != nil {
t.Fatal("could not create schema:", err)
}
}
// return DB
return database, connURI
}
func Must(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatalf("error returned for operation: %v", err)
}
}
func MustDo(t testing.TB, what string, err error) {
t.Helper()
if err != nil {
t.Fatalf("%s, expected no error, got err=%s", what, err)
}
}
func NewBlockAdapterByType(t testing.TB, translator block.UploadIDTranslator, blockstoreType string) block.Adapter {
switch blockstoreType {
case block.BlockstoreTypeGS:
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
t.Fatal("Google Storage new client", err)
}
return gs.NewAdapter(client, gs.WithTranslator(translator))
case block.BlockstoreTypeS3:
awsRegion, regionOk := os.LookupEnv(envKeyAwsRegion)
if !regionOk {
awsRegion = "us-east-1"
}
cfg := &aws.Config{
Region: aws.String(awsRegion),
}
awsSecret, secretOk := os.LookupEnv(envKeyAwsSecretKey)
awsKey, keyOk := os.LookupEnv(envKeyAwsKeyID)
if keyOk && secretOk {
cfg.Credentials = credentials.NewStaticCredentials(awsKey, awsSecret, "")
} else {
cfg.Credentials = credentials.NewSharedCredentials("", "default")
}
sess := session.Must(session.NewSession(cfg))
return lakefsS3.NewAdapter(sess, lakefsS3.WithTranslator(translator))
default:
return mem.New(mem.WithTranslator(translator))
}
}
// migrate functions for test scenarios
func MigrateEmpty(_ context.Context, _ *pgxpool.Pool, _ io.Writer) error {
return nil
}
func MigrateBasic(_ context.Context, _ *pgxpool.Pool, writer io.Writer) error {
buildTestData(1, 5, writer) //nolint: gomnd
return nil
}
func MigrateNoHeader(_ context.Context, _ *pgxpool.Pool, writer io.Writer) error {
jd := json.NewEncoder(writer)
for i := 1; i < 5; i++ {
err := jd.Encode(kv.Entry{
PartitionKey: []byte(strconv.Itoa(i)),
Key: []byte(strconv.Itoa(i)),
Value: []byte(fmt.Sprint(i, ". ", testMigrateValue)),
})
if err != nil {
log.Fatal("Failed to encode struct")
}
}
return nil
}
func MigrateBadEntry(_ context.Context, _ *pgxpool.Pool, writer io.Writer) error {
jd := json.NewEncoder(writer)
err := jd.Encode(kv.Entry{
Key: []byte("test"),
Value: nil,
})
if err != nil {
log.Fatal("Failed to encode struct")
}
return nil
}
func MigrateParallel(_ context.Context, _ *pgxpool.Pool, writer io.Writer) error {
const index = 6 // Magic number WA
buildTestData(index, 5, writer) //nolint: gomnd
return nil
}
func ValidateKV(ctx context.Context, t *testing.T, store kv.Store, entries int) {
for i := 1; i <= entries; i++ {
expectedVal := fmt.Sprint(i, ". ", testMigrateValue)
res, err := store.Get(ctx, []byte(testPartitionKey), []byte(strconv.Itoa(i)))
require.NoError(t, err)
require.Equal(t, expectedVal, string(res.Value))
}
}
func CleanupKV(ctx context.Context, t *testing.T, store kv.Store) {
t.Helper()
scan, err := store.Scan(ctx, []byte(testPartitionKey), []byte{0})
MustDo(t, "scan store", err)
defer scan.Close()
for scan.Next() {
ent := scan.Entry()
MustDo(t, "Clean store", store.Delete(ctx, ent.PartitionKey, ent.Key))
}
// Zero KV version
MustDo(t, "Reset migration", store.Set(ctx, []byte(kv.MetadataPartitionKey), []byte(kv.DBSchemaVersionKey), []byte("0")))
}
func buildTestData(startIdx, count int, writer io.Writer) {
jd := json.NewEncoder(writer)
err := jd.Encode(kv.Header{
LakeFSVersion: version.Version,
PackageName: "test_package_name",
DBSchemaVersion: kv.InitialMigrateVersion,
CreatedAt: time.Now(),
})
if err != nil {
log.Fatal("Failed to encode struct")
}
for i := startIdx; i < startIdx+count; i++ {
err = jd.Encode(kv.Entry{
PartitionKey: []byte(testPartitionKey),
Key: []byte(strconv.Itoa(i)),
Value: []byte(fmt.Sprint(i, ". ", testMigrateValue)),
})
if err != nil {
log.Fatal("Failed to encode struct")
}
}
}