1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.mapred;
19 
20 import java.io.IOException;
21 
22 import org.apache.hadoop.conf.Configured;
23 import org.apache.hadoop.security.UserGroupInformation;
24 import org.apache.hadoop.util.Tool;
25 import org.apache.hadoop.util.ToolRunner;
26 /**
27  * <code>JobQueueClient</code> is interface provided to the user in order
28  * to get JobQueue related information from the {@link JobTracker}
29  *
30  * It provides the facility to list the JobQueues present and ability to
31  * view the list of jobs within a specific JobQueue
32  *
33 **/
34 
35 class JobQueueClient extends Configured implements  Tool {
36 
37   JobClient jc;
38 
JobQueueClient()39   public JobQueueClient() {
40   }
41 
JobQueueClient(JobConf conf)42   public JobQueueClient(JobConf conf) throws IOException {
43     setConf(conf);
44   }
45 
init(JobConf conf)46   private void init(JobConf conf) throws IOException {
47     setConf(conf);
48     jc = new JobClient(conf);
49   }
50 
51   @Override
run(String[] argv)52   public int run(String[] argv) throws Exception {
53     int exitcode = -1;
54 
55     if(argv.length < 1){
56       displayUsage("");
57       return exitcode;
58     }
59     String cmd = argv[0];
60     boolean displayQueueList = false;
61     boolean displayQueueInfoWithJobs = false;
62     boolean displayQueueInfoWithoutJobs = false;
63     boolean displayQueueAclsInfoForCurrentUser = false;
64 
65     if("-list".equals(cmd)){
66       displayQueueList = true;
67     }else if("-showacls".equals(cmd)) {
68       displayQueueAclsInfoForCurrentUser = true;
69     }else if("-info".equals(cmd)){
70       if(argv.length == 2 && !(argv[1].equals("-showJobs"))) {
71         displayQueueInfoWithoutJobs = true;
72       } else if(argv.length == 3){
73         if(argv[2].equals("-showJobs")){
74           displayQueueInfoWithJobs = true;
75         }else {
76           displayUsage(cmd);
77           return exitcode;
78         }
79       }else {
80         displayUsage(cmd);
81         return exitcode;
82       }
83     } else {
84       displayUsage(cmd);
85       return exitcode;
86     }
87     JobConf conf = new JobConf(getConf());
88     init(conf);
89     if (displayQueueList) {
90       displayQueueList();
91       exitcode = 0;
92     } else if (displayQueueInfoWithoutJobs){
93       displayQueueInfo(argv[1],false);
94       exitcode = 0;
95     } else if (displayQueueInfoWithJobs) {
96       displayQueueInfo(argv[1],true);
97       exitcode = 0;
98     }else if (displayQueueAclsInfoForCurrentUser) {
99       this.displayQueueAclsInfoForCurrentUser();
100       exitcode = 0;
101     }
102 
103     return exitcode;
104   }
105 
106   /**
107    * Method used to display information pertaining to a Single JobQueue
108    * registered with the {@link QueueManager}. Display of the Jobs is
109    * determine by the boolean
110    *
111    * @throws IOException
112    */
113 
displayQueueInfo(String queue, boolean showJobs)114   private void displayQueueInfo(String queue, boolean showJobs)
115       throws IOException {
116     JobQueueInfo jobQueueInfo = jc.getQueueInfo(queue);
117     if (jobQueueInfo == null) {
118       System.out.println("Queue Name : " + queue +
119           " has no scheduling information");
120     } else {
121       printJobQueueInfo(jobQueueInfo);
122     }
123     if (showJobs) {
124       System.out.printf("Job List\n");
125       JobStatus[] jobs = jc.getJobsFromQueue(queue);
126       if (jobs == null)
127         jobs = new JobStatus[0];
128       jc.displayJobList(jobs);
129     }
130   }
131 
132   /**
133    * format and print information about the passed in job queue.
134    */
printJobQueueInfo(JobQueueInfo jobQueueInfo)135   private void printJobQueueInfo(JobQueueInfo jobQueueInfo) {
136     System.out.println("Queue Name : " + jobQueueInfo.getQueueName());
137     System.out.println("Queue State : " + jobQueueInfo.getQueueState());
138     String schedInfo = jobQueueInfo.getSchedulingInfo();
139     if (null == schedInfo || "".equals(schedInfo.trim())) {
140       schedInfo = JobQueueInfo.EMPTY_INFO;
141     }
142     System.out.println("Scheduling Info : " + schedInfo);
143   }
144 
145   /**
146    * Method used to display the list of the JobQueues registered
147    * with the {@link QueueManager}
148    *
149    * @throws IOException
150    */
displayQueueList()151   private void displayQueueList() throws IOException {
152     JobQueueInfo[] queues = jc.getQueues();
153     for (JobQueueInfo queue : queues) {
154       printJobQueueInfo(queue);
155     }
156   }
157 
displayQueueAclsInfoForCurrentUser()158   private void displayQueueAclsInfoForCurrentUser() throws IOException {
159     QueueAclsInfo[] queueAclsInfoList = jc.getQueueAclsForCurrentUser();
160     UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
161     if (queueAclsInfoList.length > 0) {
162       System.out.println("Queue acls for user :  "
163               + ugi.getShortUserName());
164       System.out.println("\nQueue  Operations");
165       System.out.println("=====================");
166       for (QueueAclsInfo queueInfo : queueAclsInfoList) {
167         System.out.print(queueInfo.getQueueName() + "  ");
168         String[] ops = queueInfo.getOperations();
169         int max = ops.length - 1;
170         for (int j = 0; j < ops.length; j++) {
171           System.out.print(ops[j].replaceFirst("acl-", ""));
172           if (j < max) {
173             System.out.print(",");
174           }
175         }
176         System.out.println();
177       }
178     } else {
179       System.out.println("User " +
180               ugi.getShortUserName() +
181               " does not have access to any queue. \n");
182     }
183   }
184 
displayUsage(String cmd)185   private void displayUsage(String cmd) {
186     String prefix = "Usage: JobQueueClient ";
187     if ("-queueinfo".equals(cmd)){
188       System.err.println(prefix + "[" + cmd + "<job-queue-name> [-showJobs]]");
189     }else {
190       System.err.printf(prefix + "<command> <args>\n");
191       System.err.printf("\t[-list]\n");
192       System.err.printf("\t[-info <job-queue-name> [-showJobs]]\n");
193       System.err.printf("\t[-showacls] \n\n");
194       ToolRunner.printGenericCommandUsage(System.out);
195     }
196   }
197 
main(String[] argv)198   public static void main(String[] argv) throws Exception {
199     int res = ToolRunner.run(new JobQueueClient(), argv);
200     System.exit(res);
201   }
202 
203 }
204