KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > lsf > LSFBSubProcess


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.core.process.lsf;
32
33 import org.objectweb.proactive.core.process.SimpleExternalProcess;
34 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
35 import org.objectweb.proactive.core.process.ExternalProcess;
36 import org.objectweb.proactive.core.process.MessageSink;
37 import org.objectweb.proactive.core.util.MessageLogger;
38
39 /**
40  * <p>
41  * The LSFBSubProcess class is able to start any class, of the ProActive library,
42  * on a cluster managed by LSF prtocol. An istance of this class can be coupled for instance with
43  * RlLoginProcess or SSHProcess classes in order to log into the cluster's front end with rlogin or
44  * ssh and then to run a job with LSFBSubProcess.
45  * </p>
46  * <p>
47  * For instance:
48  * </p><pre>
49  * ..............
50  * LSFBSubProcess lsf = new LSFBSubProcess(new SimpleExternalProcess("ls -lsa"));
51  * RLoginProcess p = new RLoginProcess(lsf, false);
52  * p.setHostname("cluster_front_end_name");
53  * p.startProcess();
54  * ...............
55  * </pre>
56  * @author ProActive Team
57  * @version 1.0, 2002/09/20
58  * @since ProActive 0.9.4
59  */

60
61 public class LSFBSubProcess extends AbstractExternalProcessDecorator {
62     
63   private static final String JavaDoc FILE_SEPARATOR = System.getProperty("file.separator");
64   
65   private static final String JavaDoc DEFAULT_SCRIPT_LOCATION = System.getProperty("user.home")+FILE_SEPARATOR+"ProActive"+FILE_SEPARATOR+"scripts"+FILE_SEPARATOR+"unix"+FILE_SEPARATOR+"cluster"+FILE_SEPARATOR+"startRuntime.sh ";
66   
67   public final static String JavaDoc DEFAULT_LSFPATH =FILE_SEPARATOR+"usr"+FILE_SEPARATOR+"local"+FILE_SEPARATOR+"lsf"+FILE_SEPARATOR+"bin";
68   
69   public final static String JavaDoc DEFAULT_BSUBPATH=DEFAULT_LSFPATH+FILE_SEPARATOR+"bsub";
70   
71   public final static String JavaDoc DEFAULT_BJOBPATH=DEFAULT_LSFPATH+FILE_SEPARATOR+"bjobs";
72   
73   public static final String JavaDoc DEFAULT_QUEUE_NAME = "normal";
74   
75   protected static final String JavaDoc DEFAULT_PROCESSOR_NUMBER = "1";
76   
77   protected int jobID;
78   
79   protected String JavaDoc queueName = DEFAULT_QUEUE_NAME;
80   
81   protected String JavaDoc hostList;
82   
83   protected String JavaDoc scriptLocation = DEFAULT_SCRIPT_LOCATION;
84   
85   protected String JavaDoc processor = DEFAULT_PROCESSOR_NUMBER;
86   
87   protected String JavaDoc interactive = "false";
88   
89   protected String JavaDoc res_requirement="";
90   
91   //
92
// -- CONSTRUCTORS -----------------------------------------------
93
//
94
/**
95    * Creates a new LSFBsubProcess
96    * Used with XML Descriptors
97    */

98   public LSFBSubProcess() {
99     super();
100     setCompositionType(GIVE_COMMAND_AS_PARAMETER);
101     this.hostname = null;
102   }
103   
104   
105   /**
106    * Creates a new LSFBsubProcess
107    * @param targetProcess The target process associated to this process. The target process
108    * represents the process that will be launched with the bsub command
109    */

110   public LSFBSubProcess(ExternalProcess targetProcess) {
111     super(targetProcess);
112     this.hostname = null;
113   }
114
115     
116   //
117
// -- PUBLIC METHODS -----------------------------------------------
118
//
119

120   public void setInputMessageLogger(MessageLogger inputMessageLogger) {
121     super.setInputMessageLogger(new CompositeMessageLogger(new ParserMessageLogger(), inputMessageLogger));
122   }
123     
124   public void setOutputMessageSink(MessageSink outputMessageSink) {
125     if (outputMessageSink == null) {
126       super.setOutputMessageSink(new SimpleMessageSink());
127     } else {
128       super.setOutputMessageSink(outputMessageSink);
129     }
130   }
131   
132
133     /**
134      * Builds bkill command and encapsulates it in a process
135      * @param jobID The id of the job previously launched
136      * @return ExternalProcess The process encapsulating the bkill command
137      */

138   public static ExternalProcess buildBKillProcess(int jobID) {
139     return new SimpleExternalProcess("bkill "+jobID);
140   }
141   
142     
143   public static void main(String JavaDoc[] args) {
144     try {
145       LSFBSubProcess p = new LSFBSubProcess(new SimpleExternalProcess("ls -lsa"));
146       p.startProcess();
147     } catch (Exception JavaDoc e) {
148       e.printStackTrace();
149     }
150   }
151   
152   
153     /**
154      * Returns the id of the job associated to this process
155      * @return int
156      */

157   public int getJobID() {
158     return jobID;
159   }
160   
161   
162     /**
163      * Returns the name of the queue where the job was launched
164      * @return String
165      */

166   public String JavaDoc getQueueName() {
167     return queueName;
168   }
169   
170   
171     /**
172      * Sets the value of the queue where the job will be launched. The default is 'normal'
173      * @param queueName
174      */

175   public void setQueueName(String JavaDoc queueName) {
176     checkStarted();
177     if (queueName == null) throw new NullPointerException JavaDoc();
178     this.queueName = queueName;
179   }
180   
181   
182     /**
183      * Sets the value of the hostList parameter with the given value
184      * @param hostList
185      */

186   public void setHostList(String JavaDoc hostList){
187     checkStarted();
188     this.hostList = hostList;
189   }
190    
191    
192     /**
193      * Returns the hostList value of this process.
194      * @return String
195      */

196   public String JavaDoc getHostList(){
197     return hostList;
198   }
199   
200   
201     /**
202      * Returns true if this BsubProcess is lauched with -I option false otherwise
203      * @return boolean
204      */

205   public String JavaDoc isInteractive(){
206     return interactive;
207   }
208   
209   
210     /**
211      * Allows to launch this BsubProcess with -I (interactive option)
212      * @param interactive true for -I option false otherwise
213      */

214   public void setInteractive(String JavaDoc interactive){
215     this.interactive = interactive;
216   }
217   
218   
219     /**
220      * Sets the number of processor requested when running the job
221      * @param processor
222      */

223   public void setProcessorNumber(String JavaDoc processor){
224     checkStarted();
225     if(processor != null){
226     this.processor = processor;
227     }
228   }
229   
230   
231     /**
232      * Returns the number of processor requested for the job
233      * @return String
234      */

235   public String JavaDoc getProcessorNumber(){
236     return processor;
237   }
238   
239   
240   
241   public void setScriptLocation(String JavaDoc location){
242     checkStarted();
243     if(location != null){
244         this.scriptLocation = location;
245     }
246   }
247   
248   public String JavaDoc getScriptLocation(){
249     return scriptLocation;
250   }
251   
252   
253  
254   
255   public String JavaDoc getRes_requirement() {
256       return res_requirement;
257   }
258
259   
260   public void setRes_requirement(String JavaDoc res_requirement) {
261       this.res_requirement = "-R "+res_requirement+" ";
262   }
263
264   //
265
// -- PROTECTED METHODS -----------------------------------------------
266
//
267

268   protected String JavaDoc internalBuildCommand() {
269     return buildEnvironmentCommand()+buildBSubCommand();
270   }
271   
272   
273   protected String JavaDoc buildBSubCommand() {
274     StringBuffer JavaDoc bSubCommand = new StringBuffer JavaDoc();
275     bSubCommand.append(DEFAULT_BSUBPATH);
276     if(interactive.equals("true")) bSubCommand.append(" -I");
277     bSubCommand.append(" -n "+processor+" -q "+queueName+" ");
278     if(hostList != null){
279         bSubCommand.append("-m "+hostList+" ");
280     }
281     if(getCompositionType() == GIVE_COMMAND_AS_PARAMETER){
282     bSubCommand.append(getRes_requirement()+scriptLocation+" "+getTargetProcess().getCommand());
283     }
284     
285     //logger.info("bsub command is "+bSubCommand.toString());
286
return bSubCommand.toString();
287   }
288   
289   
290   protected String JavaDoc buildBJobsCommand() {
291     return DEFAULT_BJOBPATH+" "+jobID;
292   }
293   
294   /**
295    * parses a message in order to find the job id of the
296    * launched job.
297    * we assume here that the jobid is displayed following this
298    * convention :
299    * Job <...>
300    */

301   protected int parseJobID(String JavaDoc message) {
302     logger.info("parseJobID analyzing "+message);
303     String JavaDoc beginJobIDMarkup = "Job <";
304     String JavaDoc endJobIDMarkup = ">";
305     int n1 = message.indexOf(beginJobIDMarkup);
306     if (n1 == -1) return 0;
307     int n2 = message.indexOf(endJobIDMarkup, n1+beginJobIDMarkup.length());
308     if (n2 == -1) return 0;
309     String JavaDoc id = message.substring(n1+beginJobIDMarkup.length(), n2);
310    logger.info("!!!!!!!!!!!!!! JOBID = "+id);
311     try {
312       return Integer.parseInt(id);
313     } catch (NumberFormatException JavaDoc e) {
314       return 0;
315     }
316   }
317   
318   
319   /**
320    * parses the hostname from a string. We assume that the line
321    * looks like that :
322    * 191009 user status queue fromHost targetHost *eep 10000 Jan 25 13:33
323    * Where targetHost is the hostname we are looking for.
324    * status could be at least
325    * - PEND for pending (means targethost is undetermined
326    * - anything else (means targethost is known
327    * @param message the string that may contains the hostname
328    * @return null if the message did not contains any hostname,
329    * an empty string if the message did contains the target host but
330    * was undertermined because the job was still pending. Return the
331    * hostname if it is found.
332    */

333   protected String JavaDoc parseHostname(String JavaDoc message) {
334     logger.info("parseHostname analyzing "+message);
335     java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(message);
336     if (st.countTokens() < 6) return null; // we expect at least 6 tokens
337
try {
338       int currentJobID = Integer.parseInt(st.nextToken());
339       if (currentJobID != jobID) return null; // not the same id
340
} catch (NumberFormatException JavaDoc e) {
341       return null;
342     }
343     st.nextToken(); // ignore user
344
String JavaDoc status = st.nextToken();
345     if (status.equals("PEND")) {
346       return ""; // not running yet
347
}
348     st.nextToken(); // ignore queue
349
st.nextToken(); // ignore fromHost
350
String JavaDoc hostname = st.nextToken();
351     logger.info("!!!!!!!!!!!!!! hostname = "+hostname);
352     logger.info("token "+st.countTokens());
353     return hostname;
354   }
355   
356   protected void sendJobDetailsCommand() {
357     outputMessageSink.setMessage(buildBJobsCommand());
358   }
359   
360   
361   //
362
// -- PRIVATE METHODS -----------------------------------------------
363
//
364

365
366
367   //
368
// -- INNER CLASSES -----------------------------------------------
369
//
370

371   /**
372    * Implementation of a MessageLogger that look for the jobID of the launched job
373    */

374   public class ParserMessageLogger implements MessageLogger,java.io.Serializable JavaDoc {
375   
376     private boolean foundJobID;
377     private boolean foundHostname;
378   
379     public ParserMessageLogger() {
380     }
381     
382     public void log(String JavaDoc message) {
383         //int nbProcessor = (new Integer(processor)).intValue();
384
//parseHostname(message);
385
if (! foundJobID) {
386         jobID = parseJobID(message);
387         foundJobID = jobID != 0;
388         if (foundJobID) sendJobDetailsCommand();
389       } else if (! foundHostname) {
390         hostname = parseHostname(message);
391         if (hostname != null) {
392             //int counter=1;
393
foundHostname = hostname.length() > 0;
394           //while(counter < nbProcessor){
395
//parseHostname(message);
396
//counter ++;
397
//}
398
if (foundHostname) {
399             // we are done
400
outputMessageSink.setMessage(null);
401           } else {
402             // send another command to fetch the hostname
403
try {
404               Thread.sleep(2000);
405             } catch (InterruptedException JavaDoc e) {
406             }
407             sendJobDetailsCommand();
408           }
409         }
410       }
411     }
412     
413     public void log(Throwable JavaDoc t) {
414     }
415     
416     public void log(String JavaDoc message, Throwable JavaDoc t) {
417     }
418   
419   } // end inner class CompositeMessageLogger
420

421
422
423
424 }
425
Popular Tags