forked from pcmanus/cassandra-dtest-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_test.py
207 lines (189 loc) · 9.72 KB
/
batch_test.py
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
import time
from assertions import assert_invalid, assert_unavailable
from cql.cassandra.ttypes import Compression, ConsistencyLevel, TimedOutException
from dtest import Tester
cql_version="3.0.0"
class TestBatch(Tester):
def counter_batch_accepts_counter_mutations_test(self):
""" Test that counter batch accepts counter mutations """
cursor = self.prepare()
cursor.execute("""
BEGIN COUNTER BATCH
UPDATE clicks SET total = total + 1 WHERE userid = 1 and url = 'http://foo.com'
UPDATE clicks SET total = total + 1 WHERE userid = 1 and url = 'http://bar.com'
UPDATE clicks SET total = total + 1 WHERE userid = 2 and url = 'http://baz.com'
APPLY BATCH
""")
cursor.execute("SELECT total FROM clicks")
res = cursor.fetchall()
assert res == [[1], [1], [1]], res
def counter_batch_rejects_regular_mutations_test(self):
""" Test that counter batch rejects non-counter mutations """
cursor = self.prepare()
assert_invalid(cursor, """
BEGIN COUNTER BATCH
UPDATE clicks SET total = total + 1 WHERE userid = 1 and url = 'http://foo.com'
UPDATE clicks SET total = total + 1 WHERE userid = 1 and url = 'http://bar.com'
UPDATE clicks SET total = total + 1 WHERE userid = 2 and url = 'http://baz.com'
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
APPLY BATCH
""", matching="Only counter mutations are allowed in COUNTER batches")
def logged_batch_accepts_regular_mutations_test(self):
""" Test that logged batch accepts regular mutations """
cursor = self.prepare()
cursor.execute("""
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""")
cursor.execute("SELECT * FROM users")
res = sorted(cursor.fetchall())
assert res == [[0, u'Jack', u'Sparrow'], [1, u'Will', u'Turner']], res
def logged_batch_rejects_counter_mutations_test(self):
""" Test that logged batch rejects counter mutations """
cursor = self.prepare()
assert_invalid(cursor, """
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
UPDATE clicks SET total = total + 1 WHERE userid = 1 and url = 'http://foo.com'
APPLY BATCH
""", matching="Counter mutations are only allowed in COUNTER batches")
def unlogged_batch_accepts_regular_mutations_test(self):
""" Test that unlogged batch accepts regular mutations """
cursor = self.prepare()
cursor.execute("""
BEGIN UNLOGGED BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (2, 'Elizabeth', 'Swann')
APPLY BATCH
""")
cursor.execute("SELECT * FROM users")
res = sorted(cursor.fetchall())
assert res == [[0, u'Jack', u'Sparrow'], [2, u'Elizabeth', u'Swann']], res
def unlogged_batch_rejects_counter_mutations_test(self):
""" Test that unlogged batch rejects counter mutations """
cursor = self.prepare()
assert_invalid(cursor, """
BEGIN UNLOGGED BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (2, 'Elizabeth', 'Swann')
UPDATE clicks SET total = total + 1 WHERE userid = 1 AND url = 'http://foo.com'
APPLY BATCH
""", matching="Counter mutations are only allowed in COUNTER batches")
def logged_batch_throws_uae_test(self):
""" Test that logged batch throws UAE if there aren't enough live nodes """
cursor = self.prepare(nodes=3)
[ node.stop(wait_other_notice=True) for node in self.cluster.nodelist()[1:] ]
cursor.consistency_level = 'ONE'
assert_unavailable(cursor.execute, """
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""")
def logged_batch_doesnt_throw_uae_test(self):
""" Test that logged batch DOES NOT throw UAE if there are at least 2 live nodes """
cursor = self.prepare(nodes=3)
self.cluster.nodelist()[-1].stop(wait_other_notice=True)
cursor.execute("""
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""", consistency_level="ANY")
assert True
def aknowledged_by_batchlog_not_set_when_batchlog_write_fails_test(self):
""" Test that acknowledged_by_batchlog is False if batchlog can't be written """
cursor = self.prepare(nodes=3)
# kill 2 of the 3 nodes (all the batchlog write candidates).
[ node.stop(gently=False) for node in self.cluster.nodelist()[1:] ]
self.assert_timedout(cursor, """
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""", ConsistencyLevel.ONE, acknowledged_by_batchlog=False)
def aknowledged_by_batchlog_set_when_batchlog_write_succeeds_test(self):
""" Test that acknowledged_by_batchlog is True if batchlog can be written """
cursor = self.prepare(nodes=3)
# kill one of the nodes so that batchlog will be written, but the write will fail.
self.cluster.nodelist()[-1].stop(gently=False)
self.assert_timedout(cursor, """
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""", ConsistencyLevel.THREE, acknowledged_by_batchlog=True)
def batch_uses_proper_timestamp_test(self):
""" Test that each statement will be executed with provided BATCH timestamp """
cursor = self.prepare()
cursor.execute("""
BEGIN BATCH USING TIMESTAMP 1111111111111111
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow')
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""")
cursor.execute("SELECT id, writetime(firstname), writetime(lastname) FROM users")
res = sorted(cursor.fetchall())
assert res == [[0, 1111111111111111, 1111111111111111], [1, 1111111111111111, 1111111111111111]], res
def only_one_timestamp_is_valid_test(self):
""" Test that TIMESTAMP must not be used in the statements within the batch. """
cursor = self.prepare()
assert_invalid(cursor, """
BEGIN BATCH USING TIMESTAMP 1111111111111111
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow') USING TIMESTAMP 2
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner')
APPLY BATCH
""", matching="Timestamp must be set either on BATCH or individual statements")
def each_statement_in_batch_uses_proper_timestamp_test(self):
""" Test that each statement will be executed with its own timestamp """
cursor = self.prepare()
cursor.execute("""
BEGIN BATCH
INSERT INTO users (id, firstname, lastname) VALUES (0, 'Jack', 'Sparrow') USING TIMESTAMP 1111111111111111
INSERT INTO users (id, firstname, lastname) VALUES (1, 'Will', 'Turner') USING TIMESTAMP 1111111111111112
APPLY BATCH
""")
cursor.execute("SELECT id, writetime(firstname), writetime(lastname) FROM users")
res = sorted(cursor.fetchall())
assert res == [[0, 1111111111111111, 1111111111111111], [1, 1111111111111112, 1111111111111112]], res
def assert_timedout(self, cursor, query, cl, acknowledged_by=None,
acknowledged_by_batchlog=None):
client = cursor._connection.client
try:
client.execute_cql3_query(query, Compression.NONE, cl)
except TimedOutException as e:
if not acknowledged_by_batchlog is None:
msg = "Expecting acknowledged_by_batchlog to be %s, got: %s" % (
acknowledged_by_batchlog, e.acknowledged_by_batchlog,)
assert e.acknowledged_by_batchlog == acknowledged_by_batchlog, msg
except Exception as e:
assert False, "Expecting TimedOutException, got:" + str(e)
else:
assert False, "Expecting TimedOutException but no exception was raised"
def prepare(self, nodes=1):
self.cluster.populate(nodes).start()
time.sleep(.5)
node1 = self.cluster.nodelist()[0]
cursor = self.cql_connection(node1, version=cql_version).cursor()
self.create_ks(cursor, 'ks', nodes)
cursor.execute("""
CREATE TABLE clicks (
userid int,
url text,
total counter,
PRIMARY KEY (userid, url)
);
""")
cursor.execute("""
CREATE TABLE users (
id int,
firstname text,
lastname text,
PRIMARY KEY (id)
);
""")
time.sleep(.5)
return cursor