cluster.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "errors"
  7. "time"
  8. )
  9. // ClusterConfig is a struct to configure the default cluster implementation
  10. // of gocoql. It has a varity of attributes that can be used to modify the
  11. // behavior to fit the most common use cases. Applications that requre a
  12. // different setup must implement their own cluster.
  13. type ClusterConfig struct {
  14. Hosts []string // addresses for the initial connections
  15. CQLVersion string // CQL version (default: 3.0.0)
  16. ProtoVersion int // version of the native protocol (default: 2)
  17. Timeout time.Duration // connection timeout (default: 600ms)
  18. DefaultPort int // default port (default: 9042)
  19. Keyspace string // initial keyspace (optional)
  20. NumConns int // number of connections per host (default: 2)
  21. NumStreams int // number of streams per connection (default: 128)
  22. Consistency Consistency // default consistency level (default: Quorum)
  23. Compressor Compressor // compression algorithm (default: nil)
  24. Authenticator Authenticator // authenticator (default: nil)
  25. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  26. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  27. ConnPoolType NewPoolFunc // The function used to create the connection pool for the session (default: NewSimplePool)
  28. DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
  29. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  30. }
  31. // NewCluster generates a new config for the default cluster implementation.
  32. func NewCluster(hosts ...string) *ClusterConfig {
  33. cfg := &ClusterConfig{
  34. Hosts: hosts,
  35. CQLVersion: "3.0.0",
  36. ProtoVersion: 2,
  37. Timeout: 600 * time.Millisecond,
  38. DefaultPort: 9042,
  39. NumConns: 2,
  40. NumStreams: 128,
  41. Consistency: Quorum,
  42. ConnPoolType: NewSimplePool,
  43. DiscoverHosts: false,
  44. MaxPreparedStmts: 1000,
  45. }
  46. return cfg
  47. }
  48. // CreateSession initializes the cluster based on this config and returns a
  49. // session object that can be used to interact with the database.
  50. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  51. //Check that hosts in the ClusterConfig is not empty
  52. if len(cfg.Hosts) < 1 {
  53. return nil, ErrNoHosts
  54. }
  55. pool := cfg.ConnPoolType(cfg)
  56. //Adjust the size of the prepared statements cache to match the latest configuration
  57. stmtsLRU.mu.Lock()
  58. for stmtsLRU.lru.Len() > cfg.MaxPreparedStmts {
  59. stmtsLRU.lru.RemoveOldest()
  60. }
  61. stmtsLRU.lru.MaxEntries = cfg.MaxPreparedStmts
  62. stmtsLRU.mu.Unlock()
  63. //See if there are any connections in the pool
  64. if pool.Size() > 0 {
  65. s := NewSession(pool, *cfg)
  66. s.SetConsistency(cfg.Consistency)
  67. if cfg.DiscoverHosts {
  68. //Fill out cfg.Hosts
  69. query := "SELECT peer FROM system.peers"
  70. peers := s.Query(query).Iter()
  71. var ip string
  72. for peers.Scan(&ip) {
  73. exists := false
  74. for ii := 0; ii < len(cfg.Hosts); ii++ {
  75. if cfg.Hosts[ii] == ip {
  76. exists = true
  77. }
  78. }
  79. if !exists {
  80. cfg.Hosts = append(cfg.Hosts, ip)
  81. }
  82. }
  83. if err := peers.Close(); err != nil {
  84. return s, ErrHostQueryFailed
  85. }
  86. }
  87. return s, nil
  88. }
  89. pool.Close()
  90. return nil, ErrNoConnectionsStarted
  91. }
  92. var (
  93. ErrNoHosts = errors.New("no hosts provided")
  94. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  95. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  96. )