From 5dee135d2204678da40528166a0db2f8485e12d4 Mon Sep 17 00:00:00 2001 From: scoiatael Date: Wed, 8 Mar 2017 11:39:15 +0100 Subject: [PATCH] Allow specifying replication opts, default to simple --- config.go | 16 +++++++++------- main.go | 6 ++++++ persistence/provider.go | 14 +++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index 233d114..4f12f95 100644 --- a/config.go +++ b/config.go @@ -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 @@ -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 { diff --git a/main.go b/main.go index 2805956..c78860f 100644 --- a/main.go +++ b/main.go @@ -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{} @@ -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{}) } diff --git a/persistence/provider.go b/persistence/provider.go index 7cb9c44..6eedc29 100644 --- a/persistence/provider.go +++ b/persistence/provider.go @@ -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 { @@ -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() @@ -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") } @@ -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