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    package org.apache.hadoop.mapreduce;
019    
020    import java.io.DataInput;
021    import java.io.DataOutput;
022    import java.io.IOException;
023    import java.util.ArrayList;
024    import java.util.List;
025    import java.util.Properties;
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    
033    /**
034     * Class that contains the information regarding the Job Queues which are 
035     * maintained by the Hadoop Map/Reduce framework.
036     * 
037     */
038    @InterfaceAudience.Public
039    @InterfaceStability.Evolving
040    public class QueueInfo implements Writable {
041    
042      private String queueName = "";
043      
044      //The scheduling Information object is read back as String.
045      //Once the scheduling information is set there is no way to recover it.
046      private String schedulingInfo; 
047      
048      private QueueState queueState;
049      
050      // Jobs submitted to the queue
051      private JobStatus[] stats;
052      
053      private List<QueueInfo> children;
054    
055      private Properties props;
056    
057      /**
058       * Default constructor for QueueInfo.
059       * 
060       */
061      public QueueInfo() {
062        // make it running by default.
063        this.queueState = QueueState.RUNNING;
064        children = new ArrayList<QueueInfo>();
065        props = new Properties();
066      }
067      
068      /**
069       * Construct a new QueueInfo object using the queue name and the
070       * scheduling information passed.
071       * 
072       * @param queueName Name of the job queue
073       * @param schedulingInfo Scheduling Information associated with the job
074       * queue
075       */
076      public QueueInfo(String queueName, String schedulingInfo) {
077        this();
078        this.queueName = queueName;
079        this.schedulingInfo = schedulingInfo;
080      }
081      
082      /**
083       * 
084       * @param queueName
085       * @param schedulingInfo
086       * @param state
087       * @param stats
088       */
089      public QueueInfo(String queueName, String schedulingInfo, QueueState state,
090                       JobStatus[] stats) {
091        this(queueName, schedulingInfo);
092        this.queueState = state;
093        this.stats = stats;
094      }
095    
096      /**
097       * Set the queue name of the JobQueueInfo
098       * 
099       * @param queueName Name of the job queue.
100       */
101      protected void setQueueName(String queueName) {
102        this.queueName = queueName;
103      }
104    
105      /**
106       * Get the queue name from JobQueueInfo
107       * 
108       * @return queue name
109       */
110      public String getQueueName() {
111        return queueName;
112      }
113    
114      /**
115       * Set the scheduling information associated to particular job queue
116       * 
117       * @param schedulingInfo
118       */
119      protected void setSchedulingInfo(String schedulingInfo) {
120        this.schedulingInfo = schedulingInfo;
121      }
122    
123      /**
124       * Gets the scheduling information associated to particular job queue.
125       * If nothing is set would return <b>"N/A"</b>
126       * 
127       * @return Scheduling information associated to particular Job Queue
128       */
129      public String getSchedulingInfo() {
130        if(schedulingInfo != null) {
131          return schedulingInfo;
132        }else {
133          return "N/A";
134        }
135      }
136      
137      /**
138       * Set the state of the queue
139       * @param state state of the queue.
140       */
141      protected void setState(QueueState state) {
142        queueState = state;
143      }
144      
145      /**
146       * Return the queue state
147       * @return the queue state.
148       */
149      public QueueState getState() {
150        return queueState;
151      }
152      
153      protected void setJobStatuses(JobStatus[] stats) {
154        this.stats = stats;
155      }
156    
157      /** 
158       * Get immediate children.
159       * 
160       * @return list of QueueInfo
161       */
162      public List<QueueInfo> getQueueChildren() {
163        return children;
164      }
165    
166      protected void setQueueChildren(List<QueueInfo> children) {
167        this.children =  children; 
168      }
169    
170      /**
171       * Get properties.
172       * 
173       * @return Properties
174       */
175      public Properties getProperties() {
176        return props;
177      }
178    
179      protected void setProperties(Properties props) {
180        this.props = props;
181      }
182    
183      /**
184       * Get the jobs submitted to queue
185       * @return list of JobStatus for the submitted jobs
186       */
187      public JobStatus[] getJobStatuses() {
188        return stats;
189      }
190      
191      @Override
192      public void readFields(DataInput in) throws IOException {
193        queueName = Text.readString(in);
194        queueState = WritableUtils.readEnum(in, QueueState.class);
195        schedulingInfo = Text.readString(in);
196        int length = in.readInt();
197        stats = new JobStatus[length];
198        for (int i = 0; i < length; i++) {
199          stats[i] = new JobStatus();
200          stats[i].readFields(in);
201        }
202        int count = in.readInt();
203        children.clear();
204        for (int i = 0; i < count; i++) {
205          QueueInfo childQueueInfo = new QueueInfo();
206          childQueueInfo.readFields(in);
207          children.add(childQueueInfo);
208        }
209      }
210    
211      @Override
212      public void write(DataOutput out) throws IOException {
213        Text.writeString(out, queueName);
214        WritableUtils.writeEnum(out, queueState);
215        
216        if(schedulingInfo!= null) {
217          Text.writeString(out, schedulingInfo);
218        }else {
219          Text.writeString(out, "N/A");
220        }
221        out.writeInt(stats.length);
222        for (JobStatus stat : stats) {
223          stat.write(out);
224        }
225        out.writeInt(children.size());
226        for(QueueInfo childQueueInfo : children) {
227          childQueueInfo.write(out);
228        }
229      }
230    }