Elffuss BraidHub

kikocis/java-driver public

Merge pull request #816 from datastax/consistency_test_rewrite

Updating ConsistencyTest to use scassandra.
Changeset5c67c21338806ec75aabcf29b6a60d1066b52593f3863ecb0ebd9e017aab1c65
AuthorGreg Bestland <Greg.Bestland@datastax.com> · 2017-02-27 17:39 -06:00
CommitterGitHub <noreply@github.com> · 2017-02-27 17:39 -06:00
Parents73adafb103cb, 407469973ff4

Browse the files at this changeset

2 files changed

modified driver-core/src/test/java/com/datastax/driver/core/ConsistencyTest.java +285 −676
@@ -15,716 +15,325 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.datastax.driver.core.exceptions.InvalidQueryException;
19-import com.datastax.driver.core.exceptions.ReadTimeoutException;
20-import com.datastax.driver.core.exceptions.UnavailableException;
21-import com.datastax.driver.core.exceptions.WriteTimeoutException;
22-import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
23-import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy;
24-import com.datastax.driver.core.policies.RoundRobinPolicy;
25-import com.datastax.driver.core.policies.TokenAwarePolicy;
26-import com.google.common.util.concurrent.Uninterruptibles;
18+import org.scassandra.Scassandra;
19+import org.scassandra.http.client.BatchExecution;
20+import org.scassandra.http.client.PreparedStatementExecution;
21+import org.scassandra.http.client.Query;
2722 import org.slf4j.Logger;
2823 import org.slf4j.LoggerFactory;
24+import org.testng.annotations.AfterClass;
25+import org.testng.annotations.AfterMethod;
26+import org.testng.annotations.BeforeClass;
2927 import org.testng.annotations.Test;
3028
31-import java.util.Arrays;
32-import java.util.EnumSet;
3329 import java.util.List;
34-import java.util.Set;
35-import java.util.concurrent.TimeUnit;
36-
37-import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
38-import static com.datastax.driver.core.TestUtils.IP_PREFIX;
39-import static com.datastax.driver.core.TestUtils.ipOfNode;
40-import static java.util.Collections.emptyList;
41-import static java.util.Collections.singletonList;
42-import static org.testng.Assert.assertTrue;
43-import static org.testng.Assert.fail;
44-
45-@SuppressWarnings("unused")
46-@CreateCCM(PER_METHOD)
47-@CCMConfig(
48- dirtiesContext = true,
49- createKeyspace = false,
50- config = {
51- // tests fail often with write or read timeouts
52- "phi_convict_threshold:5",
53- "read_request_timeout_in_ms:200000",
54- "write_request_timeout_in_ms:200000"
55- }
56-)
57-public class ConsistencyTest extends AbstractPoliciesTest {
58-
59- private static final Logger logger = LoggerFactory.getLogger(ConsistencyTest.class);
60-
61- private Cluster.Builder tokenAwareRoundRobin() {
62- return Cluster.builder()
63- .withSocketOptions(new SocketOptions().setReadTimeoutMillis(300000))
64- .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
65- }
66-
67- private Cluster.Builder tokenAwareRoundRobinNoShuffle() {
68- return Cluster.builder()
69- .withSocketOptions(new SocketOptions().setReadTimeoutMillis(300000))
70- .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy(), false));
71- }
7230
73- private Cluster.Builder tokenAwareRoundRobinDowngrading() {
74- return Cluster.builder()
75- .withSocketOptions(new SocketOptions().setReadTimeoutMillis(300000))
76- .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()))
77- .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
78- }
31+import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
32+import static org.testng.Assert.*;
7933
80- private Cluster.Builder tokenAwareRoundRobinNoShuffleDowngrading() {
81- return Cluster.builder()
82- .withSocketOptions(new SocketOptions().setReadTimeoutMillis(300000))
83- .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy(), false))
84- .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
85- }
34+public class ConsistencyTest {
8635
87- private Cluster.Builder roundRobinDowngrading() {
88- return Cluster.builder()
89- .withSocketOptions(new SocketOptions().setReadTimeoutMillis(300000))
90- .withLoadBalancingPolicy(new RoundRobinPolicy())
91- .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
92- }
36+ private static final Logger logger = LoggerFactory.getLogger(ConsistencyTest.class);
37+ private ScassandraCluster sCluster;
9338
94- private Cluster.Builder tokenAwareDCAwareRoundRobinNoShuffleDowngrading() {
95- return Cluster.builder()
96- .withSocketOptions(new SocketOptions().setReadTimeoutMillis(300000))
97- .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("dc2").build(), false))
98- .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
39+ @BeforeClass(groups = "short")
40+ public void setUp() {
41+ sCluster = ScassandraCluster.builder().withNodes(1).build();
42+ sCluster.init();
9943 }
10044
101- @Test(groups = "long")
102- @CCMConfig(
103- numberOfNodes = 3,
104- clusterProvider = "tokenAwareRoundRobin")
105- public void testRFOneTokenAware() throws Throwable {
106-
107- createSchema(1);
108- init(12, ConsistencyLevel.ONE);
109- query(12, ConsistencyLevel.ONE);
110-
111- assertQueried(IP_PREFIX + '1', 0);
112- assertQueried(IP_PREFIX + '2', 12);
113- assertQueried(IP_PREFIX + '3', 0);
114-
115- resetCoordinators();
116- stopAndWait(2);
117-
118- List<ConsistencyLevel> acceptedList = singletonList(ConsistencyLevel.ANY);
119-
120- List<ConsistencyLevel> failList = Arrays.asList(
121- ConsistencyLevel.ONE,
122- ConsistencyLevel.TWO,
123- ConsistencyLevel.THREE,
124- ConsistencyLevel.QUORUM,
125- ConsistencyLevel.ALL,
126- ConsistencyLevel.LOCAL_QUORUM,
127- ConsistencyLevel.EACH_QUORUM);
128-
129- // Test successful writes
130- for (ConsistencyLevel cl : acceptedList) {
131- try {
132- init(12, cl);
133- } catch (Exception e) {
134- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
135- }
136- }
137-
138- // Test successful reads
139- for (ConsistencyLevel cl : acceptedList) {
140- try {
141- query(12, cl);
142- } catch (InvalidQueryException e) {
143- List<String> acceptableErrorMessages = singletonList(
144- "ANY ConsistencyLevel is only supported for writes");
145- assertTrue(acceptableErrorMessages.contains(e.getMessage()));
146- }
147- }
148-
149- // Test writes which should fail
150- for (ConsistencyLevel cl : failList) {
151- try {
152- init(12, cl);
153- fail(String.format("Test passed at CL.%s.", cl));
154- } catch (InvalidQueryException e) {
155- List<String> acceptableErrorMessages = Arrays.asList(
156- "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)",
157- "consistency level EACH_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)");
158- assertTrue(acceptableErrorMessages.contains(e.getMessage()), String.format("Received: %s", e.getMessage()));
159- } catch (UnavailableException e) {
160- // expected to fail when the client has already marked the
161- // node as DOWN
162- } catch (WriteTimeoutException e) {
163- // expected to fail when the client hasn't marked the
164- // node as DOWN yet
165- }
166- }
167-
168- // Test reads which should fail
169- for (ConsistencyLevel cl : failList) {
170- try {
171- query(12, cl);
172- fail(String.format("Test passed at CL.%s.", cl));
173- } catch (InvalidQueryException e) {
174- List<String> acceptableErrorMessages = Arrays.asList(
175- "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)",
176- "EACH_QUORUM ConsistencyLevel is only supported for writes");
177- assertTrue(acceptableErrorMessages.contains(e.getMessage()), String.format("Received: %s", e.getMessage()));
178- } catch (ReadTimeoutException e) {
179- // expected to fail when the client hasn't marked the
180- // node as DOWN yet
181- } catch (UnavailableException e) {
182- // expected to fail when the client has already marked the
183- // node as DOWN
184- }
185- }
45+ @AfterClass(groups = "short")
46+ public void tearDownClass() {
47+ sCluster.stop();
18648 }
18749
188- @Test(groups = "long")
189- @CCMConfig(numberOfNodes = 3,
190- clusterProvider = "tokenAwareRoundRobinNoShuffle")
191- public void testRFTwoTokenAware() throws Throwable {
192- createSchema(2);
193- init(12, ConsistencyLevel.TWO);
194- query(12, ConsistencyLevel.TWO);
195-
196- assertQueried(IP_PREFIX + '1', 0);
197- assertQueried(IP_PREFIX + '2', 12);
198- assertQueried(IP_PREFIX + '3', 0);
199-
200- resetCoordinators();
201- stopAndWait(2);
202-
203- List<ConsistencyLevel> acceptedList = Arrays.asList(
204- ConsistencyLevel.ANY,
205- ConsistencyLevel.ONE
206- );
207-
208- List<ConsistencyLevel> failList = Arrays.asList(
209- ConsistencyLevel.TWO,
210- ConsistencyLevel.QUORUM,
211- ConsistencyLevel.THREE,
212- ConsistencyLevel.ALL,
213- ConsistencyLevel.LOCAL_QUORUM,
214- ConsistencyLevel.EACH_QUORUM);
215-
216- // Test successful writes
217- for (ConsistencyLevel cl : acceptedList) {
218- try {
219- init(12, cl);
220- } catch (Exception e) {
221- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
222- }
223- }
224-
225- // Test successful reads
226- for (ConsistencyLevel cl : acceptedList) {
227- try {
228- query(12, cl);
229- } catch (InvalidQueryException e) {
230- List<String> acceptableErrorMessages = singletonList(
231- "ANY ConsistencyLevel is only supported for writes");
232- assertTrue(acceptableErrorMessages.contains(e.getMessage()));
233- }
234- }
235-
236- // Test writes which should fail
237- for (ConsistencyLevel cl : failList) {
238- try {
239- init(12, cl);
240- fail(String.format("Test passed at CL.%s.", cl));
241- } catch (InvalidQueryException e) {
242- List<String> acceptableErrorMessages = Arrays.asList(
243- "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)",
244- "consistency level EACH_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)");
245- assertTrue(acceptableErrorMessages.contains(e.getMessage()), String.format("Received: %s", e.getMessage()));
246- } catch (UnavailableException e) {
247- // expected to fail when the client has already marked the
248- // node as DOWN
249- } catch (WriteTimeoutException e) {
250- // expected to fail when the client hasn't marked the
251- // node as DOWN yet
252- }
253- }
254-
255- // Test reads which should fail
256- for (ConsistencyLevel cl : failList) {
257- try {
258- query(12, cl);
259- fail(String.format("Test passed at CL.%s.", cl));
260- } catch (InvalidQueryException e) {
261- List<String> acceptableErrorMessages = Arrays.asList(
262- "consistency level LOCAL_QUORUM not compatible with replication strategy (org.apache.cassandra.locator.SimpleStrategy)",
263- "EACH_QUORUM ConsistencyLevel is only supported for writes");
264- assertTrue(acceptableErrorMessages.contains(e.getMessage()), String.format("Received: %s", e.getMessage()));
265- } catch (ReadTimeoutException e) {
266- // expected to fail when the client hasn't marked the
267- // node as DOWN yet
268- } catch (UnavailableException e) {
269- // expected to fail when the client has already marked the
270- // node as DOWN
271- }
272- }
50+ @AfterMethod(groups = "short")
51+ public void tearDown() {
52+ clearActivityLog();
27353 }
27454
275- @Test(groups = "long")
276- @CCMConfig(numberOfNodes = 3,
277- clusterProvider = "tokenAwareRoundRobinNoShuffle")
278- public void testRFThreeTokenAware() throws Throwable {
279- createSchema(3);
280- init(12, ConsistencyLevel.TWO);
281- query(12, ConsistencyLevel.TWO);
282-
283- assertQueried(IP_PREFIX + '1', 0);
284- assertQueried(IP_PREFIX + '2', 12);
285- assertQueried(IP_PREFIX + '3', 0);
286-
287- resetCoordinators();
288- stopAndWait(2);
289-
290- Set<ConsistencyLevel> cls = EnumSet.allOf(ConsistencyLevel.class);
291- // Remove serial consistencies as they require conditional read/writes
292- cls.remove(ConsistencyLevel.SERIAL);
293- cls.remove(ConsistencyLevel.LOCAL_SERIAL);
294-
295- List<ConsistencyLevel> acceptedList = Arrays.asList(
296- ConsistencyLevel.ANY,
297- ConsistencyLevel.ONE,
298- ConsistencyLevel.TWO,
299- ConsistencyLevel.QUORUM,
300- ConsistencyLevel.LOCAL_QUORUM,
301- ConsistencyLevel.EACH_QUORUM
302- );
303-
304- List<ConsistencyLevel> failList = Arrays.asList(
305- ConsistencyLevel.THREE,
306- ConsistencyLevel.ALL
307- );
308-
309-
310- // Test successful writes
311- for (ConsistencyLevel cl : acceptedList) {
312- try {
313- init(12, cl);
314- } catch (Exception e) {
315- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
316- }
317- }
318-
319- // Test successful reads
320- for (ConsistencyLevel cl : acceptedList) {
321- try {
322- query(12, cl);
323- } catch (InvalidQueryException e) {
324- List<String> acceptableErrorMessages = Arrays.asList(
325- "ANY ConsistencyLevel is only supported for writes",
326- "EACH_QUORUM ConsistencyLevel is only supported for writes"
327- );
328- assertTrue(acceptableErrorMessages.contains(e.getMessage()), "Got unexpected message " + e.getMessage());
329- }
330- }
331-
332- // Test writes which should fail
333- for (ConsistencyLevel cl : failList) {
334- try {
335- init(12, cl);
336- fail(String.format("Test passed at CL.%s.", cl));
337- } catch (UnavailableException e) {
338- // expected to fail when the client has already marked the
339- // node as DOWN
340- } catch (WriteTimeoutException e) {
341- // expected to fail when the client hasn't marked the
342- // node as DOWN yet
343- }
344- }
345-
346- // Test reads which should fail
347- for (ConsistencyLevel cl : failList) {
348- try {
349- query(12, cl);
350- fail(String.format("Test passed at CL.%s.", cl));
351- } catch (ReadTimeoutException e) {
352- // expected to fail when the client hasn't marked the
353- // node as DOWN yet
354- } catch (UnavailableException e) {
355- // expected to fail when the client has already marked the
356- // node as DOWN
357- }
55+ public void clearActivityLog() {
56+ for (Scassandra node : sCluster.nodes()) {
57+ node.activityClient().clearAllRecordedActivity();
35858 }
35959 }
36060
361-
362- @Test(groups = "long")
363- @CCMConfig(numberOfNodes = 3,
364- clusterProvider = "tokenAwareRoundRobinDowngrading")
365- public void testRFOneDowngradingCL() throws Throwable {
366- createSchema(1);
367- init(12, ConsistencyLevel.ONE);
368- query(12, ConsistencyLevel.ONE);
369-
370- assertQueried(IP_PREFIX + '1', 0);
371- assertQueried(IP_PREFIX + '2', 12);
372- assertQueried(IP_PREFIX + '3', 0);
373-
374- resetCoordinators();
375- stopAndWait(2);
376-
377- List<ConsistencyLevel> acceptedList = singletonList(
378- ConsistencyLevel.ANY
379- );
380-
381- List<ConsistencyLevel> failList = Arrays.asList(
382- ConsistencyLevel.ONE,
383- ConsistencyLevel.TWO,
384- ConsistencyLevel.THREE,
385- ConsistencyLevel.QUORUM,
386- ConsistencyLevel.ALL,
387- ConsistencyLevel.LOCAL_QUORUM,
388- ConsistencyLevel.EACH_QUORUM);
389-
390- // Test successful writes
391- for (ConsistencyLevel cl : acceptedList) {
392- try {
393- init(12, cl);
394- } catch (Exception e) {
395- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
396- }
397- }
398-
399- // Test successful reads
400- for (ConsistencyLevel cl : acceptedList) {
401- try {
402- query(12, cl);
403- } catch (InvalidQueryException e) {
404- List<String> acceptableErrorMessages = singletonList(
405- "ANY ConsistencyLevel is only supported for writes");
406- assertTrue(acceptableErrorMessages.contains(e.getMessage()));
407- }
408- }
409-
410- // Test writes which should fail
411- for (ConsistencyLevel cl : failList) {
412- try {
413- init(12, cl);
414- fail(String.format("Test passed at CL.%s.", cl));
415- } catch (UnavailableException e) {
416- // expected to fail when the client has already marked the
417- // node as DOWN
418- } catch (WriteTimeoutException e) {
419- // expected to fail when the client hasn't marked the
420- // node as DOWN yet
421- }
422- }
423-
424- // Test reads which should fail
425- for (ConsistencyLevel cl : failList) {
426- try {
427- query(12, cl);
428- fail(String.format("Test passed at CL.%s.", cl));
429- } catch (InvalidQueryException e) {
430- List<String> acceptableErrorMessages = singletonList(
431- "EACH_QUORUM ConsistencyLevel is only supported for writes"
432- );
433- assertTrue(acceptableErrorMessages.contains(e.getMessage()), "Got unexpected message " + e.getMessage());
434- } catch (ReadTimeoutException e) {
435- // expected to fail when the client hasn't marked the
436- // node as DOWN yet
437- } catch (UnavailableException e) {
438- // expected to fail when the client has already marked the
439- // node as DOWN
440- }
441- }
61+ public Cluster.Builder builder() {
62+ //Note: nonQuietClusterCloseOptions is used to speed up tests
63+ return Cluster.builder()
64+ .addContactPoints(sCluster.address(1).getAddress())
65+ .withPort(sCluster.getBinaryPort()).withNettyOptions(nonQuietClusterCloseOptions);
44266 }
44367
444- @Test(groups = "long")
445- @CCMConfig(numberOfNodes = 3,
446- clusterProvider = "tokenAwareRoundRobinNoShuffleDowngrading")
447- public void testRFTwoDowngradingCL() throws Throwable {
448- createSchema(2);
449- init(12, ConsistencyLevel.TWO);
450- query(12, ConsistencyLevel.TWO);
451-
452- assertQueried(IP_PREFIX + '1', 0);
453- assertQueried(IP_PREFIX + '2', 12);
454- assertQueried(IP_PREFIX + '3', 0);
455-
456- resetCoordinators();
457- stopAndWait(2);
458-
459- Set<ConsistencyLevel> acceptedList = EnumSet.allOf(ConsistencyLevel.class);
460- // Remove serial consistencies as they require conditional read/writes
461- acceptedList.remove(ConsistencyLevel.SERIAL);
462- acceptedList.remove(ConsistencyLevel.LOCAL_SERIAL);
463-
464- // Test successful writes
465- for (ConsistencyLevel cl : acceptedList) {
466- try {
467- init(12, cl);
468- } catch (Exception e) {
469- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
470- }
68+ /**
69+ * This method checks the expected/sent serial consistency level against that which is received.
70+ * ConsistencyLevel.SERIAL is the default serial consistency level, so even when sent it will return
71+ * as null.
72+ */
73+ public void checkSerialCLMatch(ConsistencyLevel expected, String received) {
74+ if (expected.equals(ConsistencyLevel.SERIAL)) {
75+ assertNull(received);
76+ } else {
77+ assertTrue(received.equals(expected.toString()));
47178 }
472-
473- // Test successful reads
474- for (ConsistencyLevel cl : acceptedList) {
475- try {
476- query(12, cl);
477- } catch (InvalidQueryException e) {
478- List<String> acceptableErrorMessages = Arrays.asList(
479- "ANY ConsistencyLevel is only supported for writes",
480- "EACH_QUORUM ConsistencyLevel is only supported for writes"
481- );
482- assertTrue(acceptableErrorMessages.contains(e.getMessage()), "Got unexpected message " + e.getMessage());
483- }
484- }
485- }
486-
487- @Test(groups = "long")
488- @CCMConfig(numberOfNodes = 3,
489- clusterProvider = "roundRobinDowngrading")
490- public void testRFThreeRoundRobinDowngradingCL() throws Throwable {
491- testRFThreeDowngradingCL();
492- }
493-
494- @Test(groups = "long")
495- @CCMConfig(numberOfNodes = 3,
496- clusterProvider = "tokenAwareRoundRobinNoShuffleDowngrading")
497- public void testRFThreeTokenAwareDowngradingCL() throws Throwable {
498- testRFThreeDowngradingCL();
49979 }
50080
501- private void testRFThreeDowngradingCL() throws Throwable {
502-
503- createSchema(3);
504- init(12, ConsistencyLevel.ALL);
505- query(12, ConsistencyLevel.ALL);
506-
507- try {
508- // This test catches TokenAwarePolicy
509- // However, full tests in LoadBalancingPolicyTest.java
510- assertQueried(IP_PREFIX + '1', 0);
511- assertQueried(IP_PREFIX + '2', 12);
512- assertQueried(IP_PREFIX + '3', 0);
513- } catch (AssertionError e) {
514- // This test catches RoundRobinPolicy
515- assertQueried(IP_PREFIX + '1', 4);
516- assertQueried(IP_PREFIX + '2', 4);
517- assertQueried(IP_PREFIX + '3', 4);
518- }
519-
520- resetCoordinators();
521- stopAndWait(2);
522-
523- Set<ConsistencyLevel> acceptedList = EnumSet.allOf(ConsistencyLevel.class);
524- // Remove serial consistencies as they require conditional read/writes
525- acceptedList.remove(ConsistencyLevel.SERIAL);
526- acceptedList.remove(ConsistencyLevel.LOCAL_SERIAL);
527-
528- // Test successful writes
529- for (ConsistencyLevel cl : acceptedList) {
530- try {
531- init(12, cl);
532- } catch (Exception e) {
533- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
534- }
81+ public PreparedStatementExecution executePrepared(Session session, String statement, ConsistencyLevel level, ConsistencyLevel serialLevel) {
82+ PreparedStatement ps = session.prepare(statement);
83+ BoundStatement bound = ps.bind();
84+ if (level != null) {
85+ bound.setConsistencyLevel(level);
53586 }
536-
537- // Test successful reads
538- for (ConsistencyLevel cl : acceptedList) {
539- try {
540- query(12, cl);
541- } catch (InvalidQueryException e) {
542- List<String> acceptableErrorMessages = Arrays.asList(
543- "ANY ConsistencyLevel is only supported for writes",
544- "EACH_QUORUM ConsistencyLevel is only supported for writes"
545- );
546- assertTrue(acceptableErrorMessages.contains(e.getMessage()), "Got unexpected message " + e.getMessage());
547- }
87+ if (serialLevel != null) {
88+ bound.setSerialConsistencyLevel(serialLevel);
54889 }
90+ session.execute(bound);
91+ List<PreparedStatementExecution> pses = sCluster.node(1).activityClient().retrievePreparedStatementExecutions();
92+ PreparedStatementExecution pse = pses.get(0);
93+ assertTrue(pse.getPreparedStatementText().equals(statement));
94+ return pse;
54995 }
55096
551- @Test(groups = "long")
552- @CCMConfig(
553- numberOfNodes = {3, 3},
554- clusterProvider = "tokenAwareRoundRobinNoShuffleDowngrading"
555- )
556- public void testRFThreeDowngradingCLTwoDCs() throws Throwable {
557- createMultiDCSchema(3, 3);
558- init(12, ConsistencyLevel.TWO);
559- query(12, ConsistencyLevel.TWO);
560-
561- assertQueried(IP_PREFIX + '1', 0);
562- assertQueried(IP_PREFIX + '2', 12);
563- assertQueried(IP_PREFIX + '3', 0);
564- assertQueried(IP_PREFIX + '4', 0);
565- assertQueried(IP_PREFIX + '5', 0);
566- assertQueried(IP_PREFIX + '6', 0);
567-
568- resetCoordinators();
569- stopAndWait(3);
570-
571- List<ConsistencyLevel> acceptedList = Arrays.asList(
572- ConsistencyLevel.ANY,
573- ConsistencyLevel.ONE,
574- ConsistencyLevel.TWO,
575- ConsistencyLevel.QUORUM,
576- ConsistencyLevel.THREE,
577- ConsistencyLevel.ALL,
578- ConsistencyLevel.LOCAL_QUORUM,
579- ConsistencyLevel.EACH_QUORUM
580- );
581-
582- List<ConsistencyLevel> failList = emptyList();
583-
584- // Test successful writes
585- for (ConsistencyLevel cl : acceptedList) {
586- logger.debug("Test successful init(): " + cl);
587- try {
588- init(12, cl);
589- } catch (Exception e) {
590- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
591- }
592- }
593-
594- // Test successful reads
595- for (ConsistencyLevel cl : acceptedList) {
596- logger.debug("Test successful query(): " + cl);
597- try {
598- query(12, cl);
599- } catch (InvalidQueryException e) {
600- List<String> acceptableErrorMessages = Arrays.asList(
601- "EACH_QUORUM ConsistencyLevel is only supported for writes",
602- "ANY ConsistencyLevel is only supported for writes");
603- assertTrue(acceptableErrorMessages.contains(e.getMessage()), String.format("Received: %s", e.getMessage()));
604- }
605- }
97+ public BatchExecution executeBatch(Session session, String statement, ConsistencyLevel level, ConsistencyLevel serialLevel) {
98+ BatchStatement batch = new BatchStatement();
99+ batch.add(new SimpleStatement(statement));
606100
607- // Test writes which should fail
608- for (ConsistencyLevel cl : failList) {
609- logger.debug("Test failure init(): " + cl);
610- try {
611- init(12, cl);
612- fail(String.format("Test passed at CL.%s.", cl));
613- } catch (UnavailableException e) {
614- // expected to fail when the client has already marked the
615- // node as DOWN
616- } catch (WriteTimeoutException e) {
617- // expected to fail when the client hasn't marked the
618- // node as DOWN yet
619- }
101+ if (level != null) {
102+ batch.setConsistencyLevel(level);
620103 }
621-
622- // Test reads which should fail
623- for (ConsistencyLevel cl : failList) {
624- logger.debug("Test failure query(): " + cl);
625- try {
626- query(12, cl);
627- fail(String.format("Test passed at CL.%s.", cl));
628- } catch (ReadTimeoutException e) {
629- // expected to fail when the client hasn't marked the
630- // node as DOWN yet
631- } catch (UnavailableException e) {
632- // expected to fail when the client has already marked the
633- // node as DOWN
634- }
104+ if (serialLevel != null) {
105+ batch.setSerialConsistencyLevel(serialLevel);
635106 }
107+ session.execute(batch);
108+ List<BatchExecution> batches = sCluster.node(1).activityClient().retrieveBatches();
109+ assertEquals(batches.size(), 1);
110+ return batches.get(0);
636111 }
637112
638- @Test(groups = "long")
639- @CCMConfig(
640- numberOfNodes = {3, 3},
641- clusterProvider = "tokenAwareDCAwareRoundRobinNoShuffleDowngrading"
642- )
643- public void testRFThreeDowngradingCLTwoDCsDCAware() throws Throwable {
644- createMultiDCSchema(3, 3);
645- init(12, ConsistencyLevel.TWO);
646- query(12, ConsistencyLevel.TWO);
647-
648- assertQueried(IP_PREFIX + '1', 0);
649- assertQueried(IP_PREFIX + '2', 0);
650- assertQueried(IP_PREFIX + '3', 0);
651- assertQueried(IP_PREFIX + '4', 0);
652- assertQueried(IP_PREFIX + '5', 12);
653- assertQueried(IP_PREFIX + '6', 0);
654-
655- resetCoordinators();
656- stopAndWait(2);
657-
658- List<ConsistencyLevel> acceptedList = Arrays.asList(
659- ConsistencyLevel.ANY,
660- ConsistencyLevel.ONE,
661- ConsistencyLevel.TWO,
662- ConsistencyLevel.QUORUM,
663- ConsistencyLevel.THREE,
664- ConsistencyLevel.ALL,
665- ConsistencyLevel.LOCAL_QUORUM,
666- ConsistencyLevel.EACH_QUORUM
667- );
668-
669- List<ConsistencyLevel> failList = emptyList();
670-
671- // Test successful writes
672- for (ConsistencyLevel cl : acceptedList) {
673- try {
674- init(12, cl);
675- } catch (Exception e) {
676- fail(String.format("Test failed at CL.%s with message: %s", cl, e.getMessage()));
677- }
678- }
679-
680- // Test successful reads
681- for (ConsistencyLevel cl : acceptedList) {
682- try {
683- query(12, cl);
684- } catch (InvalidQueryException e) {
685- List<String> acceptableErrorMessages = Arrays.asList(
686- "EACH_QUORUM ConsistencyLevel is only supported for writes",
687- "ANY ConsistencyLevel is only supported for writes");
688- assertTrue(acceptableErrorMessages.contains(e.getMessage()), String.format("Received: %s", e.getMessage()));
689- }
690- }
691-
692- // Test writes which should fail
693- for (ConsistencyLevel cl : failList) {
694- try {
695- init(12, cl);
696- fail(String.format("Test passed at CL.%s.", cl));
697- } catch (UnavailableException e) {
698- // expected to fail when the client has already marked the
699- // node as DOWN
700- } catch (WriteTimeoutException e) {
701- // expected to fail when the client hasn't marked the
702- // node as DOWN yet
703- }
113+ public Query executeSimple(Session session, String statement, ConsistencyLevel level, ConsistencyLevel serialLevel) {
114+ SimpleStatement simpleStatement = new SimpleStatement(statement);
115+ if (level != null) {
116+ simpleStatement.setConsistencyLevel(level);
704117 }
118+ if (serialLevel != null) {
119+ simpleStatement.setSerialConsistencyLevel(serialLevel);
120+ }
121+ session.execute(simpleStatement);
122+ //Find the unique query in the activity log.
123+ List<Query> queries = sCluster.node(1).activityClient().retrieveQueries();
124+ for (Query query : queries) {
125+ if (query.getQuery().equals(statement))
126+ return query;
127+ }
128+ return null;
129+ }
130+
131+ /**
132+ * When no consistency level is defined the default of LOCAL_ONE should be used.
133+ *
134+ * @test_category consistency
135+ */
136+ @Test(groups = "short")
137+ public void should_use_global_default_cl_when_none_specified() throws Throwable {
138+ //Build a cluster with no CL level set in the query options.
139+ Cluster cluster = builder().build();
140+ try {
141+ Session session = cluster.connect();
142+
143+ //Construct unique simple statement query, with no CL defined.
144+ //Check to ensure
145+ String queryString = "default_cl";
146+ Query clQuery = executeSimple(session, queryString, null, null);
147+ assertTrue(clQuery.getConsistency().equals(ConsistencyLevel.LOCAL_ONE.toString()));
148+
149+ //Check prepared statement default CL
150+ String prepareString = "prepared_default_cl";
151+ PreparedStatementExecution pse = executePrepared(session, prepareString, null, null);
152+ assertTrue(pse.getConsistency().equals(ConsistencyLevel.LOCAL_ONE.toString()));
153+
154+ //Check batch statement default CL
155+ String batchStateString = "batch_default_cl";
156+ BatchExecution batch = executeBatch(session, batchStateString, null, null);
157+ assertTrue(batch.getConsistency().equals(ConsistencyLevel.LOCAL_ONE.toString()));
158+ } finally {
159+ cluster.close();
160+ }
161+ }
162+
163+ /**
164+ * Exhaustively tests all consistency levels when they are set via QueryOptions.
165+ *
166+ * @test_category consistency
167+ */
168+ @Test(groups = "short", dataProvider = "consistencyLevels", dataProviderClass = DataProviders.class)
169+ public void should_use_query_option_cl(ConsistencyLevel cl) throws Throwable {
170+ //Build a cluster with a CL level set in the query options.
171+ Cluster cluster = builder().withQueryOptions(new QueryOptions().setConsistencyLevel(cl)).build();
172+ try {
173+ Session session = cluster.connect();
174+ //Construct unique query, with no CL defined.
175+ String queryString = "query_cl";
176+ Query clQuery = executeSimple(session, queryString, null, null);
177+ assertTrue(clQuery.getConsistency().equals(cl.toString()));
178+
179+ //Check prepared statement CL
180+ String prepareString = "preapred_query_cl";
181+ PreparedStatementExecution pse = executePrepared(session, prepareString, null, null);
182+ assertTrue(pse.getConsistency().equals(cl.toString()));
183+
184+ //Check batch statement CL
185+ String batchStateString = "batch_query_cl";
186+ BatchExecution batch = executeBatch(session, batchStateString, null, null);
187+ assertTrue(batch.getConsistency().equals(cl.toString()));
188+ } finally {
189+ cluster.close();
190+ }
191+ }
192+
193+ /**
194+ * Exhaustively tests all consistency levels when they are set at the statement level.
195+ *
196+ * @test_category consistency
197+ */
198+ @Test(groups = "short", dataProvider = "consistencyLevels", dataProviderClass = DataProviders.class)
199+ public void should_use_statement_cl(ConsistencyLevel cl) throws Throwable {
200+ //Build a cluster with no CL set in the query options.
201+ //Note: nonQuietClusterCloseOptions is used to speed up tests
202+ Cluster cluster = builder().build();
203+ try {
204+ Session session = cluster.connect();
205+ //Construct unique query statement with a CL defined.
206+ String queryString = "statement_cl";
207+ Query clQuery = executeSimple(session, queryString, cl, null);
208+ assertTrue(clQuery.getConsistency().equals(cl.toString()));
209+
210+ //Check prepared statement CL
211+ String prepareString = "preapred_statement_cl";
212+ PreparedStatementExecution pse = executePrepared(session, prepareString, cl, null);
213+ assertTrue(pse.getConsistency().equals(cl.toString()));
214+
215+ //Check batch statement CL
216+ String batchStateString = "batch_statement_cl";
217+ BatchExecution batch = executeBatch(session, batchStateString, cl, null);
218+ assertTrue(batch.getConsistency().equals(cl.toString()));
219+ } finally {
220+ cluster.close();
221+ }
222+ }
223+
224+ /**
225+ * Tests that order of precedence is followed when defining CLs.
226+ * Statement level CL should be honored above QueryOptions.
227+ * QueryOptions should be honored above default CL.
228+ *
229+ * @test_category consistency
230+ */
231+ @Test(groups = "short")
232+ public void should_use_appropriate_cl_when_multiple_defined() throws Throwable {
233+ ConsistencyLevel cl_one = ConsistencyLevel.ONE;
234+ //Build a cluster with no CL set in the query options.
235+ Cluster cluster = builder().withQueryOptions(new QueryOptions().setConsistencyLevel(cl_one)).build();
236+ try {
705237
706- // Test reads which should fail
707- for (ConsistencyLevel cl : failList) {
708- try {
709- query(12, cl);
710- fail(String.format("Test passed at CL.%s.", cl));
711- } catch (ReadTimeoutException e) {
712- // expected to fail when the client hasn't marked the
713- // node as DOWN yet
714- } catch (UnavailableException e) {
715- // expected to fail when the client has already marked the
716- // node as DOWN
717- }
238+ Session session = cluster.connect();
239+
240+ //Check order of precedence for simple statements
241+ //Construct unique query statement with no CL defined.
242+ String queryString = "opts_cl";
243+ Query clQuery = executeSimple(session, queryString, null, null);
244+ assertTrue(clQuery.getConsistency().equals(cl_one.toString()));
245+
246+ //Construct unique query statement with a CL defined.
247+ ConsistencyLevel cl_all = ConsistencyLevel.ALL;
248+ queryString = "stm_cl";
249+ clQuery = executeSimple(session, queryString, cl_all, null);
250+ assertTrue(clQuery.getConsistency().equals(cl_all.toString()));
251+
252+ //Check order of precedence for prepared statements
253+ //Construct unique prepared statement with no CL defined.
254+ String prepareString = "prep_opts_cl";
255+ PreparedStatementExecution pse = executePrepared(session, prepareString, null, null);
256+ assertTrue(pse.getConsistency().equals(cl_one.toString()));
257+ clearActivityLog();
258+
259+ //Construct unique prepared statement with a CL defined.
260+ prepareString = "prep_stm_cl";
261+ pse = executePrepared(session, prepareString, cl_all, null);
262+ assertTrue(pse.getConsistency().equals(cl_all.toString()));
263+
264+ //Check order of precedence for batch statements
265+ //Construct unique batch statement with no CL defined.
266+ String batchString = "batch_opts_cl";
267+ BatchExecution batch = executeBatch(session, batchString, null, null);
268+ assertTrue(batch.getConsistency().equals(cl_one.toString()));
269+ clearActivityLog();
270+
271+ //Construct unique prepared statement with a CL defined.
272+ batchString = "prep_stm_cl";
273+ batch = executeBatch(session, batchString, cl_all, null);
274+ assertTrue(batch.getConsistency().equals(cl_all.toString()));
275+ } finally {
276+ cluster.close();
277+ }
278+ }
279+
280+ /**
281+ * Exhaustively tests all serial consistency levels when they are set via QueryOptions.
282+ *
283+ * @test_category consistency
284+ */
285+ @Test(groups = "short", dataProvider = "serialConsistencyLevels", dataProviderClass = DataProviders.class)
286+ public void should_use_query_option_serial_cl(ConsistencyLevel cl) throws Throwable {
287+ //Build a cluster with a CL level set in the query options.
288+ Cluster cluster = builder().withQueryOptions(new QueryOptions().setSerialConsistencyLevel(cl)).build();
289+ try {
290+ Session session = cluster.connect();
291+ //Construct unique query, with no CL defined.
292+ String queryString = "serial_query_cl";
293+ Query clQuery = executeSimple(session, queryString, null, cl);
294+ checkSerialCLMatch(cl, clQuery.getSerialConsistency());
295+
296+ //Check prepared statement CL
297+ String prepareString = "preapred_statement_serial_cl";
298+ PreparedStatementExecution pse = executePrepared(session, prepareString, null, null);
299+ checkSerialCLMatch(cl, pse.getSerialConsistency());
300+
301+ //Check batch statement CL
302+ String batchStateString = "batch_statement_serial_cl";
303+ BatchExecution batch = executeBatch(session, batchStateString, null, null);
304+ checkSerialCLMatch(cl, batch.getSerialConsistency());
305+ } finally {
306+ cluster.close();
307+ }
308+ }
309+
310+ /**
311+ * Exhaustively tests all serial consistency levels when they are set at the statement level.
312+ *
313+ * @test_category consistency
314+ */
315+ @Test(groups = "short", dataProvider = "serialConsistencyLevels", dataProviderClass = DataProviders.class)
316+ public void should_use_statement_serial_cl(ConsistencyLevel cl) throws Throwable {
317+ //Build a cluster with no CL set in the query options.
318+ Cluster cluster = builder().build();
319+ try {
320+ Session session = cluster.connect();
321+ //Construct unique query statement with a CL defined.
322+ String queryString = "statement_serial_cl";
323+ Query clQuery = executeSimple(session, queryString, null, cl);
324+ checkSerialCLMatch(cl, clQuery.getSerialConsistency());
325+
326+ //Check prepared statement CL
327+ String prepareString = "preapred_statement_serial_cl";
328+ PreparedStatementExecution pse = executePrepared(session, prepareString, null, cl);
329+ checkSerialCLMatch(cl, pse.getSerialConsistency());
330+
331+ //Check batch statement CL
332+ String batchStateString = "batch_statement_serial_cl";
333+ BatchExecution batch = executeBatch(session, batchStateString, null, cl);
334+ checkSerialCLMatch(cl, batch.getSerialConsistency());
335+ } finally {
336+ cluster.close();
718337 }
719338 }
720-
721- private void stopAndWait(int node) {
722- logger.debug("Stopping node " + node);
723- ccm().stop(node);
724- ccm().waitForDown(node);// this uses port ping
725- TestUtils.waitForDown(ipOfNode(node), cluster()); // this uses UP/DOWN events
726- logger.debug("Node " + node + " stopped, sleeping one extra minute to allow nodes to gossip");
727- Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MINUTES);
728- }
729-
730339 }
modified pom.xml +1 −1
@@ -66,7 +66,7 @@
6666 <assertj.version>1.7.0</assertj.version>
6767 <mockito.version>1.10.8</mockito.version>
6868 <commons-exec.version>1.3</commons-exec.version>
69- <scassandra.version>1.1.1</scassandra.version>
69+ <scassandra.version>1.1.2</scassandra.version>
7070 <main.basedir>${project.basedir}</main.basedir>
7171 <ipprefix>127.0.1.</ipprefix>
7272 <test.groups>unit</test.groups>