|
37 | 37 | import org.apache.hadoop.yarn.api.protocolrecords.GetAllResourceTypeInfoResponse;
|
38 | 38 | import org.apache.hadoop.yarn.api.protocolrecords.GetResourceProfileResponse;
|
39 | 39 | import org.apache.hadoop.yarn.api.protocolrecords.GetAllResourceProfilesResponse;
|
| 40 | +import org.apache.hadoop.yarn.api.protocolrecords.GetAttributesToNodesResponse; |
| 41 | +import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodeAttributesResponse; |
40 | 42 | import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
|
41 | 43 | import org.apache.hadoop.yarn.api.records.ApplicationId;
|
42 | 44 | import org.apache.hadoop.yarn.api.records.ApplicationReport;
|
|
53 | 55 | import org.apache.hadoop.yarn.api.records.ReservationDefinition;
|
54 | 56 | import org.apache.hadoop.yarn.api.records.ReservationId;
|
55 | 57 | import org.apache.hadoop.yarn.api.records.ResourceTypeInfo;
|
| 58 | +import org.apache.hadoop.yarn.api.records.NodeAttribute; |
| 59 | +import org.apache.hadoop.yarn.api.records.NodeAttributeType; |
| 60 | +import org.apache.hadoop.yarn.api.records.NodeAttributeKey; |
| 61 | +import org.apache.hadoop.yarn.api.records.NodeToAttributeValue; |
| 62 | +import org.apache.hadoop.yarn.api.records.NodeAttributeInfo; |
56 | 63 | import org.apache.hadoop.yarn.util.Records;
|
57 | 64 | import org.apache.hadoop.yarn.server.uam.UnmanagedApplicationManager;
|
58 | 65 | import org.junit.Assert;
|
@@ -610,4 +617,100 @@ public void testMergeResourceProfile() {
|
610 | 617 | Assert.assertEquals(3, resource.getVirtualCores());
|
611 | 618 | Assert.assertEquals(3072, resource.getMemorySize());
|
612 | 619 | }
|
| 620 | + |
| 621 | + @Test |
| 622 | + public void testMergeAttributesToNodesResponse() { |
| 623 | + // normal response1 |
| 624 | + NodeAttribute gpu = NodeAttribute.newInstance(NodeAttribute.PREFIX_CENTRALIZED, "GPU", |
| 625 | + NodeAttributeType.STRING, "nvidia"); |
| 626 | + Map<NodeAttributeKey, List<NodeToAttributeValue>> map1 = new HashMap<>(); |
| 627 | + List<NodeToAttributeValue> lists1 = new ArrayList<>(); |
| 628 | + NodeToAttributeValue attributeValue1 = NodeToAttributeValue.newInstance("node1", gpu.getAttributeValue()); |
| 629 | + lists1.add(attributeValue1); |
| 630 | + map1.put(gpu.getAttributeKey(), lists1); |
| 631 | + GetAttributesToNodesResponse response1 = GetAttributesToNodesResponse.newInstance(map1); |
| 632 | + |
| 633 | + // normal response2 |
| 634 | + NodeAttribute docker = NodeAttribute.newInstance(NodeAttribute.PREFIX_DISTRIBUTED, "DOCKER", |
| 635 | + NodeAttributeType.STRING, "docker0"); |
| 636 | + Map<NodeAttributeKey, List<NodeToAttributeValue>> map2 = new HashMap<>(); |
| 637 | + List<NodeToAttributeValue> lists2 = new ArrayList<>(); |
| 638 | + NodeToAttributeValue attributeValue2 = NodeToAttributeValue.newInstance("node2", docker.getAttributeValue()); |
| 639 | + lists2.add(attributeValue2); |
| 640 | + map2.put(docker.getAttributeKey(), lists2); |
| 641 | + GetAttributesToNodesResponse response2 = GetAttributesToNodesResponse.newInstance(map2); |
| 642 | + |
| 643 | + // empty response3 |
| 644 | + GetAttributesToNodesResponse response3 = GetAttributesToNodesResponse.newInstance(new HashMap<>()); |
| 645 | + |
| 646 | + // null response4 |
| 647 | + GetAttributesToNodesResponse response4 = null; |
| 648 | + |
| 649 | + List<GetAttributesToNodesResponse> responses = new ArrayList<>(); |
| 650 | + responses.add(response1); |
| 651 | + responses.add(response2); |
| 652 | + responses.add(response3); |
| 653 | + responses.add(response4); |
| 654 | + |
| 655 | + GetAttributesToNodesResponse response = |
| 656 | + RouterYarnClientUtils.mergeAttributesToNodesResponse(responses); |
| 657 | + |
| 658 | + Assert.assertNotNull(response); |
| 659 | + Assert.assertEquals(2, response.getAttributesToNodes().size()); |
| 660 | + |
| 661 | + Map<NodeAttributeKey, List<NodeToAttributeValue>> attrs = response.getAttributesToNodes(); |
| 662 | + Assert.assertTrue(findHostnameAndValInMapping("node2", "docker0", |
| 663 | + attrs.get(docker.getAttributeKey()))); |
| 664 | + } |
| 665 | + |
| 666 | + @Test |
| 667 | + public void testMergeClusterNodeAttributesResponse() { |
| 668 | + // normal response1 |
| 669 | + NodeAttributeInfo nodeAttributeInfo1 = |
| 670 | + NodeAttributeInfo.newInstance(NodeAttributeKey.newInstance("GPU"), NodeAttributeType.STRING); |
| 671 | + Set<NodeAttributeInfo> attributes1 = new HashSet<>(); |
| 672 | + attributes1.add(nodeAttributeInfo1); |
| 673 | + GetClusterNodeAttributesResponse response1 = GetClusterNodeAttributesResponse.newInstance(attributes1); |
| 674 | + |
| 675 | + // normal response2 |
| 676 | + NodeAttributeInfo nodeAttributeInfo2 = |
| 677 | + NodeAttributeInfo.newInstance(NodeAttributeKey.newInstance("CPU"), NodeAttributeType.STRING); |
| 678 | + Set<NodeAttributeInfo> attributes2 = new HashSet<>(); |
| 679 | + attributes2.add(nodeAttributeInfo2); |
| 680 | + GetClusterNodeAttributesResponse response2 = GetClusterNodeAttributesResponse.newInstance(attributes2); |
| 681 | + |
| 682 | + // empty response3 |
| 683 | + GetClusterNodeAttributesResponse response3 = GetClusterNodeAttributesResponse.newInstance(new HashSet<>()); |
| 684 | + |
| 685 | + // null response4 |
| 686 | + GetClusterNodeAttributesResponse response4 = null; |
| 687 | + |
| 688 | + List<GetClusterNodeAttributesResponse> responses = new ArrayList<>(); |
| 689 | + responses.add(response1); |
| 690 | + responses.add(response2); |
| 691 | + responses.add(response3); |
| 692 | + responses.add(response4); |
| 693 | + |
| 694 | + GetClusterNodeAttributesResponse response = |
| 695 | + RouterYarnClientUtils.mergeClusterNodeAttributesResponse(responses); |
| 696 | + |
| 697 | + Assert.assertNotNull(response); |
| 698 | + |
| 699 | + Set<NodeAttributeInfo> nodeAttributeInfos = response.getNodeAttributes(); |
| 700 | + Assert.assertEquals(2, nodeAttributeInfos.size()); |
| 701 | + |
| 702 | + Object[] objectArr = nodeAttributeInfos.toArray(); |
| 703 | + Assert.assertEquals("rm.yarn.io/GPU(STRING)", objectArr[0].toString()); |
| 704 | + Assert.assertEquals("rm.yarn.io/CPU(STRING)", objectArr[1].toString()); |
| 705 | + } |
| 706 | + |
| 707 | + private boolean findHostnameAndValInMapping(String hostname, String attrVal, |
| 708 | + List<NodeToAttributeValue> mappingVals) { |
| 709 | + for (NodeToAttributeValue value : mappingVals) { |
| 710 | + if (value.getHostname().equals(hostname)) { |
| 711 | + return attrVal.equals(value.getAttributeValue()); |
| 712 | + } |
| 713 | + } |
| 714 | + return false; |
| 715 | + } |
613 | 716 | }
|
0 commit comments