forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_consistency.py
More file actions
308 lines (257 loc) · 12.7 KB
/
test_consistency.py
File metadata and controls
308 lines (257 loc) · 12.7 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
# Copyright 2013-2016 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.
import struct, time, traceback, sys, logging
from cassandra import ConsistencyLevel, OperationTimedOut, ReadTimeout, WriteTimeout, Unavailable
from cassandra.cluster import Cluster
from cassandra.policies import TokenAwarePolicy, RoundRobinPolicy, DowngradingConsistencyRetryPolicy
from cassandra.query import SimpleStatement
from tests.integration import use_singledc, PROTOCOL_VERSION, execute_until_pass
from tests.integration.long.utils import (force_stop, create_schema, wait_for_down, wait_for_up,
start, CoordinatorStats)
try:
import unittest2 as unittest
except ImportError:
import unittest # noqa
ALL_CONSISTENCY_LEVELS = set([
ConsistencyLevel.ANY, ConsistencyLevel.ONE, ConsistencyLevel.TWO,
ConsistencyLevel.QUORUM, ConsistencyLevel.THREE,
ConsistencyLevel.ALL, ConsistencyLevel.LOCAL_QUORUM,
ConsistencyLevel.EACH_QUORUM])
MULTI_DC_CONSISTENCY_LEVELS = set([
ConsistencyLevel.LOCAL_QUORUM, ConsistencyLevel.EACH_QUORUM])
SINGLE_DC_CONSISTENCY_LEVELS = ALL_CONSISTENCY_LEVELS - MULTI_DC_CONSISTENCY_LEVELS
log = logging.getLogger(__name__)
def setup_module():
use_singledc()
class ConsistencyTests(unittest.TestCase):
def setUp(self):
self.coordinator_stats = CoordinatorStats()
def _cl_failure(self, consistency_level, e):
self.fail('Instead of success, saw %s for CL.%s:\n\n%s' % (
e, ConsistencyLevel.value_to_name[consistency_level],
traceback.format_exc()))
def _cl_expected_failure(self, cl):
self.fail('Test passed at ConsistencyLevel.%s:\n\n%s' % (
ConsistencyLevel.value_to_name[cl], traceback.format_exc()))
def _insert(self, session, keyspace, count, consistency_level=ConsistencyLevel.ONE):
session.execute('USE %s' % keyspace)
for i in range(count):
ss = SimpleStatement('INSERT INTO cf(k, i) VALUES (0, 0)',
consistency_level=consistency_level)
execute_until_pass(session, ss)
def _query(self, session, keyspace, count, consistency_level=ConsistencyLevel.ONE):
routing_key = struct.pack('>i', 0)
for i in range(count):
ss = SimpleStatement('SELECT * FROM cf WHERE k = 0',
consistency_level=consistency_level,
routing_key=routing_key)
tries = 0
while True:
if tries > 100:
raise RuntimeError("Failed to execute query after 100 attempts: {0}".format(ss))
try:
self.coordinator_stats.add_coordinator(session.execute_async(ss))
break
except (OperationTimedOut, ReadTimeout):
ex_type, ex, tb = sys.exc_info()
log.warn("{0}: {1} Backtrace: {2}".format(ex_type.__name__, ex, traceback.extract_tb(tb)))
del tb
tries += 1
time.sleep(1)
def _assert_writes_succeed(self, session, keyspace, consistency_levels):
for cl in consistency_levels:
self.coordinator_stats.reset_counts()
try:
self._insert(session, keyspace, 1, cl)
except Exception as e:
self._cl_failure(cl, e)
def _assert_reads_succeed(self, session, keyspace, consistency_levels, expected_reader=3):
for cl in consistency_levels:
self.coordinator_stats.reset_counts()
try:
self._query(session, keyspace, 1, cl)
for i in range(3):
if i == expected_reader:
self.coordinator_stats.assert_query_count_equals(self, i, 1)
else:
self.coordinator_stats.assert_query_count_equals(self, i, 0)
except Exception as e:
self._cl_failure(cl, e)
def _assert_writes_fail(self, session, keyspace, consistency_levels):
for cl in consistency_levels:
self.coordinator_stats.reset_counts()
try:
self._insert(session, keyspace, 1, cl)
self._cl_expected_failure(cl)
except (Unavailable, WriteTimeout):
pass
def _assert_reads_fail(self, session, keyspace, consistency_levels):
for cl in consistency_levels:
self.coordinator_stats.reset_counts()
try:
self._query(session, keyspace, 1, cl)
self._cl_expected_failure(cl)
except (Unavailable, ReadTimeout):
pass
def _test_tokenaware_one_node_down(self, keyspace, rf, accepted):
cluster = Cluster(
load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()),
protocol_version=PROTOCOL_VERSION)
session = cluster.connect()
wait_for_up(cluster, 1, wait=False)
wait_for_up(cluster, 2)
create_schema(cluster, session, keyspace, replication_factor=rf)
self._insert(session, keyspace, count=1)
self._query(session, keyspace, count=1)
self.coordinator_stats.assert_query_count_equals(self, 1, 0)
self.coordinator_stats.assert_query_count_equals(self, 2, 1)
self.coordinator_stats.assert_query_count_equals(self, 3, 0)
try:
force_stop(2)
wait_for_down(cluster, 2)
self._assert_writes_succeed(session, keyspace, accepted)
self._assert_reads_succeed(session, keyspace,
accepted - set([ConsistencyLevel.ANY]))
self._assert_writes_fail(session, keyspace,
SINGLE_DC_CONSISTENCY_LEVELS - accepted)
self._assert_reads_fail(session, keyspace,
SINGLE_DC_CONSISTENCY_LEVELS - accepted)
finally:
start(2)
wait_for_up(cluster, 2)
cluster.shutdown()
def test_rfone_tokenaware_one_node_down(self):
self._test_tokenaware_one_node_down(
keyspace='test_rfone_tokenaware',
rf=1,
accepted=set([ConsistencyLevel.ANY]))
def test_rftwo_tokenaware_one_node_down(self):
self._test_tokenaware_one_node_down(
keyspace='test_rftwo_tokenaware',
rf=2,
accepted=set([ConsistencyLevel.ANY, ConsistencyLevel.ONE]))
def test_rfthree_tokenaware_one_node_down(self):
self._test_tokenaware_one_node_down(
keyspace='test_rfthree_tokenaware',
rf=3,
accepted=set([ConsistencyLevel.ANY, ConsistencyLevel.ONE,
ConsistencyLevel.TWO, ConsistencyLevel.QUORUM]))
def test_rfthree_tokenaware_none_down(self):
keyspace = 'test_rfthree_tokenaware_none_down'
cluster = Cluster(
load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()),
protocol_version=PROTOCOL_VERSION)
session = cluster.connect()
wait_for_up(cluster, 1, wait=False)
wait_for_up(cluster, 2)
create_schema(cluster, session, keyspace, replication_factor=3)
self._insert(session, keyspace, count=1)
self._query(session, keyspace, count=1)
self.coordinator_stats.assert_query_count_equals(self, 1, 0)
self.coordinator_stats.assert_query_count_equals(self, 2, 1)
self.coordinator_stats.assert_query_count_equals(self, 3, 0)
self.coordinator_stats.reset_counts()
self._assert_writes_succeed(session, keyspace, SINGLE_DC_CONSISTENCY_LEVELS)
self._assert_reads_succeed(session, keyspace,
SINGLE_DC_CONSISTENCY_LEVELS - set([ConsistencyLevel.ANY]),
expected_reader=2)
cluster.shutdown()
def _test_downgrading_cl(self, keyspace, rf, accepted):
cluster = Cluster(
load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()),
default_retry_policy=DowngradingConsistencyRetryPolicy(),
protocol_version=PROTOCOL_VERSION)
session = cluster.connect()
create_schema(cluster, session, keyspace, replication_factor=rf)
self._insert(session, keyspace, 1)
self._query(session, keyspace, 1)
self.coordinator_stats.assert_query_count_equals(self, 1, 0)
self.coordinator_stats.assert_query_count_equals(self, 2, 1)
self.coordinator_stats.assert_query_count_equals(self, 3, 0)
try:
force_stop(2)
wait_for_down(cluster, 2)
self._assert_writes_succeed(session, keyspace, accepted)
self._assert_reads_succeed(session, keyspace,
accepted - set([ConsistencyLevel.ANY]))
self._assert_writes_fail(session, keyspace,
SINGLE_DC_CONSISTENCY_LEVELS - accepted)
self._assert_reads_fail(session, keyspace,
SINGLE_DC_CONSISTENCY_LEVELS - accepted)
finally:
start(2)
wait_for_up(cluster, 2)
cluster.shutdown()
def test_rfone_downgradingcl(self):
self._test_downgrading_cl(
keyspace='test_rfone_downgradingcl',
rf=1,
accepted=set([ConsistencyLevel.ANY]))
def test_rftwo_downgradingcl(self):
self._test_downgrading_cl(
keyspace='test_rftwo_downgradingcl',
rf=2,
accepted=SINGLE_DC_CONSISTENCY_LEVELS)
def test_rfthree_roundrobin_downgradingcl(self):
keyspace = 'test_rfthree_roundrobin_downgradingcl'
cluster = Cluster(
load_balancing_policy=RoundRobinPolicy(),
default_retry_policy=DowngradingConsistencyRetryPolicy(),
protocol_version=PROTOCOL_VERSION)
self.rfthree_downgradingcl(cluster, keyspace, True)
def test_rfthree_tokenaware_downgradingcl(self):
keyspace = 'test_rfthree_tokenaware_downgradingcl'
cluster = Cluster(
load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()),
default_retry_policy=DowngradingConsistencyRetryPolicy(),
protocol_version=PROTOCOL_VERSION)
self.rfthree_downgradingcl(cluster, keyspace, False)
def rfthree_downgradingcl(self, cluster, keyspace, roundrobin):
session = cluster.connect()
create_schema(cluster, session, keyspace, replication_factor=2)
self._insert(session, keyspace, count=12)
self._query(session, keyspace, count=12)
if roundrobin:
self.coordinator_stats.assert_query_count_equals(self, 1, 4)
self.coordinator_stats.assert_query_count_equals(self, 2, 4)
self.coordinator_stats.assert_query_count_equals(self, 3, 4)
else:
self.coordinator_stats.assert_query_count_equals(self, 1, 0)
self.coordinator_stats.assert_query_count_equals(self, 2, 12)
self.coordinator_stats.assert_query_count_equals(self, 3, 0)
try:
self.coordinator_stats.reset_counts()
force_stop(2)
wait_for_down(cluster, 2)
self._assert_writes_succeed(session, keyspace, SINGLE_DC_CONSISTENCY_LEVELS)
# Test reads that expected to complete successfully
for cl in SINGLE_DC_CONSISTENCY_LEVELS - set([ConsistencyLevel.ANY]):
self.coordinator_stats.reset_counts()
self._query(session, keyspace, 12, consistency_level=cl)
if roundrobin:
self.coordinator_stats.assert_query_count_equals(self, 1, 6)
self.coordinator_stats.assert_query_count_equals(self, 2, 0)
self.coordinator_stats.assert_query_count_equals(self, 3, 6)
else:
self.coordinator_stats.assert_query_count_equals(self, 1, 0)
self.coordinator_stats.assert_query_count_equals(self, 2, 0)
self.coordinator_stats.assert_query_count_equals(self, 3, 12)
finally:
start(2)
wait_for_up(cluster, 2)
session.cluster.shutdown()
# TODO: can't be done in this class since we reuse the ccm cluster
# instead we should create these elsewhere
# def test_rfthree_downgradingcl_twodcs(self):
# def test_rfthree_downgradingcl_twodcs_dcaware(self):