-
Notifications
You must be signed in to change notification settings - Fork 780
/
test_sql_persistence.go
228 lines (207 loc) · 6.27 KB
/
test_sql_persistence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package sql
import (
"fmt"
"os"
"path"
"strings"
"time"
"go.temporal.io/server/common/backoff"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/sql/sqlplugin"
"go.temporal.io/server/common/primitives"
"go.temporal.io/server/common/resolver"
"go.temporal.io/server/tests/testutils"
)
// TestCluster allows executing cassandra operations in testing.
type TestCluster struct {
dbName string
schemaDir string
cfg config.SQL
faultInjection *config.FaultInjection
logger log.Logger
}
// NewTestCluster returns a new SQL test cluster
func NewTestCluster(
pluginName string,
dbName string,
username string,
password string,
host string,
port int,
connectAttributes map[string]string,
schemaDir string,
faultInjection *config.FaultInjection,
logger log.Logger,
) *TestCluster {
var result TestCluster
result.logger = logger
result.dbName = dbName
result.schemaDir = schemaDir
result.cfg = config.SQL{
User: username,
Password: password,
ConnectAddr: fmt.Sprintf("%v:%v", host, port),
ConnectProtocol: "tcp",
PluginName: pluginName,
DatabaseName: dbName,
TaskScanPartitions: 4,
ConnectAttributes: connectAttributes,
}
result.faultInjection = faultInjection
return &result
}
// DatabaseName from PersistenceTestCluster interface
func (s *TestCluster) DatabaseName() string {
return s.dbName
}
// SetupTestDatabase from PersistenceTestCluster interface
func (s *TestCluster) SetupTestDatabase() {
s.CreateDatabase()
if s.schemaDir == "" {
s.logger.Info("No schema directory provided, skipping schema setup")
return
}
schemaDir := s.schemaDir + "/"
if !strings.HasPrefix(schemaDir, "/") && !strings.HasPrefix(schemaDir, "../") {
temporalPackageDir := testutils.GetRepoRootDirectory()
schemaDir = path.Join(temporalPackageDir, schemaDir)
}
s.LoadSchema(path.Join(schemaDir, "temporal", "schema.sql"))
s.LoadSchema(path.Join(schemaDir, "visibility", "schema.sql"))
}
// Config returns the persistence config for connecting to this test cluster
func (s *TestCluster) Config() config.Persistence {
cfg := s.cfg
return config.Persistence{
DefaultStore: "test",
VisibilityStore: "test",
DataStores: map[string]config.DataStore{
"test": {SQL: &cfg, FaultInjection: s.faultInjection},
},
TransactionSizeLimit: dynamicconfig.GetIntPropertyFn(primitives.DefaultTransactionSizeLimit),
}
}
// TearDownTestDatabase from PersistenceTestCluster interface
func (s *TestCluster) TearDownTestDatabase() {
s.DropDatabase()
}
// CreateDatabase from PersistenceTestCluster interface
func (s *TestCluster) CreateDatabase() {
cfg2 := s.cfg
// NOTE need to connect with empty name to create new database
if cfg2.PluginName != "sqlite" {
cfg2.DatabaseName = ""
}
var db sqlplugin.AdminDB
var err error
err = backoff.ThrottleRetry(
func() error {
db, err = NewSQLAdminDB(sqlplugin.DbKindUnknown, &cfg2, resolver.NewNoopResolver())
return err
},
backoff.NewExponentialRetryPolicy(time.Second).WithExpirationInterval(time.Minute),
nil,
)
if err != nil {
panic(err)
}
defer func() {
err := db.Close()
if err != nil {
panic(err)
}
}()
err = db.CreateDatabase(s.cfg.DatabaseName)
if err != nil {
panic(err)
}
s.logger.Info("created database", tag.NewStringTag("database", s.cfg.DatabaseName))
}
// DropDatabase from PersistenceTestCluster interface
func (s *TestCluster) DropDatabase() {
cfg2 := s.cfg
if cfg2.PluginName == "sqlite" && cfg2.DatabaseName != ":memory:" && cfg2.ConnectAttributes["mode"] != "memory" {
if len(cfg2.DatabaseName) > 3 { // 3 should mean not ., .., empty, or /
err := os.Remove(cfg2.DatabaseName)
if err != nil {
panic(err)
}
}
return
}
// NOTE need to connect with empty name to drop the database
cfg2.DatabaseName = ""
db, err := NewSQLAdminDB(sqlplugin.DbKindUnknown, &cfg2, resolver.NewNoopResolver())
if err != nil {
panic(err)
}
defer func() {
err := db.Close()
if err != nil {
panic(err)
}
}()
err = db.DropDatabase(s.cfg.DatabaseName)
if err != nil {
panic(err)
}
s.logger.Info("dropped database", tag.NewStringTag("database", s.cfg.DatabaseName))
}
// LoadSchema from PersistenceTestCluster interface
func (s *TestCluster) LoadSchema(schemaFile string) {
statements, err := p.LoadAndSplitQuery([]string{schemaFile})
if err != nil {
s.logger.Fatal("LoadSchema", tag.Error(err))
}
var db sqlplugin.AdminDB
err = backoff.ThrottleRetry(
func() error {
db, err = NewSQLAdminDB(sqlplugin.DbKindUnknown, &s.cfg, resolver.NewNoopResolver())
return err
},
backoff.NewExponentialRetryPolicy(time.Second).WithExpirationInterval(time.Minute),
nil,
)
if err != nil {
panic(err)
}
defer func() {
err := db.Close()
if err != nil {
panic(err)
}
}()
for _, stmt := range statements {
if err = db.Exec(stmt); err != nil {
s.logger.Fatal("LoadSchema", tag.Error(err))
}
}
s.logger.Info("loaded schema")
}