From aa602381c595db4b958709c03874ad54597ba197 Mon Sep 17 00:00:00 2001 From: slfan1989 <55643692+slfan1989@users.noreply.github.com> Date: Tue, 28 Mar 2023 22:48:46 +0800 Subject: [PATCH] YARN-11426. Improve YARN NodeLabel Memory Display. (#5335) YARN-11426. Improve YARN NodeLabel Memory Display. Co-authored-by: slfan1989 Reviewed-by: Inigo Goiri Reviewed-by: Chris Nauroth Signed-off-by: Shilun Fan --- .../hadoop-yarn/hadoop-yarn-api/pom.xml | 5 ++++ .../hadoop/yarn/api/records/Resource.java | 11 ++++++++ .../hadoop/yarn/api/records/TestResource.java | 25 +++++++++++++++++++ .../webapp/NodeLabelsPage.java | 2 +- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml index 61747c2cd8..e4b8ee2822 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/pom.xml @@ -115,6 +115,11 @@ com.fasterxml.jackson.core jackson-annotations + + org.mockito + mockito-core + test + diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java index 0c10e01768..80e569d5a9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java @@ -29,6 +29,7 @@ import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.classification.InterfaceStability.Stable; +import org.apache.hadoop.classification.VisibleForTesting; import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.yarn.api.ApplicationMasterProtocol; import org.apache.hadoop.yarn.api.protocolrecords.ResourceTypes; @@ -543,4 +544,14 @@ protected static ResourceInformation newDefaultInformation(String name, ri.setMaximumAllocation(Long.MAX_VALUE); return ri; } + + @VisibleForTesting + protected void setResources(ResourceInformation[] resources) { + this.resources = resources; + } + + public String getFormattedString(long memory) { + return getFormattedString( + StringUtils.byteDesc(memory * 1024 * 1024)); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java index 638ecf9d32..060ca39c69 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java @@ -20,6 +20,8 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; /** * The class to test {@link Resource}. @@ -42,4 +44,27 @@ void testCastToIntSafely() { "Cast to Integer.MAX_VALUE if the long is greater than " + "Integer.MAX_VALUE"); } + + @Test + public void testResourceFormatted() { + Resource resource = spy(Resource.class); + resource.setResources(new ResourceInformation[0]); + when(resource.getVirtualCores()).thenReturn(1); + + // We set 10MB + String expectedResult1 = ""; + assertEquals(expectedResult1, resource.getFormattedString(10)); + + // We set 1024 MB = 1GB + String expectedResult2 = ""; + assertEquals(expectedResult2, resource.getFormattedString(1024)); + + // We set 1024 * 1024 MB = 1024 GB = 1TB + String expectedResult3 = ""; + assertEquals(expectedResult3, resource.getFormattedString(1024 * 1024)); + + // We set 1024 * 1024 * 1024 MB = 1024 * 1024 GB = 1 * 1024 TB = 1 PB + String expectedResult4 = ""; + assertEquals(expectedResult4, resource.getFormattedString(1024 * 1024 * 1024)); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/NodeLabelsPage.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/NodeLabelsPage.java index 6ff7628100..c4df6aa0e2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/NodeLabelsPage.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/NodeLabelsPage.java @@ -75,7 +75,7 @@ protected void render(Block html) { } else { row = row.td(String.valueOf(nActiveNMs)); } - row.td(info.getResource().toString()).__(); + row.td(info.getResource().toFormattedString()).__(); } tbody.__().__(); }