cluster.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. PageSize int // Default page size to use for created sessions (default: 0)
  57. Discovery DiscoveryConfig
  58. SslOpts *SslOptions
  59. }
  60. // NewCluster generates a new config for the default cluster implementation.
  61. func NewCluster(hosts ...string) *ClusterConfig {
  62. cfg := &ClusterConfig{
  63. Hosts: hosts,
  64. CQLVersion: "3.0.0",
  65. ProtoVersion: 2,
  66. Timeout: 600 * time.Millisecond,
  67. Port: 9042,
  68. NumConns: 2,
  69. NumStreams: 128,
  70. Consistency: Quorum,
  71. ConnPoolType: NewSimplePool,
  72. DiscoverHosts: false,
  73. MaxPreparedStmts: 1000,
  74. }
  75. return cfg
  76. }
  77. // CreateSession initializes the cluster based on this config and returns a
  78. // session object that can be used to interact with the database.
  79. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  80. //Check that hosts in the ClusterConfig is not empty
  81. if len(cfg.Hosts) < 1 {
  82. return nil, ErrNoHosts
  83. }
  84. pool := cfg.ConnPoolType(cfg)
  85. //Adjust the size of the prepared statements cache to match the latest configuration
  86. stmtsLRU.mu.Lock()
  87. if stmtsLRU.lru != nil {
  88. stmtsLRU.Max(cfg.MaxPreparedStmts)
  89. } else {
  90. stmtsLRU.lru = lru.New(cfg.MaxPreparedStmts)
  91. }
  92. stmtsLRU.mu.Unlock()
  93. //See if there are any connections in the pool
  94. if pool.Size() > 0 {
  95. s := NewSession(pool, *cfg)
  96. s.SetConsistency(cfg.Consistency)
  97. s.SetPageSize(cfg.PageSize)
  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. )