yugabyte-ycql-driver 3.2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/README.md +242 -0
  4. data/ext/cassandra_murmur3/cassandra_murmur3.c +178 -0
  5. data/ext/cassandra_murmur3/extconf.rb +2 -0
  6. data/lib/cassandra/address_resolution.rb +36 -0
  7. data/lib/cassandra/address_resolution/policies.rb +2 -0
  8. data/lib/cassandra/address_resolution/policies/ec2_multi_region.rb +56 -0
  9. data/lib/cassandra/address_resolution/policies/none.rb +35 -0
  10. data/lib/cassandra/aggregate.rb +123 -0
  11. data/lib/cassandra/argument.rb +51 -0
  12. data/lib/cassandra/attr_boolean.rb +33 -0
  13. data/lib/cassandra/auth.rb +100 -0
  14. data/lib/cassandra/auth/providers.rb +17 -0
  15. data/lib/cassandra/auth/providers/password.rb +65 -0
  16. data/lib/cassandra/cassandra_logger.rb +80 -0
  17. data/lib/cassandra/cluster.rb +331 -0
  18. data/lib/cassandra/cluster/client.rb +1612 -0
  19. data/lib/cassandra/cluster/connection_pool.rb +78 -0
  20. data/lib/cassandra/cluster/connector.rb +372 -0
  21. data/lib/cassandra/cluster/control_connection.rb +962 -0
  22. data/lib/cassandra/cluster/failed_connection.rb +35 -0
  23. data/lib/cassandra/cluster/metadata.rb +142 -0
  24. data/lib/cassandra/cluster/options.rb +145 -0
  25. data/lib/cassandra/cluster/registry.rb +284 -0
  26. data/lib/cassandra/cluster/schema.rb +405 -0
  27. data/lib/cassandra/cluster/schema/cql_type_parser.rb +112 -0
  28. data/lib/cassandra/cluster/schema/fetchers.rb +1627 -0
  29. data/lib/cassandra/cluster/schema/fqcn_type_parser.rb +175 -0
  30. data/lib/cassandra/cluster/schema/partitioners.rb +21 -0
  31. data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +45 -0
  32. data/lib/cassandra/cluster/schema/partitioners/ordered.rb +37 -0
  33. data/lib/cassandra/cluster/schema/partitioners/random.rb +37 -0
  34. data/lib/cassandra/cluster/schema/replication_strategies.rb +21 -0
  35. data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +102 -0
  36. data/lib/cassandra/cluster/schema/replication_strategies/none.rb +39 -0
  37. data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +44 -0
  38. data/lib/cassandra/column.rb +66 -0
  39. data/lib/cassandra/column_container.rb +326 -0
  40. data/lib/cassandra/compression.rb +69 -0
  41. data/lib/cassandra/compression/compressors/lz4.rb +73 -0
  42. data/lib/cassandra/compression/compressors/snappy.rb +69 -0
  43. data/lib/cassandra/custom_data.rb +53 -0
  44. data/lib/cassandra/driver.rb +260 -0
  45. data/lib/cassandra/errors.rb +784 -0
  46. data/lib/cassandra/execution/info.rb +69 -0
  47. data/lib/cassandra/execution/options.rb +267 -0
  48. data/lib/cassandra/execution/profile.rb +153 -0
  49. data/lib/cassandra/execution/profile_manager.rb +71 -0
  50. data/lib/cassandra/execution/trace.rb +192 -0
  51. data/lib/cassandra/executors.rb +113 -0
  52. data/lib/cassandra/function.rb +156 -0
  53. data/lib/cassandra/function_collection.rb +85 -0
  54. data/lib/cassandra/future.rb +794 -0
  55. data/lib/cassandra/host.rb +102 -0
  56. data/lib/cassandra/index.rb +118 -0
  57. data/lib/cassandra/keyspace.rb +473 -0
  58. data/lib/cassandra/listener.rb +87 -0
  59. data/lib/cassandra/load_balancing.rb +121 -0
  60. data/lib/cassandra/load_balancing/policies.rb +20 -0
  61. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +172 -0
  62. data/lib/cassandra/load_balancing/policies/round_robin.rb +141 -0
  63. data/lib/cassandra/load_balancing/policies/token_aware.rb +149 -0
  64. data/lib/cassandra/load_balancing/policies/white_list.rb +100 -0
  65. data/lib/cassandra/materialized_view.rb +92 -0
  66. data/lib/cassandra/null_logger.rb +56 -0
  67. data/lib/cassandra/protocol.rb +102 -0
  68. data/lib/cassandra/protocol/coder.rb +1085 -0
  69. data/lib/cassandra/protocol/cql_byte_buffer.rb +418 -0
  70. data/lib/cassandra/protocol/cql_protocol_handler.rb +448 -0
  71. data/lib/cassandra/protocol/request.rb +41 -0
  72. data/lib/cassandra/protocol/requests/auth_response_request.rb +51 -0
  73. data/lib/cassandra/protocol/requests/batch_request.rb +117 -0
  74. data/lib/cassandra/protocol/requests/credentials_request.rb +51 -0
  75. data/lib/cassandra/protocol/requests/execute_request.rb +122 -0
  76. data/lib/cassandra/protocol/requests/options_request.rb +39 -0
  77. data/lib/cassandra/protocol/requests/prepare_request.rb +59 -0
  78. data/lib/cassandra/protocol/requests/query_request.rb +112 -0
  79. data/lib/cassandra/protocol/requests/register_request.rb +38 -0
  80. data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
  81. data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
  82. data/lib/cassandra/protocol/response.rb +28 -0
  83. data/lib/cassandra/protocol/responses/already_exists_error_response.rb +50 -0
  84. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +36 -0
  85. data/lib/cassandra/protocol/responses/auth_success_response.rb +36 -0
  86. data/lib/cassandra/protocol/responses/authenticate_response.rb +36 -0
  87. data/lib/cassandra/protocol/responses/error_response.rb +142 -0
  88. data/lib/cassandra/protocol/responses/event_response.rb +30 -0
  89. data/lib/cassandra/protocol/responses/function_failure_error_response.rb +52 -0
  90. data/lib/cassandra/protocol/responses/prepared_result_response.rb +62 -0
  91. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +59 -0
  92. data/lib/cassandra/protocol/responses/read_failure_error_response.rb +71 -0
  93. data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +61 -0
  94. data/lib/cassandra/protocol/responses/ready_response.rb +43 -0
  95. data/lib/cassandra/protocol/responses/result_response.rb +42 -0
  96. data/lib/cassandra/protocol/responses/rows_result_response.rb +39 -0
  97. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +73 -0
  98. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +70 -0
  99. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +37 -0
  100. data/lib/cassandra/protocol/responses/status_change_event_response.rb +39 -0
  101. data/lib/cassandra/protocol/responses/supported_response.rb +36 -0
  102. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +33 -0
  103. data/lib/cassandra/protocol/responses/unavailable_error_response.rb +58 -0
  104. data/lib/cassandra/protocol/responses/unprepared_error_response.rb +48 -0
  105. data/lib/cassandra/protocol/responses/void_result_response.rb +34 -0
  106. data/lib/cassandra/protocol/responses/write_failure_error_response.rb +73 -0
  107. data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +63 -0
  108. data/lib/cassandra/protocol/v1.rb +326 -0
  109. data/lib/cassandra/protocol/v3.rb +358 -0
  110. data/lib/cassandra/protocol/v4.rb +478 -0
  111. data/lib/cassandra/reconnection.rb +49 -0
  112. data/lib/cassandra/reconnection/policies.rb +20 -0
  113. data/lib/cassandra/reconnection/policies/constant.rb +46 -0
  114. data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
  115. data/lib/cassandra/result.rb +276 -0
  116. data/lib/cassandra/retry.rb +154 -0
  117. data/lib/cassandra/retry/policies.rb +21 -0
  118. data/lib/cassandra/retry/policies/default.rb +53 -0
  119. data/lib/cassandra/retry/policies/downgrading_consistency.rb +73 -0
  120. data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
  121. data/lib/cassandra/session.rb +270 -0
  122. data/lib/cassandra/statement.rb +32 -0
  123. data/lib/cassandra/statements.rb +23 -0
  124. data/lib/cassandra/statements/batch.rb +146 -0
  125. data/lib/cassandra/statements/bound.rb +65 -0
  126. data/lib/cassandra/statements/prepared.rb +235 -0
  127. data/lib/cassandra/statements/simple.rb +118 -0
  128. data/lib/cassandra/statements/void.rb +38 -0
  129. data/lib/cassandra/table.rb +240 -0
  130. data/lib/cassandra/time.rb +103 -0
  131. data/lib/cassandra/time_uuid.rb +78 -0
  132. data/lib/cassandra/timestamp_generator.rb +37 -0
  133. data/lib/cassandra/timestamp_generator/simple.rb +38 -0
  134. data/lib/cassandra/timestamp_generator/ticking_on_duplicate.rb +58 -0
  135. data/lib/cassandra/trigger.rb +67 -0
  136. data/lib/cassandra/tuple.rb +131 -0
  137. data/lib/cassandra/types.rb +1704 -0
  138. data/lib/cassandra/udt.rb +443 -0
  139. data/lib/cassandra/util.rb +464 -0
  140. data/lib/cassandra/uuid.rb +110 -0
  141. data/lib/cassandra/uuid/generator.rb +212 -0
  142. data/lib/cassandra/version.rb +21 -0
  143. data/lib/datastax/cassandra.rb +47 -0
  144. data/lib/ycql.rb +842 -0
  145. metadata +243 -0
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ # Cassandra state listener.
21
+ #
22
+ # @abstract Actual state listener implementations don't need to inherit from
23
+ # this class as long as they conform to its interface. This class exists
24
+ # solely for documentation purposes
25
+ #
26
+ # @see Cassandra::Cluster#register
27
+ class Listener
28
+ # This method is called whenever a host is considered to be up, whether
29
+ # by Cassandra's gossip exchange or when the driver has successfully
30
+ # established a connection to it.
31
+ #
32
+ # @param host [Cassandra::Host] a host instance
33
+ # @return [void]
34
+ def host_up(host)
35
+ end
36
+
37
+ # This method is called whenever a host is considered to be down, whether
38
+ # by Cassandra's gossip exchange or when the driver failed to establish
39
+ # any connections to it.
40
+ #
41
+ # @param host [Cassandra::Host] a host instance
42
+ # @return [void]
43
+ def host_down(host)
44
+ end
45
+
46
+ # This method is called whenever a host is discovered by the driver,
47
+ # whether because it is a completely new node or if its
48
+ # {Cassandra::Host#datacenter} or {Cassandra::Host#rack} have changed.
49
+ #
50
+ # @param host [Cassandra::Host] a host instance
51
+ # @return [void]
52
+ def host_found(host)
53
+ end
54
+
55
+ # This method is called whenever a host leaves the cluster, whether
56
+ # because it is completely gone or if its {Cassandra::Host#datacenter} or
57
+ # {Cassandra::Host#rack} have changed.
58
+ #
59
+ # @param host [Cassandra::Host] a host instance
60
+ # @return [void]
61
+ def host_lost(host)
62
+ end
63
+
64
+ # This method is called whenever a new keyspace is created.
65
+ #
66
+ # @param keyspace [Cassandra::Keyspace] a keyspace instance
67
+ # @return [void]
68
+ def keyspace_created(keyspace)
69
+ end
70
+
71
+ # This method is called whenever an existing keyspace is changed. This
72
+ # happens when a new table is created or an existing table is dropped or
73
+ # altered.
74
+ #
75
+ # @param keyspace [Cassandra::Keyspace] a keyspace instance
76
+ # @return [void]
77
+ def keyspace_changed(keyspace)
78
+ end
79
+
80
+ # This method is called whenever an existing keyspace is dropped.
81
+ #
82
+ # @param keyspace [Cassandra::Keyspace] a keyspace instance
83
+ # @return [void]
84
+ def keyspace_dropped(keyspace)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,121 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module LoadBalancing
21
+ # A list of possible load balancing distances that
22
+ # {Cassandra::LoadBalancing::Policy#distance} must return
23
+ DISTANCES = [:ignore, :local, :remote].freeze
24
+
25
+ # @abstract Actual load balancing policies don't need to extend this class,
26
+ # only implement its methods. This class exists for documentation
27
+ # purposes only.
28
+ class Policy
29
+ # Allows policy to initialize with the cluster instance. This method is
30
+ # called once before connecting to the cluster.
31
+ #
32
+ # @param cluster [Cassandra::Cluster] current cluster instance
33
+ # @return [void]
34
+ def setup(cluster)
35
+ end
36
+
37
+ # Allows policy to release any external resources it might be holding.
38
+ # This method is called once the cluster has been terminated after calling
39
+ # {Cassandra::Cluster#close}
40
+ #
41
+ # @param cluster [Cassandra::Cluster] current cluster instance
42
+ # @return [void]
43
+ def teardown(cluster)
44
+ end
45
+
46
+ # @see Cassandra::Listener#host_up
47
+ def host_up(host)
48
+ end
49
+
50
+ # @see Cassandra::Listener#host_down
51
+ def host_down(host)
52
+ end
53
+
54
+ # @see Cassandra::Listener#host_found
55
+ def host_found(host)
56
+ end
57
+
58
+ # @see Cassandra::Listener#host_lost
59
+ def host_lost(host)
60
+ end
61
+
62
+ # Returns a distance that determines how many connections (if any) the
63
+ # driver will open to the host.
64
+ #
65
+ # @param host [Cassandra::Host] a host instance
66
+ # @return [Symbol] distance to host. Must be one of
67
+ # {Cassandra::LoadBalancing::DISTANCES}
68
+ def distance(host)
69
+ end
70
+
71
+ # Load balancing plan is used to determine the order in which hosts
72
+ # should be tried in case of a network failure.
73
+ #
74
+ # @note Hosts that should be ignored, must not be included in the Plan
75
+ #
76
+ # @param keyspace [String] current keyspace of the {Cassandra::Session}
77
+ # @param statement [Cassandra::Statement] actual statement to be executed
78
+ # @param options [Cassandra::Execution::Options] execution options to be used
79
+ # @raise [NotImplementedError] override this method to return a plan
80
+ # @return [Cassandra::LoadBalancing::Plan] a load balancing plan
81
+ def plan(keyspace, statement, options)
82
+ end
83
+
84
+ # @private
85
+ def inspect
86
+ "#<#{self.class.name}:0x#{object_id.to_s(16)}>"
87
+ end
88
+ end
89
+
90
+ # A load balancing plan is used to determine the order of hosts for running
91
+ # queries, preparing statements and establishing connections.
92
+ # @abstract Plans returned by {Cassandra::LoadBalancing::Policy#plan}
93
+ # implementations don't need to extend this class, only implement its
94
+ # methods. This class exists for documentation purposes only.
95
+ class Plan
96
+ # @return [Boolean] whether the plan contains any more hosts
97
+ def has_next?
98
+ end
99
+
100
+ # @return [Cql::Host] next host to try
101
+ def next
102
+ end
103
+ end
104
+
105
+ # @private
106
+ class EmptyPlan
107
+ def has_next?
108
+ false
109
+ end
110
+
111
+ def next
112
+ nil
113
+ end
114
+ end
115
+
116
+ # @private
117
+ EMPTY_PLAN = EmptyPlan.new
118
+ end
119
+ end
120
+
121
+ require 'cassandra/load_balancing/policies'
@@ -0,0 +1,20 @@
1
+ #--
2
+ # Copyright DataStax, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #++
16
+
17
+ require 'cassandra/load_balancing/policies/dc_aware_round_robin'
18
+ require 'cassandra/load_balancing/policies/round_robin'
19
+ require 'cassandra/load_balancing/policies/token_aware'
20
+ require 'cassandra/load_balancing/policies/white_list'
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module LoadBalancing
21
+ module Policies
22
+ class DCAwareRoundRobin < Policy
23
+ # @private
24
+ LOCAL_CONSISTENCIES = [:local_quorum, :local_one].freeze
25
+ # @private
26
+ EMPTY_ARRAY = [].freeze
27
+
28
+ # @private
29
+ class Plan
30
+ def initialize(local, remote, index)
31
+ @local = local
32
+ @remote = remote
33
+ @index = index
34
+
35
+ @local_remaining = @local_total = local.size
36
+ @remote_remaining = @remote_total = remote.size
37
+ end
38
+
39
+ def has_next?
40
+ @local_remaining > 0 || @remote_remaining > 0
41
+ end
42
+
43
+ def next
44
+ if @local_remaining > 0
45
+ @local_remaining -= 1
46
+ i = (@index % @local_total)
47
+ @index += 1
48
+
49
+ return @local[i]
50
+ end
51
+
52
+ if @remote_remaining > 0
53
+ @remote_remaining -= 1
54
+ i = (@index % @remote_total)
55
+ @index += 1
56
+
57
+ return @remote[i]
58
+ end
59
+ end
60
+ end
61
+
62
+ include MonitorMixin
63
+
64
+ def initialize(datacenter = nil,
65
+ max_remote_hosts_to_use = 0,
66
+ use_remote_hosts_for_local_consistency = false)
67
+ datacenter &&= String(datacenter)
68
+ max_remote_hosts_to_use &&= Integer(max_remote_hosts_to_use)
69
+
70
+ Util.assert_not_empty(datacenter) { 'datacenter cannot be empty' } unless datacenter.nil?
71
+
72
+ unless max_remote_hosts_to_use.nil?
73
+ Util.assert(max_remote_hosts_to_use >= 0) do
74
+ 'max_remote_hosts_to_use must be nil or >= 0'
75
+ end
76
+ end
77
+
78
+ # If use_remote* is true, max_remote* must be > 0
79
+ if use_remote_hosts_for_local_consistency
80
+ Util.assert(max_remote_hosts_to_use.nil? || max_remote_hosts_to_use > 0,
81
+ 'max_remote_hosts_to_use must be nil (meaning unlimited) or > 0 when ' \
82
+ 'use_remote_hosts_for_local_consistency is true')
83
+ end
84
+
85
+ @datacenter = datacenter
86
+ @max_remote = max_remote_hosts_to_use
87
+ @local = ::Array.new
88
+ @remote = ::Array.new
89
+ @position = 0
90
+
91
+ @use_remote = !!use_remote_hosts_for_local_consistency
92
+
93
+ mon_initialize
94
+ end
95
+
96
+ def host_up(host)
97
+ @datacenter = host.datacenter if !@datacenter && host.datacenter
98
+
99
+ if host.datacenter.nil? || host.datacenter == @datacenter
100
+ synchronize { @local = @local.dup.push(host) }
101
+ elsif @max_remote.nil? || @remote.size < @max_remote
102
+ synchronize { @remote = @remote.dup.push(host) }
103
+ end
104
+
105
+ self
106
+ end
107
+
108
+ def host_down(host)
109
+ if host.datacenter.nil? || host.datacenter == @datacenter
110
+ synchronize do
111
+ @local = @local.dup
112
+ @local.delete(host)
113
+ end
114
+ else
115
+ synchronize do
116
+ @remote = @remote.dup
117
+ @remote.delete(host)
118
+ end
119
+ end
120
+
121
+ self
122
+ end
123
+
124
+ def host_found(host)
125
+ self
126
+ end
127
+
128
+ def host_lost(host)
129
+ self
130
+ end
131
+
132
+ def distance(host)
133
+ if host.datacenter.nil? || host.datacenter == @datacenter
134
+ @local.include?(host) ? :local : :ignore
135
+ else
136
+ @remote.include?(host) ? :remote : :ignore
137
+ end
138
+ end
139
+
140
+ def plan(keyspace, statement, options)
141
+ local = @local
142
+
143
+ remote = if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
144
+ EMPTY_ARRAY
145
+ else
146
+ @remote
147
+ end
148
+
149
+ total = local.size + remote.size
150
+
151
+ return EMPTY_PLAN if total == 0
152
+
153
+ position = @position % total
154
+ @position = position + 1
155
+
156
+ Plan.new(local, remote, position)
157
+ end
158
+
159
+ # @private
160
+ def inspect
161
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
162
+ "datacenter=#{@datacenter.inspect}, " \
163
+ "use_remote=#{@use_remote.inspect}, " \
164
+ "max_remote=#{@max_remote.inspect}, " \
165
+ "local=#{@local.inspect}, " \
166
+ "remote=#{@remote.inspect}, " \
167
+ "position=#{@position.inspect}>"
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module LoadBalancing
21
+ module Policies
22
+ class RoundRobin < Policy
23
+ # @private
24
+ class Plan
25
+ def initialize(hosts, index)
26
+ @hosts = hosts
27
+ @index = index
28
+
29
+ @total = @remaining = hosts.size
30
+ end
31
+
32
+ def has_next?
33
+ @remaining > 0
34
+ end
35
+
36
+ def next
37
+ return if @remaining == 0
38
+
39
+ @remaining -= 1
40
+ index = @index
41
+ @index = (index + 1) % @total
42
+
43
+ @hosts[index]
44
+ end
45
+ end
46
+
47
+ include MonitorMixin
48
+
49
+ def initialize
50
+ @hosts = ::Array.new
51
+ @position = 0
52
+
53
+ mon_initialize
54
+ end
55
+
56
+ # Adds this host to rotation
57
+ #
58
+ # @param host [Cassandra::Host] a host instance
59
+ # @return [Cassandra::LoadBalancing::Policies::RoundRobin] self
60
+ # @see Cassandra::Listener#host_up
61
+ def host_up(host)
62
+ synchronize { @hosts = @hosts.dup.push(host) }
63
+
64
+ self
65
+ end
66
+
67
+ # Removes this host from rotation
68
+ #
69
+ # @param host [Cassandra::Host] a host instance
70
+ # @return [Cassandra::LoadBalancing::Policies::RoundRobin] self
71
+ # @see Cassandra::Listener#host_down
72
+ def host_down(host)
73
+ synchronize do
74
+ @hosts = @hosts.dup
75
+ @hosts.delete(host)
76
+ end
77
+
78
+ self
79
+ end
80
+
81
+ # Noop
82
+ #
83
+ # @param host [Cassandra::Host] a host instance
84
+ # @return [Cassandra::LoadBalancing::Policies::RoundRobin] self
85
+ # @see Cassandra::Listener#host_found
86
+ def host_found(host)
87
+ self
88
+ end
89
+
90
+ # Noop
91
+ #
92
+ # @param host [Cassandra::Host] a host instance
93
+ # @return [Cassandra::LoadBalancing::Policies::RoundRobin] self
94
+ # @see Cassandra::Listener#host_lost
95
+ def host_lost(host)
96
+ self
97
+ end
98
+
99
+ # Returns distance to host. All hosts in rotation are considered
100
+ # `:local`, all other hosts - `:ignore`.
101
+ #
102
+ # @param host [Cassandra::Host] a host instance
103
+ # @return [Symbol] `:local` for all hosts in rotation and `:ignore` for
104
+ # all other hosts.
105
+ # @see Cassandra::LoadBalancing::Policy#distance
106
+ def distance(host)
107
+ @hosts.include?(host) ? :local : :ignore
108
+ end
109
+
110
+ # Returns a load balancing plan that rotates hosts by 1 each time a
111
+ # plan is requested.
112
+ #
113
+ # @param keyspace [String] current keyspace of the {Cassandra::Session}
114
+ # @param statement [Cassandra::Statement] actual statement to be
115
+ # executed
116
+ # @param options [Cassandra::Execution::Options] execution options to
117
+ # be used
118
+ # @return [Cassandra::LoadBalancing::Plan] a rotated load balancing plan
119
+ # @see Cassandra::LoadBalancing::Policy#plan
120
+ def plan(keyspace, statement, options)
121
+ hosts = @hosts
122
+ total = hosts.size
123
+
124
+ return EMPTY_PLAN if total == 0
125
+
126
+ position = @position % total
127
+ @position = position + 1
128
+
129
+ Plan.new(hosts, position)
130
+ end
131
+
132
+ # @private
133
+ def inspect
134
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
135
+ "hosts=#{@hosts.inspect}, " \
136
+ "position=#{@position.inspect}>"
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end