cluster.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/internal/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. // PoolConfig configures the connection pool used by the driver, it defaults to
  35. // using a round robbin host selection policy and a round robbin connection selection
  36. // policy for each host.
  37. type PoolConfig struct {
  38. // HostSelectionPolicy sets the policy for selecting which host to use for a
  39. // given query (default: RoundRobinHostPolicy())
  40. HostSelectionPolicy HostSelectionPolicy
  41. // ConnSelectionPolicy sets the policy factory for selecting a connection to use for
  42. // each host for a query (default: RoundRobinConnPolicy())
  43. ConnSelectionPolicy func() ConnSelectionPolicy
  44. }
  45. func (p PoolConfig) buildPool(session *Session) *policyConnPool {
  46. hostSelection := p.HostSelectionPolicy
  47. if hostSelection == nil {
  48. hostSelection = RoundRobinHostPolicy()
  49. }
  50. connSelection := p.ConnSelectionPolicy
  51. if connSelection == nil {
  52. connSelection = RoundRobinConnPolicy()
  53. }
  54. return newPolicyConnPool(session, hostSelection, connSelection)
  55. }
  56. type DiscoveryConfig struct {
  57. // If not empty will filter all discoverred hosts to a single Data Centre (default: "")
  58. DcFilter string
  59. // If not empty will filter all discoverred hosts to a single Rack (default: "")
  60. RackFilter string
  61. // ignored
  62. Sleep time.Duration
  63. }
  64. func (d DiscoveryConfig) matchFilter(host *HostInfo) bool {
  65. if d.DcFilter != "" && d.DcFilter != host.DataCenter() {
  66. return false
  67. }
  68. if d.RackFilter != "" && d.RackFilter != host.Rack() {
  69. return false
  70. }
  71. return true
  72. }
  73. // ClusterConfig is a struct to configure the default cluster implementation
  74. // of gocoql. It has a varity of attributes that can be used to modify the
  75. // behavior to fit the most common use cases. Applications that requre a
  76. // different setup must implement their own cluster.
  77. type ClusterConfig struct {
  78. Hosts []string // addresses for the initial connections
  79. CQLVersion string // CQL version (default: 3.0.0)
  80. ProtoVersion int // version of the native protocol (default: 2)
  81. Timeout time.Duration // connection timeout (default: 600ms)
  82. Port int // port (default: 9042)
  83. Keyspace string // initial keyspace (optional)
  84. NumConns int // number of connections per host (default: 2)
  85. Consistency Consistency // default consistency level (default: Quorum)
  86. Compressor Compressor // compression algorithm (default: nil)
  87. Authenticator Authenticator // authenticator (default: nil)
  88. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  89. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  90. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  91. MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
  92. PageSize int // Default page size to use for created sessions (default: 5000)
  93. SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
  94. SslOpts *SslOptions
  95. 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)
  96. // PoolConfig configures the underlying connection pool, allowing the
  97. // configuration of host selection and connection selection policies.
  98. PoolConfig PoolConfig
  99. Discovery DiscoveryConfig
  100. // The maximum amount of time to wait for schema agreement in a cluster after
  101. // receiving a schema change frame. (deault: 60s)
  102. MaxWaitSchemaAgreement time.Duration
  103. // internal config for testing
  104. disableControlConn bool
  105. }
  106. // NewCluster generates a new config for the default cluster implementation.
  107. func NewCluster(hosts ...string) *ClusterConfig {
  108. cfg := &ClusterConfig{
  109. Hosts: hosts,
  110. CQLVersion: "3.0.0",
  111. ProtoVersion: 2,
  112. Timeout: 600 * time.Millisecond,
  113. Port: 9042,
  114. NumConns: 2,
  115. Consistency: Quorum,
  116. MaxPreparedStmts: defaultMaxPreparedStmts,
  117. MaxRoutingKeyInfo: 1000,
  118. PageSize: 5000,
  119. DefaultTimestamp: true,
  120. MaxWaitSchemaAgreement: 60 * time.Second,
  121. }
  122. return cfg
  123. }
  124. // CreateSession initializes the cluster based on this config and returns a
  125. // session object that can be used to interact with the database.
  126. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  127. return NewSession(*cfg)
  128. }
  129. var (
  130. ErrNoHosts = errors.New("no hosts provided")
  131. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  132. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  133. )