Skip to content

Commit

Permalink
[BUG] Fix em sort error (#4915)
Browse files Browse the repository at this point in the history
* fix em sort error

* Adjust the sorting when there are null values.
  • Loading branch information
sjgllgh authored Sep 25, 2023
1 parent 118435f commit c0cebe4
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ private Comparator<Node> sortByResource() {
RMNode nodeBRm = (RMNode) nodeB;
if (nodeARm.getNodeResource() == null
|| nodeARm.getNodeResource().getLeftResource() == null) {
return -1;
return 1;
} else if (nodeBRm.getNodeResource() == null
|| nodeBRm.getNodeResource().getLeftResource() == null) {
return 1;
return -1;
} else {
if (nodeARm
.getNodeResource()
Expand All @@ -69,7 +69,11 @@ private Comparator<Node> sortByResource() {
.moreThan(nodeBRm.getNodeResource().getLeftResource())) {
return -1;
} else {
return 1;
// 从大到小排序 (Sort from large to small)
return -(nodeARm
.getNodeResource()
.getLeftResource()
.compare(nodeBRm.getNodeResource().getLeftResource()));
}
}
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource r) {
CPUResource cpuResource = toCPUResource(r);
return Integer.compare(this.getCores(), cpuResource.getCores());
}

@Override
public String toJson() {
return String.format(" \"cpu\":%s ", cores);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,42 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource resource) {
DriverAndKubernetesResource r = new DriverAndKubernetesResource(resource);
if (isModuleOperate(r)) {
return loadInstanceResource.compare(r.loadInstanceResource);
} else {
if (loadInstanceResource.getMemory() > r.loadInstanceResource.getMemory()) {
return 1;
} else if (loadInstanceResource.getMemory() < r.loadInstanceResource.getMemory()) {
return -1;
} else {
// If memory is equal, compare cores
if (loadInstanceResource.getCores() > r.loadInstanceResource.getCores()) {
return 1;
} else if (loadInstanceResource.getCores() < r.loadInstanceResource.getCores()) {
return -1;
} else {
// If cores are equal, compare instances
if (loadInstanceResource.getInstances() > r.loadInstanceResource.getInstances()) {
return 1;
} else if (loadInstanceResource.getInstances() < r.loadInstanceResource.getInstances()) {
return -1;
} else {
if (kubernetesResource.getMemory() > r.kubernetesResource.getMemory()) {
return 1;
} else if (kubernetesResource.getMemory() < r.kubernetesResource.getMemory()) {
return -1;
} else {
return Long.compare(kubernetesResource.getCores(), r.kubernetesResource.getCores());
}
}
}
}
}
}

public String toJson() {
String load = "null";
String kubernetes = "null";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,43 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource resource) {
DriverAndYarnResource r = new DriverAndYarnResource(resource);

if (isModuleOperate(r)) {
return loadInstanceResource.compare(r.loadInstanceResource);
} else {
if (loadInstanceResource.getMemory() > r.loadInstanceResource.getMemory()) {
return 1;
} else if (loadInstanceResource.getMemory() < r.loadInstanceResource.getMemory()) {
return -1;
} else {
// If memory is equal, compare cores
if (loadInstanceResource.getCores() > r.loadInstanceResource.getCores()) {
return 1;
} else if (loadInstanceResource.getCores() < r.loadInstanceResource.getCores()) {
return -1;
} else {
// If cores are equal, compare instances
if (loadInstanceResource.getInstances() > r.loadInstanceResource.getInstances()) {
return 1;
} else if (loadInstanceResource.getInstances() < r.loadInstanceResource.getInstances()) {
return -1;
} else {
if (yarnResource.getQueueMemory() > r.yarnResource.getQueueMemory()) {
return 1;
} else if (yarnResource.getQueueMemory() < r.yarnResource.getQueueMemory()) {
return -1;
} else {
return Integer.compare(yarnResource.getQueueCores(), r.yarnResource.getQueueCores());
}
}
}
}
}
}

public String toJson() {
String load = "null";
String yarn = "null";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource r) {
KubernetesResource temp = new KubernetesResource(r);
if (this.getMemory() > temp.getMemory()) {
return 1;
} else if (this.getMemory() < temp.getMemory()) {
return -1;
} else {
// If memory is equal, compare cores
return Long.compare(this.getCores(), temp.getCores());
}
}

@Override
public String toJson() {
return String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource r) {
LoadInstanceResource temp = new LoadInstanceResource(r);

if (this.getMemory() > temp.getMemory()) {
return 1;
} else if (this.getMemory() < temp.getMemory()) {
return -1;
} else {
// If memory is equal, compare cores
if (this.getCores() > temp.getCores()) {
return 1;
} else if (this.getCores() < temp.getCores()) {
return -1;
} else {
// If cores are equal, compare instances
return Integer.compare(this.getInstances(), temp.getInstances());
}
}
}

@Override
public String toJson() {
return String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource r) {
LoadResource temp = new LoadResource(r);

if (this.getMemory() > temp.getMemory()) {
return 1;
} else if (this.getMemory() < temp.getMemory()) {
return -1;
} else {
// If memory is equal, compare cores
return Integer.compare(this.getCores(), temp.getCores());
}
}

@Override
public String toJson() {
return String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource resource) {
MemoryResource r = toMemoryResource(resource);
return Long.compare(this.getMemory(), r.getMemory());
}

@Override
public boolean caseMore(Resource r) {
return moreThan(r);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public static Resource getZeroResource(Resource resource) {

public abstract boolean less(Resource r);

public abstract int compare(Resource r);

public Resource add(Resource r, float rate) {
return this.add(r.multiplied(rate));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource r) {
if (this.moreThan(r)) {
return 1;
} else if (this.less(r)) {
return -1;
} else {
return 0;
}
}

@Override
public String toJson() {
return String.format("Special:%s", resources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ public boolean less(Resource r) {
return !notLess(r);
}

@Override
public int compare(Resource resource) {
YarnResource r = toYarnResource(resource);
if (this.getQueueMemory() > r.getQueueMemory()) {
return 1;
} else if (this.getQueueMemory() < r.getQueueMemory()) {
return -1;
} else {
return Integer.compare(this.getQueueCores(), r.getQueueCores());
}
}

@Override
public String toJson() {
return String.format(
Expand Down

0 comments on commit c0cebe4

Please sign in to comment.