2021-04-26 10:35:44 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
2020-10-28 15:28:06 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-04-26 10:35:44 +01:00
|
|
|
package metabasetest
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-01 11:18:56 +01:00
|
|
|
"context"
|
2023-01-11 07:30:14 +00:00
|
|
|
"strings"
|
2020-10-28 15:28:06 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-11 07:30:14 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-09-24 15:18:21 +01:00
|
|
|
"github.com/spf13/pflag"
|
2023-01-11 07:30:14 +00:00
|
|
|
"github.com/zeebo/errs"
|
2020-11-05 13:30:39 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
"storj.io/common/memory"
|
2020-10-28 15:28:06 +00:00
|
|
|
"storj.io/common/testcontext"
|
2021-09-24 15:18:21 +01:00
|
|
|
"storj.io/private/cfgstruct"
|
2023-01-11 07:30:14 +00:00
|
|
|
"storj.io/private/dbutil/pgutil"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2021-09-24 15:18:21 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2020-11-05 13:30:39 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2020-10-28 15:28:06 +00:00
|
|
|
)
|
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
// RunWithConfig runs tests with specific metabase configuration.
|
|
|
|
func RunWithConfig(t *testing.T, config metabase.Config, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) {
|
2022-06-01 11:18:56 +01:00
|
|
|
RunWithConfigAndMigration(t, config, fn, func(ctx context.Context, db *metabase.DB) error {
|
|
|
|
return db.TestMigrateToLatest(ctx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithConfigAndMigration runs tests with specific metabase configuration and migration type.
|
|
|
|
func RunWithConfigAndMigration(t *testing.T, config metabase.Config, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB), migration func(ctx context.Context, db *metabase.DB) error) {
|
2021-05-11 11:08:06 +01:00
|
|
|
for _, dbinfo := range satellitedbtest.Databases() {
|
|
|
|
dbinfo := dbinfo
|
|
|
|
t.Run(dbinfo.Name, func(t *testing.T) {
|
2020-10-28 15:28:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2023-01-11 07:30:14 +00:00
|
|
|
// generate unique application name to filter out full table scan queries from other tests executions
|
|
|
|
config.ApplicationName += pgutil.CreateRandomTestingSchemaName(6)
|
2021-09-24 15:18:21 +01:00
|
|
|
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, dbinfo.MetabaseDB, config)
|
2020-10-28 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-06-01 11:18:56 +01:00
|
|
|
if err := migration(ctx, db); err != nil {
|
2020-10-28 15:28:06 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:30:14 +00:00
|
|
|
fullScansBefore, err := fullTableScanQueries(ctx, db, config.ApplicationName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:20:06 +01:00
|
|
|
fn(ctx, t, db)
|
2023-01-11 07:30:14 +00:00
|
|
|
|
|
|
|
fullScansAfter, err := fullTableScanQueries(ctx, db, config.ApplicationName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := cmp.Diff(fullScansBefore, fullScansAfter)
|
|
|
|
if diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:18:21 +01:00
|
|
|
// Run runs tests against all configured databases.
|
|
|
|
func Run(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) {
|
|
|
|
var config metainfo.Config
|
|
|
|
cfgstruct.Bind(pflag.NewFlagSet("", pflag.PanicOnError), &config,
|
|
|
|
cfgstruct.UseTestDefaults(),
|
|
|
|
)
|
|
|
|
|
|
|
|
RunWithConfig(t, metabase.Config{
|
2023-01-11 07:30:14 +00:00
|
|
|
ApplicationName: "satellite-metabase-test",
|
2022-06-24 00:20:36 +01:00
|
|
|
MinPartSize: config.MinPartSize,
|
|
|
|
MaxNumberOfParts: config.MaxNumberOfParts,
|
|
|
|
ServerSideCopy: config.ServerSideCopy,
|
|
|
|
ServerSideCopyDisabled: config.ServerSideCopyDisabled,
|
2022-09-19 14:26:50 +01:00
|
|
|
MultipleVersions: config.MultipleVersions,
|
2021-09-24 15:18:21 +01:00
|
|
|
}, fn)
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:35:44 +01:00
|
|
|
// Bench runs benchmark for all configured databases.
|
2021-01-22 17:34:08 +00:00
|
|
|
func Bench(b *testing.B, fn func(ctx *testcontext.Context, b *testing.B, db *metabase.DB)) {
|
2021-05-11 11:08:06 +01:00
|
|
|
for _, dbinfo := range satellitedbtest.Databases() {
|
|
|
|
dbinfo := dbinfo
|
|
|
|
b.Run(dbinfo.Name, func(b *testing.B) {
|
2021-01-22 17:34:08 +00:00
|
|
|
ctx := testcontext.New(b)
|
|
|
|
defer ctx.Cleanup()
|
2021-09-24 15:18:21 +01:00
|
|
|
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, dbinfo.MetabaseDB, metabase.Config{
|
2022-02-20 21:29:41 +00:00
|
|
|
ApplicationName: "satellite-bench",
|
2021-09-24 15:18:21 +01:00
|
|
|
MinPartSize: 5 * memory.MiB,
|
|
|
|
MaxNumberOfParts: 10000,
|
|
|
|
})
|
2021-01-22 17:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := db.MigrateToLatest(ctx); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2021-06-07 10:20:06 +01:00
|
|
|
fn(ctx, b, db)
|
2021-01-22 17:34:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 07:30:14 +00:00
|
|
|
|
|
|
|
func fullTableScanQueries(ctx context.Context, db *metabase.DB, applicationName string) (_ map[string]int, err error) {
|
|
|
|
if db.Implementation().String() != "cockroach" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.UnderlyingTagSQL().QueryContext(ctx,
|
|
|
|
"SELECT key, count FROM crdb_internal.node_statement_statistics WHERE full_scan = TRUE AND application_name = $1 ORDER BY count DESC",
|
|
|
|
applicationName,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, rows.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
result := map[string]int{}
|
|
|
|
for rows.Next() {
|
|
|
|
var query string
|
|
|
|
var count int
|
|
|
|
err := rows.Scan(&query, &count)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.Contains(query, "WITH testing AS (SELECT _)"):
|
|
|
|
continue
|
|
|
|
case !strings.Contains(strings.ToUpper(query), "WHERE"): // find smarter way to ignore known full table scan queries
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result[query] += count
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|