mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
filer/postgres: postgres and postgres2 share their connection setup (#13252)
* filer/store_test: conformance suite for filer stores Entries in and out of buckets, byte-ordered and prefixed listings, key-value records, transactions, bucket tables and the snapshot scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer: run postgres2 and mysql2 through the conformance suite RUN_POSTGRES_TESTS and RUN_MYSQL_TESTS enable them, as RUN_REDIS_TESTS does the Redis stores; each test gets a schema or database of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: give the go test job Postgres and MySQL services Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: generators bind the arguments of row statements Insert, update, find and delete now take the row key and return their arguments with the query, so a generator decides how a key maps to its columns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: generators bind the arguments of directory statements Listing and deleting a directory's children take the directory and return their arguments with the query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: batch lookup takes row keys and matches rows by name and directory A key-value key's directory column already determines its dirhash, so the batch read no longer selects dirhash to match its rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: build the connection string with pgxutil.BuildDSN The credential store already does; it quotes values per libpq, so a password or certificate path with spaces or quotes no longer breaks the connection string. The OpenPGXDB alias has no callers left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/sql: scan pages take the row key of the last row as their cursor Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * 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 * filer/sql: test the arguments each generator binds Each statement's arguments must match its placeholders in count and column. Check them for every generator method, including the Postgres UPDATE whose parameters are now numbered key first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer/postgres: escape the table name inside its quotes A double quote in it is doubled, as PostgreSQL reads a quoted identifier. Bucket names cannot hold one; the statements no longer rely on that. Reported by CodeQL (go/unsafe-quoting) on the lines this branch changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/postgres: keep the nameExpr doc comment on nameExpr Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
abe623c570
commit
ca09dae400
5 files changed
+65
-162
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user