1919package org .apache .hadoop .yarn .server .webproxy ;
2020
2121import java .io .IOException ;
22+ import org .apache .hadoop .classification .VisibleForTesting ;
2223import org .apache .hadoop .conf .Configuration ;
23- import org .apache .hadoop .ipc .RPC ;
24- import org .apache .hadoop .yarn .api .ApplicationClientProtocol ;
2524import org .apache .hadoop .yarn .api .ApplicationHistoryProtocol ;
26- import org .apache .hadoop .yarn .api .protocolrecords .GetApplicationReportRequest ;
2725import org .apache .hadoop .yarn .api .records .ApplicationId ;
2826import org .apache .hadoop .yarn .api .records .ApplicationReport ;
2927import org .apache .hadoop .yarn .client .AHSProxy ;
30- import org .apache .hadoop .yarn .client .ClientRMProxy ;
3128import org .apache .hadoop .yarn .conf .YarnConfiguration ;
32- import org .apache .hadoop .yarn .exceptions .ApplicationNotFoundException ;
3329import org .apache .hadoop .yarn .exceptions .YarnException ;
3430import org .apache .hadoop .yarn .exceptions .YarnRuntimeException ;
3531import org .apache .hadoop .yarn .factories .RecordFactory ;
3632import org .apache .hadoop .yarn .factory .providers .RecordFactoryProvider ;
33+ import org .apache .hadoop .yarn .util .StringHelper ;
34+ import org .apache .hadoop .yarn .webapp .util .WebAppUtils ;
3735
3836/**
3937 * This class abstracts away how ApplicationReports are fetched.
4038 */
41- public class AppReportFetcher {
42- enum AppReportSource { RM , AHS }
39+ public abstract class AppReportFetcher {
40+
41+ protected enum AppReportSource {RM , AHS }
42+
4343 private final Configuration conf ;
44- private final ApplicationClientProtocol applicationsManager ;
45- private final ApplicationHistoryProtocol historyManager ;
46- private final RecordFactory recordFactory = RecordFactoryProvider .getRecordFactory (null );
44+ private ApplicationHistoryProtocol historyManager ;
45+ private String ahsAppPageUrlBase ;
46+ private final RecordFactory recordFactory = RecordFactoryProvider
47+ .getRecordFactory (null );
4748 private boolean isAHSEnabled ;
4849
49- /**
50- * Create a new Connection to the RM/Application History Server
51- * to fetch Application reports.
52- * @param conf the conf to use to know where the RM is.
53- */
5450 public AppReportFetcher (Configuration conf ) {
51+ this .conf = conf ;
5552 if (conf .getBoolean (YarnConfiguration .APPLICATION_HISTORY_ENABLED ,
5653 YarnConfiguration .DEFAULT_APPLICATION_HISTORY_ENABLED )) {
57- isAHSEnabled = true ;
54+ this .isAHSEnabled = true ;
55+ this .ahsAppPageUrlBase =
56+ StringHelper .pjoin (WebAppUtils .getHttpSchemePrefix (conf )
57+ + WebAppUtils .getAHSWebAppURLWithoutScheme (conf ),
58+ "applicationhistory" , "app" );
5859 }
59- this .conf = conf ;
6060 try {
61- applicationsManager = ClientRMProxy .createRMProxy (conf ,
62- ApplicationClientProtocol .class );
63- if (isAHSEnabled ) {
64- historyManager = getAHSProxy (conf );
61+ if (this .isAHSEnabled ) {
62+ this .historyManager = getAHSProxy (conf );
6563 } else {
6664 this .historyManager = null ;
6765 }
6866 } catch (IOException e ) {
6967 throw new YarnRuntimeException (e );
7068 }
7169 }
72-
73- /**
74- * Create a direct connection to RM instead of a remote connection when
75- * the proxy is running as part of the RM. Also create a remote connection to
76- * Application History Server if it is enabled.
77- * @param conf the configuration to use
78- * @param applicationsManager what to use to get the RM reports.
79- */
80- public AppReportFetcher (Configuration conf , ApplicationClientProtocol applicationsManager ) {
81- if (conf .getBoolean (YarnConfiguration .APPLICATION_HISTORY_ENABLED ,
82- YarnConfiguration .DEFAULT_APPLICATION_HISTORY_ENABLED )) {
83- isAHSEnabled = true ;
84- }
85- this .conf = conf ;
86- this .applicationsManager = applicationsManager ;
87- if (isAHSEnabled ) {
88- try {
89- historyManager = getAHSProxy (conf );
90- } catch (IOException e ) {
91- throw new YarnRuntimeException (e );
92- }
93- } else {
94- this .historyManager = null ;
95- }
96- }
9770
9871 protected ApplicationHistoryProtocol getAHSProxy (Configuration configuration )
9972 throws IOException {
10073 return AHSProxy .createAHSProxy (configuration ,
101- ApplicationHistoryProtocol .class ,
102- configuration .getSocketAddr (YarnConfiguration .TIMELINE_SERVICE_ADDRESS ,
103- YarnConfiguration .DEFAULT_TIMELINE_SERVICE_ADDRESS ,
104- YarnConfiguration .DEFAULT_TIMELINE_SERVICE_PORT ));
74+ ApplicationHistoryProtocol .class ,
75+ configuration .getSocketAddr (YarnConfiguration .TIMELINE_SERVICE_ADDRESS ,
76+ YarnConfiguration .DEFAULT_TIMELINE_SERVICE_ADDRESS ,
77+ YarnConfiguration .DEFAULT_TIMELINE_SERVICE_PORT ));
10578 }
10679
10780 /**
@@ -112,46 +85,46 @@ protected ApplicationHistoryProtocol getAHSProxy(Configuration configuration)
11285 * @throws YarnException on any error.
11386 * @throws IOException
11487 */
115- public FetchedAppReport getApplicationReport (ApplicationId appId )
116- throws YarnException , IOException {
117- GetApplicationReportRequest request = recordFactory
118- .newRecordInstance (GetApplicationReportRequest .class );
119- request .setApplicationId (appId );
120-
121- ApplicationReport appReport ;
122- FetchedAppReport fetchedAppReport ;
123- try {
124- appReport = applicationsManager .
125- getApplicationReport (request ).getApplicationReport ();
126- fetchedAppReport = new FetchedAppReport (appReport , AppReportSource .RM );
127- } catch (ApplicationNotFoundException e ) {
128- if (!isAHSEnabled ) {
129- // Just throw it as usual if historyService is not enabled.
130- throw e ;
131- }
132- //Fetch the application report from AHS
133- appReport = historyManager .
134- getApplicationReport (request ).getApplicationReport ();
135- fetchedAppReport = new FetchedAppReport (appReport , AppReportSource .AHS );
136- }
137- return fetchedAppReport ;
88+ public abstract FetchedAppReport getApplicationReport (ApplicationId appId )
89+ throws YarnException , IOException ;
90+
91+ public abstract String getRmAppPageUrlBase (ApplicationId appId )
92+ throws IOException , YarnException ;
93+
94+ public String getAhsAppPageUrlBase () {
95+ return this .ahsAppPageUrlBase ;
13896 }
13997
140- public void stop () {
141- if (this .applicationsManager != null ) {
142- RPC .stopProxy (this .applicationsManager );
143- }
144- if (this .historyManager != null ) {
145- RPC .stopProxy (this .historyManager );
146- }
98+ public abstract void stop ();
99+
100+ protected Configuration getConf () {
101+ return this .conf ;
102+ }
103+
104+ protected ApplicationHistoryProtocol getHistoryManager () {
105+ return this .historyManager ;
106+ }
107+
108+ protected RecordFactory getRecordFactory () {
109+ return this .recordFactory ;
110+ }
111+
112+ protected boolean isAHSEnabled () {
113+ return this .isAHSEnabled ;
114+ }
115+
116+ @ VisibleForTesting
117+ public void setHistoryManager (
118+ ApplicationHistoryProtocol historyManager ) {
119+ this .historyManager = historyManager ;
147120 }
148121
149122 /*
150123 * This class creates a bundle of the application report and the source from
151124 * where the the report was fetched. This allows the WebAppProxyServlet
152125 * to make decisions for the application report based on the source.
153126 */
154- static class FetchedAppReport {
127+ protected static class FetchedAppReport {
155128 private ApplicationReport appReport ;
156129 private AppReportSource appReportSource ;
157130
0 commit comments