Skip to content

Commit 5f47f09

Browse files
authored
YARN-11537. [Addendum][Federation] Router CLI Supports List SubClusterPolicyConfiguration Of Queues. (#6121) Contributed by Shilun Fan.
Reviewed-by: Inigo Goiri <inigoiri@apache.org> Signed-off-by: Shilun Fan <slfan1989@apache.org>
1 parent 8931393 commit 5f47f09

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/rmadmin/FederationRMAdminInterceptor.java

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void init(String userName) {
130130
ThreadFactory threadFactory = new ThreadFactoryBuilder()
131131
.setNameFormat("RPC Router RMAdminClient-" + userName + "-%d ").build();
132132

133-
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
133+
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
134134
this.executorService = new ThreadPoolExecutor(numThreads, numThreads,
135135
0L, TimeUnit.MILLISECONDS, workQueue, threadFactory);
136136

@@ -1032,7 +1032,7 @@ public QueryFederationQueuePoliciesResponse listFederationQueuePolicies(
10321032
}
10331033

10341034
try {
1035-
QueryFederationQueuePoliciesResponse response = null;
1035+
QueryFederationQueuePoliciesResponse response;
10361036

10371037
long startTime = clock.getTime();
10381038
String queue = request.getQueue();
@@ -1056,9 +1056,15 @@ public QueryFederationQueuePoliciesResponse listFederationQueuePolicies(
10561056
// We filter by pagination.
10571057
response = filterPoliciesConfigurationsByQueues(queues, policiesConfigurations,
10581058
pageSize, currentPage);
1059+
} else {
1060+
// If we don't have any filtering criteria, we should also support paginating the results.
1061+
response = filterPoliciesConfigurations(policiesConfigurations, pageSize, currentPage);
10591062
}
10601063
long stopTime = clock.getTime();
10611064
routerMetrics.succeededListFederationQueuePoliciesRetrieved(stopTime - startTime);
1065+
if (response == null) {
1066+
response = QueryFederationQueuePoliciesResponse.newInstance();
1067+
}
10621068
return response;
10631069
} catch (Exception e) {
10641070
routerMetrics.incrListFederationQueuePoliciesFailedRetrieved();
@@ -1137,12 +1143,75 @@ private QueryFederationQueuePoliciesResponse filterPoliciesConfigurationsByQueue
11371143
}
11381144
}
11391145

1146+
// Step3. To paginate the returned results.
1147+
return queryFederationQueuePoliciesPagination(federationQueueWeights, pageSize, currentPage);
1148+
}
1149+
1150+
/**
1151+
* Filter PoliciesConfigurations, and we paginate Policies within this method.
1152+
*
1153+
* @param policiesConfigurations policy configurations.
1154+
* @param pageSize Items per page.
1155+
* @param currentPage The number of pages to be queried.
1156+
* @return federation queue policies response.
1157+
* @throws YarnException indicates exceptions from yarn servers.
1158+
*/
1159+
private QueryFederationQueuePoliciesResponse filterPoliciesConfigurations(
1160+
Map<String, SubClusterPolicyConfiguration> policiesConfigurations,
1161+
int pageSize, int currentPage) throws YarnException {
1162+
1163+
// Step1. Check the parameters, if the policy list is empty, return empty directly.
1164+
if (MapUtils.isEmpty(policiesConfigurations)) {
1165+
return null;
1166+
}
1167+
1168+
// Step2. Traverse policiesConfigurations and obtain the FederationQueueWeight list.
1169+
List<FederationQueueWeight> federationQueueWeights = new ArrayList<>();
1170+
for (Map.Entry<String, SubClusterPolicyConfiguration> entry :
1171+
policiesConfigurations.entrySet()) {
1172+
String queue = entry.getKey();
1173+
SubClusterPolicyConfiguration policyConf = entry.getValue();
1174+
if (policyConf == null) {
1175+
continue;
1176+
}
1177+
FederationQueueWeight federationQueueWeight = parseFederationQueueWeight(queue, policyConf);
1178+
if (federationQueueWeight != null) {
1179+
federationQueueWeights.add(federationQueueWeight);
1180+
}
1181+
}
1182+
1183+
// Step3. To paginate the returned results.
1184+
return queryFederationQueuePoliciesPagination(federationQueueWeights, pageSize, currentPage);
1185+
}
1186+
1187+
/**
1188+
* Pagination for FederationQueuePolicies.
1189+
*
1190+
* @param queueWeights List Of FederationQueueWeight.
1191+
* @param pageSize Items per page.
1192+
* @param currentPage The number of pages to be queried.
1193+
* @return federation queue policies response.
1194+
* @throws YarnException indicates exceptions from yarn servers.
1195+
*/
1196+
private QueryFederationQueuePoliciesResponse queryFederationQueuePoliciesPagination(
1197+
List<FederationQueueWeight> queueWeights, int pageSize, int currentPage)
1198+
throws YarnException {
1199+
if (CollectionUtils.isEmpty(queueWeights)) {
1200+
return null;
1201+
}
1202+
11401203
int startIndex = (currentPage - 1) * pageSize;
1141-
int endIndex = Math.min(startIndex + pageSize, federationQueueWeights.size());
1204+
int endIndex = Math.min(startIndex + pageSize, queueWeights.size());
1205+
1206+
if (startIndex > endIndex) {
1207+
throw new YarnException("The index of the records to be retrieved " +
1208+
"has exceeded the maximum index.");
1209+
}
1210+
11421211
List<FederationQueueWeight> subFederationQueueWeights =
1143-
federationQueueWeights.subList(startIndex, endIndex);
1212+
queueWeights.subList(startIndex, endIndex);
11441213

1145-
int totalSize = federationQueueWeights.size();
1214+
int totalSize = queueWeights.size();
11461215
int totalPage =
11471216
(totalSize % pageSize == 0) ? totalSize / pageSize : (totalSize / pageSize) + 1;
11481217

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/rmadmin/TestFederationRMAdminInterceptor.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,5 +965,28 @@ public void testFilterPoliciesConfigurationsByQueues() throws Exception {
965965
List<FederationQueueWeight> federationQueueWeights6 = response6.getFederationQueueWeights();
966966
assertNotNull(federationQueueWeights6);
967967
assertEquals(1, federationQueueWeights6.size());
968+
969+
// Queue7: We design such a test case, we do not set any filter conditions,
970+
// but we need to get the return results
971+
QueryFederationQueuePoliciesRequest request7 =
972+
QueryFederationQueuePoliciesRequest.newInstance(10, 1, null, null);
973+
QueryFederationQueuePoliciesResponse response7 =
974+
interceptor.listFederationQueuePolicies(request7);
975+
assertNotNull(response7);
976+
assertEquals(1, response7.getCurrentPage());
977+
assertEquals(10, response7.getPageSize());
978+
assertEquals(3, response7.getTotalPage());
979+
assertEquals(26, response7.getTotalSize());
980+
List<FederationQueueWeight> federationQueueWeights7 = response7.getFederationQueueWeights();
981+
assertNotNull(federationQueueWeights7);
982+
assertEquals(10, federationQueueWeights7.size());
983+
984+
// Queue8: We are designing a unit test where the number of records
985+
// we need to retrieve exceeds the maximum number of Policies available.
986+
QueryFederationQueuePoliciesRequest request8 =
987+
QueryFederationQueuePoliciesRequest.newInstance(10, 10, null, null);
988+
LambdaTestUtils.intercept(YarnException.class,
989+
"The index of the records to be retrieved has exceeded the maximum index.",
990+
() -> interceptor.listFederationQueuePolicies(request8));
968991
}
969992
}

0 commit comments

Comments
 (0)