forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Differential Revision: https://code.uberinternal.com/D649070
- Loading branch information
Showing
12 changed files
with
549 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"bytes" | ||
"os/exec" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/gocql/gocql" | ||
) | ||
|
||
// NewCassandraCluster creates a cassandra cluster given comma separated list of clusterHosts | ||
func NewCassandraCluster(clusterHosts string) *gocql.ClusterConfig { | ||
var hosts []string | ||
for _, h := range strings.Split(clusterHosts, ",") { | ||
if host := strings.TrimSpace(h); len(host) > 0 { | ||
hosts = append(hosts, host) | ||
} | ||
} | ||
|
||
return gocql.NewCluster(hosts...) | ||
} | ||
|
||
// CreateCassandraKeyspace creates the keyspace using this session for given replica count | ||
func CreateCassandraKeyspace(s *gocql.Session, keyspace string, replicas int, overwrite bool) (err error) { | ||
// if overwrite flag is set, drop the keyspace and create a new one | ||
if overwrite { | ||
DropCassandraKeyspace(s, keyspace) | ||
} | ||
err = s.Query(fmt.Sprintf(`CREATE KEYSPACE %s WITH replication = { | ||
'class' : 'SimpleStrategy', 'replication_factor' : %d}`, keyspace, replicas)).Exec() | ||
if err != nil { | ||
log.WithField(TagErr, err).Error(`create keyspace error`) | ||
return | ||
} | ||
log.WithField(`keyspace`, keyspace).Info(`created namespace`) | ||
|
||
return | ||
} | ||
|
||
// DropCassandraKeyspace drops the given keyspace, if it exists | ||
func DropCassandraKeyspace(s *gocql.Session, keyspace string) (err error) { | ||
err = s.Query(fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", keyspace)).Exec() | ||
if err != nil { | ||
log.WithField(TagErr, err).Error(`drop keyspace error`) | ||
return | ||
} | ||
log.WithField(`keyspace`, keyspace).Info(`dropped namespace`) | ||
return | ||
} | ||
|
||
// LoadCassandraSchema loads the schema from the given .cql file on this keyspace using cqlsh | ||
func LoadCassandraSchema(cqlshpath string, fileName string, keyspace string) (err error) { | ||
// Using cqlsh as I couldn't find a way to execute multiple commands through gocql.Session | ||
var out bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd := exec.Command(cqlshpath, fmt.Sprintf("--keyspace=%v", keyspace), fmt.Sprintf("--file=%v", fileName)) | ||
cmd.Stdout = &out | ||
cmd.Stderr = &stderr | ||
err = cmd.Run() | ||
|
||
// CQLSH doesn't return non-zero for some errors | ||
if err != nil || strings.Contains(stderr.String(), `Errno`) { | ||
err = fmt.Errorf("LoadSchema %v returned %v. STDERR: %v", cmd.Path, err, stderr.String()) | ||
} | ||
return | ||
} | ||
|
||
// CQLTimestampToUnixNano converts CQL timestamp to UnixNano | ||
func CQLTimestampToUnixNano(milliseconds int64) int64 { | ||
return milliseconds * 1000 * 1000 // Milliseconds are 10⁻³, nanoseconds are 10⁻⁹, (-3) - (-9) = 6, so multiply by 10⁶ | ||
} | ||
|
||
// UnixNanoToCQLTimestamp converts UnixNano to CQL timestamp | ||
func UnixNanoToCQLTimestamp(timestamp int64) int64 { | ||
return timestamp / (1000 * 1000) // Milliseconds are 10⁻³, nanoseconds are 10⁻⁹, (-9) - (-3) = -6, so divide by 10⁶ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package common | ||
|
||
type ( | ||
// Daemon is the base interfaces implemented by | ||
// background tasks within cherami | ||
Daemon interface { | ||
Start() | ||
Stop() | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package common | ||
|
||
// TagErr is the tag for error object message | ||
const TagErr = `err` |
Oops, something went wrong.