This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new fc5c72560 RATIS-2004. Fix EQ_COMPARETO_USE_OBJECT_EQUALS in LogSegment 
(#1020)
fc5c72560 is described below

commit fc5c7256096c6f5b85308b47a1b2e5242ded4d03
Author: Doroszlai, Attila <[email protected]>
AuthorDate: Thu Jan 18 21:31:57 2024 +0100

    RATIS-2004. Fix EQ_COMPARETO_USE_OBJECT_EQUALS in LogSegment (#1020)
---
 .../ratis/server/raftlog/segmented/LogSegment.java    | 19 ++++++++++++++-----
 .../raftlog/segmented/SegmentedRaftLogCache.java      |  6 +++---
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
index 1e0ef666e..0750d2cc8 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java
@@ -17,7 +17,6 @@
  */
 package org.apache.ratis.server.raftlog.segmented;
 
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import org.apache.ratis.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.server.RaftServerConfigKeys.Log.CorruptionPolicy;
 import org.apache.ratis.server.metrics.SegmentedRaftLogMetrics;
@@ -39,6 +38,7 @@ import org.slf4j.LoggerFactory;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -56,7 +56,8 @@ import java.util.function.Consumer;
  *
  * This class will be protected by the {@link SegmentedRaftLog}'s read-write 
lock.
  */
-public final class LogSegment implements Comparable<Long> {
+public final class LogSegment {
+
   static final Logger LOG = LoggerFactory.getLogger(LogSegment.class);
 
   enum Op {
@@ -452,9 +453,17 @@ public final class LogSegment implements Comparable<Long> {
         "log-" + startIndex + "_" + endIndex;
   }
 
-  @Override
-  @SuppressFBWarnings("EQ_COMPARETO_USE_OBJECT_EQUALS")
-  public int compareTo(Long l) {
+  /** Comparator to find <code>index</code> in list of 
<code>LogSegment</code>s. */
+  static final Comparator<Object> SEGMENT_TO_INDEX_COMPARATOR = (o1, o2) -> {
+    if (o1 instanceof LogSegment && o2 instanceof Long) {
+      return ((LogSegment) o1).compareTo((Long) o2);
+    } else if (o1 instanceof Long && o2 instanceof LogSegment) {
+      return Integer.compare(0, ((LogSegment) o2).compareTo((Long) o1));
+    }
+    throw new IllegalStateException("Unexpected objects to compare(" + o1 + 
"," + o2 + ")");
+  };
+
+  private int compareTo(Long l) {
     return (l >= getStartIndex() && l <= getEndIndex()) ? 0 :
         (this.getEndIndex() < l ? -1 : 1);
   }
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
index 81f467726..1d08316fd 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogCache.java
@@ -244,13 +244,13 @@ public class SegmentedRaftLogCache {
 
     int binarySearch(long index) {
       try(AutoCloseableLock readLock = readLock()) {
-        return Collections.binarySearch(segments, index);
+        return Collections.binarySearch(segments, index, 
LogSegment.SEGMENT_TO_INDEX_COMPARATOR);
       }
     }
 
     LogSegment search(long index) {
       try(AutoCloseableLock readLock = readLock()) {
-        final int i = Collections.binarySearch(segments, index);
+        final int i = Collections.binarySearch(segments, index, 
LogSegment.SEGMENT_TO_INDEX_COMPARATOR);
         return i < 0? null: segments.get(i);
       }
     }
@@ -261,7 +261,7 @@ public class SegmentedRaftLogCache {
       long index = startIndex;
 
       try(AutoCloseableLock readLock = readLock()) {
-        searchIndex = Collections.binarySearch(segments, startIndex);
+        searchIndex = Collections.binarySearch(segments, startIndex, 
LogSegment.SEGMENT_TO_INDEX_COMPARATOR);
         if (searchIndex >= 0) {
           for(int i = searchIndex; i < segments.size() && index < realEnd; 
i++) {
             final LogSegment s = segments.get(i);

Reply via email to