cluster.go 4.5 KB

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