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