Skip to content

Commit cb22aab

Browse files
authored
[Advanced Visibility with SQL] Adding PostgreSQL 12 schema and interface (#3844)
* Create base schema for PostgreSQL 12 * Changes to PostgreSQL 12 visibility schame to support advanced visibility * Create PostgreSQL 12 db interface and configs
1 parent f3cf504 commit cb22aab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1448
-16
lines changed

Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,17 @@ install-schema-postgresql: temporal-sql-tool
383383
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(VISIBILITY_DB) setup-schema -v 0.0
384384
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(VISIBILITY_DB) update-schema -d ./schema/postgresql/v96/visibility/versioned
385385

386+
install-schema-postgresql12: temporal-sql-tool
387+
@printf $(COLOR) "Install Postgres schema..."
388+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(TEMPORAL_DB) drop -f
389+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(TEMPORAL_DB) create
390+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(TEMPORAL_DB) setup -v 0.0
391+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(TEMPORAL_DB) update-schema -d ./schema/postgresql/v12/temporal/versioned
392+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(VISIBILITY_DB) drop -f
393+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(VISIBILITY_DB) create
394+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(VISIBILITY_DB) setup-schema -v 0.0
395+
./temporal-sql-tool -u $(SQL_USER) --pw $(SQL_PASSWORD) -p 5432 --pl postgres --db $(VISIBILITY_DB) update-schema -d ./schema/postgresql/v12/visibility/versioned
396+
386397
install-schema-es:
387398
@printf $(COLOR) "Install Elasticsearch schema..."
388399
curl --fail -X PUT "http://127.0.0.1:9200/_cluster/settings" -H "Content-Type: application/json" --data-binary @./schema/elasticsearch/visibility/cluster_settings_v7.json --write-out "\n"
@@ -450,6 +461,9 @@ start-mysql-es: temporal-server
450461
start-postgres: temporal-server
451462
./temporal-server --env development-postgres --allow-no-auth start
452463

464+
start-postgres12: temporal-server
465+
./temporal-server --env development-postgres12 --allow-no-auth start
466+
453467
start-sqlite: temporal-server
454468
./temporal-server --env development-sqlite --allow-no-auth start
455469

common/persistence/persistence-tests/persistenceTestBase.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func NewTestBaseWithSQL(options *TestBaseOptions) TestBase {
138138
switch options.SQLDBPluginName {
139139
case mysql.PluginName, mysql.PluginNameV8:
140140
options.DBPort = environment.GetMySQLPort()
141-
case postgresql.PluginName:
141+
case postgresql.PluginName, postgresql.PluginNameV12:
142142
options.DBPort = environment.GetPostgreSQLPort()
143143
case sqlite.PluginName:
144144
options.DBPort = 0

common/persistence/persistence-tests/setup.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ const (
3838
testMySQLSchemaDir = "schema/mysql/v57"
3939
testMySQL8SchemaDir = "schema/mysql/v8"
4040

41-
testPostgreSQLUser = "temporal"
42-
testPostgreSQLPassword = "temporal"
43-
testPostgreSQLSchemaDir = "schema/postgresql/v96"
41+
testPostgreSQLUser = "temporal"
42+
testPostgreSQLPassword = "temporal"
43+
testPostgreSQLSchemaDir = "schema/postgresql/v96"
44+
testPostgreSQL12SchemaDir = "schema/postgresql/v12"
4445

4546
testSQLiteUser = ""
4647
testSQLitePassword = ""
@@ -88,6 +89,19 @@ func GetPostgreSQLTestClusterOption() *TestBaseOptions {
8889
}
8990
}
9091

92+
// GetPostgreSQL12TestClusterOption return test options
93+
func GetPostgreSQL12TestClusterOption() *TestBaseOptions {
94+
return &TestBaseOptions{
95+
SQLDBPluginName: postgresql.PluginName,
96+
DBUsername: testPostgreSQLUser,
97+
DBPassword: testPostgreSQLPassword,
98+
DBHost: environment.GetPostgreSQLAddress(),
99+
DBPort: environment.GetPostgreSQLPort(),
100+
SchemaDir: testPostgreSQL12SchemaDir,
101+
StoreType: config.StoreTypeSQL,
102+
}
103+
}
104+
91105
// GetSQLiteTestClusterOption return test options
92106
func GetSQLiteFileTestClusterOption() *TestBaseOptions {
93107
return &TestBaseOptions{

common/persistence/sql/sqlplugin/postgresql/db.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333

3434
"go.temporal.io/server/common/persistence/schema"
3535
"go.temporal.io/server/common/persistence/sql/sqlplugin"
36-
postgresqlschema "go.temporal.io/server/schema/postgresql"
36+
postgresqlschemaV96 "go.temporal.io/server/schema/postgresql/v96"
3737
)
3838

3939
// ErrDupEntryCode indicates a duplicate primary key i.e. the row already exists,
@@ -119,9 +119,9 @@ func (pdb *db) DbName() string {
119119
func (pdb *db) ExpectedVersion() string {
120120
switch pdb.dbKind {
121121
case sqlplugin.DbKindMain:
122-
return postgresqlschema.Version
122+
return postgresqlschemaV96.Version
123123
case sqlplugin.DbKindVisibility:
124-
return postgresqlschema.VisibilityVersion
124+
return postgresqlschemaV96.VisibilityVersion
125125
default:
126126
panic(fmt.Sprintf("unknown db kind %v", pdb.dbKind))
127127
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// The MIT License
2+
//
3+
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
4+
//
5+
// Copyright (c) 2020 Uber Technologies, Inc.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
package postgresql
26+
27+
import (
28+
"context"
29+
"fmt"
30+
31+
"github.com/jmoiron/sqlx"
32+
33+
"go.temporal.io/server/common/persistence/schema"
34+
"go.temporal.io/server/common/persistence/sql/sqlplugin"
35+
postgresqlschemaV12 "go.temporal.io/server/schema/postgresql/v12"
36+
)
37+
38+
// dbV12 represents a logical connection to mysql database
39+
type dbV12 struct {
40+
db
41+
}
42+
43+
var _ sqlplugin.DB = (*dbV12)(nil)
44+
var _ sqlplugin.Tx = (*dbV12)(nil)
45+
46+
// newDB returns an instance of DB, which is a logical
47+
// connection to the underlying postgresql database
48+
func newDBV12(
49+
dbKind sqlplugin.DbKind,
50+
dbName string,
51+
xdb *sqlx.DB,
52+
tx *sqlx.Tx,
53+
) *dbV12 {
54+
mdb := &dbV12{
55+
db: db{
56+
dbKind: dbKind,
57+
dbName: dbName,
58+
db: xdb,
59+
tx: tx,
60+
},
61+
}
62+
mdb.conn = xdb
63+
if tx != nil {
64+
mdb.conn = tx
65+
}
66+
mdb.converter = &converter{}
67+
return mdb
68+
}
69+
70+
// BeginTx starts a new transaction and returns a reference to the Tx object
71+
func (pdb *dbV12) BeginTx(ctx context.Context) (sqlplugin.Tx, error) {
72+
xtx, err := pdb.db.db.BeginTxx(ctx, nil)
73+
if err != nil {
74+
return nil, err
75+
}
76+
return newDB(pdb.dbKind, pdb.dbName, pdb.db.db, xtx), nil
77+
}
78+
79+
// PluginName returns the name of the mysql plugin
80+
func (pdb *dbV12) PluginName() string {
81+
return PluginNameV12
82+
}
83+
84+
// ExpectedVersion returns expected version.
85+
func (pdb *dbV12) ExpectedVersion() string {
86+
switch pdb.dbKind {
87+
case sqlplugin.DbKindMain:
88+
return postgresqlschemaV12.Version
89+
case sqlplugin.DbKindVisibility:
90+
return postgresqlschemaV12.VisibilityVersion
91+
default:
92+
panic(fmt.Sprintf("unknown db kind %v", pdb.dbKind))
93+
}
94+
}
95+
96+
// VerifyVersion verify schema version is up to date
97+
func (pdb *dbV12) VerifyVersion() error {
98+
expectedVersion := pdb.ExpectedVersion()
99+
return schema.VerifyCompatibleVersion(pdb, pdb.dbName, expectedVersion)
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// The MIT License
2+
//
3+
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
4+
//
5+
// Copyright (c) 2020 Uber Technologies, Inc.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
package postgresql
26+
27+
import (
28+
"go.temporal.io/server/common/config"
29+
"go.temporal.io/server/common/persistence/sql"
30+
"go.temporal.io/server/common/persistence/sql/sqlplugin"
31+
"go.temporal.io/server/common/resolver"
32+
)
33+
34+
const (
35+
// PluginName is the name of the plugin
36+
PluginNameV12 = "postgres12"
37+
)
38+
39+
type pluginV12 struct {
40+
plugin
41+
}
42+
43+
var _ sqlplugin.Plugin = (*pluginV12)(nil)
44+
45+
func init() {
46+
sql.RegisterPlugin(PluginNameV12, &pluginV12{})
47+
}
48+
49+
// CreateDB initialize the db object
50+
func (d *pluginV12) CreateDB(
51+
dbKind sqlplugin.DbKind,
52+
cfg *config.SQL,
53+
r resolver.ServiceResolver,
54+
) (sqlplugin.DB, error) {
55+
conn, err := d.createDBConnection(cfg, r)
56+
if err != nil {
57+
return nil, err
58+
}
59+
db := newDBV12(dbKind, cfg.DatabaseName, conn, nil)
60+
return db, nil
61+
}
62+
63+
// CreateAdminDB initialize the adminDB object
64+
func (d *pluginV12) CreateAdminDB(
65+
dbKind sqlplugin.DbKind,
66+
cfg *config.SQL,
67+
r resolver.ServiceResolver,
68+
) (sqlplugin.AdminDB, error) {
69+
conn, err := d.createDBConnection(cfg, r)
70+
if err != nil {
71+
return nil, err
72+
}
73+
db := newDBV12(dbKind, cfg.DatabaseName, conn, nil)
74+
return db, nil
75+
}

common/persistence/tests/postgresql_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ func TestPostgreSQLVisibilityPersistenceSuite(t *testing.T) {
148148
suite.Run(t, s)
149149
}
150150

151+
func TestPostgreSQL12VisibilityPersistenceSuite(t *testing.T) {
152+
s := &VisibilityPersistenceSuite{
153+
TestBase: persistencetests.NewTestBaseWithSQL(persistencetests.GetPostgreSQL12TestClusterOption()),
154+
}
155+
suite.Run(t, s)
156+
}
157+
151158
// TODO: Merge persistence-tests into the tests directory.
152159

153160
func TestPostgreSQLHistoryV2PersistenceSuite(t *testing.T) {
@@ -189,6 +196,27 @@ FAIL: TestPostgreSQLQueuePersistence/TestNamespaceReplicationQueue (0.26s)
189196
// suite.Run(t, s)
190197
//}
191198

199+
func TestPostgreSQL12HistoryV2PersistenceSuite(t *testing.T) {
200+
s := new(persistencetests.HistoryV2PersistenceSuite)
201+
s.TestBase = persistencetests.NewTestBaseWithSQL(persistencetests.GetPostgreSQL12TestClusterOption())
202+
s.TestBase.Setup(nil)
203+
suite.Run(t, s)
204+
}
205+
206+
func TestPostgreSQL12MetadataPersistenceSuiteV2(t *testing.T) {
207+
s := new(persistencetests.MetadataPersistenceSuiteV2)
208+
s.TestBase = persistencetests.NewTestBaseWithSQL(persistencetests.GetPostgreSQL12TestClusterOption())
209+
s.TestBase.Setup(nil)
210+
suite.Run(t, s)
211+
}
212+
213+
func TestPostgreSQL12ClusterMetadataPersistence(t *testing.T) {
214+
s := new(persistencetests.ClusterMetadataManagerSuite)
215+
s.TestBase = persistencetests.NewTestBaseWithSQL(persistencetests.GetPostgreSQL12TestClusterOption())
216+
s.TestBase.Setup(nil)
217+
suite.Run(t, s)
218+
}
219+
192220
// SQL store tests
193221

194222
func TestPostgreSQLNamespaceSuite(t *testing.T) {

common/persistence/tests/postgresql_test_util.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
p "go.temporal.io/server/common/persistence"
3939
"go.temporal.io/server/common/persistence/sql"
4040
"go.temporal.io/server/common/persistence/sql/sqlplugin"
41-
_ "go.temporal.io/server/common/persistence/sql/sqlplugin/postgresql"
41+
"go.temporal.io/server/common/persistence/sql/sqlplugin/postgresql"
4242
"go.temporal.io/server/common/resolver"
4343
"go.temporal.io/server/common/shuffle"
4444
"go.temporal.io/server/environment"
@@ -56,8 +56,8 @@ const (
5656

5757
// TODO hard code this dir for now
5858
// need to merge persistence test config / initialization in one place
59-
testPostgreSQLExecutionSchema = "../../../schema/postgresql/v96/temporal/schema.sql"
60-
testPostgreSQLVisibilitySchema = "../../../schema/postgresql/v96/visibility/schema.sql"
59+
testPostgreSQLExecutionSchema = "../../../schema/postgresql/v12/temporal/schema.sql"
60+
testPostgreSQLVisibilitySchema = "../../../schema/postgresql/v12/visibility/schema.sql"
6161
)
6262

6363
type (
@@ -100,7 +100,7 @@ func NewPostgreSQLConfig() *config.SQL {
100100
strconv.Itoa(environment.GetPostgreSQLPort()),
101101
),
102102
ConnectProtocol: testPostgreSQLConnectionProtocol,
103-
PluginName: "postgres",
103+
PluginName: postgresql.PluginNameV12,
104104
DatabaseName: testPostgreSQLDatabaseNamePrefix + shuffle.String(testPostgreSQLDatabaseNameSuffix),
105105
}
106106
}

0 commit comments

Comments
 (0)