cluster.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // ClusterConfig is a struct to configure the default cluster implementation
  57. // of gocoql. It has a varity of attributes that can be used to modify the
  58. // behavior to fit the most common use cases. Applications that requre a
  59. // different setup must implement their own cluster.
  60. type ClusterConfig struct {
  61. Hosts []string // addresses for the initial connections
  62. CQLVersion string // CQL version (default: 3.0.0)
  63. ProtoVersion int // version of the native protocol (default: 2)
  64. Timeout time.Duration // connection timeout (default: 600ms)
  65. Port int // port (default: 9042)
  66. Keyspace string // initial keyspace (optional)
  67. NumConns int // number of connections per host (default: 2)
  68. Consistency Consistency // default consistency level (default: Quorum)
  69. Compressor Compressor // compression algorithm (default: nil)
  70. Authenticator Authenticator // authenticator (default: nil)
  71. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  72. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  73. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  74. MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
  75. PageSize int // Default page size to use for created sessions (default: 5000)
  76. SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
  77. SslOpts *SslOptions
  78. 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)
  79. // PoolConfig configures the underlying connection pool, allowing the
  80. // configuration of host selection and connection selection policies.
  81. PoolConfig PoolConfig
  82. // The maximum amount of time to wait for schema agreement in a cluster after
  83. // receiving a schema change frame. (deault: 60s)
  84. MaxWaitSchemaAgreement time.Duration
  85. HostFilter
  86. // internal config for testing
  87. disableControlConn bool
  88. }
  89. // NewCluster generates a new config for the default cluster implementation.
  90. func NewCluster(hosts ...string) *ClusterConfig {
  91. cfg := &ClusterConfig{
  92. Hosts: hosts,
  93. CQLVersion: "3.0.0",
  94. ProtoVersion: 2,
  95. Timeout: 600 * time.Millisecond,
  96. Port: 9042,
  97. NumConns: 2,
  98. Consistency: Quorum,
  99. MaxPreparedStmts: defaultMaxPreparedStmts,
  100. MaxRoutingKeyInfo: 1000,
  101. PageSize: 5000,
  102. DefaultTimestamp: true,
  103. MaxWaitSchemaAgreement: 60 * time.Second,
  104. }
  105. return cfg
  106. }
  107. // CreateSession initializes the cluster based on this config and returns a
  108. // session object that can be used to interact with the database.
  109. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  110. return NewSession(*cfg)
  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. )