001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.mapred;
020    
021    import java.io.DataInput;
022    import java.io.DataOutput;
023    import java.io.IOException;
024    import java.util.ArrayList;
025    import java.util.Collection;
026    
027    import org.apache.hadoop.classification.InterfaceAudience;
028    import org.apache.hadoop.classification.InterfaceStability;
029    import org.apache.hadoop.io.Text;
030    import org.apache.hadoop.io.Writable;
031    import org.apache.hadoop.io.WritableUtils;
032    import org.apache.hadoop.mapreduce.ClusterMetrics;
033    import org.apache.hadoop.mapreduce.TaskTrackerInfo;
034    import org.apache.hadoop.mapreduce.Cluster.JobTrackerStatus;
035    
036    /**
037     * Status information on the current state of the Map-Reduce cluster.
038     * 
039     * <p><code>ClusterStatus</code> provides clients with information such as:
040     * <ol>
041     *   <li>
042     *   Size of the cluster. 
043     *   </li>
044     *   <li>
045     *   Name of the trackers. 
046     *   </li>
047     *   <li>
048     *   Task capacity of the cluster. 
049     *   </li>
050     *   <li>
051     *   The number of currently running map & reduce tasks.
052     *   </li>
053     *   <li>
054     *   State of the <code>JobTracker</code>.
055     *   </li>
056     *   <li>
057     *   Details regarding black listed trackers.
058     *   </li>
059     * </ol></p>
060     * 
061     * <p>Clients can query for the latest <code>ClusterStatus</code>, via 
062     * {@link JobClient#getClusterStatus()}.</p>
063     * 
064     * @see JobClient
065     * @deprecated  Use {@link ClusterMetrics} or {@link TaskTrackerInfo} instead
066     */
067    @Deprecated
068    @InterfaceAudience.Public
069    @InterfaceStability.Stable
070    public class ClusterStatus implements Writable {
071      /**
072       * Class which encapsulates information about a blacklisted tasktracker.
073       *  
074       * The information includes the tasktracker's name and reasons for
075       * getting blacklisted. The toString method of the class will print
076       * the information in a whitespace separated fashion to enable parsing.
077       */
078      public static class BlackListInfo implements Writable {
079    
080        private String trackerName;
081    
082        private String reasonForBlackListing;
083        
084        private String blackListReport;
085        
086        BlackListInfo() {
087        }
088        
089    
090        /**
091         * Gets the blacklisted tasktracker's name.
092         * 
093         * @return tracker's name.
094         */
095        public String getTrackerName() {
096          return trackerName;
097        }
098    
099        /**
100         * Gets the reason for which the tasktracker was blacklisted.
101         * 
102         * @return reason which tracker was blacklisted
103         */
104        public String getReasonForBlackListing() {
105          return reasonForBlackListing;
106        }
107    
108        /**
109         * Sets the blacklisted tasktracker's name.
110         * 
111         * @param trackerName of the tracker.
112         */
113        void setTrackerName(String trackerName) {
114          this.trackerName = trackerName;
115        }
116    
117        /**
118         * Sets the reason for which the tasktracker was blacklisted.
119         * 
120         * @param reasonForBlackListing
121         */
122        void setReasonForBlackListing(String reasonForBlackListing) {
123          this.reasonForBlackListing = reasonForBlackListing;
124        }
125    
126        /**
127         * Gets a descriptive report about why the tasktracker was blacklisted.
128         * 
129         * @return report describing why the tasktracker was blacklisted.
130         */
131        public String getBlackListReport() {
132          return blackListReport;
133        }
134    
135        /**
136         * Sets a descriptive report about why the tasktracker was blacklisted.
137         * @param blackListReport report describing why the tasktracker 
138         *                        was blacklisted.
139         */
140        void setBlackListReport(String blackListReport) {
141          this.blackListReport = blackListReport;
142        }
143    
144        @Override
145        public void readFields(DataInput in) throws IOException {
146          trackerName = Text.readString(in);
147          reasonForBlackListing = Text.readString(in);
148          blackListReport = Text.readString(in);
149        }
150    
151        @Override
152        public void write(DataOutput out) throws IOException {
153          Text.writeString(out, trackerName);
154          Text.writeString(out, reasonForBlackListing);
155          Text.writeString(out, blackListReport);
156        }
157    
158        @Override
159        /**
160         * Print information related to the blacklisted tasktracker in a
161         * whitespace separated fashion.
162         * 
163         * The method changes any newlines in the report describing why
164         * the tasktracker was blacklisted to a ':' for enabling better
165         * parsing.
166         */
167        public String toString() {
168          StringBuilder sb = new StringBuilder();
169          sb.append(trackerName);
170          sb.append("\t");
171          sb.append(reasonForBlackListing);
172          sb.append("\t");
173          sb.append(blackListReport.replace("\n", ":"));
174          return sb.toString();
175        }
176        
177      }
178      
179      private int numActiveTrackers;
180      private Collection<String> activeTrackers = new ArrayList<String>();
181      private int numBlacklistedTrackers;
182      private int numExcludedNodes;
183      private long ttExpiryInterval;
184      private int map_tasks;
185      private int reduce_tasks;
186      private int max_map_tasks;
187      private int max_reduce_tasks;
188      private JobTrackerStatus status;
189      private Collection<BlackListInfo> blacklistedTrackersInfo =
190        new ArrayList<BlackListInfo>();
191    
192      ClusterStatus() {}
193      
194      /**
195       * Construct a new cluster status.
196       * 
197       * @param trackers no. of tasktrackers in the cluster
198       * @param blacklists no of blacklisted task trackers in the cluster
199       * @param ttExpiryInterval the tasktracker expiry interval
200       * @param maps no. of currently running map-tasks in the cluster
201       * @param reduces no. of currently running reduce-tasks in the cluster
202       * @param maxMaps the maximum no. of map tasks in the cluster
203       * @param maxReduces the maximum no. of reduce tasks in the cluster
204       * @param status the {@link JobTrackerStatus} of the <code>JobTracker</code>
205       */
206      ClusterStatus(int trackers, int blacklists, long ttExpiryInterval, 
207                    int maps, int reduces,
208                    int maxMaps, int maxReduces, JobTrackerStatus status) {
209        this(trackers, blacklists, ttExpiryInterval, maps, reduces, maxMaps, 
210             maxReduces, status, 0);
211      }
212    
213      /**
214       * Construct a new cluster status.
215       * 
216       * @param trackers no. of tasktrackers in the cluster
217       * @param blacklists no of blacklisted task trackers in the cluster
218       * @param ttExpiryInterval the tasktracker expiry interval
219       * @param maps no. of currently running map-tasks in the cluster
220       * @param reduces no. of currently running reduce-tasks in the cluster
221       * @param maxMaps the maximum no. of map tasks in the cluster
222       * @param maxReduces the maximum no. of reduce tasks in the cluster
223       * @param status the {@link JobTrackerStatus} of the <code>JobTracker</code>
224       * @param numDecommissionedNodes number of decommission trackers
225       */
226      ClusterStatus(int trackers, int blacklists, long ttExpiryInterval, 
227                    int maps, int reduces, int maxMaps, int maxReduces, 
228                    JobTrackerStatus status, int numDecommissionedNodes) {
229        numActiveTrackers = trackers;
230        numBlacklistedTrackers = blacklists;
231        this.numExcludedNodes = numDecommissionedNodes;
232        this.ttExpiryInterval = ttExpiryInterval;
233        map_tasks = maps;
234        reduce_tasks = reduces;
235        max_map_tasks = maxMaps;
236        max_reduce_tasks = maxReduces;
237        this.status = status;
238      }
239    
240      /**
241       * Construct a new cluster status.
242       * 
243       * @param activeTrackers active tasktrackers in the cluster
244       * @param blacklistedTrackers blacklisted tasktrackers in the cluster
245       * @param ttExpiryInterval the tasktracker expiry interval
246       * @param maps no. of currently running map-tasks in the cluster
247       * @param reduces no. of currently running reduce-tasks in the cluster
248       * @param maxMaps the maximum no. of map tasks in the cluster
249       * @param maxReduces the maximum no. of reduce tasks in the cluster
250       * @param status the {@link JobTrackerStatus} of the <code>JobTracker</code>
251       */
252      ClusterStatus(Collection<String> activeTrackers, 
253          Collection<BlackListInfo> blacklistedTrackers,
254          long ttExpiryInterval,
255          int maps, int reduces, int maxMaps, int maxReduces, 
256          JobTrackerStatus status) {
257        this(activeTrackers, blacklistedTrackers, ttExpiryInterval, maps, reduces, 
258             maxMaps, maxReduces, status, 0);
259      }
260    
261    
262      /**
263       * Construct a new cluster status.
264       * 
265       * @param activeTrackers active tasktrackers in the cluster
266       * @param blackListedTrackerInfo blacklisted tasktrackers information 
267       * in the cluster
268       * @param ttExpiryInterval the tasktracker expiry interval
269       * @param maps no. of currently running map-tasks in the cluster
270       * @param reduces no. of currently running reduce-tasks in the cluster
271       * @param maxMaps the maximum no. of map tasks in the cluster
272       * @param maxReduces the maximum no. of reduce tasks in the cluster
273       * @param status the {@link JobTrackerStatus} of the <code>JobTracker</code>
274       * @param numDecommissionNodes number of decommission trackers
275       */
276      
277      ClusterStatus(Collection<String> activeTrackers,
278          Collection<BlackListInfo> blackListedTrackerInfo, long ttExpiryInterval,
279          int maps, int reduces, int maxMaps, int maxReduces,
280          JobTrackerStatus status, int numDecommissionNodes) {
281        this(activeTrackers.size(), blackListedTrackerInfo.size(),
282            ttExpiryInterval, maps, reduces, maxMaps, maxReduces, status,
283            numDecommissionNodes);
284        this.activeTrackers = activeTrackers;
285        this.blacklistedTrackersInfo = blackListedTrackerInfo;
286      }
287    
288      /**
289       * Get the number of task trackers in the cluster.
290       * 
291       * @return the number of task trackers in the cluster.
292       */
293      public int getTaskTrackers() {
294        return numActiveTrackers;
295      }
296      
297      /**
298       * Get the names of task trackers in the cluster.
299       * 
300       * @return the active task trackers in the cluster.
301       */
302      public Collection<String> getActiveTrackerNames() {
303        return activeTrackers;
304      }
305    
306      /**
307       * Get the names of task trackers in the cluster.
308       * 
309       * @return the blacklisted task trackers in the cluster.
310       */
311      public Collection<String> getBlacklistedTrackerNames() {
312        ArrayList<String> blacklistedTrackers = new ArrayList<String>();
313        for(BlackListInfo bi : blacklistedTrackersInfo) {
314          blacklistedTrackers.add(bi.getTrackerName());
315        }
316        return blacklistedTrackers;
317      }
318      
319      /**
320       * Get the number of blacklisted task trackers in the cluster.
321       * 
322       * @return the number of blacklisted task trackers in the cluster.
323       */
324      public int getBlacklistedTrackers() {
325        return numBlacklistedTrackers;
326      }
327      
328      /**
329       * Get the number of excluded hosts in the cluster.
330       * @return the number of excluded hosts in the cluster.
331       */
332      public int getNumExcludedNodes() {
333        return numExcludedNodes;
334      }
335      
336      /**
337       * Get the tasktracker expiry interval for the cluster
338       * @return the expiry interval in msec
339       */
340      public long getTTExpiryInterval() {
341        return ttExpiryInterval;
342      }
343      
344      /**
345       * Get the number of currently running map tasks in the cluster.
346       * 
347       * @return the number of currently running map tasks in the cluster.
348       */
349      public int getMapTasks() {
350        return map_tasks;
351      }
352      
353      /**
354       * Get the number of currently running reduce tasks in the cluster.
355       * 
356       * @return the number of currently running reduce tasks in the cluster.
357       */
358      public int getReduceTasks() {
359        return reduce_tasks;
360      }
361      
362      /**
363       * Get the maximum capacity for running map tasks in the cluster.
364       * 
365       * @return the maximum capacity for running map tasks in the cluster.
366       */
367      public int getMaxMapTasks() {
368        return max_map_tasks;
369      }
370    
371      /**
372       * Get the maximum capacity for running reduce tasks in the cluster.
373       * 
374       * @return the maximum capacity for running reduce tasks in the cluster.
375       */
376      public int getMaxReduceTasks() {
377        return max_reduce_tasks;
378      }
379      
380      /**
381       * Get the JobTracker's status.
382       * 
383       * @return {@link JobTrackerStatus} of the JobTracker
384       */
385      public JobTrackerStatus getJobTrackerStatus() {
386        return status;
387      }
388    
389      /**
390       * Gets the list of blacklisted trackers along with reasons for blacklisting.
391       * 
392       * @return the collection of {@link BlackListInfo} objects. 
393       * 
394       */
395      public Collection<BlackListInfo> getBlackListedTrackersInfo() {
396        return blacklistedTrackersInfo;
397      }
398    
399      public void write(DataOutput out) throws IOException {
400        if (activeTrackers.size() == 0) {
401          out.writeInt(numActiveTrackers);
402          out.writeInt(0);
403        } else {
404          out.writeInt(activeTrackers.size());
405          out.writeInt(activeTrackers.size());
406          for (String tracker : activeTrackers) {
407            Text.writeString(out, tracker);
408          }
409        }
410        if (blacklistedTrackersInfo.size() == 0) {
411          out.writeInt(numBlacklistedTrackers);
412          out.writeInt(blacklistedTrackersInfo.size());
413        } else {
414          out.writeInt(blacklistedTrackersInfo.size());
415          out.writeInt(blacklistedTrackersInfo.size());
416          for (BlackListInfo tracker : blacklistedTrackersInfo) {
417            tracker.write(out);
418          }
419        }
420        out.writeInt(numExcludedNodes);
421        out.writeLong(ttExpiryInterval);
422        out.writeInt(map_tasks);
423        out.writeInt(reduce_tasks);
424        out.writeInt(max_map_tasks);
425        out.writeInt(max_reduce_tasks);
426        WritableUtils.writeEnum(out, status);
427      }
428    
429      public void readFields(DataInput in) throws IOException {
430        numActiveTrackers = in.readInt();
431        int numTrackerNames = in.readInt();
432        if (numTrackerNames > 0) {
433          for (int i = 0; i < numTrackerNames; i++) {
434            String name = Text.readString(in);
435            activeTrackers.add(name);
436          }
437        }
438        numBlacklistedTrackers = in.readInt();
439        int blackListTrackerInfoSize = in.readInt();
440        if(blackListTrackerInfoSize > 0) {
441          for (int i = 0; i < blackListTrackerInfoSize; i++) {
442            BlackListInfo info = new BlackListInfo();
443            info.readFields(in);
444            blacklistedTrackersInfo.add(info);
445          }
446        }
447        numExcludedNodes = in.readInt();
448        ttExpiryInterval = in.readLong();
449        map_tasks = in.readInt();
450        reduce_tasks = in.readInt();
451        max_map_tasks = in.readInt();
452        max_reduce_tasks = in.readInt();
453        status = WritableUtils.readEnum(in, JobTrackerStatus.class);
454      }
455    }