cluster.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "net"
  8. "time"
  9. )
  10. // PoolConfig configures the connection pool used by the driver, it defaults to
  11. // using a round-robin host selection policy and a round-robin connection selection
  12. // policy for each host.
  13. type PoolConfig struct {
  14. // HostSelectionPolicy sets the policy for selecting which host to use for a
  15. // given query (default: RoundRobinHostPolicy())
  16. HostSelectionPolicy HostSelectionPolicy
  17. }
  18. func (p PoolConfig) buildPool(session *Session) *policyConnPool {
  19. return newPolicyConnPool(session)
  20. }
  21. // ClusterConfig is a struct to configure the default cluster implementation
  22. // of gocql. It has a variety of attributes that can be used to modify the
  23. // behavior to fit the most common use cases. Applications that require a
  24. // different setup must implement their own cluster.
  25. type ClusterConfig struct {
  26. // addresses for the initial connections. It is recommended to use the value set in
  27. // the Cassandra config for broadcast_address or listen_address, an IP address not
  28. // a domain name. This is because events from Cassandra will use the configured IP
  29. // address, which is used to index connected hosts. If the domain name specified
  30. // resolves to more than 1 IP address then the driver may connect multiple times to
  31. // the same host, and will not mark the node being down or up from events.
  32. Hosts []string
  33. CQLVersion string // CQL version (default: 3.0.0)
  34. // ProtoVersion sets the version of the native protocol to use, this will
  35. // enable features in the driver for specific protocol versions, generally this
  36. // should be set to a known version (2,3,4) for the cluster being connected to.
  37. //
  38. // If it is 0 or unset (the default) then the driver will attempt to discover the
  39. // highest supported protocol for the cluster. In clusters with nodes of different
  40. // versions the protocol selected is not defined (ie, it can be any of the supported in the cluster)
  41. ProtoVersion int
  42. Timeout time.Duration // connection timeout (default: 600ms)
  43. ConnectTimeout time.Duration // initial connection timeout, used during initial dial to server (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. Consistency Consistency // default consistency level (default: Quorum)
  48. Compressor Compressor // compression algorithm (default: nil)
  49. Authenticator Authenticator // authenticator (default: nil)
  50. AuthProvider func(h *HostInfo) (Authenticator, error) // an authenticator factory. Can be used to create alternative authenticators (default: nil)
  51. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  52. ConvictionPolicy ConvictionPolicy // Decide whether to mark host as down based on the error and host info (default: SimpleConvictionPolicy)
  53. ReconnectionPolicy ReconnectionPolicy // Default reconnection policy to use for reconnecting before trying to mark host as down (default: see below)
  54. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  55. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  56. MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
  57. PageSize int // Default page size to use for created sessions (default: 5000)
  58. SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
  59. SslOpts *SslOptions
  60. 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)
  61. // PoolConfig configures the underlying connection pool, allowing the
  62. // configuration of host selection and connection selection policies.
  63. PoolConfig PoolConfig
  64. // If not zero, gocql attempt to reconnect known DOWN nodes in every ReconnectInterval.
  65. ReconnectInterval time.Duration
  66. // The maximum amount of time to wait for schema agreement in a cluster after
  67. // receiving a schema change frame. (default: 60s)
  68. MaxWaitSchemaAgreement time.Duration
  69. // HostFilter will filter all incoming events for host, any which don't pass
  70. // the filter will be ignored. If set will take precedence over any options set
  71. // via Discovery
  72. HostFilter HostFilter
  73. // AddressTranslator will translate addresses found on peer discovery and/or
  74. // node change events.
  75. AddressTranslator AddressTranslator
  76. // If IgnorePeerAddr is true and the address in system.peers does not match
  77. // the supplied host by either initial hosts or discovered via events then the
  78. // host will be replaced with the supplied address.
  79. //
  80. // For example if an event comes in with host=10.0.0.1 but when looking up that
  81. // address in system.local or system.peers returns 127.0.0.1, the peer will be
  82. // set to 10.0.0.1 which is what will be used to connect to.
  83. IgnorePeerAddr bool
  84. // If DisableInitialHostLookup then the driver will not attempt to get host info
  85. // from the system.peers table, this will mean that the driver will connect to
  86. // hosts supplied and will not attempt to lookup the hosts information, this will
  87. // mean that data_centre, rack and token information will not be available and as
  88. // such host filtering and token aware query routing will not be available.
  89. DisableInitialHostLookup bool
  90. // Configure events the driver will register for
  91. Events struct {
  92. // disable registering for status events (node up/down)
  93. DisableNodeStatusEvents bool
  94. // disable registering for topology events (node added/removed/moved)
  95. DisableTopologyEvents bool
  96. // disable registering for schema events (keyspace/table/function removed/created/updated)
  97. DisableSchemaEvents bool
  98. }
  99. // DisableSkipMetadata will override the internal result metadata cache so that the driver does not
  100. // send skip_metadata for queries, this means that the result will always contain
  101. // the metadata to parse the rows and will not reuse the metadata from the prepared
  102. // statement.
  103. //
  104. // See https://issues.apache.org/jira/browse/CASSANDRA-10786
  105. DisableSkipMetadata bool
  106. // QueryObserver will set the provided query observer on all queries created from this session.
  107. // Use it to collect metrics / stats from queries by providing an implementation of QueryObserver.
  108. QueryObserver QueryObserver
  109. // BatchObserver will set the provided batch observer on all queries created from this session.
  110. // Use it to collect metrics / stats from batch queries by providing an implementation of BatchObserver.
  111. BatchObserver BatchObserver
  112. // ConnectObserver will set the provided connect observer on all queries
  113. // created from this session.
  114. ConnectObserver ConnectObserver
  115. // FrameHeaderObserver will set the provided frame header observer on all frames' headers created from this session.
  116. // Use it to collect metrics / stats from frames by providing an implementation of FrameHeaderObserver.
  117. FrameHeaderObserver FrameHeaderObserver
  118. // Default idempotence for queries
  119. DefaultIdempotence bool
  120. // The time to wait for frames before flushing the frames connection to Cassandra.
  121. // Can help reduce syscall overhead by making less calls to write. Set to 0 to
  122. // disable.
  123. //
  124. // (default: 200 microseconds)
  125. WriteCoalesceWaitTime time.Duration
  126. // internal config for testing
  127. disableControlConn bool
  128. }
  129. // NewCluster generates a new config for the default cluster implementation.
  130. //
  131. // The supplied hosts are used to initially connect to the cluster then the rest of
  132. // the ring will be automatically discovered. It is recommended to use the value set in
  133. // the Cassandra config for broadcast_address or listen_address, an IP address not
  134. // a domain name. This is because events from Cassandra will use the configured IP
  135. // address, which is used to index connected hosts. If the domain name specified
  136. // resolves to more than 1 IP address then the driver may connect multiple times to
  137. // the same host, and will not mark the node being down or up from events.
  138. func NewCluster(hosts ...string) *ClusterConfig {
  139. cfg := &ClusterConfig{
  140. Hosts: hosts,
  141. CQLVersion: "3.0.0",
  142. Timeout: 600 * time.Millisecond,
  143. ConnectTimeout: 600 * time.Millisecond,
  144. Port: 9042,
  145. NumConns: 2,
  146. Consistency: Quorum,
  147. MaxPreparedStmts: defaultMaxPreparedStmts,
  148. MaxRoutingKeyInfo: 1000,
  149. PageSize: 5000,
  150. DefaultTimestamp: true,
  151. MaxWaitSchemaAgreement: 60 * time.Second,
  152. ReconnectInterval: 60 * time.Second,
  153. ConvictionPolicy: &SimpleConvictionPolicy{},
  154. ReconnectionPolicy: &ConstantReconnectionPolicy{MaxRetries: 3, Interval: 1 * time.Second},
  155. WriteCoalesceWaitTime: 200 * time.Microsecond,
  156. }
  157. return cfg
  158. }
  159. // CreateSession initializes the cluster based on this config and returns a
  160. // session object that can be used to interact with the database.
  161. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  162. return NewSession(*cfg)
  163. }
  164. // translateAddressPort is a helper method that will use the given AddressTranslator
  165. // if defined, to translate the given address and port into a possibly new address
  166. // and port, If no AddressTranslator or if an error occurs, the given address and
  167. // port will be returned.
  168. func (cfg *ClusterConfig) translateAddressPort(addr net.IP, port int) (net.IP, int) {
  169. if cfg.AddressTranslator == nil || len(addr) == 0 {
  170. return addr, port
  171. }
  172. newAddr, newPort := cfg.AddressTranslator.Translate(addr, port)
  173. if gocqlDebug {
  174. Logger.Printf("gocql: translating address '%v:%d' to '%v:%d'", addr, port, newAddr, newPort)
  175. }
  176. return newAddr, newPort
  177. }
  178. func (cfg *ClusterConfig) filterHost(host *HostInfo) bool {
  179. return !(cfg.HostFilter == nil || cfg.HostFilter.Accept(host))
  180. }
  181. var (
  182. ErrNoHosts = errors.New("no hosts provided")
  183. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  184. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  185. )