Skip to content

Commit 7cbca99

Browse files
author
slfan1989
committed
YARN-11238. Fix CodeStyle.
1 parent d0919f6 commit 7cbca99

File tree

2 files changed

+50
-70
lines changed

2 files changed

+50
-70
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/clientrm/FederationClientInterceptor.java

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818

1919
package org.apache.hadoop.yarn.server.router.clientrm;
2020

21-
import org.apache.commons.collections.CollectionUtils;
21+
import org.apache.commons.lang3.StringUtils;
2222
import org.apache.commons.lang3.tuple.Pair;
2323
import org.apache.hadoop.thirdparty.com.google.common.collect.Maps;
2424
import org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
2525
import java.io.IOException;
2626
import java.lang.reflect.InvocationTargetException;
2727
import java.lang.reflect.Method;
28-
import java.util.*;
28+
import java.util.ArrayList;
29+
import java.util.Collection;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.Random;
33+
import java.util.TreeMap;
34+
import java.util.Set;
2935
import java.util.concurrent.BlockingQueue;
3036
import java.util.concurrent.Callable;
3137
import java.util.concurrent.ConcurrentHashMap;
@@ -660,7 +666,7 @@ public GetApplicationsResponse getApplications(GetApplicationsRequest request)
660666
long startTime = clock.getTime();
661667
ClientMethod remoteMethod = new ClientMethod("getApplications",
662668
new Class[] {GetApplicationsRequest.class}, new Object[] {request});
663-
Map<SubClusterId, GetApplicationsResponse> applications = null;
669+
Collection<GetApplicationsResponse> applications = null;
664670
try {
665671
applications = invokeConcurrent(remoteMethod, GetApplicationsResponse.class);
666672
} catch (Exception ex) {
@@ -670,7 +676,7 @@ public GetApplicationsResponse getApplications(GetApplicationsRequest request)
670676
long stopTime = clock.getTime();
671677
routerMetrics.succeededMultipleAppsRetrieved(stopTime - startTime);
672678
// Merge the Application Reports
673-
return RouterYarnClientUtils.mergeApplications(applications.values(), returnPartialReport);
679+
return RouterYarnClientUtils.mergeApplications(applications, returnPartialReport);
674680
}
675681

676682
@Override
@@ -685,8 +691,7 @@ public GetClusterMetricsResponse getClusterMetrics(
685691
new Class[] {GetClusterMetricsRequest.class}, new Object[] {request});
686692
Collection<GetClusterMetricsResponse> clusterMetrics = null;
687693
try {
688-
clusterMetrics = invokeAppClientProtocolMethod(
689-
true, remoteMethod, GetClusterMetricsResponse.class);
694+
clusterMetrics = invokeConcurrent(remoteMethod, GetClusterMetricsResponse.class);
690695
} catch (Exception ex) {
691696
routerMetrics.incrGetClusterMetricsFailedRetrieved();
692697
RouterServerUtil.logAndThrowException("Unable to get cluster metrics due to exception.", ex);
@@ -696,7 +701,7 @@ public GetClusterMetricsResponse getClusterMetrics(
696701
return RouterYarnClientUtils.merge(clusterMetrics);
697702
}
698703

699-
<R> Map<SubClusterId, R> invokeConcurrent(ClientMethod request, Class<R> clazz)
704+
<R> Collection<R> invokeConcurrent(ClientMethod request, Class<R> clazz)
700705
throws YarnException {
701706

702707
Collection<SubClusterId> subClusterIds = federationFacade.getActiveSubClusterIds();
@@ -724,24 +729,31 @@ <R> Map<SubClusterId, R> invokeConcurrent(ClientMethod request, Class<R> clazz)
724729
futures.stream().forEach(future -> {
725730
SubClusterId subClusterId = null;
726731
try {
727-
Pair<SubClusterId, Object> mapFuture = future.get();
728-
subClusterId = mapFuture.getKey();
729-
Object result = mapFuture.getValue();
732+
Pair<SubClusterId, Object> pair = future.get();
733+
subClusterId = pair.getKey();
734+
Object result = pair.getValue();
730735
results.put(subClusterId, clazz.cast(result));
731736
} catch (InterruptedException | ExecutionException e) {
732737
Throwable cause = e.getCause();
733738
LOG.error("Cannot execute {} on {}: {}", request.getMethodName(),
734-
subClusterId.getId(), cause.getMessage());
739+
subClusterId.getId(), cause.getMessage());
735740
exceptions.put(subClusterId, e);
736741
}
737742
});
738743
} catch (InterruptedException e) {
739744
throw new YarnException(e);
740745
}
741746

