001    /*
002     * $RCSfile: Job.java,v $ 
003     *
004     * Created on August 28, 2003, 13:28 AM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    package gov.bnl.star.offline.scheduler;
024    
025    import gov.bnl.star.offline.scheduler.dataset.DatasetSubset;
026    import java.net.URL;
027    import java.util.*;
028    
029    import gov.bnl.star.offline.scheduler.request.Request;
030    import java.util.Properties;
031    import java.util.logging.Logger;
032    
033    /** Describes one of the processes to be dispatched with the underlying
034     * scheduler. Process are created not by calling the constructor, but by creating
035     * them within a ProcessList.
036     * @author Gabriele Carcassi
037     * @version $Revision: 1.16 $ $Date: 2006/11/21 00:41:32 $
038     */
039    public class Job implements java.io.Serializable{
040        static private Logger log = Logger.getLogger(Job.class.getName());
041        private Properties env = new Properties();
042        private List input;
043        private List output;
044        private String jobID;
045        private String target;
046        private String queue;
047        private URL stdin;
048        private URL stdout;
049        private URL stderr;
050        private String commandLine;
051        private DatasetSubset datasetSubset;
052        private Queue queueObj;
053        private AccessMethod accessMethod;
054        private LocalAccessPoint accessPoint;
055        private String cluster;
056        private Request request;
057        
058        
059        public void setDatasetSubset(DatasetSubset datasetSubset){this.datasetSubset = datasetSubset;}
060        public DatasetSubset getDatasetSubset(){return this.datasetSubset;}
061        
062        public void setAccessMethod(AccessMethod accessMethod){this.accessMethod = accessMethod;}
063        public AccessMethod getAccessMethod(){return accessMethod; }
064        
065        public void setAccessPoint(LocalAccessPoint accessPoint){this.accessPoint = accessPoint;}
066        public LocalAccessPoint getAccessPoint(){return this.accessPoint;}
067        
068    
069        
070        public void setRequest(Request request){
071            this.request = request;
072        }
073        
074        public Request getRequest(){
075            return request;
076        }
077        
078        //private Dispatcher dispatcher;
079        //public void setAssociatedDispatcher(Dispatcher dispatcher){
080        //      this.dispatcher = dispatcher;
081        //}
082        
083         public Dispatcher getAssociatedDispatcher(){//deprecated 
084              return accessMethod.getDispatcher();
085        }
086    
087         
088        public Job(){ 
089        }
090        
091        /** Creates a new instance of Process */
092        public Job(String jobID) {
093            // TODO: the processID and requestID needs to be set by the policy. This
094            // is temporary hack to get the process and the request from the jobID
095            if (jobID.indexOf('_') == -1) 
096                throw new RuntimeException(
097                    "jobID must be separated into two parts (needs underscore)");
098            this.jobID = jobID;
099        }
100    
101        /** Returns the environment variables to be set for the process.
102         * @return a set of properties defining the environment variable requested
103         */
104        public Properties getEnv() {
105            return env;
106        }
107    
108        /** Returns a String that uniquely describes the job.
109         * @return the job identification String
110         */
111        public String getJobID() {
112            return jobID;
113        }
114        
115        public void setJobID(String ID) {
116            this.jobID = ID;
117        }
118    
119        
120        public String getProcessID() {
121            // TODO This should really be set by the policy, and not reparsed
122            return jobID.substring(jobID.indexOf('_')+1);
123        }
124        
125        public String getRequestID() {
126            // TODO This should really be set by the policy, and not reparsed
127            return jobID.substring(0, jobID.indexOf('_'));
128        }
129    
130        /** Sets an file list describing all the files needed as inputs by the
131         * process.
132         * @param inputList the input file list of the process
133         */
134    //    public void setInput(List inputList) {
135    //        input = inputList;
136    //    }
137    
138        /** Returns the input file list required by the process.
139         * @return list of input files needed by the process
140         */
141        public List getInput() {
142            if(datasetSubset == null) return new ArrayList();
143            return datasetSubset.getPhysicalFileList();
144            //return input;
145        }
146    
147        /** Sets an file list describing all the files the process will produce as outputs.
148         * @param outputList the output file list of the process
149         */
150        public void setOutput(List outputList) {
151            output = outputList;
152        }
153    
154        /** Returns the output file list produced by the process.
155         * @return list of output files produced by the process
156         */
157        public List getOuput() {
158            return output;
159        }
160        
161        /** Returns the maximum memory needed by the process.
162         * @return maximum memory needed by the processs.
163         */
164        public int getMaxMemory() {
165            if (request == null) return -1; //for junt test
166            return request.getResource("Memory").getMax();
167        }
168        
169        /** Returns the minimum memory needed by the process.
170         * @return minimum memory needed by the processs.
171         */
172        public int getMinMemory() {
173            if (request == null) return -1; //for junt test
174            return request.getResource("Memory").getMin();
175        }
176        
177        /** Returns the minimum swap disk space needed by the process.
178         * @return minimum swap disk space needed by the processs.
179         */
180        public int getMinStorageSpace() {
181            if(request == null) return -1; //for junt test
182            return request.getResource("StorageSpace").getMin();
183            //return minStorageSpace;
184        }
185        
186        /** Returns the maximum swap disk space needed by the process.
187         * @return maximum swap disk space needed by the processs.
188         */
189        public int getMaxStorageSpace() {
190            //return maxStorageSpace;
191            if(request == null) return -1; //for junt test
192            return request.getResource("StorageSpace").getMax();
193        }
194    
195        /** Sets the target resource, typically a machine name, on which the process should
196         * be dispatched.
197         * @param target the resource to which the process should be dispatched
198         */
199    //    public void setTarget(String target) {
200    //        this.target = target;
201    //    }
202    
203        /** Returns the resource to which the process should be dispatched, typically a
204         * machine name.
205         * @return the resource to which the process should be dispatched.
206         */
207        public String getTarget() {
208            //return target;
209            if(datasetSubset == null) return null;
210            return datasetSubset.getNode();
211        }
212        
213        /** Sets the queue on which the job will be dispatched. If no queue is set,
214         * the dispatcher will use the default queue.
215         */
216        public void setQueue(String queue) { //today this is the old way of setting the queue it is still used it the junit tests.  
217            this.queue = queue;
218        }
219        
220    //    public void setQueue(Queue queue) {
221    //        queueObj = queue;
222    //        setQueue(queue.getName());
223    //        setCluster(queue.getCluster());
224    //        setAssociatedDispatcher(queue.getAssociatedDispatcher());    
225    //    }
226        
227        public void setQueueObj(Queue queue) { 
228            queueObj = queue;
229            setQueue(queue.getName());
230            setCluster(queue.getCluster());
231            //setAssociatedDispatcher(queue.getAssociatedDispatcher()); 
232        }
233        
234        public Queue getQueueObj() { return queueObj; }    
235      
236        /** Returns the queue name on which the process should be dispatched.
237         */
238        public String getQueue() {
239            return queue;
240        }
241    
242        /** Returns the location to which the standard output has to be redirected.
243         * @return an URL of the "file" protocol
244         */
245        public URL getStdout() {
246            return this.stdout;
247        }
248    
249        /** Sets the location to which the standard output has to be redirected.
250         * @param stdout an URL of the "file" protocol
251         */
252        public void setStdout(URL stdout) {
253            this.stdout = stdout;
254        }
255    
256        /** Returns the location from which the standard input will be read.
257         * @return an URL of the "file" protocol
258         */
259        public URL getStdin() {
260            return this.stdin;
261        }
262    
263        /** Sets the location from which the standard input will be reed.
264         * @param stdin an URL of the "file" protocol
265         */
266        public void setStdin(URL stdin) {
267            this.stdin = stdin;
268        }
269    
270        /** Returns the location to which the standard error has to be redirected.
271         * @return an URL of the "file" protocol
272         */
273        public URL getStderr() {
274            return this.stderr;
275        }
276    
277        /** Sets the location to which the standard error has to be redirected.
278         * @param stderr an URL of the "file" protocol
279         */
280        public void setStderr(URL stderr) {
281            this.stderr = stderr;
282        }
283        
284        /** Returns the commandline of the process, or null if the command line
285         * from the job should be used.
286         * @return the full command line or null if the on in the jobrequest should
287         * be used
288         */
289        public String getCommandline() {
290            // TODO it would be nicer if it would take the commandline from the job
291            // automatically if not defined
292            return commandLine;
293        }
294        
295        /** Changes the commandline of the process.
296         * @param commandLine the command line associated with the process
297         */
298        public void setCommandline(String commandLine) {
299            this.commandLine = commandLine;
300        }
301        
302        /** Getter for property cluster.
303         * @return Value of property cluster.
304         *
305         */
306        public String getCluster() {
307            return this.cluster;
308        }
309        
310        /** Setter for property cluster.
311         * @param cluster New value of property cluster.
312         *
313         */
314        public void setCluster(String cluster) {
315            this.cluster = cluster;
316        }
317        
318        
319        
320        
321        /*****************************************************************   
322         *  The following members and methods are filled post dispatch.  *
323         *****************************************************************/
324            
325          private List ProcesseIDs = new ArrayList();
326          
327          public void AddProcesseID(String ID){ProcesseIDs.add(ID); DispatchSuccessful();}
328          
329          public void clearProcesseIDs(){ProcesseIDs.clear();}
330          
331          public void setProcesseIDs(List ProcesseIDs){this.ProcesseIDs = ProcesseIDs;}
332          
333          public List getProcesseIDs(){return ProcesseIDs;}
334          
335          
336          //This group of items are used to make a note of if submission was successful or not.
337          private boolean dispatchSuccessful = false;
338          public void DispatchSuccessful(){
339              dispatchSuccessful = true;  
340              Date date = new Date();
341              setTimeOfDispatch(date.toString());
342          }
343          public void setDispatchSuccessful(boolean dispatchSuccessful){this.dispatchSuccessful = dispatchSuccessful;}
344          public boolean getDispatchSuccessful(){ return dispatchSuccessful;}
345          
346          
347          //This group of items are used to make note of submit time.
348          private String TimeOfDispatch = "Never Dispatched";
349          /** Accepts a string that represents the date and time a job was dispatched (in any format) (not validated).*/
350          public void setTimeOfDispatch(String TimeOfDispatch){ this.TimeOfDispatch = TimeOfDispatch;}
351          /** Returns a string that holds the data and time the job was dispatched. This is used for writing info to the log file*/
352          public String getTimeOfDispatch(){ return TimeOfDispatch; }
353          
354          //This group of items is used to note how long dispatching took 
355          private int dispatchTime = 0;
356          /** Sets how long it took to dispatch this job. This should only be set by the dispatcher*/
357          public void setDispatchTime(int dispatchTime){ this.dispatchTime = dispatchTime;}
358          /** Returns how long it took to dispatch this job in ms*/
359          public int getDispatchTime(){ return dispatchTime; }
360          
361          private String reportText = " ";
362          /**Concats a string plus a "\n" to report file. This will later be dumped into the .report file. */
363          public void addToReportText(String Text){ reportText = reportText.concat(Text).concat("\n"); }
364          /**Sets or resets the report text to any string value. Note: To add to the report text  use addToReportText() not SetReportText().*/
365          public void SetReportText(String Text){this.reportText = Text;}
366          /**Returns a report of events that happined to the job*/
367          public String GetReportText(){return reportText;}
368          
369          
370        
371    }