diff --git a/weed/filer/postgres/pgx_conn.go b/weed/filer/postgres/pgx_conn.go deleted file mode 100644 index 81b6be0afa..0000000000 --- a/weed/filer/postgres/pgx_conn.go +++ /dev/null @@ -1,14 +0,0 @@ -package postgres - -import ( - "database/sql" - - "github.com/seaweedfs/seaweedfs/weed/util/pgxutil" -) - -// OpenPGXDB is a thin alias kept for callers (notably the postgres2 filer -// store) that already depend on this entry point. New code should use -// pgxutil.OpenDB directly. -func OpenPGXDB(sqlUrl, adaptedSqlUrl string, pgbouncerCompatible bool, maxIdle, maxOpen, maxLifetimeSeconds int) (*sql.DB, error) { - return pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds) -} diff --git a/weed/filer/postgres/postgres_connection.go b/weed/filer/postgres/postgres_connection.go new file mode 100644 index 0000000000..f8146e7812 --- /dev/null +++ b/weed/filer/postgres/postgres_connection.go @@ -0,0 +1,56 @@ +package postgres + +import ( + "database/sql" + + "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/seaweedfs/seaweedfs/weed/util/pgxutil" +) + +// Connection is how a postgres filer store reaches its database. +type Connection struct { + pgxutil.DSNOptions + MaxIdle, MaxOpen, MaxLifetimeSeconds int +} + +func ReadConnection(configuration util.Configuration, prefix string) Connection { + // Fewer idle slots than concurrent operations means a fresh connection per + // operation, until the filer runs out of ephemeral ports. connection_max_open + // stays unset: a listing runs a second query from its own callback, so a + // bounded pool deadlocks once the concurrency reaches it. + configuration.SetDefault(prefix+"connection_max_idle", 50) + configuration.SetDefault(prefix+"connection_max_lifetime_seconds", 300) + return Connection{ + DSNOptions: pgxutil.DSNOptions{ + Hostname: configuration.GetString(prefix + "hostname"), + Port: configuration.GetInt(prefix + "port"), + User: configuration.GetString(prefix + "username"), + Password: configuration.GetString(prefix + "password"), + Database: configuration.GetString(prefix + "database"), + Schema: configuration.GetString(prefix + "schema"), + SSLMode: configuration.GetString(prefix + "sslmode"), + SSLCert: configuration.GetString(prefix + "sslcert"), + SSLKey: configuration.GetString(prefix + "sslkey"), + SSLRootCert: configuration.GetString(prefix + "sslrootcert"), + SSLCRL: configuration.GetString(prefix + "sslcrl"), + PgBouncerCompatible: configuration.GetBool(prefix + "pgbouncer_compatible"), + }, + MaxIdle: configuration.GetInt(prefix + "connection_max_idle"), + MaxOpen: configuration.GetInt(prefix + "connection_max_open"), + MaxLifetimeSeconds: configuration.GetInt(prefix + "connection_max_lifetime_seconds"), + } +} + +// Open opens the store's connection pools. +func (c Connection) Open(store *abstract_sql.AbstractSqlStore) error { + dsn, adaptedDSN := pgxutil.BuildDSN(c.DSNOptions) + open := func() (*sql.DB, error) { + return pgxutil.OpenDB(dsn, adaptedDSN, c.PgBouncerCompatible, c.MaxIdle, c.MaxOpen, c.MaxLifetimeSeconds) + } + db, err := open() + if err != nil { + return err + } + return store.UseConnectionPools(db, open, c.MaxIdle, c.MaxOpen, c.MaxLifetimeSeconds) +} diff --git a/weed/filer/postgres/postgres_store.go b/weed/filer/postgres/postgres_store.go index fe1e574788..8e1c295ba6 100644 --- a/weed/filer/postgres/postgres_store.go +++ b/weed/filer/postgres/postgres_store.go @@ -9,9 +9,7 @@ package postgres import ( "context" - "database/sql" "fmt" - "strconv" "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql" @@ -31,12 +29,6 @@ func (store *PostgresStore) GetName() string { } func (store *PostgresStore) Initialize(configuration util.Configuration, prefix string) (err error) { - // Fewer idle slots than concurrent operations means a fresh connection per - // operation, until the filer runs out of ephemeral ports. connection_max_open - // stays unset: a listing runs a second query from its own callback, so a - // bounded pool deadlocks once the concurrency reaches it. - configuration.SetDefault(prefix+"connection_max_idle", 50) - configuration.SetDefault(prefix+"connection_max_lifetime_seconds", 300) // Default on so minimal configs are not exposed to duplicate-key tx // poisoning on Postgres; an explicit false still disables it. configuration.SetDefault(prefix+"enableUpsert", true) @@ -44,25 +36,11 @@ func (store *PostgresStore) Initialize(configuration util.Configuration, prefix configuration.GetString(prefix+"createTable"), configuration.GetString(prefix+"upsertQuery"), configuration.GetBool(prefix+"enableUpsert"), - configuration.GetString(prefix+"username"), - configuration.GetString(prefix+"password"), - configuration.GetString(prefix+"hostname"), - configuration.GetInt(prefix+"port"), - configuration.GetString(prefix+"database"), - configuration.GetString(prefix+"schema"), - configuration.GetString(prefix+"sslmode"), - configuration.GetString(prefix+"sslcert"), - configuration.GetString(prefix+"sslkey"), - configuration.GetString(prefix+"sslrootcert"), - configuration.GetString(prefix+"sslcrl"), - configuration.GetBool(prefix+"pgbouncer_compatible"), - configuration.GetInt(prefix+"connection_max_idle"), - configuration.GetInt(prefix+"connection_max_open"), - configuration.GetInt(prefix+"connection_max_lifetime_seconds"), + ReadConnection(configuration, prefix), ) } -func (store *PostgresStore) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode, sslcert, sslkey, sslrootcert, sslcrl string, pgbouncerCompatible bool, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { +func (store *PostgresStore) initialize(createTable, upsertQuery string, enableUpsert bool, conn Connection) (err error) { store.SupportBucketTable = false createTable = ResolveCreateTableQuery(createTable) @@ -78,55 +56,7 @@ func (store *PostgresStore) initialize(createTable, upsertQuery string, enableUp } store.SqlGenerator = gen - // pgx-optimized connection string with better timeouts and connection handling - sqlUrl := "connect_timeout=30" - - if hostname != "" { - sqlUrl += " host=" + hostname - } - if port != 0 { - sqlUrl += " port=" + strconv.Itoa(port) - } - - // SSL configuration - pgx provides better SSL support than lib/pq - if sslmode != "" { - sqlUrl += " sslmode=" + sslmode - } - if sslcert != "" { - sqlUrl += " sslcert=" + sslcert - } - if sslkey != "" { - sqlUrl += " sslkey=" + sslkey - } - if sslrootcert != "" { - sqlUrl += " sslrootcert=" + sslrootcert - } - if sslcrl != "" { - sqlUrl += " sslcrl=" + sslcrl - } - if user != "" { - sqlUrl += " user=" + user - } - adaptedSqlUrl := sqlUrl - if password != "" { - sqlUrl += " password=" + password - adaptedSqlUrl += " password=ADAPTED" - } - if database != "" { - sqlUrl += " dbname=" + database - adaptedSqlUrl += " dbname=" + database - } - if schema != "" && !pgbouncerCompatible { - sqlUrl += " search_path=" + schema - adaptedSqlUrl += " search_path=" + schema - } - db, openErr := OpenPGXDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds) - if openErr != nil { - return openErr - } - if err = store.UseConnectionPools(db, func() (*sql.DB, error) { - return OpenPGXDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds) - }, maxIdle, maxOpen, maxLifetimeSeconds); err != nil { + if err = conn.Open(&store.AbstractSqlStore); err != nil { return err } diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index d523a2abbc..2215d18db1 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -9,9 +9,7 @@ package postgres2 import ( "context" - "database/sql" "fmt" - "strconv" "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql" @@ -34,12 +32,6 @@ func (store *PostgresStore2) GetName() string { } func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) { - // Fewer idle slots than concurrent operations means a fresh connection per - // operation, until the filer runs out of ephemeral ports. connection_max_open - // stays unset: a listing runs a second query from its own callback, so a - // bounded pool deadlocks once the concurrency reaches it. - configuration.SetDefault(prefix+"connection_max_idle", 50) - configuration.SetDefault(prefix+"connection_max_lifetime_seconds", 300) // Default on so minimal configs are not exposed to duplicate-key tx // poisoning on Postgres; an explicit false still disables it. configuration.SetDefault(prefix+"enableUpsert", true) @@ -47,25 +39,11 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix configuration.GetString(prefix+"createTable"), configuration.GetString(prefix+"upsertQuery"), configuration.GetBool(prefix+"enableUpsert"), - configuration.GetString(prefix+"username"), - configuration.GetString(prefix+"password"), - configuration.GetString(prefix+"hostname"), - configuration.GetInt(prefix+"port"), - configuration.GetString(prefix+"database"), - configuration.GetString(prefix+"schema"), - configuration.GetString(prefix+"sslmode"), - configuration.GetString(prefix+"sslcert"), - configuration.GetString(prefix+"sslkey"), - configuration.GetString(prefix+"sslrootcert"), - configuration.GetString(prefix+"sslcrl"), - configuration.GetBool(prefix+"pgbouncer_compatible"), - configuration.GetInt(prefix+"connection_max_idle"), - configuration.GetInt(prefix+"connection_max_open"), - configuration.GetInt(prefix+"connection_max_lifetime_seconds"), + postgres.ReadConnection(configuration, prefix), ) } -func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode, sslcert, sslkey, sslrootcert, sslcrl string, pgbouncerCompatible bool, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { +func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, conn postgres.Connection) (err error) { store.SupportBucketTable = true store.SkipDDL = createTable == "false" @@ -85,55 +63,7 @@ func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableU } store.SqlGenerator = gen - // pgx-optimized connection string with better timeouts and connection handling - sqlUrl := "connect_timeout=30" - - if hostname != "" { - sqlUrl += " host=" + hostname - } - if port != 0 { - sqlUrl += " port=" + strconv.Itoa(port) - } - - // SSL configuration - pgx provides better SSL support than lib/pq - if sslmode != "" { - sqlUrl += " sslmode=" + sslmode - } - if sslcert != "" { - sqlUrl += " sslcert=" + sslcert - } - if sslkey != "" { - sqlUrl += " sslkey=" + sslkey - } - if sslrootcert != "" { - sqlUrl += " sslrootcert=" + sslrootcert - } - if sslcrl != "" { - sqlUrl += " sslcrl=" + sslcrl - } - if user != "" { - sqlUrl += " user=" + user - } - adaptedSqlUrl := sqlUrl - if password != "" { - sqlUrl += " password=" + password - adaptedSqlUrl += " password=ADAPTED" - } - if database != "" { - sqlUrl += " dbname=" + database - adaptedSqlUrl += " dbname=" + database - } - if schema != "" && !pgbouncerCompatible { - sqlUrl += " search_path=" + schema - adaptedSqlUrl += " search_path=" + schema - } - db, openErr := postgres.OpenPGXDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds) - if openErr != nil { - return openErr - } - if err = store.UseConnectionPools(db, func() (*sql.DB, error) { - return postgres.OpenPGXDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds) - }, maxIdle, maxOpen, maxLifetimeSeconds); err != nil { + if err = conn.Open(&store.AbstractSqlStore); err != nil { return err } diff --git a/weed/filer/postgres2/postgres2_store_test.go b/weed/filer/postgres2/postgres2_store_test.go index 4b4bc708d1..aba121c374 100644 --- a/weed/filer/postgres2/postgres2_store_test.go +++ b/weed/filer/postgres2/postgres2_store_test.go @@ -3,13 +3,14 @@ package postgres2 import ( "testing" + "github.com/seaweedfs/seaweedfs/weed/filer/postgres" "github.com/seaweedfs/seaweedfs/weed/filer/store_test" ) func TestStoreConformance(t *testing.T) { - db := store_test.NewPostgresTestDatabase(t) store := &PostgresStore2{} - if err := store.initialize("", "", true, db.User, db.Password, db.Hostname, db.Port, db.Database, db.Schema, db.SSLMode, "", "", "", "", false, 10, 0, 60); err != nil { + conn := postgres.Connection{DSNOptions: store_test.NewPostgresTestDatabase(t), MaxIdle: 10, MaxLifetimeSeconds: 60} + if err := store.initialize("", "", true, conn); err != nil { t.Fatal(err) } defer store.Shutdown()