cluster.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "sync"
  8. "time"
  9. "github.com/golang/groupcache/lru"
  10. )
  11. //Package global reference to Prepared Statements LRU
  12. var stmtsLRU preparedLRU
  13. //preparedLRU is the prepared statement cache
  14. type preparedLRU struct {
  15. lru *lru.Cache
  16. mu sync.Mutex
  17. }
  18. //Max adjusts the maximum size of the cache and cleans up the oldest records if
  19. //the new max is lower than the previous value. Not concurrency safe.
  20. func (p *preparedLRU) Max(max int) {
  21. for p.lru.Len() > max {
  22. p.lru.RemoveOldest()
  23. }
  24. p.lru.MaxEntries = max
  25. }
  26. // To enable periodic node discovery enable DiscoverHosts in ClusterConfig
  27. type DiscoveryConfig struct {
  28. // If not empty will filter all discoverred hosts to a single Data Centre (default: "")
  29. DcFilter string
  30. // If not empty will filter all discoverred hosts to a single Rack (default: "")
  31. RackFilter string
  32. // The interval to check for new hosts (default: 30s)
  33. Sleep time.Duration
  34. }
  35. // ClusterConfig is a struct to configure the default cluster implementation
  36. // of gocoql. It has a varity of attributes that can be used to modify the
  37. // behavior to fit the most common use cases. Applications that requre a
  38. // different setup must implement their own cluster.
  39. type ClusterConfig struct {
  40. Hosts []string // addresses for the initial connections
  41. CQLVersion string // CQL version (default: 3.0.0)
  42. ProtoVersion int // version of the native protocol (default: 2)
  43. Timeout time.Duration // connection timeout (default: 600ms)
  44. Port int // port (default: 9042)
  45. Keyspace string // initial keyspace (optional)
  46. NumConns int // number of connections per host (default: 2)
  47. NumStreams int // number of streams per connection (default: 128)
  48. Consistency Consistency // default consistency level (default: Quorum)
  49. Compressor Compressor // compression algorithm (default: nil)
  50. Authenticator Authenticator // authenticator (default: nil)
  51. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  52. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  53. ConnPoolType NewPoolFunc // The function used to create the connection pool for the session (default: NewSimplePool)
  54. DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
  55. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  56. Discovery DiscoveryConfig
  57. SslOpts *SslOptions
  58. }
  59. // NewCluster generates a new config for the default cluster implementation.
  60. func NewCluster(hosts ...string) *ClusterConfig {
  61. cfg := &ClusterConfig{
  62. Hosts: hosts,
  63. CQLVersion: "3.0.0",
  64. ProtoVersion: 2,
  65. Timeout: 600 * time.Millisecond,
  66. Port: 9042,
  67. NumConns: 2,
  68. NumStreams: 128,
  69. Consistency: Quorum,
  70. ConnPoolType: NewSimplePool,
  71. DiscoverHosts: false,
  72. MaxPreparedStmts: 1000,
  73. }
  74. return cfg
  75. }
  76. // CreateSession initializes the cluster based on this config and returns a
  77. // session object that can be used to interact with the database.
  78. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  79. //Check that hosts in the ClusterConfig is not empty
  80. if len(cfg.Hosts) < 1 {
  81. return nil, ErrNoHosts
  82. }
  83. pool := cfg.ConnPoolType(cfg)
  84. //Adjust the size of the prepared statements cache to match the latest configuration
  85. stmtsLRU.mu.Lock()
  86. if stmtsLRU.lru != nil {
  87. stmtsLRU.Max(cfg.MaxPreparedStmts)
  88. } else {
  89. stmtsLRU.lru = lru.New(cfg.MaxPreparedStmts)
  90. }
  91. stmtsLRU.mu.Unlock()
  92. //See if there are any connections in the pool
  93. if pool.Size() > 0 {
  94. s := NewSession(pool, *cfg)
  95. s.SetConsistency(cfg.Consistency)
  96. if cfg.DiscoverHosts {
  97. hostSource := &ringDescriber{
  98. session: s,
  99. dcFilter: cfg.Discovery.DcFilter,
  100. rackFilter: cfg.Discovery.RackFilter,
  101. }
  102. go hostSource.run(cfg.Discovery.Sleep)
  103. }
  104. return s, nil
  105. }
  106. pool.Close()
  107. return nil, ErrNoConnectionsStarted
  108. }
  109. var (
  110. ErrNoHosts = errors.New("no hosts provided")
  111. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  112. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  113. )