cql-rb 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTJjMzQ5MTY0M2I5N2JjYzZmODFkODQwNzY3ZmE2MmU1MGZjZGUzMw==
5
- data.tar.gz: !binary |-
6
- N2I0MjdmODYxOTM0YjgwODFjNThhZDJmOWFlMzdiNDdjYjJmODBmNQ==
2
+ SHA1:
3
+ metadata.gz: 77764255905873f7b923f9fca746dbef0496eb15
4
+ data.tar.gz: c1c39af44330064cb4d9dd2693d81384dc69f09f
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MzE0MWU4ZTUxYmNjOGFkMDc0MzE1YTgxMGEyN2YzMzY2NDYzYTJjZTYxNDM5
10
- ZTU3NGJiY2MxYjAyZjBjOWU3NmM1NmNiMDU2ZmI0YWYxMDUyOGI5OTQ1M2Zi
11
- YzBkZmRmODIyMTUyZTUxMGM3MTk5YTg4MDE3YTZhODAxY2U4MjE=
12
- data.tar.gz: !binary |-
13
- ZmMwYTY2NTU3MGZhMjMyOTk0NjhkZjRmMTA2YmU2MTVhNzYxMmRhZDkzNmU2
14
- NWNiOTY1YmYzYjlkOGZlMTkyMDdkMzJiMjQyMzY1ZDFkNGI3ZDE4OTUyNzFj
15
- ZTgyZWU0YzA3OTVmOWQwYjEzMDgwZTRmNmM2NDgxMjQwNDc2MTA=
6
+ metadata.gz: 14452316f397f8f2bdc37f764aaf7c070625be977c6b579c4eb881d3e9736a33eb720da296f370c7dc27de7d4fe1917f192267f1a884ae3381afd870378edbd7
7
+ data.tar.gz: fa4f95a86ab0ae162ccc64c9eaed8f3c99c7bc7cf217750617e3339cfba62d31badc1609f63ffc65026818777f9bc69bacdcaa21f88d32d07b730c7f5f007ef2
@@ -101,6 +101,7 @@ module Cql
101
101
  DEFAULT_CONSISTENCY = :quorum
102
102
  DEFAULT_PORT = 9042
103
103
  DEFAULT_CONNECTION_TIMEOUT = 10
104
+ MAX_RECONNECTION_ATTEMPTS = 5
104
105
 
105
106
  def extract_hosts(options)
106
107
  if options[:hosts]
@@ -170,27 +171,37 @@ module Cql
170
171
  end
171
172
  end
172
173
  connection.on_event do |event|
173
- begin
174
- if event.change == 'UP'
175
- @logger.debug('Received UP event')
176
- handle_topology_change
174
+ if event.change == 'UP' || event.change == 'NEW_NODE'
175
+ @logger.debug('Received %s event' % event.change)
176
+ unless @looking_for_nodes
177
+ @looking_for_nodes = true
178
+ handle_topology_change.on_complete do |f|
179
+ @looking_for_nodes = false
180
+ end
177
181
  end
178
182
  end
179
183
  end
180
184
  end
181
185
 
182
- def handle_topology_change
183
- seed_connections = @connection_manager.snapshot
184
- f = @connection_helper.discover_peers(seed_connections, keyspace)
185
- f.on_value do |connections|
186
- connected_connections = connections.select(&:connected?)
187
- if connected_connections.any?
188
- @connection_manager.add_connections(connected_connections)
189
- else
190
- @logger.debug('Scheduling new peer discovery in 1s')
191
- f = @io_reactor.schedule_timer(1)
192
- f.on_value do
193
- handle_topology_change
186
+ def handle_topology_change(remaning_attempts=MAX_RECONNECTION_ATTEMPTS)
187
+ with_failure_handler do
188
+ seed_connections = @connection_manager.snapshot
189
+ f = @connection_helper.discover_peers(seed_connections, keyspace)
190
+ f.flat_map do |connections|
191
+ connected_connections = connections.select(&:connected?)
192
+ if connected_connections.any?
193
+ @connection_manager.add_connections(connected_connections)
194
+ Future.resolved
195
+ elsif remaning_attempts > 0
196
+ timeout = 2**(MAX_RECONNECTION_ATTEMPTS - remaning_attempts)
197
+ @logger.debug('Scheduling new peer discovery in %ds' % timeout)
198
+ f = @io_reactor.schedule_timer(timeout)
199
+ f.flat_map do
200
+ handle_topology_change(remaning_attempts - 1)
201
+ end
202
+ else
203
+ @logger.warn('Giving up looking for additional nodes')
204
+ Future.resolved
194
205
  end
195
206
  end
196
207
  end
data/lib/cql/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Cql
4
- VERSION = '1.1.2'.freeze
4
+ VERSION = '1.1.3'.freeze
5
5
  end
@@ -832,6 +832,13 @@ module Cql
832
832
  connections.select(&:connected?).should have(3).items
833
833
  end
834
834
 
835
+ it 'reconnects when it receives a topology change NEW_NODE event' do
836
+ connections.first.close
837
+ event = Protocol::TopologyChangeEventResponse.new('NEW_NODE', IPAddr.new('1.1.1.1'), 9999)
838
+ connections.select(&:has_event_listener?).first.trigger_event(event)
839
+ connections.select(&:connected?).should have(3).items
840
+ end
841
+
835
842
  it 'eventually reconnects even when the node doesn\'t respond at first' do
