yifan-c commented on a change in pull request #1379:
URL: https://github.com/apache/cassandra/pull/1379#discussion_r786426457



##########
File path: 
src/java/org/apache/cassandra/db/commitlog/CommitLogSegmentManagerCDC.java
##########
@@ -84,21 +84,46 @@ public void discard(CommitLogSegment segment, boolean 
delete)
 
     /**
      * Delete the oldest hard-linked CDC commit log segment to free up space.
+     * @param bytesToFree, the minimum space to free up
      * @return total deleted file size in bytes
      */
-    public long deleteOldestLinkedCDCCommitLogSegment()
+    public long deleteOldLinkedCDCCommitLogSegment(long bytesToFree)
     {
+        if (bytesToFree <= 0)
+            return 0;
+
         File cdcDir = new File(DatabaseDescriptor.getCDCLogLocation());
         Preconditions.checkState(cdcDir.isDirectory(), "The CDC directory does 
not exist.");
         File[] files = cdcDir.tryList(f -> 
CommitLogDescriptor.isValid(f.name()));
-        Preconditions.checkState(files != null && files.length > 0,
-                                 "There should be at least 1 CDC commit log 
segment.");
+        if (files == null || files.length == 0)
+        {
+            logger.warn("Skip deleting due to no CDC commit log segments 
found.");
+            return 0;
+        }
         List<File> sorted = Arrays.stream(files)
-                                  
.sorted(Comparator.comparingLong(File::lastModified))
+                                  // commit log file name (contains id) 
increases monotonically
+                                  .sorted(Comparator.comparing(File::name))
                                   .collect(Collectors.toList());
-        File oldestCdcFile = sorted.get(0);
-        File cdcIndexFile = 
CommitLogDescriptor.inferCdcIndexFile(oldestCdcFile);
-        return deleteCDCFiles(oldestCdcFile, cdcIndexFile);
+        long bytesDeleted = 0;
+        long bytesRemaining = 0;
+        boolean deletionCompleted = false;
+        // keep deleting from old to new until it reaches to the goal or the 
current writting segment
+        for (File linkedCdcFile : sorted)
+        {
+            // only evaluate/update when deletionCompleted is false
+            deletionCompleted = deletionCompleted
+                                || (bytesDeleted >= bytesToFree || 
linkedCdcFile.equals(allocatingFrom().getCDCFile()));

Review comment:
       Yep. It does not worth it to trade readability with compactness.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to