Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YARN-11745: Fix TimSort contract violation in PriorityQueueComparator Class #7278

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public static int compare(double relativeAssigned1, double relativeAssigned2,
/**
* Comparator that both looks at priority and utilization
*/
final private class PriorityQueueComparator
final public class PriorityQueueComparator
implements Comparator<PriorityQueueResourcesForSorting> {

final private String partition;

private PriorityQueueComparator(String partition) {
public PriorityQueueComparator(String partition) {
this.partition = partition;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ private int compare(PriorityQueueResourcesForSorting q1Sort,
q1Sort.configuredMinResource;
Resource minEffRes2 =
q2Sort.configuredMinResource;
if (!minEffRes1.equals(Resources.none()) && !minEffRes2.equals(
if (!minEffRes1.equals(Resources.none()) || !minEffRes2.equals(
Resources.none())) {
return minEffRes2.compareTo(minEffRes1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.*;
Hean-Chhinling marked this conversation as resolved.
Show resolved Hide resolved
import java.util.concurrent.ThreadLocalRandom;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Expand Down Expand Up @@ -309,6 +305,48 @@ public void testComparatorDoesNotValidateGeneralContract() {
assertDoesNotThrow(() -> policy.getAssignmentIterator(partition));
}

@Test
public void testComparatorClassDoesNotViolateTimSortContract() {
String partition = "testPartition";

List<PriorityUtilizationQueueOrderingPolicy.PriorityQueueResourcesForSorting> queues = new ArrayList<>();
for (int i = 0; i < 300; i++) {
queues.add(createMockPriorityQueueResourcesForSorting(partition, Resource.newInstance(0, 0))); // Need to be (0, 0)
queues.add(createMockPriorityQueueResourcesForSorting(partition, Resource.newInstance(8, 20))); // Could be any number
queues.add(createMockPriorityQueueResourcesForSorting(partition, Resource.newInstance(10, 5))); // Could be any number
}

Collections.shuffle(queues);
// java.lang.IllegalArgumentException: Comparison method violates its general contract!
assertDoesNotThrow(() -> Collections.sort(queues, new PriorityUtilizationQueueOrderingPolicy(true)
.new PriorityQueueComparator(partition)));

}

private PriorityUtilizationQueueOrderingPolicy.PriorityQueueResourcesForSorting createMockPriorityQueueResourcesForSorting(
String partition, Resource resource
) {
Hean-Chhinling marked this conversation as resolved.
Show resolved Hide resolved

QueueCapacities mockQueueCapacities = mock(QueueCapacities.class);
when(mockQueueCapacities.getAbsoluteUsedCapacity(partition)).thenReturn(4.2f); // Could be any number
when(mockQueueCapacities.getUsedCapacity(partition)).thenReturn(1.0f); // Could be any number
when(mockQueueCapacities.getAbsoluteCapacity(partition)).thenReturn(6.2f); // Could be any number

CSQueue mockQueue = mock(CSQueue.class);
when(mockQueue.getQueueCapacities()).thenReturn(mockQueueCapacities);
when(mockQueue.getPriority()).thenReturn(Priority.newInstance(7)); // Could be any number
when(mockQueue.getAccessibleNodeLabels()).thenReturn(Collections.singleton("label3")); // Could be any label

QueueResourceQuotas mockResourceQuotas = mock(QueueResourceQuotas.class);
when(mockResourceQuotas.getConfiguredMinResource(partition)).thenReturn(resource);
when(mockQueue.getQueueResourceQuotas()).thenReturn(mockResourceQuotas);

return new PriorityUtilizationQueueOrderingPolicy.PriorityQueueResourcesForSorting(
mockQueue, partition
);

}

private QueueCapacities randomQueueCapacities(String partition) {
QueueCapacities qc = new QueueCapacities(false);
qc.setAbsoluteCapacity(partition, (float) randFloat(0.0d, 100.0d));
Expand Down
Loading