cluster.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-robin host selection policy and a round-robin 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. }
  17. func (p PoolConfig) buildPool(session *Session) *policyConnPool {
  18. return newPolicyConnPool(session)
  19. }
  20. // ClusterConfig is a struct to configure the default cluster implementation
  21. // of gocql. It has a variety of attributes that can be used to modify the
  22. // behavior to fit the most common use cases. Applications that require a
  23. // different setup must implement their own cluster.
  24. type ClusterConfig struct {
  25. // addresses for the initial connections. It is recomended to use the value set in
  26. // the Cassandra config for broadcast_address or listen_address, an IP address not
  27. // a domain name. This is because events from Cassandra will use the configured IP
  28. // address, which is used to index connected hosts. If the domain name specified
  29. // resolves to more than 1 IP address then the driver may connect multiple times to
  30. // the same host, and will not mark the node being down or up from events.
  31. Hosts []string
  32. CQLVersion string // CQL version (default: 3.0.0)
  33. ProtoVersion int // version of the native protocol (default: 2)
  34. Timeout time.Duration // connection timeout (default: 600ms)
  35. Port int // port (default: 9042)
  36. Keyspace string // initial keyspace (optional)
  37. NumConns int // number of connections per host (default: 2)
  38. Consistency Consistency // default consistency level (default: Quorum)
  39. Compressor Compressor // compression algorithm (default: nil)
  40. Authenticator Authenticator // authenticator (default: nil)
  41. RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
  42. SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
  43. MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
  44. MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
  45. PageSize int // Default page size to use for created sessions (default: 5000)
  46. SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
  47. SslOpts *SslOptions
  48. 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)
  49. // PoolConfig configures the underlying connection pool, allowing the
  50. // configuration of host selection and connection selection policies.
  51. PoolConfig PoolConfig
  52. // If not zero, gocql attempt to reconnect known DOWN nodes in every ReconnectSleep.
  53. ReconnectInterval time.Duration
  54. // The maximum amount of time to wait for schema agreement in a cluster after
  55. // receiving a schema change frame. (deault: 60s)
  56. MaxWaitSchemaAgreement time.Duration
  57. // HostFilter will filter all incoming events for host, any which don't pass
  58. // the filter will be ignored. If set will take precedence over any options set
  59. // via Discovery
  60. HostFilter HostFilter
  61. // If IgnorePeerAddr is true and the address in system.peers does not match
  62. // the supplied host by either initial hosts or discovered via events then the
  63. // host will be replaced with the supplied address.
  64. //
  65. // For example if an event comes in with host=10.0.0.1 but when looking up that
  66. // address in system.local or system.peers returns 127.0.0.1, the peer will be
  67. // set to 10.0.0.1 which is what will be used to connect to.
  68. IgnorePeerAddr bool
  69. // If DisableInitialHostLookup then the driver will not attempt to get host info
  70. // from the system.peers table, this will mean that the driver will connect to
  71. // hosts supplied and will not attempt to lookup the hosts information, this will
  72. // mean that data_centre, rack and token information will not be available and as
  73. // such host filtering and token aware query routing will not be available.
  74. DisableInitialHostLookup bool
  75. // Configure events the driver will register for
  76. Events struct {
  77. // disable registering for status events (node up/down)
  78. DisableNodeStatusEvents bool
  79. // disable registering for topology events (node added/removed/moved)
  80. DisableTopologyEvents bool
  81. // disable registering for schema events (keyspace/table/function removed/created/updated)
  82. DisableSchemaEvents bool
  83. }
  84. // DisableSkipMetadata will override the internal result metadata cache so that the driver does not
  85. // send skip_metadata for queries, this means that the result will always contain
  86. // the metadata to parse the rows and will not reuse the metadata from the prepared
  87. // statement.
  88. //
  89. // See https://issues.apache.org/jira/browse/CASSANDRA-10786
  90. DisableSkipMetadata bool
  91. // internal config for testing
  92. disableControlConn bool
  93. }
  94. // NewCluster generates a new config for the default cluster implementation.
  95. //
  96. // The supplied hosts are used to initially connect to the cluster then the rest of
  97. // the ring will be automatically discovered. It is recomended to use the value set in
  98. // the Cassandra config for broadcast_address or listen_address, an IP address not
  99. // a domain name. This is because events from Cassandra will use the configured IP
  100. // address, which is used to index connected hosts. If the domain name specified
  101. // resolves to more than 1 IP address then the driver may connect multiple times to
  102. // the same host, and will not mark the node being down or up from events.
  103. func NewCluster(hosts ...string) *ClusterConfig {
  104. cfg := &ClusterConfig{
  105. Hosts: hosts,
  106. CQLVersion: "3.0.0",
  107. ProtoVersion: 2,
  108. Timeout: 600 * time.Millisecond,
  109. Port: 9042,
  110. NumConns: 2,
  111. Consistency: Quorum,
  112. MaxPreparedStmts: defaultMaxPreparedStmts,
  113. MaxRoutingKeyInfo: 1000,
  114. PageSize: 5000,
  115. DefaultTimestamp: true,
  116. MaxWaitSchemaAgreement: 60 * time.Second,
  117. ReconnectInterval: 60 * time.Second,
  118. }
  119. return cfg
  120. }
  121. // CreateSession initializes the cluster based on this config and returns a
  122. // session object that can be used to interact with the database.
  123. func (cfg *ClusterConfig) CreateSession() (*Session, error) {
  124. return NewSession(*cfg)
  125. }
  126. var (
  127. ErrNoHosts = errors.New("no hosts provided")
  128. ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
  129. ErrHostQueryFailed = errors.New("unable to populate Hosts")
  130. )