szetszwo commented on code in PR #919:
URL: https://github.com/apache/ratis/pull/919#discussion_r1327560185


##########
ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java:
##########
@@ -657,6 +657,21 @@ private class TruncateLog extends Task {
     void execute() throws IOException {
       freeSegmentedRaftLogOutputStream();
 
+      if (segments.getToDelete() != null && segments.getToDelete().length > 0) 
{
+        long minStart = segments.getToDelete()[0].getStartIndex();
+        for (SegmentFileInfo del : segments.getToDelete()) {

Review Comment:
   Let's sort the array so that the latest log will be deleted first.
   ```java
        TruncationSegments(SegmentFileInfo toTruncate,
                           List<SegmentFileInfo> toDelete) {
   -      this.toDelete = toDelete == null ? null :
   -          toDelete.toArray(new SegmentFileInfo[toDelete.size()]);
   +      this.toDelete = SegmentFileInfo.toSortedArray(toDelete);
          this.toTruncate = toTruncate;
        }
   ```
   ```java
     static final class SegmentFileInfo {
       static final SegmentFileInfo[] EMPTY_ARRAY = {};
       static final Comparator<SegmentFileInfo> REVERSED_ORDER = 
Comparator.comparingLong(SegmentFileInfo::getStartIndex)
           .thenComparingLong(SegmentFileInfo::getEndIndex)
           .reversed();
   
       static SegmentFileInfo[] toSortedArray(List<SegmentFileInfo> list) {
         if (list == null) {
           return EMPTY_ARRAY;
         }
         final SegmentFileInfo[] array = list.toArray(EMPTY_ARRAY);
         Arrays.sort(array, REVERSED_ORDER);
         return array;
       }
   ```
   



-- 
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]

Reply via email to