forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence.go
198 lines (181 loc) · 6.99 KB
/
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
// Copyright (c) 2017 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 config
import (
"fmt"
"github.com/uber/cadence/common"
)
const (
// StoreTypeSQL refers to sql based storage as persistence store
StoreTypeSQL = "sql"
// StoreTypeCassandra refers to cassandra as persistence store
StoreTypeCassandra = "cassandra"
)
// DefaultStoreType returns the storeType for the default persistence store
func (c *Persistence) DefaultStoreType() string {
if c.DataStores[c.DefaultStore].SQL != nil {
return StoreTypeSQL
}
return StoreTypeCassandra
}
// FillDefaults populates default values for unspecified fields in persistence config
func (c *Persistence) FillDefaults() {
for k, store := range c.DataStores {
if store.Cassandra != nil && store.NoSQL == nil {
// for backward-compatibility
store.NoSQL = store.Cassandra
store.NoSQL.PluginName = "cassandra"
}
if store.SQL != nil {
// filling default encodingType/decodingTypes for SQL persistence
if store.SQL.EncodingType == "" {
store.SQL.EncodingType = string(common.EncodingTypeThriftRW)
}
if len(store.SQL.DecodingTypes) == 0 {
store.SQL.DecodingTypes = []string{
string(common.EncodingTypeThriftRW),
}
}
if store.SQL.NumShards == 0 {
store.SQL.NumShards = 1
}
}
// write changes back to DataStores, as ds is a value object
c.DataStores[k] = store
}
}
// Validate validates the persistence config
func (c *Persistence) Validate() error {
dbStoreKeys := []string{c.DefaultStore}
useAdvancedVisibilityOnly := false
if _, ok := c.DataStores[c.VisibilityStore]; ok {
dbStoreKeys = append(dbStoreKeys, c.VisibilityStore)
} else {
if _, ok := c.DataStores[c.AdvancedVisibilityStore]; !ok {
return fmt.Errorf("must provide one of VisibilityStore and AdvancedVisibilityStore")
}
useAdvancedVisibilityOnly = true
}
for _, st := range dbStoreKeys {
ds, ok := c.DataStores[st]
if !ok {
return fmt.Errorf("persistence config: missing config for datastore %v", st)
}
if ds.Cassandra != nil && ds.NoSQL != nil && ds.Cassandra != ds.NoSQL {
return fmt.Errorf("persistence config: datastore %v: only one of Cassandra or NoSQL can be specified", st)
}
configCount := 0
if ds.NoSQL != nil {
configCount++
}
if ds.ShardedNoSQL != nil {
configCount++
}
if ds.SQL != nil {
configCount++
}
if configCount != 1 {
return fmt.Errorf("persistence config: datastore %v: must provide exactly one type of config, but provided %d", st, configCount)
}
if ds.SQL != nil {
if ds.SQL.UseMultipleDatabases {
if !useAdvancedVisibilityOnly {
return fmt.Errorf("sql persistence config: multipleSQLDatabases can only be used with advanced visibility only")
}
if ds.SQL.DatabaseName != "" {
return fmt.Errorf("sql persistence config: databaseName can only be configured in multipleDatabasesConfig when UseMultipleDatabases is true")
}
if ds.SQL.ConnectAddr != "" {
return fmt.Errorf("sql persistence config: connectAddr can only be configured in multipleDatabasesConfig when UseMultipleDatabases is true")
}
if ds.SQL.User != "" {
return fmt.Errorf("sql persistence config: user can only be configured in multipleDatabasesConfig when UseMultipleDatabases is true")
}
if ds.SQL.Password != "" {
return fmt.Errorf("sql persistence config: password can only be configured in multipleDatabasesConfig when UseMultipleDatabases is true")
}
if ds.SQL.NumShards <= 1 || len(ds.SQL.MultipleDatabasesConfig) != ds.SQL.NumShards {
return fmt.Errorf("sql persistence config: nShards must be greater than one and equal to the length of multipleDatabasesConfig")
}
for _, entry := range ds.SQL.MultipleDatabasesConfig {
if entry.DatabaseName == "" {
return fmt.Errorf("sql multipleDatabasesConfig persistence config: databaseName can not be empty")
}
if entry.ConnectAddr == "" {
return fmt.Errorf("sql multipleDatabasesConfig persistence config: connectAddr can not be empty")
}
}
} else {
if ds.SQL.DatabaseName == "" {
return fmt.Errorf("sql persistence config: databaseName can not be empty")
}
if ds.SQL.ConnectAddr == "" {
return fmt.Errorf("sql persistence config: connectAddr can not be empty")
}
}
}
if ds.ShardedNoSQL != nil {
cfg := ds.ShardedNoSQL
connections := cfg.Connections
// validate default shard
if cfg.DefaultShard == "" {
return fmt.Errorf("ShardedNosql config: defaultShard can not be empty")
}
if _, found := connections[cfg.DefaultShard]; !found {
return fmt.Errorf(
"ShardedNosql config: defaultShard (%v) is not defined in connections list", cfg.DefaultShard)
}
// validate history sharding
historyShardMapping := cfg.ShardingPolicy.HistoryShardMapping
currentShardID := 0
for _, shardRange := range historyShardMapping {
if _, found := connections[shardRange.Shard]; !found {
return fmt.Errorf("ShardedNosql config: Unknown history shard name: %v", shardRange.Shard)
}
if shardRange.Start != currentShardID {
return fmt.Errorf("ShardedNosql config: Non-continuous history shard range %v (%v) found while expecting %v",
shardRange.Start,
shardRange.Shard,
currentShardID,
)
}
currentShardID = shardRange.End
}
if currentShardID != c.NumHistoryShards {
return fmt.Errorf("ShardedNosql config: Last history shard found in the config is %v while the max is %v",
currentShardID-1,
c.NumHistoryShards-1,
)
}
// validate tasklist sharding
tasklistShards := cfg.ShardingPolicy.TaskListHashing.ShardOrder
for _, shardName := range tasklistShards {
if _, found := connections[shardName]; !found {
return fmt.Errorf("ShardedNosql config: Unknown tasklist shard name: %v", shardName)
}
}
}
}
return nil
}
// IsAdvancedVisibilityConfigExist returns whether user specified advancedVisibilityStore in config
func (c *Persistence) IsAdvancedVisibilityConfigExist() bool {
return len(c.AdvancedVisibilityStore) != 0
}