Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use recommended cluster settings for cockroachdb #2858

Closed
42 changes: 42 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 runStatements(ctx, container, o)
},
},
},
},
},
Expand Down Expand Up @@ -233,6 +239,42 @@ func addTLS(ctx context.Context, container testcontainers.Container, opts option
return nil
}

// runStatements runs the configured statements against the CockroachDB container.
func runStatements(ctx context.Context, container testcontainers.Container, opts options) (err error) {
martskins marked this conversation as resolved.
Show resolved Hide resolved
if len(opts.Statements) == 0 {
return nil
}

port, err := container.MappedPort(ctx, defaultSQLPort)
if err != nil {
return fmt.Errorf("mapped port: %w", err)
}

host, err := container.Host(ctx)
if err != nil {
return fmt.Errorf("host: %w", err)
}

db, err := sql.Open("pgx/v5", connString(opts, host, port))
if err != nil {
return fmt.Errorf("sql.Open: %w", err)
}
defer func() {
cerr := db.Close()
if err == nil {
err = cerr
}
}()

for _, stmt := range opts.Statements {
if _, err = db.Exec(stmt); err != nil {
return fmt.Errorf("db.Exec: %w", err)
}
}

return nil
}

func connString(opts options, host string, port nat.Port) string {
user := url.User(opts.User)
if opts.Password != "" {
Expand Down
31 changes: 26 additions & 5 deletions modules/cockroachdb/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cockroachdb
import "github.com/testcontainers/testcontainers-go"

type options struct {
Database string
User string
Password string
StoreSize string
TLS *TLSConfig
Database string
User string
Password string
StoreSize string
TLS *TLSConfig
Statements []string
martskins marked this conversation as resolved.
Show resolved Hide resolved
}

func defaultOptions() options {
Expand Down Expand Up @@ -67,3 +68,23 @@ func WithTLS(cfg *TLSConfig) Option {
o.TLS = cfg
}
}

// ClusterDefaults are the settings recommended by Cockroach Labs for testing clusters.
martskins marked this conversation as resolved.
Show resolved Hide resolved
// See https://www.cockroachlabs.com/docs/stable/local-testing for more information.
var ClusterDefaults = []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`,
}

// WithStatements sets the statements to run on the CockroachDB cluster once the container is ready.
martskins marked this conversation as resolved.
Show resolved Hide resolved
func WithStatements(statements ...string) Option {
return func(o *options) {
o.Statements = statements
}
}