2727import java .util .Collections ;
2828import java .util .Arrays ;
2929import java .util .concurrent .atomic .AtomicInteger ;
30+ import java .util .concurrent .ConcurrentHashMap ;
31+ import java .util .stream .Collectors ;
3032
3133import javax .servlet .http .HttpServletRequest ;
3234import javax .servlet .http .HttpServletResponse ;
5860import org .apache .hadoop .yarn .exceptions .ApplicationNotFoundException ;
5961import org .apache .hadoop .yarn .exceptions .YarnException ;
6062import org .apache .hadoop .yarn .server .federation .store .records .SubClusterId ;
63+ import org .apache .hadoop .yarn .server .resourcemanager .RMContext ;
64+ import org .apache .hadoop .yarn .server .resourcemanager .rmapp .RMApp ;
65+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .ActiveUsersManager ;
66+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .ResourceScheduler ;
67+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .SchedulerNode ;
68+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .activities .ActivitiesManager ;
69+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .activities .ActivitiesLogger ;
70+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .activities .ActivityDiagnosticConstant ;
71+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .activities .ActivityState ;
72+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .activities .ActivityLevel ;
73+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .capacity .LeafQueue ;
74+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .capacity .TestUtils ;
75+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .common .fica .FiCaSchedulerApp ;
76+ import org .apache .hadoop .yarn .server .resourcemanager .scheduler .common .fica .FiCaSchedulerNode ;
6177import org .apache .hadoop .yarn .server .resourcemanager .webapp .NodeIDsInfo ;
6278import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .NodeLabelsInfo ;
6379import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .LabelsToNodesInfo ;
7894import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .AppTimeoutsInfo ;
7995import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .AppPriority ;
8096import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .AppQueue ;
97+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .StatisticsItemInfo ;
98+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .ApplicationStatisticsInfo ;
99+ import org .apache .hadoop .yarn .server .resourcemanager .webapp .dao .AppActivitiesInfo ;
100+ import org .apache .hadoop .yarn .server .scheduler .SchedulerRequestKey ;
81101import org .apache .hadoop .yarn .server .webapp .dao .AppAttemptInfo ;
82102import org .apache .hadoop .yarn .server .webapp .dao .ContainerInfo ;
83103import org .apache .hadoop .yarn .server .webapp .dao .ContainersInfo ;
104+ import org .apache .hadoop .yarn .util .SystemClock ;
105+ import org .apache .hadoop .yarn .util .resource .Resources ;
84106import org .apache .hadoop .yarn .webapp .NotFoundException ;
107+ import org .mockito .Mockito ;
85108import org .slf4j .Logger ;
86109import org .slf4j .LoggerFactory ;
87110
111+ import static org .mockito .Mockito .mock ;
112+
88113/**
89114 * This class mocks the RESTRequestInterceptor.
90115 */
@@ -132,8 +157,9 @@ public Response submitApplication(ApplicationSubmissionContextInfo newApp,
132157 // Initialize appReport
133158 ApplicationReport appReport = ApplicationReport .newInstance (
134159 appId , ApplicationAttemptId .newInstance (appId , 1 ), null , newApp .getQueue (), null , null , 0 ,
135- null , YarnApplicationState .ACCEPTED , "" , null , 0 , 0 , null , null , null , 0 , null , null , null ,
136- false , Priority .newInstance (newApp .getPriority ()), null , null );
160+ null , YarnApplicationState .ACCEPTED , "" , null , 0 , 0 , null , null , null , 0 ,
161+ newApp .getApplicationType (), null , null , false , Priority .newInstance (newApp .getPriority ()),
162+ null , null );
137163
138164 // Initialize appTimeoutsMap
139165 HashMap <ApplicationTimeoutType , ApplicationTimeout > appTimeoutsMap = new HashMap <>();
@@ -661,4 +687,105 @@ public Response updateAppQueue(AppQueue targetQueue, HttpServletRequest hsr, Str
661687 AppQueue targetAppQueue = new AppQueue (targetQueue .getQueue ());
662688 return Response .status (Status .OK ).entity (targetAppQueue ).build ();
663689 }
690+
691+ public void updateApplicationState (YarnApplicationState appState , String appId )
692+ throws AuthorizationException , YarnException , InterruptedException , IOException {
693+ validateRunning ();
694+ ApplicationId applicationId = ApplicationId .fromString (appId );
695+ if (!applicationMap .containsKey (applicationId )) {
696+ throw new NotFoundException ("app with id: " + appId + " not found" );
697+ }
698+ ApplicationReport appReport = applicationMap .get (applicationId );
699+ appReport .setYarnApplicationState (appState );
700+ }
701+
702+ @ Override
703+ public ApplicationStatisticsInfo getAppStatistics (
704+ HttpServletRequest hsr , Set <String > stateQueries , Set <String > typeQueries ) {
705+ if (!isRunning ) {
706+ throw new RuntimeException ("RM is stopped" );
707+ }
708+
709+ Map <String , StatisticsItemInfo > itemInfoMap = new HashMap <>();
710+
711+ for (ApplicationReport appReport : applicationMap .values ()) {
712+
713+ YarnApplicationState appState = appReport .getYarnApplicationState ();
714+ String appType = appReport .getApplicationType ();
715+
716+ if (stateQueries .contains (appState .name ()) && typeQueries .contains (appType )) {
717+ String itemInfoMapKey = appState .toString () + "_" + appType ;
718+ StatisticsItemInfo itemInfo = itemInfoMap .getOrDefault (itemInfoMapKey , null );
719+ if (itemInfo == null ) {
720+ itemInfo = new StatisticsItemInfo (appState , appType , 1 );
721+ } else {
722+ long newCount = itemInfo .getCount () + 1 ;
723+ itemInfo .setCount (newCount );
724+ }
725+ itemInfoMap .put (itemInfoMapKey , itemInfo );
726+ }
727+ }
728+
729+ return new ApplicationStatisticsInfo (itemInfoMap .values ());
730+ }
731+
732+ @ Override
733+ public AppActivitiesInfo getAppActivities (
734+ HttpServletRequest hsr , String appId , String time , Set <String > requestPriorities ,
735+ Set <String > allocationRequestIds , String groupBy , String limit , Set <String > actions ,
736+ boolean summarize ) {
737+ if (!isRunning ) {
738+ throw new RuntimeException ("RM is stopped" );
739+ }
740+
741+ ApplicationId applicationId = ApplicationId .fromString (appId );
742+ if (!applicationMap .containsKey (applicationId )) {
743+ throw new NotFoundException ("app with id: " + appId + " not found" );
744+ }
745+
746+ SchedulerNode schedulerNode = TestUtils .getMockNode ("host0" , "rack" , 1 , 10240 );
747+
748+ RMContext rmContext = Mockito .mock (RMContext .class );
749+ Mockito .when (rmContext .getYarnConfiguration ()).thenReturn (this .getConf ());
750+ ResourceScheduler scheduler = Mockito .mock (ResourceScheduler .class );
751+ Mockito .when (scheduler .getMinimumResourceCapability ()).thenReturn (Resources .none ());
752+ Mockito .when (rmContext .getScheduler ()).thenReturn (scheduler );
753+ LeafQueue mockQueue = Mockito .mock (LeafQueue .class );
754+ Map <ApplicationId , RMApp > rmApps = new ConcurrentHashMap <>();
755+ Mockito .doReturn (rmApps ).when (rmContext ).getRMApps ();
756+
757+ FiCaSchedulerNode node = (FiCaSchedulerNode ) schedulerNode ;
758+ ApplicationAttemptId appAttemptId = ApplicationAttemptId .newInstance (applicationId , 0 );
759+ RMApp mockApp = Mockito .mock (RMApp .class );
760+ Mockito .doReturn (appAttemptId .getApplicationId ()).when (mockApp ).getApplicationId ();
761+ Mockito .doReturn (FinalApplicationStatus .UNDEFINED ).when (mockApp ).getFinalApplicationStatus ();
762+ rmApps .put (appAttemptId .getApplicationId (), mockApp );
763+ FiCaSchedulerApp app = new FiCaSchedulerApp (appAttemptId , "user" , mockQueue ,
764+ mock (ActiveUsersManager .class ), rmContext );
765+
766+ ActivitiesManager newActivitiesManager = new ActivitiesManager (rmContext );
767+ newActivitiesManager .turnOnAppActivitiesRecording (app .getApplicationId (), 3 );
768+
769+ int numActivities = 10 ;
770+ for (int i = 0 ; i < numActivities ; i ++) {
771+ ActivitiesLogger .APP .startAppAllocationRecording (newActivitiesManager , node ,
772+ SystemClock .getInstance ().getTime (), app );
773+ ActivitiesLogger .APP .recordAppActivityWithoutAllocation (newActivitiesManager , node , app ,
774+ new SchedulerRequestKey (Priority .newInstance (0 ), 0 , null ),
775+ ActivityDiagnosticConstant .NODE_IS_BLACKLISTED , ActivityState .REJECTED ,
776+ ActivityLevel .NODE );
777+ ActivitiesLogger .APP .finishSkippedAppAllocationRecording (newActivitiesManager ,
778+ app .getApplicationId (), ActivityState .SKIPPED , ActivityDiagnosticConstant .EMPTY );
779+ }
780+
781+ Set <Integer > prioritiesInt =
782+ requestPriorities .stream ().map (pri -> Integer .parseInt (pri )).collect (Collectors .toSet ());
783+ Set <Long > allocationReqIds =
784+ allocationRequestIds .stream ().map (id -> Long .parseLong (id )).collect (Collectors .toSet ());
785+ AppActivitiesInfo appActivitiesInfo = newActivitiesManager .
786+ getAppActivitiesInfo (app .getApplicationId (), prioritiesInt , allocationReqIds , null ,
787+ Integer .parseInt (limit ), summarize , 3 );
788+
789+ return appActivitiesInfo ;
790+ }
664791}
0 commit comments