747+
// All sub-clusters return results to be considered successful,
748+
// otherwise an exception will be thrown.
749+
if (exceptions != null && !exceptions.isEmpty()) {
750+
Set<SubClusterId> subClusterIdSets = exceptions.keySet();
751+
throw new YarnException("invokeConcurrent Failed, An exception occurred in subClusterIds = " +
752+
StringUtils.join(subClusterIdSets, ","));
753+
}
742754

743-
744-
return results;
755+
// return result
756+
return results.values();
745757
}
746758

747759
@Override
@@ -752,24 +764,19 @@ public GetClusterNodesResponse getClusterNodes(GetClusterNodesRequest request)
752764
RouterServerUtil.logAndThrowException("Missing getClusterNodes request.", null);
753765
}
754766
long startTime = clock.getTime();
755-
Map<SubClusterId, SubClusterInfo> subClusters =
756-
federationFacade.getSubClusters(true);
757-
Map<SubClusterId, GetClusterNodesResponse> clusterNodes = Maps.newHashMap();
758-
for (SubClusterId subClusterId : subClusters.keySet()) {
759-
ApplicationClientProtocol client;
760-
try {
761-
client = getClientRMProxyForSubCluster(subClusterId);
762-
GetClusterNodesResponse response = client.getClusterNodes(request);
763-
clusterNodes.put(subClusterId, response);
764-
} catch (Exception ex) {
765-
routerMetrics.incrClusterNodesFailedRetrieved();
766-
RouterServerUtil.logAndThrowException("Unable to get cluster nodes due to exception.", ex);
767-
}
767+
ClientMethod remoteMethod = new ClientMethod("getClusterNodes",
768+
new Class[]{GetClusterNodesRequest.class}, new Object[]{request});
769+
Collection<GetClusterNodesResponse> clusterNodes = null;
770+
try {
771+
clusterNodes = invokeConcurrent(remoteMethod, GetClusterNodesResponse.class);
772+
} catch (Exception ex) {
773+
routerMetrics.incrClusterNodesFailedRetrieved();
774+
RouterServerUtil.logAndThrowException("Unable to get cluster nodes due to exception.", ex);
768775
}
769776
long stopTime = clock.getTime();
770777
routerMetrics.succeededGetClusterNodesRetrieved(stopTime - startTime);
771778
// Merge the NodesResponse
772-
return RouterYarnClientUtils.mergeClusterNodesResponse(clusterNodes.values());
779+
return RouterYarnClientUtils.mergeClusterNodesResponse(clusterNodes);
773780
}
774781

