Skip to content

Commit

Permalink
Added support for network topology strategy (cadence-workflow#4875)
Browse files Browse the repository at this point in the history
  • Loading branch information
johndelcastillo authored Jul 5, 2022
1 parent 354e6b0 commit 663a041
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions tools/cassandra/cqlclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ const (

createKeyspaceCQL = `CREATE KEYSPACE IF NOT EXISTS %v ` +
`WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : %v};`

createNTSKeyspaceCQL = `CREATE KEYSPACE IF NOT EXISTS %v ` +
`WITH replication = { 'class' : 'NetworkTopologyStrategy', '%v' : %v};`
)

var _ schema.SchemaClient = (*CqlClient)(nil)
Expand Down Expand Up @@ -130,6 +133,11 @@ func (client *CqlClient) CreateKeyspace(name string) error {
return client.ExecDDLQuery(fmt.Sprintf(createKeyspaceCQL, name, client.nReplicas))
}

// createNTSKeyspace creates a cassandra Keyspace if it doesn't exist using network topology strategy
func (client *CqlClient) CreateNTSKeyspace(name string, datacenter string) error {
return client.ExecDDLQuery(fmt.Sprintf(createNTSKeyspaceCQL, name, datacenter, client.nReplicas))
}

// DropKeyspace drops a Keyspace
func (client *CqlClient) DropKeyspace(name string) error {
return client.ExecDDLQuery(fmt.Sprintf("DROP KEYSPACE %v", name))
Expand Down
8 changes: 6 additions & 2 deletions tools/cassandra/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,24 @@ func createKeyspace(cli *cli.Context) error {
if keyspace == "" {
return handleErr(schema.NewConfigError("missing " + flag(schema.CLIOptKeyspace) + " argument "))
}
err = doCreateKeyspace(*config, keyspace)
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) error {
func doCreateKeyspace(cfg CQLClientConfig, name string, datacenter string) error {
cfg.Keyspace = SystemKeyspace
client, err := NewCQLClient(&cfg)
if err != nil {
return err
}
defer client.Close()
if datacenter != "" {
return client.CreateNTSKeyspace(name, datacenter)
}
return client.CreateKeyspace(name)
}

Expand Down
7 changes: 6 additions & 1 deletion tools/cassandra/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,17 @@ func BuildCLIOptions() *cli.App {
{
Name: "create-Keyspace",
Aliases: []string{"create"},
Usage: "creates a Keyspace with simple strategy",
Usage: "creates a Keyspace with simple strategy. If datacenter is provided, will use network topology strategy",
Flags: []cli.Flag{
cli.StringFlag{
Name: schema.CLIFlagKeyspace,
Usage: "name of the Keyspace",
},
cli.StringFlag{
Name: schema.CLIFlagDatacenter,
Value: "",
Usage: "name of the cassandra datacenter, used when creating the keyspace with network topology strategy",
},
cli.IntFlag{
Name: schema.CLIFlagReplicationFactor,
Value: 1,
Expand Down
4 changes: 4 additions & 0 deletions tools/common/schema/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ const (
CLIOptConnectTimeout = "connect-timeout"
// CLIOptKeyspace is the cli option for keyspace
CLIOptKeyspace = "keyspace"
// CLIOptDatabase is the cli option for datacenter
CLIOptDatacenter = "datacenter"
// CLIOptDatabase is the cli option for database
CLIOptDatabase = "database"
// CLIOptPluginName is the cli option for plugin name
Expand Down Expand Up @@ -127,6 +129,8 @@ const (
CLIFlagTimeout = CLIOptTimeout + ", t"
// CLIFlagKeyspace is the cli flag for keyspace
CLIFlagKeyspace = CLIOptKeyspace + ", k"
// CLIFlagDatacenter is the cli flag for datacenter
CLIFlagDatacenter = CLIOptDatacenter + ", dc"
// CLIFlagDatabase is the cli flag for database
CLIFlagDatabase = CLIOptDatabase + ", db"
// CLIFlagPluginName is the cli flag for plugin name
Expand Down

0 comments on commit 663a041

Please sign in to comment.