cluster.go 6.0 KB

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