26
26
import java .util .Date ;
27
27
28
28
import com .google .gson .Gson ;
29
+ import org .apache .hadoop .conf .Configuration ;
29
30
import org .apache .hadoop .util .StringUtils ;
30
31
import org .apache .hadoop .yarn .server .federation .store .records .SubClusterId ;
31
32
import org .apache .hadoop .yarn .server .federation .store .records .SubClusterInfo ;
@@ -67,7 +68,7 @@ public void render(Block html) {
67
68
* @param capability metric json obtained from RM.
68
69
* @return ClusterMetricsInfo Object
69
70
*/
70
- private ClusterMetricsInfo getClusterMetricsInfo (String capability ) {
71
+ protected ClusterMetricsInfo getClusterMetricsInfo (String capability ) {
71
72
try {
72
73
if (capability != null && !capability .isEmpty ()) {
73
74
JSONJAXBContext jc = new JSONJAXBContext (
@@ -125,79 +126,10 @@ private void initHtmlPageFederation(Block html, boolean isEnabled) {
125
126
.__ ().__ ().tbody ();
126
127
127
128
try {
128
-
129
- // Sort the SubClusters
130
- List <SubClusterInfo > subclusters = getSubClusterInfoList ();
131
-
132
- for (SubClusterInfo subcluster : subclusters ) {
133
-
134
- Map <String , String > subclusterMap = new HashMap <>();
135
-
136
- // Prepare subCluster
137
- SubClusterId subClusterId = subcluster .getSubClusterId ();
138
- String subClusterIdText = subClusterId .getId ();
139
-
140
- // Prepare WebAppAddress
141
- String webAppAddress = subcluster .getRMWebServiceAddress ();
142
- String herfWebAppAddress = "" ;
143
- if (webAppAddress != null && !webAppAddress .isEmpty ()) {
144
- herfWebAppAddress =
145
- WebAppUtils .getHttpSchemePrefix (this .router .getConfig ()) + webAppAddress ;
146
- }
147
-
148
- // Prepare Capability
149
- String capability = subcluster .getCapability ();
150
- ClusterMetricsInfo subClusterInfo = getClusterMetricsInfo (capability );
151
-
152
- // Prepare LastStartTime & LastHeartBeat
153
- Date lastStartTime = new Date (subcluster .getLastStartTime ());
154
- Date lastHeartBeat = new Date (subcluster .getLastHeartBeat ());
155
-
156
- // Prepare Resource
157
- long totalMB = subClusterInfo .getTotalMB ();
158
- String totalMBDesc = StringUtils .byteDesc (totalMB * BYTES_IN_MB );
159
- long totalVirtualCores = subClusterInfo .getTotalVirtualCores ();
160
- String resources = String .format ("<memory:%s, vCores:%s>" , totalMBDesc , totalVirtualCores );
161
-
162
- // Prepare Node
163
- long totalNodes = subClusterInfo .getTotalNodes ();
164
- long activeNodes = subClusterInfo .getActiveNodes ();
165
- String nodes = String .format ("<totalNodes:%s, activeNodes:%s>" , totalNodes , activeNodes );
166
-
167
- // Prepare HTML Table
168
- String stateStyle = "color:#dc3545;font-weight:bolder" ;
169
- SubClusterState state = subcluster .getState ();
170
- if (SubClusterState .SC_RUNNING == state ) {
171
- stateStyle = "color:#28a745;font-weight:bolder" ;
172
- }
173
-
174
- tbody .tr ().$id (subClusterIdText )
175
- .td ().$class ("details-control" ).a (herfWebAppAddress , subClusterIdText ).__ ()
176
- .td ().$style (stateStyle ).__ (state .name ()).__ ()
177
- .td ().__ (lastStartTime ).__ ()
178
- .td ().__ (lastHeartBeat ).__ ()
179
- .td (resources )
180
- .td (nodes )
181
- .__ ();
182
-
183
- // Formatted memory information
184
- long allocatedMB = subClusterInfo .getAllocatedMB ();
185
- String allocatedMBDesc = StringUtils .byteDesc (allocatedMB * BYTES_IN_MB );
186
- long availableMB = subClusterInfo .getAvailableMB ();
187
- String availableMBDesc = StringUtils .byteDesc (availableMB * BYTES_IN_MB );
188
- long pendingMB = subClusterInfo .getPendingMB ();
189
- String pendingMBDesc = StringUtils .byteDesc (pendingMB * BYTES_IN_MB );
190
- long reservedMB = subClusterInfo .getReservedMB ();
191
- String reservedMBDesc = StringUtils .byteDesc (reservedMB * BYTES_IN_MB );
192
-
193
- subclusterMap .put ("totalmemory" , totalMBDesc );
194
- subclusterMap .put ("allocatedmemory" , allocatedMBDesc );
195
- subclusterMap .put ("availablememory" , availableMBDesc );
196
- subclusterMap .put ("pendingmemory" , pendingMBDesc );
197
- subclusterMap .put ("reservedmemory" , reservedMBDesc );
198
- subclusterMap .put ("subcluster" , subClusterId .getId ());
199
- subclusterMap .put ("capability" , capability );
200
- lists .add (subclusterMap );
129
+ if (isEnabled ) {
130
+ initSubClusterPage (tbody , lists );
131
+ } else {
132
+ initLocalClusterPage (tbody , lists );
201
133
}
202
134
} catch (Exception e ) {
203
135
LOG .error ("Cannot render Router Federation." , e );
@@ -210,4 +142,127 @@ private void initHtmlPageFederation(Block html, boolean isEnabled) {
210
142
tbody .__ ().__ ().div ().p ().$style ("color:red" )
211
143
.__ ("*The application counts are local per subcluster" ).__ ().__ ();
212
144
}
145
+
146
+ /**
147
+ * Initialize the Federation page of the local-cluster.
148
+ *
149
+ * @param tbody HTML tbody.
150
+ * @param lists subCluster page data list.
151
+ */
152
+ private void initLocalClusterPage (TBODY <TABLE <Hamlet >> tbody , List <Map <String , String >> lists ) {
153
+ Configuration config = this .router .getConfig ();
154
+ SubClusterInfo localCluster = getSubClusterInfoByLocalCluster (config );
155
+ if (localCluster != null ) {
156
+ try {
157
+ initSubClusterPageItem (tbody , localCluster , lists );
158
+ } catch (Exception e ) {
159
+ LOG .error ("init LocalCluster = {} page data error." , localCluster , e );
160
+ }
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Initialize the Federation page of the sub-cluster.
166
+ *
167
+ * @param tbody HTML tbody.
168
+ * @param lists subCluster page data list.
169
+ */
170
+ private void initSubClusterPage (TBODY <TABLE <Hamlet >> tbody , List <Map <String , String >> lists ) {
171
+ // Sort the SubClusters
172
+ List <SubClusterInfo > subClusters = getSubClusterInfoList ();
173
+
174
+ // Iterate through the sub-clusters and display data for each sub-cluster.
175
+ // If a sub-cluster cannot display data, skip it.
176
+ for (SubClusterInfo subCluster : subClusters ) {
177
+ try {
178
+ initSubClusterPageItem (tbody , subCluster , lists );
179
+ } catch (Exception e ) {
180
+ LOG .error ("init subCluster = {} page data error." , subCluster , e );
181
+ }
182
+ }
183
+ }
184
+
185
+ /**
186
+ * We will initialize the specific SubCluster's data within this method.
187
+ *
188
+ * @param tbody HTML TBody.
189
+ * @param subClusterInfo Sub-cluster information.
190
+ * @param lists Used to record data that needs to be displayed in JS.
191
+ */
192
+ private void initSubClusterPageItem (TBODY <TABLE <Hamlet >> tbody ,
193
+ SubClusterInfo subClusterInfo , List <Map <String , String >> lists ) {
194
+
195
+ Map <String , String > subClusterMap = new HashMap <>();
196
+
197
+ // Prepare subCluster
198
+ SubClusterId subClusterId = subClusterInfo .getSubClusterId ();
199
+ String subClusterIdText = subClusterId .getId ();
200
+
201
+ // Prepare WebAppAddress
202
+ String webAppAddress = subClusterInfo .getRMWebServiceAddress ();
203
+ String herfWebAppAddress = "" ;
204
+ if (webAppAddress != null && !webAppAddress .isEmpty ()) {
205
+ herfWebAppAddress =
206
+ WebAppUtils .getHttpSchemePrefix (this .router .getConfig ()) + webAppAddress ;
207
+ }
208
+
209
+ // Prepare Capability
210
+ String capability = subClusterInfo .getCapability ();
211
+ ClusterMetricsInfo subClusterMetricsInfo = getClusterMetricsInfo (capability );
212
+
213
+ if (subClusterMetricsInfo == null ) {
214
+ return ;
215
+ }
216
+
217
+ // Prepare LastStartTime & LastHeartBeat
218
+ Date lastStartTime = new Date (subClusterInfo .getLastStartTime ());
219
+ Date lastHeartBeat = new Date (subClusterInfo .getLastHeartBeat ());
220
+
221
+ // Prepare Resource
222
+ long totalMB = subClusterMetricsInfo .getTotalMB ();
223
+ String totalMBDesc = StringUtils .byteDesc (totalMB * BYTES_IN_MB );
224
+ long totalVirtualCores = subClusterMetricsInfo .getTotalVirtualCores ();
225
+ String resources = String .format ("<memory:%s, vCores:%s>" , totalMBDesc , totalVirtualCores );
226
+
227
+ // Prepare Node
228
+ long totalNodes = subClusterMetricsInfo .getTotalNodes ();
229
+ long activeNodes = subClusterMetricsInfo .getActiveNodes ();
230
+ String nodes = String .format ("<totalNodes:%s, activeNodes:%s>" , totalNodes , activeNodes );
231
+
232
+ // Prepare HTML Table
233
+ String stateStyle = "color:#dc3545;font-weight:bolder" ;
234
+ SubClusterState state = subClusterInfo .getState ();
235
+ if (SubClusterState .SC_RUNNING == state ) {
236
+ stateStyle = "color:#28a745;font-weight:bolder" ;
237
+ }
238
+
239
+ tbody .tr ().$id (subClusterIdText )
240
+ .td ().$class ("details-control" ).a (herfWebAppAddress , subClusterIdText ).__ ()
241
+ .td ().$style (stateStyle ).__ (state .name ()).__ ()
242
+ .td ().__ (lastStartTime ).__ ()
243
+ .td ().__ (lastHeartBeat ).__ ()
244
+ .td (resources )
245
+ .td (nodes )
246
+ .__ ();
247
+
248
+ // Formatted memory information
249
+ long allocatedMB = subClusterMetricsInfo .getAllocatedMB ();
250
+ String allocatedMBDesc = StringUtils .byteDesc (allocatedMB * BYTES_IN_MB );
251
+ long availableMB = subClusterMetricsInfo .getAvailableMB ();
252
+ String availableMBDesc = StringUtils .byteDesc (availableMB * BYTES_IN_MB );
253
+ long pendingMB = subClusterMetricsInfo .getPendingMB ();
254
+ String pendingMBDesc = StringUtils .byteDesc (pendingMB * BYTES_IN_MB );
255
+ long reservedMB = subClusterMetricsInfo .getReservedMB ();
256
+ String reservedMBDesc = StringUtils .byteDesc (reservedMB * BYTES_IN_MB );
257
+
258
+ subClusterMap .put ("totalmemory" , totalMBDesc );
259
+ subClusterMap .put ("allocatedmemory" , allocatedMBDesc );
260
+ subClusterMap .put ("availablememory" , availableMBDesc );
261
+ subClusterMap .put ("pendingmemory" , pendingMBDesc );
262
+ subClusterMap .put ("reservedmemory" , reservedMBDesc );
263
+ subClusterMap .put ("subcluster" , subClusterId .getId ());
264
+ subClusterMap .put ("capability" , capability );
265
+ lists .add (subClusterMap );
266
+ }
267
+
213
268
}
0 commit comments