filer/postgres: postgres and postgres2 share their connection setup

ReadConnection reads the settings and their defaults, Connection.Open
opens the store's pools.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-01 16:35:16 -08:00
1 parent cb9f2095c9
commit 19573691ca
4 files changed
+65 -92

No files matched your search

@@ -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)
}
+3 -45
View File
@@ -9,13 +9,11 @@ package postgres
import (
"context"
"database/sql"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/pgxutil"
)
func init() {
@@ -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,27 +56,7 @@ func (store *PostgresStore) initialize(createTable, upsertQuery string, enableUp
}
store.SqlGenerator = gen
sqlUrl, adaptedSqlUrl := pgxutil.BuildDSN(pgxutil.DSNOptions{
Hostname: hostname,
Port: port,
User: user,
Password: password,
Database: database,
Schema: schema,
SSLMode: sslmode,
SSLCert: sslcert,
SSLKey: sslkey,
SSLRootCert: sslrootcert,
SSLCRL: sslcrl,
PgBouncerCompatible: pgbouncerCompatible,
})
db, openErr := pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds)
if openErr != nil {
return openErr
}
if err = store.UseConnectionPools(db, func() (*sql.DB, error) {
return pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds)
}, maxIdle, maxOpen, maxLifetimeSeconds); err != nil {
if err = conn.Open(&store.AbstractSqlStore); err != nil {
return err
}
+3 -45
View File
@@ -9,14 +9,12 @@ package postgres2
import (
"context"
"database/sql"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
"github.com/seaweedfs/seaweedfs/weed/filer/postgres"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/pgxutil"
)
var _ filer.BucketAware = (*PostgresStore2)(nil)
@@ -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,27 +63,7 @@ func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableU
}
store.SqlGenerator = gen
sqlUrl, adaptedSqlUrl := pgxutil.BuildDSN(pgxutil.DSNOptions{
Hostname: hostname,
Port: port,
User: user,
Password: password,
Database: database,
Schema: schema,
SSLMode: sslmode,
SSLCert: sslcert,
SSLKey: sslkey,
SSLRootCert: sslrootcert,
SSLCRL: sslcrl,
PgBouncerCompatible: pgbouncerCompatible,
})
db, openErr := pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds)
if openErr != nil {
return openErr
}
if err = store.UseConnectionPools(db, func() (*sql.DB, error) {
return pgxutil.OpenDB(sqlUrl, adaptedSqlUrl, pgbouncerCompatible, maxIdle, maxOpen, maxLifetimeSeconds)
}, maxIdle, maxOpen, maxLifetimeSeconds); err != nil {
if err = conn.Open(&store.AbstractSqlStore); err != nil {
return err
}
+3 -2
View File
@@ -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()