@@ -283,9 +283,7 @@ private void sortPendingDeviceTasks() {
283283 if (deviceEntryComparator != null ) {
284284 pendingDeviceTasks .sort (
285285 (left , right ) ->
286- deviceEntryComparator .compare (
287- sharedDeviceEntries .get (left .deviceEntryIndex ),
288- sharedDeviceEntries .get (right .deviceEntryIndex )));
286+ compareDeviceEntryIndexes (left .deviceEntryIndex , right .deviceEntryIndex ));
289287 } else {
290288 pendingDeviceTasks .sort (
291289 (left , right ) ->
@@ -345,10 +343,7 @@ void finish() {
345343
346344 private void sortDeviceEntries () {
347345 if (deviceEntryComparator != null ) {
348- deviceEntryIndexes .sort (
349- (left , right ) ->
350- deviceEntryComparator .compare (
351- sharedDeviceEntries .get (left ), sharedDeviceEntries .get (right )));
346+ deviceEntryIndexes .sort (ExternalTsFileQueryResource .this ::compareDeviceEntryIndexes );
352347 } else {
353348 deviceEntryIndexes .sort (
354349 (left , right ) ->
@@ -368,6 +363,15 @@ private List<DeviceTask> getPendingDeviceTasks() {
368363 }
369364 }
370365
366+ private int compareDeviceEntryIndexes (int leftIndex , int rightIndex ) {
367+ int result =
368+ deviceEntryComparator .compare (
369+ sharedDeviceEntries .get (leftIndex ), sharedDeviceEntries .get (rightIndex ));
370+ // Use the stable device entry index as a tie-breaker so list sorting and run-file merging keep
371+ // the same deterministic order when the pushed-down comparator is not a total order.
372+ return result != 0 ? result : Integer .compare (leftIndex , rightIndex );
373+ }
374+
371375 private void createDeviceTaskPartitions (int partitionCount ) {
372376 if (partitionCount <= 0 ) {
373377 throw new IllegalArgumentException (
@@ -420,8 +424,9 @@ public class DeviceTaskRunReader implements AutoCloseable {
420424 usePriorityQueue
421425 ? new PriorityQueue <>(
422426 (left , right ) ->
423- comparator .compare (
424- left .getCurrentDeviceEntry (), right .getCurrentDeviceEntry ()))
427+ compareDeviceEntryIndexes (
428+ left .getCurrentDeviceTask ().deviceEntryIndex ,
429+ right .getCurrentDeviceTask ().deviceEntryIndex ))
425430 : new ArrayDeque <>();
426431 for (Path runFile : partition .getRunFiles ()) {
427432 DeviceTaskRunCursor cursor = new DiskDeviceTaskRunCursor (runFile , sharedDeviceEntries );
0 commit comments