Uploaded image for project: 'Red Hat Decision Manager'
  1. Red Hat Decision Manager
  2. RHDM-459

ProtobufOutputMarshaller.orderFacts causes a "Comparison method violates its general contract" error

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 7.0.1.GA
    • 7.0.0.GA
    • BRE
    • CR1
    • 2018 Week 07-08

      When marshalling kieSession in production (long running kieSession with a lot of fact insertion), we encountered the following error :

      java.lang.IllegalArgumentException: Comparison method violates its general contract!
      	at java.util.TimSort.mergeLo(TimSort.java:777)
      	at java.util.TimSort.mergeAt(TimSort.java:514)
      	at java.util.TimSort.mergeCollapse(TimSort.java:439)
      	at java.util.TimSort.sort(TimSort.java:245)
      	at java.util.Arrays.sort(Arrays.java:1438)
      	at org.drools.core.marshalling.impl.ProtobufOutputMarshaller.orderFacts(ProtobufOutputMarshaller.java:776)
      	at org.drools.core.marshalling.impl.ProtobufOutputMarshaller.writeFactHandles(ProtobufOutputMarshaller.java:707)
      	at org.drools.core.marshalling.impl.ProtobufOutputMarshaller.serializeSession(ProtobufOutputMarshaller.java:176)
      	at org.drools.core.marshalling.impl.ProtobufOutputMarshaller.writeSession(ProtobufOutputMarshaller.java:120)
      	at org.drools.core.marshalling.impl.ProtobufMarshaller.marshall(ProtobufMarshaller.java:164)
      	at org.drools.core.marshalling.impl.ProtobufMarshaller.marshall(ProtobufMarshaller.java:148)
      

      Indeed, the orderFacts method in ProtobufoutputMarshaller uses a HandleSorter as comparator, which returns the result of o1.getId() - o2.getId() where o1 and o2 are InternalFactHandle.

      Since the id of InternalFactHandle is an int, this comparator can cause "Comparison method violates its general contract!" IllegalArgumentException because Integer.MAX_INT + 1 < 0.
      The comparator should return the result of Integer.compare(o1.getId(), o2.getId())

      The root cause is that InternalFactHandle.getId() is an int and should be a long for long running kieSession.
      This id is generated by the FactHandleFactory (in AbstractFactHandleFactory) and is an AtomicInteger incremented at each fact creation.
      With long running kieSession a lot of fact handles can be created (inserted in memory or with "from") so this id can overflow Integer.MAX_INT and become negative, causing this issue when ordering.

            mfusco@redhat.com Mario Fusco
            rhn-support-mputz Martin Weiler (Inactive)
            Tibor Zimányi Tibor Zimányi
            Tibor Zimányi Tibor Zimányi
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: