Skip to content

Commit

Permalink
Janus#463 cassandra repo (#470)
Browse files Browse the repository at this point in the history
* updating image in circle

* fixed add user logic

* added env var for cassandra cluster consistency

* removed extra debug logging
  • Loading branch information
tuxranger authored Mar 22, 2021
1 parent b624812 commit 9fa15f6
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions cassandra/wrapper/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

var schemaPath = "/usr/local/bin"
var schemaFileName = "schema.sql"
var clusterConsistency = gocql.Quorum

// Package level initialization.
//
Expand All @@ -43,6 +44,7 @@ func init() {
type sessionInitializer struct {
clusterHostName string
keyspace string
consistency gocql.Consistency
}

// sessionHolder stores a cassandra session
Expand All @@ -52,9 +54,13 @@ type sessionHolder struct {

// New return a cassandra session Initializer
func New(clusterHostName, keyspace string) Initializer {
log.Debugf("in new")
consistencyEnv := getenv("CLUSTER_CONSISTENCY", clusterConsistency.String())
consistency := checkConsistency(consistencyEnv)
return sessionInitializer{
clusterHostName: clusterHostName,
keyspace: keyspace,
consistency: consistency,
}
}

Expand Down Expand Up @@ -117,6 +123,9 @@ func newKeyspaceSession(clusterHostName, keyspace string, clusterTimeout time.Du
cluster := gocql.NewCluster(clusterHostName)
cluster.Keyspace = keyspace
cluster.Timeout = clusterTimeout
consistencyEnv := getenv("CLUSTER_CONSISTENCY", clusterConsistency.String())
consistency := checkConsistency(consistencyEnv)
cluster.Consistency = consistency
return cluster.CreateSession()
}

Expand Down Expand Up @@ -151,7 +160,6 @@ func createAppKeyspaceIfRequired(clusterHostName, systemKeyspace, appKeyspace st

for _, stmt := range stmtList {
log.Debugf("Executing statement: %s", stmt)

// New session for use statement
newKeyspace, isCaseSensitive := getKeyspaceNameFromUseStmt(stmt)
if newKeyspace != "" {
Expand All @@ -172,12 +180,13 @@ func createAppKeyspaceIfRequired(clusterHostName, systemKeyspace, appKeyspace st
// execute statement
err = session.Query(stmt).Exec()
if err != nil {
log.Error("statement error: %v", err)
return err
}
log.Debug("Statement executed")
}

log.Debugf("Created new keyspace: %s", appKeyspace)
log.Debugf("app keyspace set to: %s", appKeyspace)
return nil
}

Expand Down Expand Up @@ -309,3 +318,38 @@ func getenv(envVariable string, defaultValue string) string {

return returnValue
}

func checkConsistency(envVar string) gocql.Consistency {
switch strings.ToLower(envVar) {
case "any":
log.Debugf("consistency set to any")
return gocql.Any
case "one":
log.Debugf("consistency set to one")
return gocql.One
case "two":
log.Debugf("consistency set to two")
return gocql.Two
case "three":
log.Debugf("consistency set to three")
return gocql.Three
case "quorum":
log.Debugf("consistency set to quorum")
return gocql.Quorum
case "all":
log.Debugf("consistency set to all")
return gocql.All
case "localquorum":
log.Debugf("consistency set to local quorum")
return gocql.LocalQuorum
case "eachquorum":
log.Debugf("consistency set to each quorum")
return gocql.EachQuorum
case "localone":
log.Debugf("consistency set to local one")
return gocql.LocalOne
default:
log.Debugf("consistency set to %s", clusterConsistency.String())
return clusterConsistency
}
}

0 comments on commit 9fa15f6

Please sign in to comment.