From d5add83e85da0c61fe107842e7dd82b52af2bcdb Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 19 Jan 2021 18:07:29 -0800 Subject: filer store: add postgres2 --- weed/filer/postgres2/postgres2_store.go | 87 +++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 weed/filer/postgres2/postgres2_store.go (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go new file mode 100644 index 000000000..82552376f --- /dev/null +++ b/weed/filer/postgres2/postgres2_store.go @@ -0,0 +1,87 @@ +package postgres2 + +import ( + "context" + "database/sql" + "fmt" + + "github.com/chrislusf/seaweedfs/weed/filer" + "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql" + "github.com/chrislusf/seaweedfs/weed/filer/postgres" + "github.com/chrislusf/seaweedfs/weed/util" + _ "github.com/lib/pq" +) + +const ( + CONNECTION_URL_PATTERN = "host=%s port=%d sslmode=%s connect_timeout=30" +) + +func init() { + filer.Stores = append(filer.Stores, &PostgresStore2{}) +} + +type PostgresStore2 struct { + abstract_sql.AbstractSqlStore +} + +func (store *PostgresStore2) GetName() string { + return "postgres2" +} + +func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) { + return store.initialize( + configuration.GetString(prefix+"createTable"), + 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.GetInt(prefix+"connection_max_idle"), + configuration.GetInt(prefix+"connection_max_open"), + ) +} + +func (store *PostgresStore2) initialize(createTable, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen int) (err error) { + + store.SupportBucketTable = true + store.SqlGenerator = &postgres.SqlGenPostgres{ + CreateTableSqlTemplate: createTable, + DropTableSqlTemplate: "drop table %s", + } + + sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) + if user != "" { + sqlUrl += " user=" + user + } + if password != "" { + sqlUrl += " password=" + password + } + if database != "" { + sqlUrl += " dbname=" + database + } + if schema != "" { + sqlUrl += " search_path=" + schema + } + var dbErr error + store.DB, dbErr = sql.Open("postgres", sqlUrl) + if dbErr != nil { + store.DB.Close() + store.DB = nil + return fmt.Errorf("can not connect to %s error:%v", sqlUrl, err) + } + + store.DB.SetMaxIdleConns(maxIdle) + store.DB.SetMaxOpenConns(maxOpen) + + if err = store.DB.Ping(); err != nil { + return fmt.Errorf("connect to %s error:%v", sqlUrl, err) + } + + if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil { + return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err) + } + + return nil +} -- cgit v1.2.3 From 51b4963e2eaa6caff8ed4702453b9af371cf6914 Mon Sep 17 00:00:00 2001 From: LazyDBA247-Anyvision Date: Sun, 14 Feb 2021 13:14:36 +0200 Subject: postgres2 & memsql2 add escape (quote identifiers) for the dynamic sql so tables (collections) with special characters will work. --- weed/filer/postgres2/postgres2_store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index 82552376f..94c760401 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -48,7 +48,7 @@ func (store *PostgresStore2) initialize(createTable, user, password, hostname st store.SupportBucketTable = true store.SqlGenerator = &postgres.SqlGenPostgres{ CreateTableSqlTemplate: createTable, - DropTableSqlTemplate: "drop table %s", + DropTableSqlTemplate: `drop table "%s"`, } sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) -- cgit v1.2.3 From 7f458d5e78e380890771d925fee3df4f5f692e78 Mon Sep 17 00:00:00 2001 From: LazyDBA247-Anyvision Date: Mon, 15 Feb 2021 07:45:09 +0200 Subject: better postgres connection pool management adding SetConnMaxLifetime configuration (https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime) to enable refresh of stale connections. --- weed/filer/postgres2/postgres2_store.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index 94c760401..44487fd0e 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "time" "github.com/chrislusf/seaweedfs/weed/filer" "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql" @@ -40,10 +41,11 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix configuration.GetString(prefix+"sslmode"), configuration.GetInt(prefix+"connection_max_idle"), configuration.GetInt(prefix+"connection_max_open"), + configuration.GetInt(prefix+"connection_max_lifetime_seconds"), ) } -func (store *PostgresStore2) initialize(createTable, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen int) (err error) { +func (store *PostgresStore2) initialize(createTable, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { store.SupportBucketTable = true store.SqlGenerator = &postgres.SqlGenPostgres{ @@ -74,6 +76,7 @@ func (store *PostgresStore2) initialize(createTable, user, password, hostname st store.DB.SetMaxIdleConns(maxIdle) store.DB.SetMaxOpenConns(maxOpen) + store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second) if err = store.DB.Ping(); err != nil { return fmt.Errorf("connect to %s error:%v", sqlUrl, err) -- cgit v1.2.3 From 3f8b0da6772ea99b600303bb1ad3f6430a45290a Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 17 Feb 2021 02:13:50 -0800 Subject: filer: do not print password on error fix https://github.com/chrislusf/seaweedfs/issues/1809 --- weed/filer/postgres2/postgres2_store.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index 44487fd0e..4b5fbc8cf 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -57,21 +57,25 @@ func (store *PostgresStore2) initialize(createTable, user, password, hostname st 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 != "" { sqlUrl += " search_path=" + schema + adaptedSqlUrl += " search_path=" + schema } var dbErr error store.DB, dbErr = sql.Open("postgres", sqlUrl) if dbErr != nil { store.DB.Close() store.DB = nil - return fmt.Errorf("can not connect to %s error:%v", sqlUrl, err) + return fmt.Errorf("can not connect to %s error:%v", adaptedSqlUrl, err) } store.DB.SetMaxIdleConns(maxIdle) -- cgit v1.2.3 From 3575d41009e4367658e75e6ae780c6260b80daf9 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 17 Feb 2021 20:57:08 -0800 Subject: go fmt --- weed/filer/postgres2/postgres2_store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index 4b5fbc8cf..92893bf7a 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -50,7 +50,7 @@ func (store *PostgresStore2) initialize(createTable, user, password, hostname st store.SupportBucketTable = true store.SqlGenerator = &postgres.SqlGenPostgres{ CreateTableSqlTemplate: createTable, - DropTableSqlTemplate: `drop table "%s"`, + DropTableSqlTemplate: `drop table "%s"`, } sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) -- cgit v1.2.3 From 4a02389eb08c3b7fcb7fe157f51e526df5220ac4 Mon Sep 17 00:00:00 2001 From: LazyDBA247-Anyvision Date: Mon, 29 Mar 2021 09:58:13 +0300 Subject: Adding custom insertQuery support for postgres/2 mysql/2 --- weed/filer/postgres2/postgres2_store.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index 92893bf7a..b83945db6 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -32,6 +32,7 @@ func (store *PostgresStore2) GetName() string { func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) { return store.initialize( configuration.GetString(prefix+"createTable"), + configuration.GetString(prefix+"insertQuery"), configuration.GetString(prefix+"username"), configuration.GetString(prefix+"password"), configuration.GetString(prefix+"hostname"), @@ -45,12 +46,13 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix ) } -func (store *PostgresStore2) initialize(createTable, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { +func (store *PostgresStore2) initialize(createTable, insertQuery, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { store.SupportBucketTable = true store.SqlGenerator = &postgres.SqlGenPostgres{ CreateTableSqlTemplate: createTable, DropTableSqlTemplate: `drop table "%s"`, + InsertQueryTemplate: insertQuery, } sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) -- cgit v1.2.3 From 4c51e6a6605ce4592957c047c73c345a0a12bae3 Mon Sep 17 00:00:00 2001 From: LazyDBA247-Anyvision Date: Tue, 30 Mar 2021 00:32:03 +0300 Subject: add enableUpsert=true and rename config to upsertQuery --- weed/filer/postgres2/postgres2_store.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index b83945db6..b5947bb96 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -32,7 +32,8 @@ func (store *PostgresStore2) GetName() string { func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) { return store.initialize( configuration.GetString(prefix+"createTable"), - configuration.GetString(prefix+"insertQuery"), + configuration.GetString(prefix+"upsertQuery"), + configuration.GetString(prefix+"enableUpsert"), configuration.GetString(prefix+"username"), configuration.GetString(prefix+"password"), configuration.GetString(prefix+"hostname"), @@ -46,13 +47,16 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix ) } -func (store *PostgresStore2) initialize(createTable, insertQuery, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { +func (store *PostgresStore2) initialize(createTable, upsertQuery, enableUpsert, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { store.SupportBucketTable = true + if !enableUpsert { + upsertQuery = "" + } store.SqlGenerator = &postgres.SqlGenPostgres{ CreateTableSqlTemplate: createTable, DropTableSqlTemplate: `drop table "%s"`, - InsertQueryTemplate: insertQuery, + UpsertQueryTemplate: upsertQuery, } sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) -- cgit v1.2.3 From 7f44d953b56dc74281b6de3dcac184d97c9715f1 Mon Sep 17 00:00:00 2001 From: LazyDBA247-Anyvision Date: Tue, 30 Mar 2021 01:36:02 +0300 Subject: fix GetBool --- weed/filer/postgres2/postgres2_store.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index b5947bb96..01fc4a869 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -33,7 +33,7 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix return store.initialize( configuration.GetString(prefix+"createTable"), configuration.GetString(prefix+"upsertQuery"), - configuration.GetString(prefix+"enableUpsert"), + configuration.GetBool(prefix+"enableUpsert"), configuration.GetString(prefix+"username"), configuration.GetString(prefix+"password"), configuration.GetString(prefix+"hostname"), @@ -47,7 +47,7 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix ) } -func (store *PostgresStore2) initialize(createTable, upsertQuery, enableUpsert, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { +func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { store.SupportBucketTable = true if !enableUpsert { -- cgit v1.2.3 From 8e404a1433fe437de22b3356176cf36eb3719018 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 2 Apr 2021 02:22:26 -0700 Subject: go fmt --- weed/filer/postgres2/postgres2_store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'weed/filer/postgres2/postgres2_store.go') diff --git a/weed/filer/postgres2/postgres2_store.go b/weed/filer/postgres2/postgres2_store.go index 01fc4a869..0f573d8d0 100644 --- a/weed/filer/postgres2/postgres2_store.go +++ b/weed/filer/postgres2/postgres2_store.go @@ -51,7 +51,7 @@ func (store *PostgresStore2) initialize(createTable, upsertQuery string, enableU store.SupportBucketTable = true if !enableUpsert { - upsertQuery = "" + upsertQuery = "" } store.SqlGenerator = &postgres.SqlGenPostgres{ CreateTableSqlTemplate: createTable, -- cgit v1.2.3