forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_host_connection_pool.py
More file actions
542 lines (452 loc) · 23.9 KB
/
Copy pathtest_host_connection_pool.py
File metadata and controls
542 lines (452 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from concurrent.futures import ThreadPoolExecutor
import logging
import time
import uuid
from cassandra.protocol_features import ProtocolFeatures
from cassandra.shard_info import _ShardingInfo
import unittest
from threading import Thread, Event, Lock, Condition
from unittest.mock import Mock, NonCallableMagicMock, MagicMock
from cassandra.cluster import Cluster, Session, ShardAwareOptions
from cassandra.connection import ClientRoutesEndPoint, Connection, DefaultEndPoint
from cassandra.metadata import Metadata
from cassandra.pool import HostConnection
from cassandra.pool import Host, NoConnectionsAvailable
from cassandra.policies import HostDistance, SimpleConvictionPolicy
import pytest
from tests.unit.util import HashableMock
LOGGER = logging.getLogger(__name__)
class _PoolTests(unittest.TestCase):
__test__ = False
PoolImpl = None
uses_single_connection = None
def make_session(self):
session = NonCallableMagicMock(spec=Session, keyspace='foobarkeyspace', _trash=[])
return session
def test_borrow_and_return(self):
host = Mock(spec=Host, address='ip1')
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False, max_request_id=100)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
c, request_id = pool.borrow_connection(timeout=0.01)
assert c is conn
assert 1 == conn.in_flight
conn.set_keyspace_blocking.assert_called_once_with('foobarkeyspace')
pool.return_connection(conn)
assert 0 == conn.in_flight
if not self.uses_single_connection:
assert conn not in pool._trash
def test_failed_wait_for_connection(self):
host = Mock(spec=Host, address='ip1')
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False, max_request_id=100)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
pool.borrow_connection(timeout=0.01)
assert 1 == conn.in_flight
conn.in_flight = conn.max_request_id
# we're already at the max number of requests for this connection,
# so we this should fail
with pytest.raises(NoConnectionsAvailable):
pool.borrow_connection(0)
def test_successful_wait_for_connection(self):
host = Mock(spec=Host, address='ip1')
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False, max_request_id=100,
lock=Lock())
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
pool.borrow_connection(timeout=0.01)
assert 1 == conn.in_flight
def get_second_conn():
c, request_id = pool.borrow_connection(1.0)
assert conn is c
pool.return_connection(c)
t = Thread(target=get_second_conn)
t.start()
pool.return_connection(conn)
t.join()
assert 0 == conn.in_flight
def test_spawn_when_at_max(self):
host = Mock(spec=Host, address='ip1')
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False, max_request_id=100)
conn.max_request_id = 100
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
pool.borrow_connection(timeout=0.01)
assert 1 == conn.in_flight
# make this conn full
conn.in_flight = conn.max_request_id
# we don't care about making this borrow_connection call succeed for the
# purposes of this test, as long as it results in a new connection
# creation being scheduled
with pytest.raises(NoConnectionsAvailable):
pool.borrow_connection(0)
if not self.uses_single_connection:
session.submit.assert_called_once_with(pool._create_new_connection)
def test_return_defunct_connection(self):
host = Mock(spec=Host, address='ip1')
host.lock = Lock()
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
pool.borrow_connection(timeout=0.01)
conn.is_defunct = True
session.cluster.signal_connection_failure.return_value = False
host.signal_connection_failure.return_value = False
pool.return_connection(conn)
# the connection should be closed a new creation scheduled
assert session.submit.call_args
assert not pool.is_shutdown
def test_return_defunct_connection_on_down_host(self):
host = Mock(spec=Host, address='ip1')
host.lock = Lock()
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False)
session.cluster.connection_factory.return_value = conn
session.cluster.shard_aware_options = ShardAwareOptions()
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
pool.borrow_connection(timeout=0.01)
conn.is_defunct = True
session.cluster.signal_connection_failure.return_value = True
host.signal_connection_failure.return_value = True
pool.return_connection(conn)
# the connection should be closed a new creation scheduled
assert conn.close.call_args
if self.PoolImpl is HostConnection:
# on shard aware implementation we use submit function regardless
assert host.signal_connection_failure.call_args
session.cluster.on_down.assert_called_once_with(
host, is_host_addition=False, expected_endpoint=pool.endpoint)
assert session.submit.called
else:
assert not session.submit.called
assert session.cluster.signal_connection_failure.call_args
assert pool.is_shutdown
def test_return_closed_connection(self):
host = Mock(spec=Host, address='ip1')
host.lock = Lock()
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=True, max_request_id=100,
signaled_error=False, orphaned_threshold_reached=False)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
session.cluster.connection_factory.assert_called_once_with(host.endpoint, on_orphaned_stream_released=pool.on_orphaned_stream_released)
pool.borrow_connection(timeout=0.01)
conn.is_closed = True
session.cluster.signal_connection_failure.return_value = False
host.signal_connection_failure.return_value = False
pool.return_connection(conn)
# a new creation should be scheduled
assert session.submit.call_args
assert not pool.is_shutdown
def test_return_defunct_connection_after_endpoint_swap_is_ignored(self):
old_endpoint = DefaultEndPoint('127.0.0.1')
new_endpoint = DefaultEndPoint('127.0.0.2')
host = Mock(spec=Host, address='ip1')
host.endpoint = old_endpoint
host.lock = Lock()
session = self.make_session()
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
pool.borrow_connection(timeout=0.01)
host.endpoint = new_endpoint
conn.is_defunct = True
host.signal_connection_failure.return_value = True
pool.return_connection(conn)
host.signal_connection_failure.assert_not_called()
session.cluster.on_down.assert_not_called()
session.submit.assert_not_called()
conn.close.assert_called_once_with()
assert not pool.is_shutdown
def test_return_defunct_connection_after_client_route_endpoint_port_swap_is_ignored(self):
host_id = uuid.uuid4()
old_endpoint = ClientRoutesEndPoint(
host_id, Mock(), '127.0.0.1', original_port=9042)
new_endpoint = ClientRoutesEndPoint(
host_id, Mock(), '127.0.0.1', original_port=9142)
assert old_endpoint == new_endpoint
assert not Cluster._endpoints_match(old_endpoint, new_endpoint)
host = Mock(spec=Host, address='ip1')
host.endpoint = old_endpoint
host.lock = Lock()
session = self.make_session()
session.cluster._endpoints_match.side_effect = Cluster._endpoints_match
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(host, HostDistance.LOCAL, session)
pool.borrow_connection(timeout=0.01)
host.endpoint = new_endpoint
conn.is_defunct = True
host.signal_connection_failure.return_value = True
pool.return_connection(conn)
host.signal_connection_failure.assert_not_called()
session.cluster.on_down.assert_not_called()
session.submit.assert_not_called()
conn.close.assert_called_once_with()
assert not pool.is_shutdown
def test_return_defunct_connection_after_endpoint_reassignment_is_ignored(self):
endpoint = DefaultEndPoint('127.0.0.1')
stale_host = Host(endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4())
replacement_host = Host(endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4())
stale_host.signal_connection_failure = Mock(return_value=True)
session = self.make_session()
session.remove_pool.return_value = None
session.cluster.metadata = Metadata()
session.cluster.metadata.add_or_return_host(replacement_host)
session.cluster._endpoints_match.side_effect = Cluster._endpoints_match
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False,
is_closed=False, max_request_id=100,
signaled_error=False,
orphaned_threshold_reached=False)
session.cluster.connection_factory.return_value = conn
pool = self.PoolImpl(stale_host, HostDistance.LOCAL, session)
pool.borrow_connection(timeout=0.01)
conn.is_defunct = True
pool.return_connection(conn)
stale_host.signal_connection_failure.assert_not_called()
session.cluster.on_down.assert_not_called()
session.submit.assert_not_called()
session.remove_pool.assert_called_once_with(
stale_host, expected_host=stale_host,
expected_endpoint=endpoint, expected_pool=pool)
conn.close.assert_called_once_with()
assert not pool.is_shutdown
def test_return_defunct_connection_from_removed_pool_after_endpoint_flip_back_is_ignored(self):
endpoint = DefaultEndPoint('127.0.0.1')
host = Host(endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4())
host.signal_connection_failure = Mock(return_value=True)
session = self.make_session()
session._lock = Lock()
session._pools = {}
session.remove_pool.return_value = None
session.cluster.metadata = Metadata()
session.cluster.metadata.add_or_return_host(host)
session.cluster._endpoints_match.side_effect = Cluster._endpoints_match
conn = HashableMock(spec=Connection, in_flight=0, is_defunct=False,
is_closed=False, max_request_id=100,
signaled_error=False,
orphaned_threshold_reached=False)
session.cluster.connection_factory.return_value = conn
stale_pool = self.PoolImpl(host, HostDistance.LOCAL, session)
stale_pool.borrow_connection(timeout=0.01)
# The old pool was already removed, then the host endpoint flipped back
# to the same endpoint and a replacement pool became current.
current_pool = Mock()
current_pool.host = host
current_pool.endpoint = endpoint
session._pools[host] = current_pool
conn.is_defunct = True
stale_pool.return_connection(conn)
host.signal_connection_failure.assert_not_called()
session.cluster.on_down.assert_not_called()
session.remove_pool.assert_called_once_with(
host, expected_host=host, expected_endpoint=endpoint,
expected_pool=stale_pool)
conn.close.assert_called_once_with()
assert not stale_pool.is_shutdown
def test_host_instantiations(self):
"""
Ensure Host fails if not initialized properly
"""
with pytest.raises(ValueError):
Host(None, None, host_id=uuid.uuid4())
with pytest.raises(ValueError):
Host('127.0.0.1', None, host_id=uuid.uuid4())
with pytest.raises(ValueError):
Host(None, SimpleConvictionPolicy, host_id=uuid.uuid4())
def test_host_equality(self):
"""
Test host equality has correct logic
"""
a = Host('127.0.0.1', SimpleConvictionPolicy, host_id=uuid.uuid4())
b = Host('127.0.0.1', SimpleConvictionPolicy, host_id=uuid.uuid4())
c = Host('127.0.0.2', SimpleConvictionPolicy, host_id=uuid.uuid4())
assert a == b, 'Two Host instances should be equal when sharing.'
assert a != c, 'Two Host instances should NOT be equal when using two different addresses.'
assert b != c, 'Two Host instances should NOT be equal when using two different addresses.'
class HostConnectionTests(_PoolTests):
__test__ = True
PoolImpl = HostConnection
uses_single_connection = True
def test_fast_shutdown(self):
class MockSession(MagicMock):
is_shutdown = False
keyspace = "reprospace"
def __init__(self, *args, **kwargs):
super(MockSession, self).__init__(*args, **kwargs)
self.cluster = MagicMock()
self.connection_created = Event()
self.cluster.executor = ThreadPoolExecutor(max_workers=2)
self.cluster.signal_connection_failure = lambda *args, **kwargs: False
self.cluster.connection_factory = self.mock_connection_factory
self.connection_counter = 0
def submit(self, fn, *args, **kwargs):
LOGGER.info("Scheduling %s with args: %s, kwargs: %s", fn, args, kwargs)
if not self.is_shutdown:
return self.cluster.executor.submit(fn, *args, **kwargs)
def mock_connection_factory(self, *args, **kwargs):
connection = HashableMock()
connection.is_shutdown = False
connection.is_defunct = False
connection.is_closed = False
connection.features = ProtocolFeatures(shard_id=self.connection_counter,
sharding_info=_ShardingInfo(shard_id=1, shards_count=14,
partitioner="", sharding_algorithm="", sharding_ignore_msb=0,
shard_aware_port="", shard_aware_port_ssl=""))
self.connection_counter += 1
self.connection_created.set()
return connection
for attempt_num in range(3):
LOGGER.info("Testing fast shutdown %d / 3 times", attempt_num + 1)
host = MagicMock()
host.endpoint = "1.2.3.4"
session = MockSession()
pool = HostConnection(host=host, host_distance=HostDistance.REMOTE, session=session)
LOGGER.info("Initialized pool %s", pool)
# Wait for initial connection to be created (with timeout)
if not session.connection_created.wait(timeout=2.0):
pytest.fail("Initial connection failed to be created within 2 seconds")
LOGGER.info("Connections: %s", pool._connections)
# Shutdown the pool
pool.shutdown()
# Verify pool is shut down
assert pool.is_shutdown, "Pool should be marked as shutdown"
# Cleanup executor with proper wait
session.cluster.executor.shutdown(wait=True)
def test_replace_retries_when_replacement_keyspace_set_fails(self):
host = Host(DefaultEndPoint('127.0.0.1'), SimpleConvictionPolicy,
host_id=uuid.uuid4())
session = NonCallableMagicMock(spec=Session, keyspace='ks')
session.cluster = MagicMock()
session.cluster.shard_aware_options = ShardAwareOptions()
session.cluster._endpoints_match.side_effect = Cluster._endpoints_match
initial_connection = HashableMock(
spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False,
features=ProtocolFeatures(shard_id=0))
replacement_connection = HashableMock(
spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False,
features=ProtocolFeatures(shard_id=0))
replacement_connection.set_keyspace_blocking.side_effect = RuntimeError(
"keyspace failed")
session.cluster.connection_factory.side_effect = [
initial_connection, replacement_connection]
pool = HostConnection(host, HostDistance.LOCAL, session)
pool._is_replacing = True
pool._replace(initial_connection)
assert session.submit.call_count == 1
submitted_fn, submitted_connection = session.submit.call_args.args
assert submitted_fn == pool._replace
assert submitted_connection is initial_connection
def test_replace_discards_replacement_when_endpoint_changes_during_keyspace_set(self):
old_endpoint = DefaultEndPoint('127.0.0.1')
new_endpoint = DefaultEndPoint('127.0.0.2')
host = Host(old_endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4())
session = NonCallableMagicMock(spec=Session, keyspace='ks')
session.cluster = MagicMock()
session.cluster.shard_aware_options = ShardAwareOptions()
session.cluster._endpoints_match.side_effect = Cluster._endpoints_match
session.remove_pool.return_value = None
initial_connection = HashableMock(
spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False,
features=ProtocolFeatures(shard_id=0))
replacement_connection = HashableMock(
spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False,
features=ProtocolFeatures(shard_id=0))
replacement_connection.set_keyspace_blocking.side_effect = (
lambda keyspace: setattr(host, 'endpoint', new_endpoint))
session.cluster.connection_factory.side_effect = [
initial_connection, replacement_connection]
pool = HostConnection(host, HostDistance.LOCAL, session)
pool._is_replacing = True
pool._replace(initial_connection)
replacement_connection.close.assert_called_once_with()
session.remove_pool.assert_called_once_with(
host, expected_host=host, expected_endpoint=old_endpoint,
expected_pool=pool)
assert pool._connections == {}
assert not pool._is_replacing
def test_missing_shard_discards_connection_when_endpoint_changes_during_keyspace_set(self):
old_endpoint = DefaultEndPoint('127.0.0.1')
new_endpoint = DefaultEndPoint('127.0.0.2')
host = Host(old_endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4())
host.sharding_info = _ShardingInfo(
shard_id=0, shards_count=1, partitioner='',
sharding_algorithm='', sharding_ignore_msb=0,
shard_aware_port='', shard_aware_port_ssl='')
session = NonCallableMagicMock(spec=Session, keyspace='ks')
session.cluster = MagicMock()
session.cluster.shard_aware_options = ShardAwareOptions()
session.cluster.ssl_options = None
session.cluster._endpoints_match.side_effect = Cluster._endpoints_match
session.remove_pool.return_value = None
connection = HashableMock(
spec=Connection, in_flight=0, is_defunct=False, is_closed=False,
max_request_id=100, signaled_error=False,
orphaned_threshold_reached=False,
features=ProtocolFeatures(shard_id=0))
connection.set_keyspace_blocking.side_effect = (
lambda keyspace: setattr(host, 'endpoint', new_endpoint))
session.cluster.connection_factory.return_value = connection
pool = HostConnection.__new__(HostConnection)
pool.host = host
pool.endpoint = old_endpoint
pool.host_distance = HostDistance.LOCAL
pool.is_shutdown = False
pool._session = session
pool._lock = Lock()
pool._stream_available_condition = Condition(Lock())
pool._connections = {}
pool._pending_connections = []
pool._connecting = {0}
pool._excess_connections = set()
pool._trash = set()
pool._shard_connections_futures = []
pool._keyspace = 'ks'
pool.advanced_shardaware_block_until = 0
pool.tablets_routing_v1 = False
pool._open_connection_to_missing_shard(0)
connection.close.assert_called_once_with()
session.remove_pool.assert_called_once_with(
host, expected_host=host, expected_endpoint=old_endpoint,
expected_pool=pool)
assert pool._connections == {}
assert pool._connecting == set()