@@ -31,6 +31,18 @@ class RetryPolicyIntegrationTest extends BasicIntegrationTest {
3131 * recursive calls/
3232 */
3333 const NUMBER_OF_TIMEOUT_EXCEPTIONS = 5 ;
34+ /**
35+ * Batch statement type
36+ */
37+ const BATCH_STATEMENT = 1 ;
38+ /**
39+ * Prepared statement type
40+ */
41+ const PREPARED_STATEMENT = 2 ;
42+ /**
43+ * Simple statement type
44+ */
45+ const SIMPLE_STATEMENT = 3 ;
3446
3547 /**
3648 * Insert query generated for a retry policy test.
@@ -43,8 +55,7 @@ class RetryPolicyIntegrationTest extends BasicIntegrationTest {
4355 * Setup the retry policy for multiple nodes.
4456 */
4557 public function setUp () {
46- // Ensure there are three nodes in data center one with RF = 3
47- $ this ->numberDC1Nodes = 3 ;
58+ // Ensure RF = 3 (anything greater than 1 is good)
4859 $ this ->replicationFactor = 3 ;
4960
5061 // Process parent setup steps
@@ -58,53 +69,58 @@ public function setUp() {
5869 $ this ->insertQuery = "INSERT INTO {$ this ->tableNamePrefix } (key, value_int) VALUES (?, ?) " ;
5970 }
6071
61- /**
62- * Teardown; Ensure all nodes have been restarted
63- */
64- public function tearDown () {
65- $ nodes = range (1 , $ this ->numberDC1Nodes );
66- $ this ->ccm ->startNode ($ nodes );
67-
68- // Process parent teardown steps
69- parent ::tearDown ();
70- }
71-
7272 /**
7373 * Insert n values into the table for a given key.
7474 *
75+ * @param $statementType Type of statement to create for inserts
7576 * @param RetryPolicy $policy RetryPolicy to use when executing statements
7677 * @param $key Key value
7778 * @param $numberOfInserts Number of inserts to perform
7879 * @param $consistency Consistency level to execute statement
79- * @param $retries Number of TimeoutException retries
80- * (DEFAULT: self::NUMBER_OF_TIMEOUT_EXCEPTIONS)
80+ * @param int $retries Number of TimeoutException retries
81+ * (DEFAULT: self::NUMBER_OF_TIMEOUT_EXCEPTIONS)
8182 */
82- private function insert (RetryPolicy $ policy , $ key , $ numberOfInserts , $ consistency , $ retries = self ::NUMBER_OF_TIMEOUT_EXCEPTIONS ) {
83+ private function insert ($ statementType , RetryPolicy $ policy , $ key , $ numberOfInserts , $ consistency , $ retries = self ::NUMBER_OF_TIMEOUT_EXCEPTIONS ) {
8384 try {
84- // Create and prepare the insert statements
85+ // Create all statement types
86+ $ batch = new BatchStatement (\Cassandra::BATCH_UNLOGGED );
8587 $ prepare = $ this ->session ->prepare ($ this ->insertQuery );
8688 $ simple = new SimpleStatement ($ this ->insertQuery );
87- $ batch = new BatchStatement (\Cassandra::BATCH_UNLOGGED );
8889
89- // Create the batched inserts
90+
91+ // Create the default execution options
92+ $ options = array (
93+ "consistency " => $ consistency ,
94+ "retry_policy " => $ policy
95+ );
96+
97+ // Create the inserts
9098 foreach (range (1 , $ numberOfInserts ) as $ i ) {
9199 $ values = array (
92100 $ key ,
93101 $ i
94102 );
95- if ($ i % 2 == 0 ) {
96- $ batch ->add ($ prepare , $ values );
103+ if ($ statementType == self ::BATCH_STATEMENT ) {
104+ if ($ i % 2 == 0 ) {
105+ $ batch ->add ($ prepare , $ values );
106+ } else {
107+ $ batch ->add ($ simple , $ values );
108+ }
97109 } else {
98- $ batch ->add ($ simple , $ values );
110+ // Execute either the prepare or simple statment
111+ $ statement = $ prepare ;
112+ if ($ statementType == self ::SIMPLE_STATEMENT ) {
113+ $ statement = $ simple ;
114+ }
115+ $ options ["arguments " ] = $ values ;
116+ $ this ->session ->execute ($ statement , new ExecutionOptions ($ options ));
99117 }
100118 }
101119
102120 // Execute the batched insert
103- $ options = new ExecutionOptions (array (
104- "consistency " => $ consistency ,
105- "retry_policy " => $ policy
106- ));
107- $ this ->session ->execute ($ batch , $ options );
121+ if ($ statementType == self ::BATCH_STATEMENT ) {
122+ $ this ->session ->execute ($ batch , new ExecutionOptions ($ options ));
123+ }
108124 } catch (Exception \TimeoutException $ te ) {
109125 if (Integration::isDebug ()) {
110126 fprintf (STDOUT , "Insert TimeoutException: %s (%s:%d) " . PHP_EOL ,
@@ -125,8 +141,8 @@ private function insert(RetryPolicy $policy, $key, $numberOfInserts, $consistenc
125141 * @param $key Key value
126142 * @param $numberOfAsserts Number of inserts to perform
127143 * @param $consistency Consistency level to execute statement
128- * @param $retries Number of TimeoutException retries
129- * (DEFAULT: self::NUMBER_OF_TIMEOUT_EXCEPTIONS)
144+ * @param int $retries Number of TimeoutException retries
145+ * (DEFAULT: self::NUMBER_OF_TIMEOUT_EXCEPTIONS)
130146 */
131147 private function assert (RetryPolicy $ policy , $ key , $ numberOfAsserts , $ consistency , $ retries = self ::NUMBER_OF_TIMEOUT_EXCEPTIONS ) {
132148 try {
@@ -169,30 +185,32 @@ private function assert(RetryPolicy $policy, $key, $numberOfAsserts, $consistenc
169185 * @cassandra-version-2.0
170186 */
171187 public function testDowngradingPolicy () {
172- // Create the retry policy
188+ // Create the retry policy (RF = 3 with 1 node)
173189 $ policy = new RetryPolicy \DowngradingConsistency ();
174190
175- // Disable node one
176- $ this -> ccm -> stopNode ( 1 );
177-
178- // Insert and assert values with CONSISTENCY_ALL
179- $ this -> insert ( $ policy , 0 , self :: NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
180- $ this -> assert ( $ policy , 0 , self :: NUMBER_OF_INSERTS , \Cassandra:: CONSISTENCY_ALL );
181-
182- // Disable node three
183- $ this -> ccm -> stopNode ( 3 );
184-
185- // Insert and assert values with CONSISTENCY_ALL
186- $ this ->insert ($ policy , 1 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
187- $ this ->assert ($ policy , 1 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
191+ // Iterate over each statement type
192+ foreach ( range ( 1 , 3 ) as $ statementType ) {
193+ // Determine if the statement type should be skipped
194+ if ( $ statementType == self :: BATCH_STATEMENT
195+ && version_compare ( \Cassandra::CPP_DRIVER_VERSION , " 2.2.3 " ) < 0 ) {
196+ if (Integration:: isDebug ()) {
197+ fprintf ( STDOUT , " Skipping Batch Statements in %s: Issue fixed in DataStax C/C++ v2.2.3 " . PHP_EOL ,
198+ $ this -> getName ());
199+ }
200+ } else {
201+ // Insert and assert values with CONSISTENCY_ALL
202+ $ this ->insert ($ statementType , $ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
203+ $ this ->assert ($ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
188204
189- // Insert and assert values with CONSISTENCY_QUORUM
190- $ this ->insert ($ policy , 2 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_QUORUM );
191- $ this ->assert ($ policy , 2 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_QUORUM );
205+ // Insert and assert values with CONSISTENCY_QUORUM
206+ $ this ->insert ($ statementType , $ policy , 1 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_QUORUM );
207+ $ this ->assert ($ policy , 1 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_QUORUM );
192208
193- // Insert and assert values with CONSISTENCY_TWO
194- $ this ->insert ($ policy , 3 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_TWO );
195- $ this ->assert ($ policy , 3 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_TWO );
209+ // Insert and assert values with CONSISTENCY_TWO
210+ $ this ->insert ($ statementType , $ policy , 2 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_TWO );
211+ $ this ->assert ($ policy , 2 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_TWO );
212+ }
213+ }
196214 }
197215
198216 /**
@@ -208,16 +226,27 @@ public function testDowngradingPolicy() {
208226 *
209227 * @cassandra-version-2.0
210228 *
211- * @expectedException \Cassandra\Exception\WriteTimeoutException
212- * @expectedExceptionMessageRegExp |Operation timed out - received only .* responses |
229+ * @expectedException \Cassandra\Exception\UnavailableException
230+ * @expectedExceptionMessageRegExp |Cannot achieve consistency level .* |
213231 */
214232 public function testFallThroughPolicyWrite () {
215- // Create the retry policy
233+ // Create the retry policy (RF = 3 with 1 node)
216234 $ policy = new RetryPolicy \Fallthrough ();
217235
218- // Create a WriteTimeoutException
219- $ this ->ccm ->stopNode (1 );
220- $ this ->insert ($ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
236+ // Iterate over each statement type
237+ foreach (range (1 , 3 ) as $ statementType ) {
238+ // Determine if the statement type should be skipped
239+ if ($ statementType == self ::BATCH_STATEMENT
240+ && version_compare (\Cassandra::CPP_DRIVER_VERSION , "2.2.3 " ) < 0 ) {
241+ if (Integration::isDebug ()) {
242+ fprintf (STDOUT , "Skipping Batch Statements in %s: Issue fixed in DataStax C/C++ v2.2.3 " . PHP_EOL ,
243+ $ this ->getName ());
244+ }
245+ } else {
246+ // Create an exception during write
247+ $ this ->insert ($ statementType , $ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
248+ }
249+ }
221250 }
222251
223252 /**
@@ -233,18 +262,27 @@ public function testFallThroughPolicyWrite() {
233262 *
234263 * @cassandra-version-2.0
235264 *
236- * @expectedException \Cassandra\Exception\ReadTimeoutException
237- * @expectedExceptionMessageRegExp |Operation timed out - received only .* responses |
265+ * @expectedException \Cassandra\Exception\UnavailableException
266+ * @expectedExceptionMessageRegExp |Cannot achieve consistency level .* |
238267 */
239268 public function testFallThroughPolicyRead () {
240- // Create the retry policy
269+ // Create the retry policy (RF = 3 with 1 node)
241270 $ policy = new RetryPolicy \Fallthrough ();
242271
243- // Insert values with CONSISTENCY_ALL
244- $ this ->insert ($ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
245-
246- // Create a ReadTimeoutException
247- $ this ->ccm ->stopNode (1 );
248- $ this ->assert ($ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
272+ // Iterate over each statement type
273+ foreach (range (1 , 3 ) as $ statementType ) {
274+ // Determine if the statement type should be skipped
275+ if ($ statementType == self ::BATCH_STATEMENT
276+ && version_compare (\Cassandra::CPP_DRIVER_VERSION , "2.2.3 " ) < 0 ) {
277+ if (Integration::isDebug ()) {
278+ fprintf (STDOUT , "Skipping Batch Statements in %s: Issue fixed in DataStax C/C++ v2.2.3 " . PHP_EOL ,
279+ $ this ->getName ());
280+ }
281+ } else {
282+ // Create an exception during read
283+ $ this ->insert ($ statementType , $ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ONE );
284+ $ this ->assert ($ policy , 0 , self ::NUMBER_OF_INSERTS , \Cassandra::CONSISTENCY_ALL );
285+ }
286+ }
249287 }
250288}
0 commit comments