forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
247 lines (219 loc) · 8.01 KB
/
handler.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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 cassandra
import (
"fmt"
"log"
"github.com/urfave/cli"
"github.com/uber/cadence/common/config"
"github.com/uber/cadence/common/persistence/nosql/nosqlplugin/cassandra/gocql"
"github.com/uber/cadence/schema/cassandra"
"github.com/uber/cadence/tools/common/schema"
)
const defaultNumReplicas = 1
// SetupSchemaConfig contains the configuration params needed to setup schema tables
type SetupSchemaConfig struct {
CQLClientConfig
schema.SetupConfig
}
// VerifyCompatibleVersion ensures that the installed version of cadence and visibility keyspaces
// is greater than or equal to the expected version.
// In most cases, the versions should match. However if after a schema upgrade there is a code
// rollback, the code version (expected version) would fall lower than the actual version in
// cassandra.
func VerifyCompatibleVersion(
cfg config.Persistence, expectedConsistency gocql.Consistency,
) error {
if ds, ok := cfg.DataStores[cfg.DefaultStore]; ok {
if err := verifyCompatibleVersion(ds, cassandra.Version, expectedConsistency); err != nil {
return err
}
}
if ds, ok := cfg.DataStores[cfg.VisibilityStore]; ok {
if err := verifyCompatibleVersion(ds, cassandra.VisibilityVersion, expectedConsistency); err != nil {
return err
}
}
return nil
}
func verifyCompatibleVersion(
ds config.DataStore,
expectedCassandraVersion string, expectedConsistency gocql.Consistency,
) error {
if ds.NoSQL != nil {
return verifyPluginVersion(ds.NoSQL, expectedCassandraVersion, expectedConsistency)
}
if ds.ShardedNoSQL != nil {
for shardName, connection := range ds.ShardedNoSQL.Connections {
err := verifyPluginVersion(connection.NoSQLPlugin, expectedCassandraVersion, expectedConsistency)
if err != nil {
return fmt.Errorf("Failed to verify version for DB shard: %v. Error: %v", shardName, err.Error())
}
}
}
// not using nosql
return nil
}
func verifyPluginVersion(plugin *config.NoSQL, expectedCassandraVersion string, expectedConsistency gocql.Consistency) error {
// Use hardcoded instead of constant because of cycle dependency issue.
// However, this file will be refactor to support NoSQL soon. After the refactoring, cycle dependency issue
// should be gone and we can use constant at that time
if plugin.PluginName != "cassandra" {
return fmt.Errorf("unknown NoSQL plugin name: %q", plugin.PluginName)
}
return CheckCompatibleVersion(*plugin, expectedCassandraVersion, expectedConsistency)
}
// CheckCompatibleVersion check the version compatibility
func CheckCompatibleVersion(
cfg config.Cassandra,
expectedVersion string,
expectedConsistency gocql.Consistency,
) error {
client, err := NewCQLClient(&CQLClientConfig{
Hosts: cfg.Hosts,
Port: cfg.Port,
User: cfg.User,
Password: cfg.Password,
Keyspace: cfg.Keyspace,
AllowedAuthenticators: cfg.AllowedAuthenticators,
Timeout: DefaultTimeout,
ConnectTimeout: DefaultConnectTimeout,
TLS: cfg.TLS,
ProtoVersion: cfg.ProtoVersion,
}, expectedConsistency)
if err != nil {
return fmt.Errorf("creating CQL client: %w", err)
}
defer client.Close()
return schema.VerifyCompatibleVersion(client, cfg.Keyspace, expectedVersion)
}
// setupSchema executes the setupSchemaTask
// using the given command line arguments
// as input
func setupSchema(cli *cli.Context) error {
config, err := newCQLClientConfig(cli)
if err != nil {
return handleErr(schema.NewConfigError(err.Error()))
}
client, err := NewCQLClient(config, gocql.All)
if err != nil {
return handleErr(err)
}
defer client.Close()
if err := schema.Setup(cli, client); err != nil {
return handleErr(err)
}
return nil
}
// updateSchema executes the updateSchemaTask
// using the given command lien args as input
func updateSchema(cli *cli.Context) error {
config, err := newCQLClientConfig(cli)
if err != nil {
return handleErr(schema.NewConfigError(err.Error()))
}
client, err := NewCQLClient(config, gocql.All)
if err != nil {
return handleErr(err)
}
defer client.Close()
if err := schema.Update(cli, client); err != nil {
return handleErr(err)
}
return nil
}
// createKeyspace creates a cassandra Keyspace
func createKeyspace(cli *cli.Context) error {
config, err := newCQLClientConfig(cli)
if err != nil {
return handleErr(schema.NewConfigError(err.Error()))
}
keyspace := cli.String(schema.CLIOptKeyspace)
if keyspace == "" {
return handleErr(schema.NewConfigError("missing " + flag(schema.CLIOptKeyspace) + " argument "))
}
datacenter := cli.String(schema.CLIOptDatacenter)
err = doCreateKeyspace(*config, keyspace, datacenter)
if err != nil {
return handleErr(fmt.Errorf("error creating Keyspace:%v", err))
}
return nil
}
func doCreateKeyspace(cfg CQLClientConfig, name string, datacenter string) error {
cfg.Keyspace = SystemKeyspace
client, err := NewCQLClient(&cfg, gocql.All)
if err != nil {
return err
}
defer client.Close()
if datacenter != "" {
return client.CreateNTSKeyspace(name, datacenter)
}
return client.CreateKeyspace(name)
}
func newCQLClientConfig(cli *cli.Context) (*CQLClientConfig, error) {
cqlConfig := new(CQLClientConfig)
cqlConfig.Hosts = cli.GlobalString(schema.CLIOptEndpoint)
cqlConfig.Port = cli.GlobalInt(schema.CLIOptPort)
cqlConfig.User = cli.GlobalString(schema.CLIOptUser)
cqlConfig.Password = cli.GlobalString(schema.CLIOptPassword)
cqlConfig.AllowedAuthenticators = cli.GlobalStringSlice(schema.CLIOptAllowedAuthenticators)
cqlConfig.Timeout = cli.GlobalInt(schema.CLIOptTimeout)
cqlConfig.ConnectTimeout = cli.GlobalInt(schema.CLIOptConnectTimeout)
cqlConfig.Keyspace = cli.GlobalString(schema.CLIOptKeyspace)
cqlConfig.NumReplicas = cli.Int(schema.CLIOptReplicationFactor)
cqlConfig.ProtoVersion = cli.Int(schema.CLIOptProtoVersion)
if cli.GlobalBool(schema.CLIFlagEnableTLS) {
cqlConfig.TLS = &config.TLS{
Enabled: true,
CertFile: cli.GlobalString(schema.CLIFlagTLSCertFile),
KeyFile: cli.GlobalString(schema.CLIFlagTLSKeyFile),
CaFile: cli.GlobalString(schema.CLIFlagTLSCaFile),
EnableHostVerification: cli.GlobalBool(schema.CLIFlagTLSEnableHostVerification),
ServerName: cli.GlobalString(schema.CLIFlagTLSServerName),
}
}
if err := validateCQLClientConfig(cqlConfig); err != nil {
return nil, err
}
return cqlConfig, nil
}
func validateCQLClientConfig(config *CQLClientConfig) error {
if len(config.Hosts) == 0 {
return schema.NewConfigError("missing cassandra endpoint argument " + flag(schema.CLIOptEndpoint))
}
if config.Keyspace == "" {
return schema.NewConfigError("missing " + flag(schema.CLIOptKeyspace) + " argument ")
}
if config.Port == 0 {
config.Port = DefaultCassandraPort
}
if config.NumReplicas == 0 {
config.NumReplicas = defaultNumReplicas
}
return nil
}
func flag(opt string) string {
return "(-" + opt + ")"
}
func handleErr(err error) error {
log.Println(err)
return err
}