diff --git a/CHANGELOG.md b/CHANGELOG.md index fcf6ded..8d6a3c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [v0.5.1] +- Fixed get record of status type [#27](https://github.com/xmidt-org/codex-db/pull/27) +- Improved cassandra connect logic [#27](https://github.com/xmidt-org/codex-db/pull/27) + ## [v0.5.0] - Added cassandra row_id with TIMEUUID for long-polling [#25](https://github.com/xmidt-org/codex-db/pull/25) @@ -43,7 +47,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [v0.1.0] - Initial creation, moved from: https://github.com/xmidt-org/codex-deploy -[Unreleased]: https://github.com/xmidt-org/codex-db/compare/v0.5.0..HEAD +[Unreleased]: https://github.com/xmidt-org/codex-db/compare/v0.5.1..HEAD +[v0.5.1]: https://github.com/xmidt-org/codex-db/compare/v0.5.0..v0.5.1 [v0.5.0]: https://github.com/xmidt-org/codex-db/compare/v0.4.0..v0.5.0 [v0.4.0]: https://github.com/xmidt-org/codex-db/compare/v0.3.3..v0.4.0 [v0.3.3]: https://github.com/xmidt-org/codex-db/compare/v0.3.2..v0.3.3 diff --git a/cassandra/db.go b/cassandra/db.go index 826e168..61654ca 100644 --- a/cassandra/db.go +++ b/cassandra/db.go @@ -39,8 +39,11 @@ var ( ) const ( - defaultOpTimeout = time.Duration(10) * time.Second - defaultDatabase = "devices" + defaultOpTimeout = time.Duration(10) * time.Second + defaultDatabase = "devices" + defaultNumRetries = 0 + defaultWaitTimeMult = 1 + defaultMaxNumberConnsPerHost = 2 ) type Config struct { @@ -68,6 +71,15 @@ type Config struct { Username string // Password to authenticate into the cluster. Username must also be provided. Password string + + // NumRetries for connecting to the db + NumRetries int + + // WaitTimeMult the amount of time to wait before retrying to connect to the db + WaitTimeMult time.Duration + + // MaxConnsPerHost max number of connections per host + MaxConnsPerHost int } type Connection struct { @@ -95,6 +107,8 @@ func CreateDbConnection(config Config, provider provider.Provider, health *healt clusterConfig.Consistency = gocql.LocalQuorum clusterConfig.Keyspace = config.Database clusterConfig.Timeout = config.OpTimeout + // let retry package handle it + clusterConfig.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 1} // setup ssl if config.SSLRootCert != "" && config.SSLCert != "" && config.SSLKey != "" { clusterConfig.SslOpts = &gocql.SslOptions{ @@ -118,6 +132,14 @@ func CreateDbConnection(config Config, provider provider.Provider, health *healt } conn, err := connectWithMetrics(clusterConfig, dbConn.measures) + + // retry if it fails + waitTime := 1 * time.Second + for attempt := 0; attempt < config.NumRetries && err != nil; attempt++ { + time.Sleep(waitTime) + conn, err = connectWithMetrics(clusterConfig, dbConn.measures) + waitTime = waitTime * config.WaitTimeMult + } if err != nil { return &Connection{}, emperror.WrapWith(err, "Connecting to database failed", "hosts", config.Hosts) } @@ -142,6 +164,15 @@ func validateConfig(config *Config) { if config.Database == "" { config.Database = defaultDatabase } + if config.NumRetries < 0 { + config.NumRetries = defaultNumRetries + } + if config.WaitTimeMult < 1 { + config.WaitTimeMult = defaultWaitTimeMult + } + if config.MaxConnsPerHost <= 0 { + config.MaxConnsPerHost = defaultMaxNumberConnsPerHost + } } // GetRecords returns a list of records for a given device. @@ -170,7 +201,7 @@ func (c *Connection) GetRecordsOfType(deviceID string, limit int, eventType db.E filterString = "WHERE device_id = ? AND record_type = ? AND row_id > ?" items = []interface{}{deviceID, eventType, stateHash} } - deviceInfo, err := c.finder.findRecords(limit, filterString, items) + deviceInfo, err := c.finder.findRecords(limit, filterString, items...) if err != nil { c.measures.SQLQueryFailureCount.With(db.TypeLabel, db.ReadType).Add(1.0) return []db.Record{}, emperror.WrapWith(err, "Getting records from database failed", "device id", deviceID) diff --git a/cassandra/executer.go b/cassandra/executer.go index fabbb3d..0ef8946 100644 --- a/cassandra/executer.go +++ b/cassandra/executer.go @@ -178,5 +178,4 @@ func connect(clusterConfig *gocql.ClusterConfig) (*dbDecorator, error) { } return &dbDecorator{session: session}, nil - }