diff --git a/linkis-computation-governance/linkis-manager/linkis-application-manager/src/main/java/org/apache/linkis/manager/am/selector/rule/ResourceNodeSelectRule.java b/linkis-computation-governance/linkis-manager/linkis-application-manager/src/main/java/org/apache/linkis/manager/am/selector/rule/ResourceNodeSelectRule.java index 8e86f6906a..ff30588a35 100644 --- a/linkis-computation-governance/linkis-manager/linkis-application-manager/src/main/java/org/apache/linkis/manager/am/selector/rule/ResourceNodeSelectRule.java +++ b/linkis-computation-governance/linkis-manager/linkis-application-manager/src/main/java/org/apache/linkis/manager/am/selector/rule/ResourceNodeSelectRule.java @@ -53,10 +53,10 @@ private Comparator 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() @@ -69,7 +69,11 @@ private Comparator 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) { diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/CPUResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/CPUResource.java index d985dcbc46..aecd3ee3c4 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/CPUResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/CPUResource.java @@ -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); diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndKubernetesResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndKubernetesResource.java index 6d047b33a5..3426e91e5c 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndKubernetesResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndKubernetesResource.java @@ -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"; diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndYarnResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndYarnResource.java index 4aff24bac0..c9cfcf378e 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndYarnResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/DriverAndYarnResource.java @@ -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"; diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/KubernetesResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/KubernetesResource.java index d716569c55..c247620685 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/KubernetesResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/KubernetesResource.java @@ -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( diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadInstanceResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadInstanceResource.java index 7fb332f351..2eb37cc551 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadInstanceResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadInstanceResource.java @@ -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( diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadResource.java index 79a6374844..313ce7b92b 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/LoadResource.java @@ -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( diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/MemoryResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/MemoryResource.java index 23dc96fdb0..da69c930c7 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/MemoryResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/MemoryResource.java @@ -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); diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/Resource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/Resource.java index 361a6f28e0..ca45f374c5 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/Resource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/Resource.java @@ -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)); } diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/SpecialResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/SpecialResource.java index b670eb174e..60518a1552 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/SpecialResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/SpecialResource.java @@ -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); diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/YarnResource.java b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/YarnResource.java index 25f4d82984..48a63cd0f5 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/YarnResource.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-common/src/main/java/org/apache/linkis/manager/common/entity/resource/YarnResource.java @@ -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(