3232import org .apache .hadoop .security .UserGroupInformation ;
3333import org .apache .hadoop .test .GenericTestUtils ;
3434import org .apache .hadoop .util .Time ;
35+ import org .apache .hadoop .yarn .api .records .NodeLabel ;
3536import org .apache .hadoop .yarn .conf .YarnConfiguration ;
3637import org .apache .hadoop .yarn .exceptions .YarnException ;
3738import org .apache .hadoop .yarn .server .federation .store .FederationStateStore ;
3839import org .apache .hadoop .yarn .server .federation .store .records .GetSubClustersInfoRequest ;
3940import org .apache .hadoop .yarn .server .federation .store .records .GetSubClustersInfoResponse ;
4041import org .apache .hadoop .yarn .server .federation .store .records .SubClusterInfo ;
4142import org .apache .hadoop .yarn .server .federation .utils .FederationStateStoreFacade ;
43+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .ApplicationSubmissionContextInfo ;
44+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .NewApplication ;
45+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .NodeInfo ;
46+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .NodesInfo ;
47+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .NewReservation ;
48+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .NodeLabelsInfo ;
49+ import org .apache .hadoop .yarn .server .router .webapp .HTTPMethods ;
4250import org .apache .hadoop .yarn .server .router .webapp .JavaProcess ;
4351import org .slf4j .Logger ;
4452import org .slf4j .LoggerFactory ;
4856import java .security .PrivilegedExceptionAction ;
4957import java .util .LinkedList ;
5058import java .util .List ;
59+ import java .util .ArrayList ;
5160import java .util .concurrent .TimeoutException ;
61+ import java .util .regex .Matcher ;
62+ import java .util .regex .Pattern ;
5263
5364import static javax .servlet .http .HttpServletResponse .SC_OK ;
65+ import static javax .ws .rs .core .MediaType .APPLICATION_JSON ;
5466import static javax .ws .rs .core .MediaType .APPLICATION_XML ;
5567import static org .apache .hadoop .yarn .server .resourcemanager .webapp .RMWSConsts .RM_WEB_SERVICE_PATH ;
68+ import static org .apache .hadoop .yarn .server .resourcemanager .webapp .RMWSConsts .NODES ;
69+ import static org .apache .hadoop .yarn .server .resourcemanager .webapp .RMWSConsts .APPS_NEW_APPLICATION ;
70+ import static org .apache .hadoop .yarn .server .resourcemanager .webapp .RMWSConsts .APPS ;
71+ import static org .apache .hadoop .yarn .server .resourcemanager .webapp .RMWSConsts .RESERVATION_NEW ;
72+ import static org .apache .hadoop .yarn .server .resourcemanager .webapp .RMWSConsts .ADD_NODE_LABELS ;
5673import static org .apache .hadoop .yarn .server .router .webapp .TestRouterWebServicesREST .waitWebAppRunning ;
5774import static org .junit .Assert .assertEquals ;
5875
@@ -190,6 +207,8 @@ public static <T> T performGetCalls(final String routerAddress, final String pat
190207 final String queryValue ) throws IOException , InterruptedException {
191208
192209 Client clientToRouter = Client .create ();
210+ clientToRouter .setReadTimeout (5000 );
211+ clientToRouter .setConnectTimeout (5000 );
193212 WebResource toRouter = clientToRouter .resource (routerAddress ).path (path );
194213
195214 final WebResource .Builder toRouterBuilder ;
@@ -207,4 +226,120 @@ public static <T> T performGetCalls(final String routerAddress, final String pat
207226 return response .getEntity (returnType );
208227 });
209228 }
229+
230+ public static ClientResponse performCall (final String routerAddress , final String webAddress ,
231+ final String queryKey , final String queryValue , final Object context ,
232+ final HTTPMethods method ) throws IOException , InterruptedException {
233+
234+ return UserGroupInformation .createRemoteUser (userName ).doAs (
235+ (PrivilegedExceptionAction <ClientResponse >) () -> {
236+ Client clientToRouter = Client .create ();
237+ WebResource toRouter = clientToRouter .resource (routerAddress ).path (webAddress );
238+
239+ WebResource toRouterWR = toRouter ;
240+ if (queryKey != null && queryValue != null ) {
241+ toRouterWR = toRouterWR .queryParam (queryKey , queryValue );
242+ }
243+
244+ WebResource .Builder builder ;
245+ if (context != null ) {
246+ builder = toRouterWR .entity (context , APPLICATION_JSON );
247+ builder = builder .accept (APPLICATION_JSON );
248+ } else {
249+ builder = toRouterWR .accept (APPLICATION_JSON );
250+ }
251+
252+ ClientResponse response = null ;
253+
254+ switch (method ) {
255+ case DELETE :
256+ response = builder .delete (ClientResponse .class );
257+ break ;
258+ case POST :
259+ response = builder .post (ClientResponse .class );
260+ break ;
261+ case PUT :
262+ response = builder .put (ClientResponse .class );
263+ break ;
264+ default :
265+ break ;
266+ }
267+
268+ return response ;
269+ });
270+ }
271+
272+ public String getNodeId (String rmAddress ) {
273+ Client clientToRM = Client .create ();
274+ clientToRM .setConnectTimeout (3000 );
275+ clientToRM .setReadTimeout (3000 );
276+ WebResource toRM = clientToRM .resource (rmAddress ).path (RM_WEB_SERVICE_PATH + NODES );
277+ ClientResponse response =
278+ toRM .accept (APPLICATION_XML ).get (ClientResponse .class );
279+ NodesInfo ci = response .getEntity (NodesInfo .class );
280+ List <NodeInfo > nodes = ci .getNodes ();
281+ if (nodes .isEmpty ()) {
282+ return null ;
283+ }
284+ clientToRM .destroy ();
285+ return nodes .get (0 ).getNodeId ();
286+ }
287+
288+ public NewApplication getNewApplicationId (String routerAddress ) {
289+ Client clientToRM = Client .create ();
290+ clientToRM .setConnectTimeout (3000 );
291+ clientToRM .setReadTimeout (3000 );
292+ WebResource toRM = clientToRM .resource (routerAddress ).path (
293+ RM_WEB_SERVICE_PATH + APPS_NEW_APPLICATION );
294+ ClientResponse response = toRM .accept (APPLICATION_XML ).post (ClientResponse .class );
295+ clientToRM .destroy ();
296+ return response .getEntity (NewApplication .class );
297+ }
298+
299+ public String submitApplication (String routerAddress ) {
300+ ApplicationSubmissionContextInfo context = new ApplicationSubmissionContextInfo ();
301+ String appId = getNewApplicationId (routerAddress ).getApplicationId ();
302+ context .setApplicationId (appId );
303+ Client clientToRouter = Client .create ();
304+ clientToRouter .setConnectTimeout (3000 );
305+ clientToRouter .setReadTimeout (3000 );
306+ WebResource toRM = clientToRouter .resource (routerAddress ).path (
307+ RM_WEB_SERVICE_PATH + APPS );
308+ toRM .entity (context , APPLICATION_XML ).accept (APPLICATION_XML ).post (ClientResponse .class );
309+ clientToRouter .destroy ();
310+ return appId ;
311+ }
312+
313+ public NewReservation getNewReservationId (String routerAddress ) {
314+ Client clientToRM = Client .create ();
315+ clientToRM .setConnectTimeout (3000 );
316+ clientToRM .setReadTimeout (3000 );
317+ WebResource toRM = clientToRM .resource (routerAddress ).
318+ path (RM_WEB_SERVICE_PATH + RESERVATION_NEW );
319+ ClientResponse response = toRM .accept (APPLICATION_XML ).post (ClientResponse .class );
320+ return response .getEntity (NewReservation .class );
321+ }
322+
323+ public String addNodeLabel (String routerAddress ) {
324+ Client clientToRM = Client .create ();
325+ clientToRM .setConnectTimeout (3000 );
326+ clientToRM .setReadTimeout (3000 );
327+ WebResource toRM = clientToRM .resource (routerAddress )
328+ .path (RM_WEB_SERVICE_PATH + ADD_NODE_LABELS );
329+ List <NodeLabel > nodeLabels = new ArrayList <>();
330+ nodeLabels .add (NodeLabel .newInstance ("default" ));
331+ NodeLabelsInfo context = new NodeLabelsInfo (nodeLabels );
332+ ClientResponse response = toRM
333+ .entity (context , APPLICATION_XML )
334+ .accept (APPLICATION_XML )
335+ .post (ClientResponse .class );
336+ return response .getEntity (String .class );
337+ }
338+
339+ public static String format (String format , Object ... args ) {
340+ Pattern p = Pattern .compile ("\\ {.*?}" );
341+ Matcher m = p .matcher (format );
342+ String newFormat = m .replaceAll ("%s" );
343+ return String .format (newFormat , args );
344+ }
210345}
0 commit comments