cluster.go 4.5 KB

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