Uploaded image for project: 'Confluence Data Center'
  1. Confluence Data Center
  2. CONFSERVER-45910

Confluence throws "Comparison method violates its general contract!" when trying to sort multiple long-running tasks

      Code to reproduce

      Use a unit test below to reproduce this by running the comparator:

              Collections.sort(fullList, (o1, o2) -> {
                  // Sorting by name is not as good as sorting by start time (unavailable) but better than anything
                  // else we can think of this iteration. Changing this order later is unable to raise any blockers
                  // in production.
                  Message o1Name = o1.getName();
                  if (o1Name == null || o1Name.getKey() == null)
                      return 1;  // push o1 to bottom
      
                  Message o2Name = o2.getName();
                  if (o2Name == null || o2Name.getKey() == null)
                      return -1;  // push o2 to bottom
      
                  return o1Name.getKey().compareTo(o2Name.getKey());
              });
      

      Which is copied from Confluence code.

      Confluence will throw the following error message:

      Comparison method violates its general contract!
      

      That basically means that the comparator is not written correctly.

      Environments

      Attached a test class to reproduce the error: ContractViolationTest.java

      • on Windows 10,
      • java version "1.8.0_111" Java(TM) SE Runtime Environment (build 1.8.0_111-b14), Java HotSpot(TM) Client VM (build 25.111-b14, mixed mode)
      • Important: it only breaks if collection size is >= 32

      A possible fix in Confluence

      Switching the +1 and -1 return values in the comparator's two if blocks fixes the problem.
      Note that this change makes "null key" cases appear before "not-null key" cases in the sorted list. It does not affect the original order of objects where the key is not-null.

      Workaround

      Adding this JVM system property fixes the issue:
      -Djava.util.Arrays.useLegacyMergeSort=true

      References

            [CONFSERVER-45910] Confluence throws "Comparison method violates its general contract!" when trying to sort multiple long-running tasks

            Nobuyuki Mukai made changes -
            Link New: This issue is related to CONFSERVER-58059 [ CONFSERVER-58059 ]
            Katherine Yabut made changes -
            Workflow Original: JAC Bug Workflow v3 [ 2888370 ] New: CONFSERVER Bug Workflow v4 [ 2999520 ]
            Owen made changes -
            Workflow Original: JAC Bug Workflow v2 [ 2798013 ] New: JAC Bug Workflow v3 [ 2888370 ]
            Status Original: Resolved [ 5 ] New: Closed [ 6 ]
            Owen made changes -
            Workflow Original: JAC Bug Workflow [ 2728129 ] New: JAC Bug Workflow v2 [ 2798013 ]
            Owen made changes -
            Symptom Severity Original: Major [ 14431 ] New: Severity 2 - Major [ 15831 ]
            Owen made changes -
            Workflow Original: Confluence Workflow - Public Facing - Restricted v5 - TEMP [ 2385180 ] New: JAC Bug Workflow [ 2728129 ]
            ferrari made changes -
            Remote Link New: This issue links to "Page (Confluence)" [ 368516 ]
            ferrari made changes -
            Remote Link New: This issue links to "Page (Confluence)" [ 367239 ]
            Alex Yakovlev (Inactive) made changes -
            Labels Original: affects-server bugfix long-running-task loyalty pse-request New: affects-server long-running-task loyalty pse-request
            Alex Yakovlev (Inactive) made changes -
            Labels Original: affects-server bugfix long-running-task pse-request New: affects-server bugfix long-running-task loyalty pse-request

              mtran@atlassian.com Minh Tran
              mkhairuliana Monique Khairuliana (Inactive)
              Affected customers:
              6 This affects my team
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: