How to fix “IllegalArgumentException: Comparison method violates its general contract!” when indexing a Gems Repository

Loren Yeung
2022-04-27 09:11

Subject

When indexing a Gems repository, the version comparator may fail if there are a lot of versions

Affected Versions

Artifactory 4.0.0+, 5.0.0+

Details

Stack trace of the issue:[art-exec-273] ERROR o.a.a.g.i.GemsLocalIndexHandler:223 - Could not recalculate index for repository gems-local
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:866)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:483)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:406)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:213)
at java.util.Arrays.sort(Arrays.java:1312)
at java.util.Arrays.sort(Arrays.java:1506)
at java.util.ArrayList.sort(ArrayList.java:1462)
at java.util.Collections.sort(Collections.java:141)
at org.artifactory.addon.gems.helpers.FinderHelper.buildNameVersionPlatformTuples(FinderHelper.java:184)
at org.artifactory.addon.gems.helpers.RecalculateIndexHelper.getIndexEntries(RecalculateIndexHelper.java:52)
at org.artifactory.addon.gems.helpers.RecalculateIndexHelper.recalculateIndex(RecalculateIndexHelper.java:36)
at org.artifactory.addon.gems.index.GemsLocalIndexHandler.doRecalculateIndex(GemsLocalIndexHandler.java:213)
at org.artifactory.addon.gems.index.GemsLocalIndexHandler.recalculateIndex(GemsLocalIndexHandler.java:169)
at org.artifactory.addon.gems.GemsAddonImpl.reindexAsync(GemsAddonImpl.java:61)
...
 

Resolution

Add java.util.Arrays.useLegacyMergeSort=true as a java option to your default file.
For a zip installation, this will be $ARTIFACTORY_HOME/bin/artifactory.default:
export JAVA_OPTIONS="-Djava.util.Arrays.useLegacyMergeSort=true"

For a Service installation, modify JAVA_OPTIONS in $ARTIFACTORY_HOME/etc/default.
For a RPM or Debian installation, modify JAVA_OPTIONS in /etc/opt/jfrog/artifactory/default. 

If you see this error in Artifactory 7, you can apply this workaround by putting it in the system.yaml, under
shared:  extraJavaOpts: -Djava.util.Arrays.useLegacyMergeSort=true
Followed by a restart.