cluster.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/gocql/gocql/lru"
  10. )
  11. const defaultMaxPreparedStmts = 1000
  12. //Package global reference to Prepared Statements LRU
  13. var stmtsLRU preparedLRU
  14. //preparedLRU is the prepared statement cache
  15. type preparedLRU struct {
  16. sync.Mutex
  17. lru *lru.Cache
  18. }
  19. //Max adjusts the maximum size of the cache and cleans up the oldest records if
  20. //the new max is lower than the previous value. Not concurrency safe.
  21. func (p *preparedLRU) Max(max int) {
  22. for p.lru.Len() > max {
  23. p.lru.RemoveOldest()
  24. }
  25. p.lru.MaxEntries = max
  26. }
  27. func initStmtsLRU(max int) {
  28. if stmtsLRU.lru != nil {
  29. stmtsLRU.Max(max)
  30. } else {
  31. stmtsLRU.lru = lru.New(max)
  32. }
  33. }
  34. // To enable periodic node discovery enable DiscoverHosts in ClusterConfig
  35. type DiscoveryConfig struct {
  36. // If not empty will filter all discoverred hosts to a single Data Centre (default: "")
  37. DcFilter string
  38. // If not empty will filter all discoverred hosts to a single Rack (default: "")
  39. RackFilter string
  40. // The interval to check for new hosts (default: 30s)
  41. Sleep time.Duration
  42. }
  43. // PoolConfig configures the connection pool used by the driver, it defaults to
  44. // using a round robbin host selection policy and a round robbin connection selection
  45. // policy for each host.
  46. type PoolConfig struct {
  47. // HostSelectionPolicy sets the policy for selecting which host to use for a
  48. // given query (default: RoundRobinHostPolicy())
  49. HostSelectionPolicy HostSelectionPolicy
  50. // ConnSelectionPolicy sets the policy factory for selecting a connection to use for
  51. // each host for a query (default: RoundRobinConnPolicy())
  52. ConnSelectionPolicy func() ConnSelectionPolicy
  53. }
  54. func (p PoolConfig) buildPool(cfg *ClusterConfig) (*policyConnPool, error) {
  55. hostSelection := p.HostSelectionPolicy
  56. if hostSelection == nil {
  57. hostSelection = RoundRobinHostPolicy()
  58. }
  59. connSelection := p.ConnSelectionPolicy
  60. if connSelection == nil {
  61. connSelection = RoundRobinConnPolicy()
  62. }
  63. return newPolicyConnPool(cfg, hostSelection, connSelection)
  64. }
  65. // ClusterConfig is a struct to configure the default cluster implementation
  66. // of gocoql. It has a varity of attributes that can be used to modify the
  67. // behavior to fit the most common use cases. Applications that requre a
  68. // different setup must implement their own cluster.
  69. type ClusterConfig struct {
  70. Hosts []string // addresses for the initial connections
  71. CQLVersion string // CQL version (default: 3.0.0)
  72. ProtoVersion int // version of the native protocol (default: 2)
  73. Timeout time.Duration // connection timeout (default: 600ms)
  74. Port int // port (default: 9042)
  75. Keyspace string // initial keyspace (optional)
  76. NumConns int // number of connections per host (default: 2)
  77. NumStreams int // number of streams per connection (default: max per protocol, either 128 or 32768)
  78. Consistency Consistency // default consistency level (default: Quorum)
  79. Compressor Compressor // compression algorithm (default: nil)
  80. Authenticator Authenticator // authenticator (default: nil)
  81. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  82. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  83. DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
  84. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  85. MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
  86. PageSize int // Default page size to use for created sessions (default: 5000)
  87. SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
  88. Discovery DiscoveryConfig
  89. SslOpts *SslOptions
  90. DefaultTimestamp bool // Sends a client side timestamp for all requests which overrides the timestamp at which it arrives at the server. (default: true, only enabled for protocol 3 and above)
  91. // PoolConfig configures the underlying connection pool, allowing the
  92. // configuration of host selection and connection selection policies.
  93. PoolConfig PoolConfig
  94. }
  95. // NewCluster generates a new config for the default cluster implementation.
  96. func NewCluster(hosts ...string) *ClusterConfig {
  97. cfg := &ClusterConfig{
  98. Hosts: hosts,
  99. CQLVersion: "3.0.0",
  100. ProtoVersion: 2,
  101. Timeout: 600 * time.Millisecond,
  102. Port: 9042,
  103. NumConns: 2,
  104. Consistency: Quorum,
  105. DiscoverHosts: false,
  106. MaxPreparedStmts: defaultMaxPreparedStmts,
  107. MaxRoutingKeyInfo: 1000,
  108. PageSize: 5000,
  109. DefaultTimestamp: true,
  110. }
  111. return cfg
  112. }
  113. // CreateSession initializes the cluster based on this config and returns a
  114. // session object that can be used to interact with the database.
  115. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  116. return NewSession(*cfg)
  117. }
  118. var (
  119. ErrNoHosts = errors.New("no hosts provided")
  120. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  121. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  122. )