836
843
  timer_promise = Promise.new
837
844
  io_reactor.stub(:schedule_timer).and_return(timer_promise.future)
@@ -845,11 +852,39 @@ module Cql
845
852
  connections.select(&:connected?).should have(3).items
846
853
  end
847
854
 
848
- it 'connects when it receives a topology change UP event' do
849
- min_peers[0] = 3
850
- event = Protocol::TopologyChangeEventResponse.new('UP', IPAddr.new('1.1.1.1'), 9999)
855
+ it 'eventually stops attempting to reconnect if no new nodes are found' do
856
+ io_reactor.stub(:schedule_timer).and_return(Future.resolved)
857
+ io_reactor.stub(:connect).and_return(Future.failed(Io::ConnectionError.new))
858
+ connections.first.close
859
+ event = Protocol::TopologyChangeEventResponse.new('NEW_NODE', IPAddr.new('1.1.1.1'), 9999)
851
860
  connections.select(&:has_event_listener?).first.trigger_event(event)
852
- connections.select(&:connected?).should have(4).items
861
+ io_reactor.should have_received(:schedule_timer).exactly(5).times
862
+ end
863
+
864
+ it 'does not start a new reconnection loop when one is already in progress' do
865
+ timer_promises = Array.new(5) { Promise.new }
866
+ io_reactor.stub(:schedule_timer).and_return(*timer_promises.map(&:future))
867
+ io_reactor.stub(:connect).and_return(Future.failed(Io::ConnectionError.new))
868
+ connections.first.close
869
+ event = Protocol::StatusChangeEventResponse.new('UP', IPAddr.new('1.1.1.1'), 9999)
870
+ connections.select(&:has_event_listener?).first.trigger_event(event)
871
+ timer_promises.first.fulfill
872
+ connections.select(&:has_event_listener?).first.trigger_event(event)
873
+ timer_promises.drop(1).each(&:fulfill)
874
+ io_reactor.should have_received(:schedule_timer).exactly(5).times
875
+ connections.select(&:has_event_listener?).first.trigger_event(event)
876
+ io_reactor.should have_received(:schedule_timer).exactly(10).times
877
+ end
878
+
879
+ it 'allows a new reconnection loop to start even if the previous failed' do
880
+ io_reactor.stub(:schedule_timer).and_raise('BORK!')
881
+ io_reactor.stub(:connect).and_return(Future.failed(Io::ConnectionError.new))
882
+ connections.first.close
883
+ event = Protocol::TopologyChangeEventResponse.new('NEW_NODE', IPAddr.new('1.1.1.1'), 9999)
884
+ connections.select(&:has_event_listener?).first.trigger_event(event)
885
+ io_reactor.stub(:schedule_timer).and_return(Future.resolved)
886
+ connections.select(&:has_event_listener?).first.trigger_event(event)
887
+ io_reactor.should have_received(:schedule_timer).exactly(6).times
853
888
  end
854
889
 
855
890
  it 'registers a new event listener when the current event listening connection closes' do
@@ -916,6 +951,14 @@ module Cql
916
951
  logger.should have_received(:debug).with(/Received UP event/)
917
952
  end
918
953
 
954
+ it 'logs when it receives a NEW_NODE event' do
955
+ logger.stub(:debug)
956
+ client.connect.value
957
+ event = Protocol::TopologyChangeEventResponse.new('NEW_NODE', IPAddr.new('1.1.1.1'), 9999)
958
+ connections.select(&:has_event_listener?).first.trigger_event(event)
959
+ logger.should have_received(:debug).with(/Received NEW_NODE event/)
960
+ end
961
+
919
962
  it 'logs when it fails with a connect after an UP event' do
920
963
  logger.stub(:debug)
921
964
  logger.stub(:warn)
@@ -927,6 +970,16 @@ module Cql
927
970
  logger.should have_received(:debug).with(/Scheduling new peer discovery in \d+s/)
928
971
  end
929
972
 
973
+ it 'logs when it gives up attempting to reconnect' do
974
+ logger.stub(:warn)
975
+ client.connect.value
976
+ io_reactor.stub(:schedule_timer).and_return(Future.resolved)
977
+ io_reactor.stub(:connect).and_return(Future.failed(Io::ConnectionError.new))
978
+ event = Protocol::StatusChangeEventResponse.new('UP', IPAddr.new('1.1.1.1'), 9999)
979
+ connections.select(&:has_event_listener?).first.trigger_event(event)
980
+ logger.should have_received(:warn).with(/Giving up looking for additional nodes/).at_least(1).times
981
+ end
982
+
930
983
  it 'logs when it disconnects' do
931
984
  logger.stub(:info)
932
985
  client.connect.value
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cql-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Hultberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-14 00:00:00.000000000 Z
11
+ date: 2014-01-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A pure Ruby CQL3 driver for Cassandra
14
14
  email:
@@ -120,12 +120,12 @@ require_paths:
120
120
  - lib
121
121
  required_ruby_version: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - ! '>='
123
+ - - '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: 1.9.2
126
126
  required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  requirements:
128
- - - ! '>='
128
+ - - '>='
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  requirements: []