775782
@Override
@@ -785,8 +792,7 @@ public GetQueueInfoResponse getQueueInfo(GetQueueInfoRequest request)
785792
new Class[]{GetQueueInfoRequest.class}, new Object[]{request});
786793
Collection<GetQueueInfoResponse> queues = null;
787794
try {
788-
queues = invokeAppClientProtocolMethod(true, remoteMethod,
789-
GetQueueInfoResponse.class);
795+
queues = invokeConcurrent(remoteMethod, GetQueueInfoResponse.class);
790796
} catch (Exception ex) {
791797
routerMetrics.incrGetQueueInfoFailedRetrieved();
792798
RouterServerUtil.logAndThrowException("Unable to get queue [" +
@@ -810,8 +816,7 @@ public GetQueueUserAclsInfoResponse getQueueUserAcls(
810816
new Class[] {GetQueueUserAclsInfoRequest.class}, new Object[] {request});
811817
Collection<GetQueueUserAclsInfoResponse> queueUserAcls = null;
812818
try {
813-
queueUserAcls = invokeAppClientProtocolMethod(true, remoteMethod,
814-
GetQueueUserAclsInfoResponse.class);
819+
queueUserAcls = invokeConcurrent(remoteMethod, GetQueueUserAclsInfoResponse.class);
815820
} catch (Exception ex) {
816821
routerMetrics.incrQueueUserAclsFailedRetrieved();
817822
RouterServerUtil.logAndThrowException("Unable to get queue user Acls due to exception.", ex);
@@ -971,8 +976,7 @@ public ReservationListResponse listReservations(
971976
new Class[] {ReservationListRequest.class}, new Object[] {request});
972977
Collection<ReservationListResponse> listResponses = null;
973978
try {
974-
listResponses = invokeAppClientProtocolMethod(true, remoteMethod,
975-
ReservationListResponse.class);
979+
listResponses = invokeConcurrent(remoteMethod, ReservationListResponse.class);
976980
} catch (Exception ex) {
977981
routerMetrics.incrListReservationsFailedRetrieved();
978982
RouterServerUtil.logAndThrowException(
@@ -1051,24 +1055,6 @@ public ReservationDeleteResponse deleteReservation(
10511055
throw new YarnException(msg);
10521056
}
10531057

1054-
private <R> Collection<R> invokeAppClientProtocolMethod(
1055-
Boolean filterInactiveSubClusters, ClientMethod request, Class<R> clazz)
1056-
throws YarnException, RuntimeException {
1057-
Map<SubClusterId, SubClusterInfo> subClusters =
1058-
federationFacade.getSubClusters(filterInactiveSubClusters);
1059-
return subClusters.keySet().stream().map(subClusterId -> {
1060-
try {
1061-
ApplicationClientProtocol protocol = getClientRMProxyForSubCluster(subClusterId);
1062-
Method method = ApplicationClientProtocol.class.
1063-
getMethod(request.getMethodName(), request.getTypes());
1064-
return clazz.cast(method.invoke(protocol, request.getParams()));
1065-
} catch (YarnException | NoSuchMethodException |
1066-
IllegalAccessException | InvocationTargetException ex) {
1067-
throw new RuntimeException(ex);
1068-
}
1069-
}).collect(Collectors.toList());
1070-
}
1071-
10721058
@Override
10731059
public GetNodesToLabelsResponse getNodeToLabels(
10741060
GetNodesToLabelsRequest request) throws YarnException, IOException {
@@ -1081,8 +1067,7 @@ public GetNodesToLabelsResponse getNodeToLabels(
10811067
new Class[] {GetNodesToLabelsRequest.class}, new Object[] {request});
10821068
Collection<GetNodesToLabelsResponse> clusterNodes = null;
10831069
try {
1084-
clusterNodes = invokeAppClientProtocolMethod(true, remoteMethod,
1085-
GetNodesToLabelsResponse.class);
1070+
clusterNodes = invokeConcurrent(remoteMethod, GetNodesToLabelsResponse.class);
10861071
} catch (Exception ex) {
10871072
routerMetrics.incrNodeToLabelsFailedRetrieved();
10881073
RouterServerUtil.logAndThrowException("Unable to get node label due to exception.", ex);
@@ -1105,8 +1090,7 @@ public GetLabelsToNodesResponse getLabelsToNodes(
11051090
new Class[] {GetLabelsToNodesRequest.class}, new Object[] {request});
11061091
Collection<GetLabelsToNodesResponse> labelNodes = null;
11071092
try {
1108-
labelNodes = invokeAppClientProtocolMethod(true, remoteMethod,
1109-
GetLabelsToNodesResponse.class);
1093+
labelNodes = invokeConcurrent(remoteMethod, GetLabelsToNodesResponse.class);
11101094
} catch (Exception ex) {
11111095
routerMetrics.incrLabelsToNodesFailedRetrieved();
11121096
RouterServerUtil.logAndThrowException("Unable to get label node due to exception.", ex);
@@ -1129,8 +1113,7 @@ public GetClusterNodeLabelsResponse getClusterNodeLabels(
11291113
new Class[] {GetClusterNodeLabelsRequest.class}, new Object[] {request});
11301114
Collection<GetClusterNodeLabelsResponse> nodeLabels = null;
11311115
try {
1132-
nodeLabels = invokeAppClientProtocolMethod(true, remoteMethod,
1133-
GetClusterNodeLabelsResponse.class);
1116+
nodeLabels = invokeConcurrent(remoteMethod, GetClusterNodeLabelsResponse.class);
11341117
} catch (Exception ex) {
11351118
routerMetrics.incrClusterNodeLabelsFailedRetrieved();
11361119
RouterServerUtil.logAndThrowException("Unable to get cluster nodeLabels due to exception.",
@@ -1542,8 +1525,7 @@ public GetAllResourceProfilesResponse getResourceProfiles(
15421525
new Class[] {GetAllResourceProfilesRequest.class}, new Object[] {request});
15431526
Collection<GetAllResourceProfilesResponse> resourceProfiles = null;
15441527
try {
1545-
resourceProfiles = invokeAppClientProtocolMethod(true, remoteMethod,
1546-
GetAllResourceProfilesResponse.class);
1528+
resourceProfiles = invokeConcurrent(remoteMethod, GetAllResourceProfilesResponse.class);
15471529
} catch (Exception ex) {
15481530
routerMetrics.incrGetResourceProfilesFailedRetrieved();
15491531
RouterServerUtil.logAndThrowException("Unable to get resource profiles due to exception.",
@@ -1567,8 +1549,7 @@ public GetResourceProfileResponse getResourceProfile(
15671549
new Class[] {GetResourceProfileRequest.class}, new Object[] {request});
15681550
Collection<GetResourceProfileResponse> resourceProfile = null;
15691551
try {
1570-
resourceProfile = invokeAppClientProtocolMethod(true, remoteMethod,
1571-
GetResourceProfileResponse.class);
1552+
resourceProfile = invokeConcurrent(remoteMethod, GetResourceProfileResponse.class);
15721553
} catch (Exception ex) {
15731554
routerMetrics.incrGetResourceProfileFailedRetrieved();
15741555
RouterServerUtil.logAndThrowException("Unable to get resource profile due to exception.",
@@ -1591,8 +1572,7 @@ public GetAllResourceTypeInfoResponse getResourceTypeInfo(
15911572
new Class[] {GetAllResourceTypeInfoRequest.class}, new Object[] {request});
15921573
Collection<GetAllResourceTypeInfoResponse> listResourceTypeInfo;
15931574
try {
1594-
listResourceTypeInfo = invokeAppClientProtocolMethod(true, remoteMethod,
1595-
GetAllResourceTypeInfoResponse.class);
1575+
listResourceTypeInfo = invokeConcurrent(remoteMethod, GetAllResourceTypeInfoResponse.class);
15961576
} catch (Exception ex) {
15971577
routerMetrics.incrResourceTypeInfoFailedRetrieved();
15981578
LOG.error("Unable to get all resource type info node due to exception.", ex);
@@ -1623,8 +1603,7 @@ public GetAttributesToNodesResponse getAttributesToNodes(
16231603
new Class[] {GetAttributesToNodesRequest.class}, new Object[] {request});
16241604
Collection<GetAttributesToNodesResponse> attributesToNodesResponses = null;
16251605
try {
1626-
attributesToNodesResponses = invokeAppClientProtocolMethod(true, remoteMethod,
1627-
GetAttributesToNodesResponse.class);
1606+
attributesToNodesResponses = invokeConcurrent(remoteMethod, GetAttributesToNodesResponse.class);
16281607
} catch (Exception ex) {
16291608
routerMetrics.incrGetAttributesToNodesFailedRetrieved();
16301609
RouterServerUtil.logAndThrowException("Unable to get attributes to nodes due to exception.",
@@ -1647,7 +1626,7 @@ public GetClusterNodeAttributesResponse getClusterNodeAttributes(
16471626
new Class[] {GetClusterNodeAttributesRequest.class}, new Object[] {request});
16481627
Collection<GetClusterNodeAttributesResponse> clusterNodeAttributesResponses = null;
16491628
try {
1650-
clusterNodeAttributesResponses = invokeAppClientProtocolMethod(true, remoteMethod,
1629+
clusterNodeAttributesResponses = invokeConcurrent(remoteMethod,
16511630
GetClusterNodeAttributesResponse.class);
16521631
} catch (Exception ex) {
16531632
routerMetrics.incrGetClusterNodeAttributesFailedRetrieved();
@@ -1672,7 +1651,7 @@ public GetNodesToAttributesResponse getNodesToAttributes(
16721651
new Class[] {GetNodesToAttributesRequest.class}, new Object[] {request});
16731652
Collection<GetNodesToAttributesResponse> nodesToAttributesResponses = null;
16741653
try {
1675-
nodesToAttributesResponses = invokeAppClientProtocolMethod(true, remoteMethod,
1654+
nodesToAttributesResponses = invokeConcurrent(remoteMethod,
16761655
GetNodesToAttributesResponse.class);
16771656
} catch (Exception ex) {
16781657
routerMetrics.incrGetNodesToAttributesFailedRetrieved();

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestFederationClientInterceptor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Set;
3131
import java.util.stream.Collectors;
3232
import java.util.Arrays;
33+
import java.util.Collection;
3334

3435
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
3536
import org.apache.hadoop.test.LambdaTestUtils;
@@ -582,9 +583,9 @@ public void testGetClusterMetricsRequest() throws Exception {
582583
ClientMethod remoteMethod = new ClientMethod("getClusterMetrics",
583584
new Class[] {GetClusterMetricsRequest.class},
584585
new Object[] {GetClusterMetricsRequest.newInstance()});
585-
Map<SubClusterId, GetClusterMetricsResponse> clusterMetrics = interceptor.
586-
invokeConcurrent(new ArrayList<>(), remoteMethod, GetClusterMetricsResponse.class);
587-
Assert.assertTrue(clusterMetrics.isEmpty());
586+
Collection<GetClusterMetricsResponse> clusterMetrics = interceptor.
587+
invokeConcurrent(remoteMethod, GetClusterMetricsResponse.class);
588+
Assert.assertTrue(!clusterMetrics.isEmpty());
588589
}
589590

590591
/**

0 commit comments

Comments
 (0)