nielifeng opened a new issue, #11462:
URL: https://github.com/apache/seatunnel/issues/11462

   ### Search before asking
   
   - [X] I had searched in the 
[issues](https://github.com/apache/seatunnel/issues?q=is%3Aissue+label%3A%22bug%22)
 and found no similar issues.
   
   ### What happened
   
   `IMapFileData.compareTo` 
(`seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/main/java/org/apache/seatunnel/engine/imap/storage/file/bean/IMapFileData.java:48-50`)
 never returns `0`:
   
   ```java
   @Override
   public int compareTo(IMapFileData o) {
       return o.timestamp - this.timestamp > 0 ? 1 : -1;
   }
   ```
   
   When two records share the same `timestamp`, both `compare(a,b)` and 
`compare(b,a)` return `-1`, which violates the antisymmetry requirement of the 
`Comparable` contract (`sgn(compare(a,b)) == -sgn(compare(b,a))`). Timestamps 
are assigned from `System.currentTimeMillis()` (in `IMapFileStorage`), so 
same-millisecond collisions are realistic under batch writes.
   
   This has three consequences, all reproduced locally using the exact 
comparator body:
   
   1. **Antisymmetry violated** — `compare(a,b) == compare(b,a) == -1`.
   2. **`Collections.sort` throws `IllegalArgumentException: Comparison method 
violates its general contract!`** once there are enough equal-timestamp 
elements to leave TimSort's binary-insertion path. `WALReader.loadAllData` / 
`loadAllKeys` call `Collections.sort(...)` on the full record list on every 
read, so this can crash the recovery/read path.
   3. **A delete can be lost (data resurrection)** — for the same key written 
and then deleted within the same millisecond, the undefined ordering of equal 
elements can place the PUT before the DELETE during dedup, so the delete is 
dropped and the key survives.
   
   <details>
   <summary>Standalone reproduction program (uses the exact source comparator 
body)</summary>
   
   ```java
   import java.util.*;
   
   public class CompareToRepro {
       static class IMapFileData implements Comparable<IMapFileData> {
           boolean deleted; String keyStr; long timestamp;
           IMapFileData(String keyStr, boolean deleted, long timestamp) {
               this.keyStr = keyStr; this.deleted = deleted; this.timestamp = 
timestamp;
           }
           @Override
           public int compareTo(IMapFileData o) {
               // === verbatim from source ===
               return o.timestamp - this.timestamp > 0 ? 1 : -1;
           }
           public String toString() { return (deleted ? "DEL " : "PUT ") + 
keyStr + "@" + timestamp; }
       }
   
       public static void main(String[] args) {
           // Assertion 1: antisymmetry
           IMapFileData a = new IMapFileData("k", false, 1000L);
           IMapFileData b = new IMapFileData("k", true, 1000L);
           System.out.println("compare(a,b)=" + a.compareTo(b) + ", 
compare(b,a)=" + b.compareTo(a));
   
           // Assertion 2: TimSort throws
           List<IMapFileData> list = new ArrayList<>();
           Random r = new Random(42);
           for (int i = 0; i < 4000; i++) list.add(new IMapFileData("k"+i, 
r.nextBoolean(), 1000L + r.nextInt(50)));
           try { Collections.sort(list); System.out.println("no throw"); }
           catch (IllegalArgumentException e) { System.out.println("THROW: " + 
e.getMessage()); }
   
           // Assertion 3: delete lost on same key/ms
           List<IMapFileData> events = new ArrayList<>();
           events.add(new IMapFileData("userA", false, 2000L));
           events.add(new IMapFileData("userA", true, 2000L));
           Collections.sort(events);
           Map<String,IMapFileData> result = new LinkedHashMap<>();
           Set<String> deleteMap = new HashSet<>();
           for (IMapFileData d : events) {
               if (d.deleted) deleteMap.add(d.keyStr);
               else if (!deleteMap.contains(d.keyStr)) 
result.putIfAbsent(d.keyStr, d);
           }
           System.out.println("sorted=" + events + " userA survives=" + 
result.containsKey("userA"));
       }
   }
   ```
   
   Output on OpenJDK 1.8.0_462:
   
   ```
   compare(a,b)=-1, compare(b,a)=-1
   THROW: Comparison method violates its general contract!
   sorted=[DEL userA@2000, PUT userA@2000] userA survives=false
   ```
   
   </details>
   
   **Suggested fix**: return a proper total order with a stable tiebreaker, 
e.g. `int c = Long.compare(o.timestamp, this.timestamp); return c != 0 ? c : 
Boolean.compare(this.deleted, o.deleted);` — always returning `0` for 
genuinely-equal entries.
   
   ### SeaTunnel Version
   
   dev (current HEAD). The `IMapFileData` comparator is unchanged on the 
current branch.
   
   ### SeaTunnel Config
   
   N/A — this is a logic bug in the WAL record comparator, independent of any 
job config. It was reproduced with a standalone Java program that uses the 
exact `compareTo` body from the source (see the collapsible section above), 
rather than through a full job.
   
   ### Running Command
   
   N/A — see above. In a real deployment it is triggered by the file-based IMap 
storage (`imap-storage-file`) WAL read/dedup path (`WALReader.loadAllData` / 
`loadAllKeys`) when same-millisecond records exist.
   
   ### Error Exception
   
   ```log
   java.lang.IllegalArgumentException: Comparison method violates its general 
contract!
       at java.util.TimSort.mergeHi(TimSort.java:899)
       ...
       at java.util.Collections.sort(Collections.java:175)
   ```
   
   ### Zeta or Flink or Spark Version
   
   Zeta (SeaTunnel Engine), file-based IMap storage (`imap-storage-file`).
   
   ### Java or Scala Version
   
   OpenJDK 1.8.0_462 (Temurin).
   
   ### Screenshots
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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