cluster.go 11 KB

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