YARN-11426. Improve YARN NodeLabel Memory Display. (#5335)

YARN-11426. Improve YARN NodeLabel Memory Display.
Co-authored-by: slfan1989 <louj1988@@>
Reviewed-by: Inigo Goiri <inigoiri@apache.org>
Reviewed-by: Chris Nauroth <cnauroth@apache.org>
Signed-off-by: Shilun Fan <slfan1989@apache.org>
This commit is contained in:
slfan1989 2023-03-28 22:48:46 +08:00 committed by GitHub
parent 700147b4ac
commit aa602381c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 1 deletions

View File

@ -115,6 +115,11 @@
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -29,6 +29,7 @@
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.classification.InterfaceStability.Evolving;
import org.apache.hadoop.classification.InterfaceStability.Stable; import org.apache.hadoop.classification.InterfaceStability.Stable;
import org.apache.hadoop.classification.VisibleForTesting;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.yarn.api.ApplicationMasterProtocol; import org.apache.hadoop.yarn.api.ApplicationMasterProtocol;
import org.apache.hadoop.yarn.api.protocolrecords.ResourceTypes; import org.apache.hadoop.yarn.api.protocolrecords.ResourceTypes;
@ -543,4 +544,14 @@ protected static ResourceInformation newDefaultInformation(String name,
ri.setMaximumAllocation(Long.MAX_VALUE); ri.setMaximumAllocation(Long.MAX_VALUE);
return ri; return ri;
} }
@VisibleForTesting
protected void setResources(ResourceInformation[] resources) {
this.resources = resources;
}
public String getFormattedString(long memory) {
return getFormattedString(
StringUtils.byteDesc(memory * 1024 * 1024));
}
} }

View File

@ -20,6 +20,8 @@
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; 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}. * The class to test {@link Resource}.
@ -42,4 +44,27 @@ void testCastToIntSafely() {
"Cast to Integer.MAX_VALUE if the long is greater than " "Cast to Integer.MAX_VALUE if the long is greater than "
+ "Integer.MAX_VALUE"); + "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 = "<memory:10 MB, vCores:1>";
assertEquals(expectedResult1, resource.getFormattedString(10));
// We set 1024 MB = 1GB
String expectedResult2 = "<memory:1 GB, vCores:1>";
assertEquals(expectedResult2, resource.getFormattedString(1024));
// We set 1024 * 1024 MB = 1024 GB = 1TB
String expectedResult3 = "<memory:1 TB, vCores:1>";
assertEquals(expectedResult3, resource.getFormattedString(1024 * 1024));
// We set 1024 * 1024 * 1024 MB = 1024 * 1024 GB = 1 * 1024 TB = 1 PB
String expectedResult4 = "<memory:1 PB, vCores:1>";
assertEquals(expectedResult4, resource.getFormattedString(1024 * 1024 * 1024));
}
} }

View File

@ -75,7 +75,7 @@ protected void render(Block html) {
} else { } else {
row = row.td(String.valueOf(nActiveNMs)); row = row.td(String.valueOf(nActiveNMs));
} }
row.td(info.getResource().toString()).__(); row.td(info.getResource().toFormattedString()).__();
} }
tbody.__().__(); tbody.__().__();
} }