Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Allow specifying replication opts, default to simple
Browse files Browse the repository at this point in the history
  • Loading branch information
scoiatael committed Mar 8, 2017
1 parent c6097f4 commit 5dee135
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
16 changes: 9 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

// Config is a context for all application actions.
type Config struct {
Keyspace string
Hosts []string
Actions []actions.Action
StatsdAddr string
Features map[string]bool
Keyspace string
Hosts []string
Actions []actions.Action
StatsdAddr string
Features map[string]bool
ReplicationOpts string

provider persistence.Provider
telemetry telemetry.Datadog
Expand Down Expand Up @@ -64,8 +65,9 @@ func (c Config) HttpHandler() actions.HttpHandler {

func (c *Config) Init() error {
new_provider := persistence.CassandraProvider{
Hosts: c.Hosts,
Keyspace: c.Keyspace,
Hosts: c.Hosts,
Keyspace: c.Keyspace,
Replication: c.ReplicationOpts,
}
err := new_provider.Init()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func main() {
Value: 8080,
Usage: "Port to listen on",
},
cli.StringFlag{
Name: "replication",
Value: "",
Usage: "Cassandra keyspace replication options",
},
}
app.Action = func(c *cli.Context) error {
config := Config{}
Expand All @@ -55,6 +60,7 @@ func main() {
config.Keyspace = c.String("keyspace")
config.Hosts = strings.Split(c.String("hosts"), ",")
config.StatsdAddr = c.String("telemetry")
config.ReplicationOpts = c.String("replication")
if c.Bool("migrate") {
config.Append(actions.Migrate{})
}
Expand Down
14 changes: 9 additions & 5 deletions persistence/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type Provider interface {
}

type CassandraProvider struct {
Hosts []string
Keyspace string
session *Session
Hosts []string
Keyspace string
session *Session
Replication string
}

func (cp *CassandraProvider) NewCluster() *gocql.ClusterConfig {
Expand All @@ -38,7 +39,7 @@ func (cp *CassandraProvider) Session() (Session, error) {
return nil, fmt.Errorf("Initialize CassandraProvider with NewProvider first")
}

const createKeySpace = `CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class' : 'NetworkTopologyStrategy' };`
const createKeySpace = `CREATE KEYSPACE IF NOT EXISTS %s WITH replication = %s;`

func (c *CassandraProvider) createKeySpace() error {
cluster := c.NewCluster()
Expand All @@ -49,7 +50,7 @@ func (c *CassandraProvider) createKeySpace() error {
return errors.Wrap(err, "CreateSession failed")
}
defer sess.Close()
err = sess.Query(fmt.Sprintf(createKeySpace, c.Keyspace)).Exec()
err = sess.Query(fmt.Sprintf(createKeySpace, c.Keyspace, c.Replication)).Exec()
if err != nil {
return errors.Wrap(err, "Query to CreateKeyspace failed")
}
Expand All @@ -67,6 +68,9 @@ func (cp *CassandraProvider) MigrationSession() (MigrationSession, error) {
}

func (c *CassandraProvider) Init() error {
if len(c.Replication) == 0 {
c.Replication = "{ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"
}
err := c.createKeySpace()
if err != nil {
return err
Expand Down

0 comments on commit 5dee135

Please sign in to comment.