Skip to content

Commit

Permalink
fix: use recommended cluster settings for cockroachdb
Browse files Browse the repository at this point in the history
  • Loading branch information
martskins committed Oct 30, 2024
1 parent 09a5482 commit 216de17
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions modules/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/pem"
"fmt"
"net"
Expand Down Expand Up @@ -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)
},
},
},
},
},
Expand Down Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 216de17

Please sign in to comment.