From 321bf26c854d9ab4861e645435d8890656618e34 Mon Sep 17 00:00:00 2001 From: Stefan Benten Date: Sun, 20 Feb 2022 22:29:41 +0100 Subject: [PATCH] cmd,private,satellite: add application_name to metabase connections Currently the metainfo/metabase DB connections are missing the proper application_name in order to differentiate and filter queries on the DB side for analytics. Without it, it is very time-consuming to correlate processes and their load. This change adds the "check" on DB connection init and passes the fallbacks in all places to catch connection strings, that do not set it. Change-Id: Iea5cea8658bc63778ff89038e5c1c352bf482cfd --- cmd/metabase-orphaned-segments/main.go | 2 +- cmd/metabase-orphaned-segments/main_test.go | 1 + cmd/metabase-verify/main.go | 2 +- cmd/satellite/admin.go | 1 + cmd/satellite/api.go | 1 + cmd/satellite/fetchpieces.go | 1 + cmd/satellite/gc.go | 1 + cmd/satellite/main.go | 2 ++ cmd/satellite/repairer.go | 1 + private/testplanet/satellite.go | 1 + satellite/metabase/commit_test.go | 2 ++ satellite/metabase/db.go | 6 ++++++ satellite/metabase/metabasetest/run.go | 2 ++ 13 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/metabase-orphaned-segments/main.go b/cmd/metabase-orphaned-segments/main.go index d1ab71a89..6329a2371 100644 --- a/cmd/metabase-orphaned-segments/main.go +++ b/cmd/metabase-orphaned-segments/main.go @@ -155,7 +155,7 @@ func Delete(ctx context.Context, log *zap.Logger, config Config) (err error) { func findOrphanedSegments(ctx context.Context, log *zap.Logger, config Config) (_ []uuid.UUID, err error) { defer mon.Task()(&ctx)(&err) - metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), config.MetabaseDB, metabase.Config{}) + metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), config.MetabaseDB, metabase.Config{ApplicationName: "metabase-orphaned-segments"}) if err != nil { return nil, errs.New("unable to connect %q: %w", config.MetabaseDB, err) } diff --git a/cmd/metabase-orphaned-segments/main_test.go b/cmd/metabase-orphaned-segments/main_test.go index 767a719ed..e422ebfab 100644 --- a/cmd/metabase-orphaned-segments/main_test.go +++ b/cmd/metabase-orphaned-segments/main_test.go @@ -115,6 +115,7 @@ func test(t *testing.T, prepare func(t *testing.T, ctx *testcontext.Context, raw require.NoError(t, err) metabaseDB, err := satellitedbtest.CreateMetabaseDBOnTopOf(ctx, log, metabaseTempDB, metabase.Config{ + ApplicationName: "satellite-test", MinPartSize: 5 * memory.MiB, MaxNumberOfParts: 10000, }) diff --git a/cmd/metabase-verify/main.go b/cmd/metabase-verify/main.go index 3f9c27fd2..b007f9ad8 100644 --- a/cmd/metabase-verify/main.go +++ b/cmd/metabase-verify/main.go @@ -59,7 +59,7 @@ func VerifyCommand(log *zap.Logger) *cobra.Command { ctx, cancel := process.Ctx(cmd) defer cancel() - mdb, err := metabase.Open(ctx, log.Named("mdb"), metabaseDB, metabase.Config{}) + mdb, err := metabase.Open(ctx, log.Named("mdb"), metabaseDB, metabase.Config{ApplicationName: "metabase-verify"}) if err != nil { return Error.Wrap(err) } diff --git a/cmd/satellite/admin.go b/cmd/satellite/admin.go index 98d33b53f..3248cfd03 100644 --- a/cmd/satellite/admin.go +++ b/cmd/satellite/admin.go @@ -39,6 +39,7 @@ func cmdAdminRun(cmd *cobra.Command, args []string) (err error) { }() metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Config.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-admin", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) diff --git a/cmd/satellite/api.go b/cmd/satellite/api.go index 4d195331a..5285d5673 100644 --- a/cmd/satellite/api.go +++ b/cmd/satellite/api.go @@ -45,6 +45,7 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) { }() metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Config.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-api", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) diff --git a/cmd/satellite/fetchpieces.go b/cmd/satellite/fetchpieces.go index 4ef5c7410..86b8d18db 100644 --- a/cmd/satellite/fetchpieces.go +++ b/cmd/satellite/fetchpieces.go @@ -60,6 +60,7 @@ func cmdFetchPieces(cmd *cobra.Command, args []string) (err error) { }() metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-pieces-fetcher", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) diff --git a/cmd/satellite/gc.go b/cmd/satellite/gc.go index 43207f169..da370b411 100644 --- a/cmd/satellite/gc.go +++ b/cmd/satellite/gc.go @@ -37,6 +37,7 @@ func cmdGCRun(cmd *cobra.Command, args []string) (err error) { }() metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-gc", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) diff --git a/cmd/satellite/main.go b/cmd/satellite/main.go index 2d543623d..43da06faa 100644 --- a/cmd/satellite/main.go +++ b/cmd/satellite/main.go @@ -393,6 +393,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) { }() metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-core", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) @@ -480,6 +481,7 @@ func cmdMigrationRun(cmd *cobra.Command, args []string) (err error) { } metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-migration", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) diff --git a/cmd/satellite/repairer.go b/cmd/satellite/repairer.go index d9051e655..1a1e4c447 100644 --- a/cmd/satellite/repairer.go +++ b/cmd/satellite/repairer.go @@ -40,6 +40,7 @@ func cmdRepairerRun(cmd *cobra.Command, args []string) (err error) { }() metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Metainfo.DatabaseURL, metabase.Config{ + ApplicationName: "satellite-repairer", MinPartSize: runCfg.Config.Metainfo.MinPartSize, MaxNumberOfParts: runCfg.Config.Metainfo.MaxNumberOfParts, }) diff --git a/private/testplanet/satellite.go b/private/testplanet/satellite.go index b020f9065..e3ba93b89 100644 --- a/private/testplanet/satellite.go +++ b/private/testplanet/satellite.go @@ -476,6 +476,7 @@ func (planet *Planet) newSatellite(ctx context.Context, prefix string, index int } metabaseDB, err := satellitedbtest.CreateMetabaseDB(context.TODO(), log.Named("metabase"), planet.config.Name, "M", index, databases.MetabaseDB, metabase.Config{ + ApplicationName: "satellite-testplanet", MinPartSize: config.Metainfo.MinPartSize, MaxNumberOfParts: config.Metainfo.MaxNumberOfParts, }) diff --git a/satellite/metabase/commit_test.go b/satellite/metabase/commit_test.go index d72d6d84e..66df75346 100644 --- a/satellite/metabase/commit_test.go +++ b/satellite/metabase/commit_test.go @@ -2740,6 +2740,7 @@ func TestCommitObject(t *testing.T) { func TestCommitObjectWithIncorrectPartSize(t *testing.T) { metabasetest.RunWithConfig(t, metabase.Config{ + ApplicationName: "satellite-test", MinPartSize: 5 * memory.MiB, MaxNumberOfParts: 1000, }, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { @@ -3072,6 +3073,7 @@ func TestCommitObjectWithIncorrectPartSize(t *testing.T) { func TestCommitObjectWithIncorrectAmountOfParts(t *testing.T) { metabasetest.RunWithConfig(t, metabase.Config{ + ApplicationName: "satellite-test", MinPartSize: 5 * memory.MiB, MaxNumberOfParts: 3, }, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { diff --git a/satellite/metabase/db.go b/satellite/metabase/db.go index 6b0b3e2be..067ebb060 100644 --- a/satellite/metabase/db.go +++ b/satellite/metabase/db.go @@ -29,6 +29,7 @@ var ( // Config is a configuration struct for part validation. type Config struct { + ApplicationName string MinPartSize memory.Size MaxNumberOfParts int } @@ -63,6 +64,11 @@ func Open(ctx context.Context, log *zap.Logger, connstr string, config Config) ( return nil, Error.New("unsupported implementation: %s", connstr) } + connstr, err = pgutil.CheckApplicationName(connstr, config.ApplicationName) + if err != nil { + return nil, Error.Wrap(err) + } + rawdb, err := tagsql.Open(ctx, driverName, connstr) if err != nil { return nil, Error.Wrap(err) diff --git a/satellite/metabase/metabasetest/run.go b/satellite/metabase/metabasetest/run.go index 3f358f107..d1c3e4a06 100644 --- a/satellite/metabase/metabasetest/run.go +++ b/satellite/metabase/metabasetest/run.go @@ -54,6 +54,7 @@ func Run(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metab ) RunWithConfig(t, metabase.Config{ + ApplicationName: "satellite-test", MinPartSize: config.MinPartSize, MaxNumberOfParts: config.MaxNumberOfParts, }, fn) @@ -67,6 +68,7 @@ func Bench(b *testing.B, fn func(ctx *testcontext.Context, b *testing.B, db *met ctx := testcontext.New(b) defer ctx.Cleanup() db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, dbinfo.MetabaseDB, metabase.Config{ + ApplicationName: "satellite-bench", MinPartSize: 5 * memory.MiB, MaxNumberOfParts: 10000, })