KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > prun > PrunSubProcess


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.prun;
32
33 import org.objectweb.proactive.core.config.ProActiveConfiguration;
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.process.SimpleExternalProcess;
38 import org.objectweb.proactive.core.util.MessageLogger;
39
40 import java.util.StringTokenizer JavaDoc;
41
42
43 /**
44  * <p>
45  * The PrunSubProcess class is able to start any class, of the ProActive library,
46  * on a cluster managed by PBS prtocol. An instance of this class can be coupled for instance with
47  * RlLoginProcess or SSHProcess classes in order to log into the cluster's front end with rlogin or
48  * ssh and then to run a job with PBSBSubProcess.
49  * </p>
50  * <p>
51  * For instance:
52  * </p><pre>
53  * ..............
54  * PBSBSubProcess PBS = new PBSBSubProcess(new SimpleExternalProcess("ls -lsa"));
55  * RLoginProcess p = new RLoginProcess(PBS, false);
56  * p.setHostname("cluster_front_end_name");
57  * p.startProcess();
58  * ...............
59  * </pre>
60  * @author ProActive Team
61  * @version 1.0, 2002/09/20
62  * @since ProActive 0.9.4
63  */

64 public class PrunSubProcess extends AbstractExternalProcessDecorator {
65     private static final String JavaDoc FILE_SEPARATOR = System.getProperty(
66             "file.separator");
67
68     // private static final String DEFAULT_SCRIPT_LOCATION = System.getProperty(
69
// "user.home") + FILE_SEPARATOR + "ProActive" + FILE_SEPARATOR +
70
// "scripts" + FILE_SEPARATOR + "unix" + FILE_SEPARATOR + "cluster" +
71
// FILE_SEPARATOR + "startRuntime.sh ";
72
// public final static String DEFAULT_PBSPATH = FILE_SEPARATOR + "usr" +
73
// FILE_SEPARATOR + "local" + FILE_SEPARATOR + "pbs" + FILE_SEPARATOR +
74
// "bin";
75
// public final static String DEFAULT_QSUBPATH = DEFAULT_PBSPATH +
76
// FILE_SEPARATOR + "qsub";
77
public final static String JavaDoc DEFAULT_PRUNPATH = "/usr/local/bin/prun";
78
79     // public final static String DEFAULT_QJOBPATH = DEFAULT_PBSPATH +
80
// FILE_SEPARATOR + "qjobs";
81
// public static final String DEFAULT_QUEUE_NAME = "normal";
82
protected static final String JavaDoc DEFAULT_HOSTS_NUMBER = "1";
83     protected static final String JavaDoc DEFAULT_PROCESSOR_NUMBER = "1";
84     protected static final String JavaDoc DEFAULT_BOOKING_DURATION = "00:01:00";
85     protected int jobID;
86     protected String JavaDoc queueName;
87     protected String JavaDoc hostList;
88
89     // protected String scriptLocation = DEFAULT_SCRIPT_LOCATION;
90
protected String JavaDoc hosts = DEFAULT_HOSTS_NUMBER;
91     protected String JavaDoc processorPerNode = DEFAULT_PROCESSOR_NUMBER;
92     protected String JavaDoc bookingDuration = DEFAULT_BOOKING_DURATION;
93     protected String JavaDoc interactive = "false";
94     protected String JavaDoc outputFile;
95
96     //
97
// -- CONSTRUCTORS -----------------------------------------------
98
//
99

100     /**
101      * Creates a new PBSBsubProcess
102      * Used with XML Descriptors
103      */

104     public PrunSubProcess() {
105         super();
106         setCompositionType(GIVE_COMMAND_AS_PARAMETER);
107         this.hostname = null;
108     }
109
110     /**
111      * Creates a new PBSBsubProcess
112      * @param targetProcess The target process associated to this process. The target process
113      * represents the process that will be launched with the qsub command
114      */

115     public PrunSubProcess(ExternalProcess targetProcess) {
116         super(targetProcess);
117         this.hostname = null;
118     }
119
120     //
121
// -- PUBLIC METHODS -----------------------------------------------
122
//
123
// public void setInputMessageLogger(MessageLogger inputMessageLogger) {
124
// super.setInputMessageLogger(new CompositeMessageLogger(
125
// new ParserMessageLogger(), inputMessageLogger));
126
// }
127
public void setErrorMessageLogger(MessageLogger errorMessageLogger) {
128         super.setErrorMessageLogger(new CompositeMessageLogger(
129                 new ParserMessageLogger(), errorMessageLogger));
130     }
131
132     public void setOutputMessageSink(MessageSink outputMessageSink) {
133         if (outputMessageSink == null) {
134             super.setOutputMessageSink(new SimpleMessageSink());
135         } else {
136             super.setOutputMessageSink(outputMessageSink);
137         }
138     }
139
140     /**
141      * Builds bdel command and encapsulates it in a process
142      * @param jobID The id of the job previously launched
143      * @return ExternalProcess The process encapsulating the bkill command
144      */

145     public static ExternalProcess buildBKillProcess(int jobID) {
146         return new SimpleExternalProcess("qdel " + jobID);
147     }
148
149     public static void main(String JavaDoc[] args) {
150         ProActiveConfiguration.load();
151         try {
152             PrunSubProcess p = new PrunSubProcess(new SimpleExternalProcess(
153                         "/bin/ls "));
154             p.setHostsNumber("2");
155             p.startProcess();
156         } catch (Exception JavaDoc e) {
157             e.printStackTrace();
158         }
159     }
160
161     /**
162      * @return the filename given to prun using -o
163      */

164     public String JavaDoc getOutputFile() {
165         return outputFile;
166     }
167
168     /** Set the output file to be passed to prun
169      * using the -o option
170      * @param string
171      */

172     public void setOutputFile(String JavaDoc string) {
173         outputFile = string;
174     }
175
176     /**
177      * Returns the id of the job associated to this process
178      * @return int
179      */

180     public int getJobID() {
181         return jobID;
182     }
183
184     /**
185      * Returns the name of the queue where the job was launched
186      * @return String
187      */

188     public String JavaDoc getQueueName() {
189         return queueName;
190     }
191
192     /**
193      * Sets the value of the queue where the job will be launched. The default is 'normal'
194      * @param queueName
195      */

196     public void setQueueName(String JavaDoc queueName) {
197         checkStarted();
198         if (queueName == null) {
199             throw new NullPointerException JavaDoc();
200         }
201         this.queueName = queueName;
202     }
203
204     /**
205      * Sets the value of the hostList parameter with the given value
206      * @param hostList
207      */

208     public void setHostList(String JavaDoc hostList) {
209         checkStarted();
210         this.hostList = hostList;
211     }
212
213     /**
214      * Returns the hostList value of this process.
215      * @return String
216      */

217     public String JavaDoc getHostList() {
218         return hostList;
219     }
220
221     // /**
222
// * Returns true if this BsubProcess is lauched with -I option false otherwise
223
// * @return boolean
224
// */
225
// public String isInteractive() {
226
// return interactive;
227
// }
228

229     /**
230      * Set the booking duration of the cluster's nodes. The default is 00:01:00
231      * @param duration
232      */

233     public void setBookingDuration(String JavaDoc d) {
234         this.bookingDuration = d;
235     }
236
237     /**
238      * Return the booking duration of the cluster's nodes.
239      * @return String
240      */

241     public String JavaDoc getBookingDuration() {
242         return this.bookingDuration;
243     }
244
245     /**
246      * return the list of the hostnames on which the job is running
247      * the message should be in the form : host1/processor ... hostn/processor
248      * @param message
249      * @return the hostname
250      */

251     protected String JavaDoc parseHostname(String JavaDoc message) {
252         String JavaDoc result = new String JavaDoc();
253         if (logger.isDebugEnabled()) {
254             logger.info("parseHostname() analyzing " + message);
255         }
256         java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(message);
257         if (st.countTokens() < 2) {
258             return null; //at least two tokens
259
}
260         if (!":".equals(st.nextToken())) {
261             return null; //should start with :
262
}
263
264         while (st.hasMoreTokens()) {
265             result += (st.nextToken());
266             result += " ";
267         }
268         return result;
269     }
270
271     /**
272      * Allows to launch this BsubProcess with -I (interactive option)
273      * @param interactive true for -I option false otherwise
274      */

275     public void setInteractive(String JavaDoc interactive) {
276         this.interactive = interactive;
277     }
278
279     /**
280      * Sets the number of nodes requested when running the job
281      * @param processor
282      */

283     public void setHostsNumber(String JavaDoc hosts) {
284         checkStarted();
285         if (hosts != null) {
286             this.hosts = hosts;
287         }
288
289         // System.out.println("-------- setNodeNumber() " + nodes);
290
}
291
292     /**
293      * Sets the number of nodes requested when running the job
294      * @param processor
295      */

296     public void setProcessorPerNodeNumber(String JavaDoc processorPerNode) {
297         checkStarted();
298         if (processorPerNode != null) {
299             this.processorPerNode = processorPerNode;
300         }
301     }
302
303     /**
304      * Returns the number of nodes requested for the job
305      * @return String
306      */

307     public String JavaDoc getHostsNumber() {
308         return this.hosts;
309     }
310
311     public String JavaDoc getProcessorPerNodeNumber() {
312         return this.processorPerNode;
313     }
314
315     // public void setScriptLocation(String location) {
316
// checkStarted();
317
// if (location != null) {
318
// this.scriptLocation = location;
319
// }
320
// }
321
//
322
// public String getScriptLocation() {
323
// return scriptLocation;
324
// }
325
//
326
// -- PROTECTED METHODS -----------------------------------------------
327
//
328
protected String JavaDoc internalBuildCommand() {
329         return buildEnvironmentCommand(); // + buildPSubCommand();
330
}
331
332     protected void sendJobDetailsCommand() {
333         // outputMessageSink.setMessage(buildBJobsCommand());
334
}
335
336     protected void internalStartProcess(String JavaDoc command)
337         throws java.io.IOException JavaDoc {
338         // System.out.println(
339
// "---------------Internal start process of PBSSubProcess " +
340
// command);
341
super.internalStartProcess(command);
342     }
343
344     protected String JavaDoc buildCommand() {
345         StringBuffer JavaDoc prunCommand = new StringBuffer JavaDoc();
346         prunCommand.append(DEFAULT_PRUNPATH);
347         if (interactive.equals("true")) {
348             prunCommand.append(" -I");
349         }
350
351         String JavaDoc[] commandAndOptions = separateCommandFromOptions(targetProcess.getCommand());
352         prunCommand.append(" -no-panda -v -" + processorPerNode + " -t " +
353             bookingDuration + " ");
354
355         if (hostList != null) {
356             prunCommand.append("-m " + hostList + " ");
357         }
358
359         if (outputFile != null) {
360             prunCommand.append("-o " + outputFile + " ");
361         }
362         
363         if (queueName != null) {
364             
365           prunCommand.append("-q " + queueName + " ") ;
366         }
367
368         prunCommand.append(commandAndOptions[0] + " " + hosts + " " +
369             commandAndOptions[1]); // " -q " + queueName + " ");
370

371         if (logger.isDebugEnabled()) {
372             logger.debug("prun command is " + prunCommand.toString());
373         }
374         return prunCommand.toString();
375     }
376
377     protected String JavaDoc[] separateCommandFromOptions(String JavaDoc s) {
378         String JavaDoc[] result = { "", "" };
379
380         //the problem is that the command we get should be splitted in command+option
381
//since prun likes to have options for the command at the very end
382
//of the line, after the requested number of nodes
383
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s);
384         if (st.countTokens() > 1) {
385             result[0] = st.nextToken();
386             while (st.hasMoreTokens()) {
387                 result[1] += st.nextToken();
388                 result[1] += " ";
389             }
390         }
391         return result;
392     }
393
394     //
395
// -- PRIVATE METHODS -----------------------------------------------
396
//
397
//
398
// -- INNER CLASSES -----------------------------------------------
399
//
400

401     /**
402      * Implementation of a MessageLogger that look for the jobID of the launched job
403      */

404     public class ParserMessageLogger implements MessageLogger,
405         java.io.Serializable JavaDoc {
406         private boolean foundJobID;
407         private boolean foundHostname;
408
409         public ParserMessageLogger() {
410         }
411
412         public void log(String JavaDoc message) {
413             //System.out.println(" >>> log :" +message);
414
int nbProcessor = (new Integer JavaDoc(hosts)).intValue();
415             String JavaDoc h = parseHostname(message);
416
417             // if (h != null) {
418
// System.out.println(" ---- hostname " + h);
419
// }
420
}
421
422         public void log(Throwable JavaDoc t) {
423         }
424
425         public void log(String JavaDoc message, Throwable JavaDoc t) {
426         }
427     }
428
429     // end inner class CompositeMessageLogger
430
}
431
Popular Tags