From 216de176a70b73482a80bc15283e8b8626c610d6 Mon Sep 17 00:00:00 2001 From: Martin Asquino Date: Tue, 29 Oct 2024 23:25:27 +0000 Subject: [PATCH] fix: use recommended cluster settings for cockroachdb --- modules/cockroachdb/cockroachdb.go | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/modules/cockroachdb/cockroachdb.go b/modules/cockroachdb/cockroachdb.go index 4d412f04d3..706a3ea976 100644 --- a/modules/cockroachdb/cockroachdb.go +++ b/modules/cockroachdb/cockroachdb.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "crypto/x509" + "database/sql" "encoding/pem" "fmt" "net" @@ -90,6 +91,11 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom return addTLS(ctx, container, o) }, }, + PostReadies: []testcontainers.ContainerHook{ + func(ctx context.Context, container testcontainers.Container) error { + return setRecommendedSettings(ctx, container, o) + }, + }, }, }, }, @@ -233,6 +239,46 @@ func addTLS(ctx context.Context, container testcontainers.Container, opts option return nil } +// setRecommendedSettings applies the cluster settings recommended by cockroachlabs for testing clusters. +// See https://www.cockroachlabs.com/docs/stable/local-testing for more information. +func setRecommendedSettings(ctx context.Context, container testcontainers.Container, opts options) error { + port, err := container.MappedPort(ctx, defaultSQLPort) + if err != nil { + return err + } + + host, err := container.Host(ctx) + if err != nil { + return err + } + + db, err := sql.Open("pgx/v5", connString(opts, host, port)) + if err != nil { + return err + } + defer db.Close() + + stmts := []string{ + "SET CLUSTER SETTING kv.range_merge.queue_interval = '50ms'", + "SET CLUSTER SETTING jobs.registry.interval.gc = '30s'", + "SET CLUSTER SETTING jobs.registry.interval.cancel = '180s'", + "SET CLUSTER SETTING jobs.retention_time = '15s'", + "SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false", + "SET CLUSTER SETTING kv.range_split.by_load_merge_delay = '5s'", + `ALTER RANGE default CONFIGURE ZONE USING "gc.ttlseconds" = 600`, + `ALTER DATABASE system CONFIGURE ZONE USING "gc.ttlseconds" = 600`, + } + + for _, stmt := range stmts { + _, err = db.Exec(stmt) + if err != nil { + return err + } + } + + return nil +} + func connString(opts options, host string, port nat.Port) string { user := url.User(opts.User) if opts.Password != "" {