3535import org .apache .cassandra .db .RowUpdateBuilder ;
3636import org .apache .cassandra .db .commitlog .CommitLogSegment .CDCState ;
3737import org .apache .cassandra .exceptions .CDCWriteException ;
38- import org .apache .cassandra .io .util .FileUtils ;
3938import org .apache .cassandra .schema .TableMetadata ;
4039
4140public class CommitLogSegmentManagerCDCTest extends CQLTester
@@ -68,7 +67,7 @@ public void testCDCWriteFailure() throws Throwable
6867 TableMetadata cfm = currentTableMetadata ();
6968
7069 // Confirm that logic to check for whether or not we can allocate new CDC segments works
71- Integer originalCDCSize = DatabaseDescriptor .getCDCSpaceInMB ();
70+ int originalCDCSize = DatabaseDescriptor .getCDCSpaceInMB ();
7271 try
7372 {
7473 DatabaseDescriptor .setCDCSpaceInMB (32 );
@@ -98,11 +97,10 @@ public void testCDCWriteFailure() throws Throwable
9897 Keyspace .open (keyspace ()).getColumnFamilyStore (currentTable ()).forceBlockingFlush ();
9998 CommitLog .instance .forceRecycleAllSegments ();
10099 cdcMgr .awaitManagementTasksCompletion ();
101- Assert .assertTrue ("Expected files to be moved to overflow." , getCDCRawCount () > 0 );
100+ Assert .assertTrue ("Expected files to be moved to overflow." , getCDCRawFiles (). length > 0 );
102101
103102 // Simulate a CDC consumer reading files then deleting them
104- for (File f : new File (DatabaseDescriptor .getCDCLogLocation ()).listFiles ())
105- FileUtils .deleteWithConfirm (f );
103+ deleteCDCRawFiles ();
106104
107105 // Update size tracker to reflect deleted files. Should flip flag on current allocatingFrom to allow.
108106 cdcMgr .updateCDCTotalSize ();
@@ -115,7 +113,7 @@ public void testCDCWriteFailure() throws Throwable
115113 }
116114
117115 @ Test
118- public void testSegmentFlaggingOnCreation () throws Throwable
116+ public void testSegmentFlaggingOnCreation ()
119117 {
120118 CommitLogSegmentManagerCDC cdcMgr = (CommitLogSegmentManagerCDC )CommitLog .instance .segmentManager ;
121119 String ct = createTable ("CREATE TABLE %s (idx int, data text, primary key(idx)) WITH cdc=true;" );
@@ -143,16 +141,13 @@ public void testSegmentFlaggingOnCreation() throws Throwable
143141
144142 cdcMgr .awaitManagementTasksCompletion ();
145143 // Delete all files in cdc_raw
146- for (File f : new File (DatabaseDescriptor .getCDCLogLocation ()).listFiles ())
147- f .delete ();
144+ deleteCDCRawFiles ();
148145 cdcMgr .updateCDCTotalSize ();
149146 // Confirm cdc update process changes flag on active segment
150147 expectCurrentCDCState (CDCState .PERMITTED );
151148
152149 // Clear out archived CDC files
153- for (File f : new File (DatabaseDescriptor .getCDCLogLocation ()).listFiles ()) {
154- FileUtils .deleteWithConfirm (f );
155- }
150+ deleteCDCRawFiles ();
156151 }
157152 finally
158153 {
@@ -179,8 +174,8 @@ public void testCDCIndexFileWriteOnSync() throws IOException
179174 // Read index value and confirm it's == end from last sync
180175 BufferedReader in = new BufferedReader (new FileReader (cdcIndexFile ));
181176 String input = in .readLine ();
182- Integer offset = Integer .parseInt (input );
183- Assert .assertEquals (syncOffset , ( long ) offset );
177+ int offset = Integer .parseInt (input );
178+ Assert .assertEquals (syncOffset , offset );
184179 in .close ();
185180 }
186181
@@ -189,7 +184,7 @@ public void testCompletedFlag() throws IOException
189184 {
190185 createTable ("CREATE TABLE %s (idx int, data text, primary key(idx)) WITH cdc=true;" );
191186 CommitLogSegment initialSegment = CommitLog .instance .segmentManager .allocatingFrom ();
192- Integer originalCDCSize = DatabaseDescriptor .getCDCSpaceInMB ();
187+ int originalCDCSize = DatabaseDescriptor .getCDCSpaceInMB ();
193188
194189 DatabaseDescriptor .setCDCSpaceInMB (8 );
195190 try
@@ -220,7 +215,7 @@ public void testCompletedFlag() throws IOException
220215 BufferedReader in = new BufferedReader (new FileReader (cdcIndexFile ));
221216 String input = in .readLine ();
222217 input = in .readLine ();
223- Assert .assertTrue ("Expected COMPLETED in index file, got: " + input , input . equals ( "COMPLETED" ) );
218+ Assert .assertEquals ("Expected COMPLETED in index file, got: " + input , "COMPLETED" , input );
224219 in .close ();
225220 }
226221
@@ -282,7 +277,7 @@ public void testReplayLogic() throws IOException
282277 {
283278 // Assert.assertEquals(0, new File(DatabaseDescriptor.getCDCLogLocation()).listFiles().length);
284279 String table_name = createTable ("CREATE TABLE %s (idx int, data text, primary key(idx)) WITH cdc=true;" );
285- Integer originalCDCSize = DatabaseDescriptor .getCDCSpaceInMB ();
280+ int originalCDCSize = DatabaseDescriptor .getCDCSpaceInMB ();
286281
287282 DatabaseDescriptor .setCDCSpaceInMB (8 );
288283 TableMetadata ccfm = Keyspace .open (keyspace ()).getColumnFamilyStore (table_name ).metadata ();
@@ -310,13 +305,12 @@ public void testReplayLogic() throws IOException
310305
311306 // Build up a list of expected index files after replay and then clear out cdc_raw
312307 List <CDCIndexData > oldData = parseCDCIndexData ();
313- for (File f : new File (DatabaseDescriptor .getCDCLogLocation ()).listFiles ())
314- FileUtils .deleteWithConfirm (f .getAbsolutePath ());
308+ deleteCDCRawFiles ();
315309
316310 try
317311 {
318312 Assert .assertEquals ("Expected 0 files in CDC folder after deletion. " ,
319- 0 , new File ( DatabaseDescriptor . getCDCLogLocation ()). listFiles ().length );
313+ 0 , getCDCRawFiles ().length );
320314 }
321315 finally
322316 {
@@ -331,7 +325,7 @@ public void testReplayLogic() throws IOException
331325
332326 // Rough sanity check -> should be files there now.
333327 Assert .assertTrue ("Expected non-zero number of files in CDC folder after restart." ,
334- new File ( DatabaseDescriptor . getCDCLogLocation ()). listFiles ().length > 0 );
328+ getCDCRawFiles ().length > 0 );
335329
336330 // Confirm all the old indexes in old are present and >= the original offset, as we flag the entire segment
337331 // as cdc written on a replay.
@@ -345,6 +339,7 @@ public void testReplayLogic() throws IOException
345339 {
346340 Assert .assertTrue ("New CDC index file expected to have >= offset in old." , ncid .offset >= cid .offset );
347341 found = true ;
342+ break ;
348343 }
349344 }
350345 if (!found )
@@ -365,7 +360,10 @@ public void testReplayLogic() throws IOException
365360 for (CDCIndexData cid : oldData )
366361 {
367362 if (cid .fileName .equals (ncid .fileName ))
363+ {
368364 found = true ;
365+ break ;
366+ }
369367 }
370368 if (!found )
371369 Assert .fail (String .format ("Unexpected new CDCIndexData found after replay: %s\n " , ncid ));
@@ -377,7 +375,7 @@ private List<CDCIndexData> parseCDCIndexData()
377375 List <CDCIndexData > results = new ArrayList <>();
378376 try
379377 {
380- for (File f : new File ( DatabaseDescriptor . getCDCLogLocation ()). listFiles ())
378+ for (File f : getCDCRawFiles ())
381379 {
382380 if (f .getName ().contains ("_cdc.idx" ))
383381 results .add (new CDCIndexData (f ));
@@ -397,15 +395,12 @@ private static class CDCIndexData
397395
398396 CDCIndexData (File f ) throws IOException
399397 {
400- String line = "" ;
398+ String line ;
401399 try (BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (f ))))
402400 {
403401 line = br .readLine ();
404402 }
405- catch (Exception e )
406- {
407- throw e ;
408- }
403+
409404 fileName = f .getName ();
410405 offset = Integer .parseInt (line );
411406 }
@@ -419,6 +414,8 @@ public String toString()
419414 @ Override
420415 public boolean equals (Object other )
421416 {
417+ if (!(other instanceof CDCIndexData ))
418+ return false ;
422419 CDCIndexData cid = (CDCIndexData )other ;
423420 return fileName .equals (cid .fileName ) && offset == cid .offset ;
424421 }
@@ -431,11 +428,6 @@ private ByteBuffer randomizeBuffer(int size)
431428 return ByteBuffer .wrap (toWrap );
432429 }
433430
434- private int getCDCRawCount ()
435- {
436- return new File (DatabaseDescriptor .getCDCLogLocation ()).listFiles ().length ;
437- }
438-
439431 private void expectCurrentCDCState (CDCState expectedState )
440432 {
441433 CDCState currentState = CommitLog .instance .segmentManager .allocatingFrom ().getCDCState ();
@@ -447,4 +439,17 @@ private void expectCurrentCDCState(CDCState expectedState)
447439 expectedState , currentState ));
448440 }
449441 }
442+
443+ private static File [] getCDCRawFiles ()
444+ {
445+ return new File (DatabaseDescriptor .getCDCLogLocation ()).listFiles ();
446+ }
447+
448+ private static void deleteCDCRawFiles ()
449+ {
450+ for (File f : getCDCRawFiles ())
451+ {
452+ f .delete ();
453+ }
454+ }
450455}
0 commit comments