cluster.go 6.4 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. "time"
  8. )
  9. // PoolConfig configures the connection pool used by the driver, it defaults to
  10. // using a round robbin host selection policy and a round robbin connection selection
  11. // policy for each host.
  12. type PoolConfig struct {
  13. // HostSelectionPolicy sets the policy for selecting which host to use for a
  14. // given query (default: RoundRobinHostPolicy())
  15. HostSelectionPolicy HostSelectionPolicy
  16. // ConnSelectionPolicy sets the policy factory for selecting a connection to use for
  17. // each host for a query (default: RoundRobinConnPolicy())
  18. ConnSelectionPolicy func() ConnSelectionPolicy
  19. }
  20. func (p PoolConfig) buildPool(session *Session) *policyConnPool {
  21. hostSelection := p.HostSelectionPolicy
  22. if hostSelection == nil {
  23. hostSelection = RoundRobinHostPolicy()
  24. }
  25. connSelection := p.ConnSelectionPolicy
  26. if connSelection == nil {
  27. connSelection = RoundRobinConnPolicy()
  28. }
  29. return newPolicyConnPool(session, hostSelection, connSelection)
  30. }
  31. type DiscoveryConfig struct {
  32. // If not empty will filter all discoverred hosts to a single Data Centre (default: "")
  33. DcFilter string
  34. // If not empty will filter all discoverred hosts to a single Rack (default: "")
  35. RackFilter string
  36. // ignored
  37. Sleep time.Duration
  38. }
  39. func (d DiscoveryConfig) matchFilter(host *HostInfo) bool {
  40. if d.DcFilter != "" && d.DcFilter != host.DataCenter() {
  41. return false
  42. }
  43. if d.RackFilter != "" && d.RackFilter != host.Rack() {
  44. return false
  45. }
  46. return true
  47. }
  48. // ClusterConfig is a struct to configure the default cluster implementation
  49. // of gocoql. It has a varity of attributes that can be used to modify the
  50. // behavior to fit the most common use cases. Applications that requre a
  51. // different setup must implement their own cluster.
  52. type ClusterConfig struct {
  53. Hosts []string // addresses for the initial connections
  54. CQLVersion string // CQL version (default: 3.0.0)
  55. ProtoVersion int // version of the native protocol (default: 2)
  56. Timeout time.Duration // connection timeout (default: 600ms)
  57. Port int // port (default: 9042)
  58. Keyspace string // initial keyspace (optional)
  59. NumConns int // number of connections per host (default: 2)
  60. Consistency Consistency // default consistency level (default: Quorum)
  61. Compressor Compressor // compression algorithm (default: nil)
  62. Authenticator Authenticator // authenticator (default: nil)
  63. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  64. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  65. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  66. MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
  67. PageSize int // Default page size to use for created sessions (default: 5000)
  68. SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
  69. SslOpts *SslOptions
  70. 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)
  71. // PoolConfig configures the underlying connection pool, allowing the
  72. // configuration of host selection and connection selection policies.
  73. PoolConfig PoolConfig
  74. Discovery DiscoveryConfig
  75. // The maximum amount of time to wait for schema agreement in a cluster after
  76. // receiving a schema change frame. (deault: 60s)
  77. MaxWaitSchemaAgreement time.Duration
  78. // HostFilter will filter all incoming events for host, any which dont pass
  79. // the filter will be ignored. If set will take precedence over any options set
  80. // via Discovery
  81. HostFilter HostFilter
  82. // If IgnorePeerAddr is true and the address in system.peers does not match
  83. // the supplied host by either initial hosts or discovered via events then the
  84. // host will be replaced with the supplied address.
  85. //
  86. // For example if an event comes in with host=10.0.0.1 but when looking up that
  87. // address in system.local or system.peers returns 127.0.0.1, the peer will be
  88. // set to 10.0.0.1 which is what will be used to connect to.
  89. IgnorePeerAddr bool
  90. // If DisableInitialHostLookup then the driver will not attempt to get host info
  91. // from the system.peers table, this will mean that the driver will connect to
  92. // hosts supplied and will not attempt to lookup the hosts information, this will
  93. // mean that data_centre, rack and token information will not be available and as
  94. // such host filtering and token aware query routing will not be available.
  95. DisableInitialHostLookup bool
  96. // Configure events the driver will register for
  97. Events struct {
  98. // disable registering for status events (node up/down)
  99. DisableNodeStatusEvents bool
  100. // disable registering for topology events (node added/removed/moved)
  101. DisableTopologyEvents bool
  102. // disable registering for schema events (keyspace/table/function removed/created/updated)
  103. DisableSchemaEvents bool
  104. }
  105. // internal config for testing
  106. disableControlConn bool
  107. }
  108. // NewCluster generates a new config for the default cluster implementation.
  109. func NewCluster(hosts ...string) *ClusterConfig {
  110. cfg := &ClusterConfig{
  111. Hosts: hosts,
  112. CQLVersion: "3.0.0",
  113. ProtoVersion: 2,
  114. Timeout: 600 * time.Millisecond,
  115. Port: 9042,
  116. NumConns: 2,
  117. Consistency: Quorum,
  118. MaxPreparedStmts: defaultMaxPreparedStmts,
  119. MaxRoutingKeyInfo: 1000,
  120. PageSize: 5000,
  121. DefaultTimestamp: true,
  122. MaxWaitSchemaAgreement: 60 * time.Second,
  123. }
  124. return cfg
  125. }
  126. // CreateSession initializes the cluster based on this config and returns a
  127. // session object that can be used to interact with the database.
  128. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  129. return NewSession(*cfg)
  130. }
  131. var (
  132. ErrNoHosts = errors.New("no hosts provided")
  133. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  134. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  135. )