org/objectweb/proactive/core/process/oar/OARGRIDSubProcess.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.process.oar;
00032 
00033 import java.io.Serializable;
00034 import java.util.ArrayList;
00035 import java.util.regex.Matcher;
00036 import java.util.regex.Pattern;
00037 
00038 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
00039 import org.objectweb.proactive.core.process.ExternalProcess;
00040 import org.objectweb.proactive.core.process.JVMProcessImpl;
00041 import org.objectweb.proactive.core.process.UniversalProcess;
00042 
00043 
00053 public class OARGRIDSubProcess extends AbstractExternalProcessDecorator {
00054     private static final String FILE_SEPARATOR = System.getProperty(
00055             "file.separator");
00056     public final static String DEFAULT_OARGRIDSUBPATH = "/usr/local/bin/oargridsub";
00057     private static final String DEFAULT_SCRIPT_LOCATION = System.getProperty(
00058             "user.home") + FILE_SEPARATOR + "ProActive" + FILE_SEPARATOR +
00059         "scripts" + FILE_SEPARATOR + "unix" + FILE_SEPARATOR + "cluster" +
00060         FILE_SEPARATOR + "oarGRIDStartRuntime.sh ";
00061     protected static final int DEFAULT_HOSTS_NUMBER = 1;
00062     protected static final int DEFAULT_WEIGHT = 1;
00063     protected static final String DEFAULT_WALLTIME = "01:00:00"; //1 hour
00064     protected OarSite[] oarsite = null; //OAR site array
00065     protected String scriptLocation = DEFAULT_SCRIPT_LOCATION;
00066     protected int jobID;
00067     protected String queueName = "default";
00068     protected String accessProtocol = "ssh";
00069     protected String resources = "";
00070     protected String walltime = DEFAULT_WALLTIME;
00071 
00072     public OARGRIDSubProcess() {
00073         super();
00074         setCompositionType(GIVE_COMMAND_AS_PARAMETER);
00075         this.hostname = null;
00076         this.command_path = DEFAULT_OARGRIDSUBPATH;
00077     }
00078 
00079     public OARGRIDSubProcess(ExternalProcess targetProcess) {
00080         super(targetProcess);
00081         this.hostname = null;
00082         this.command_path = DEFAULT_OARGRIDSUBPATH;
00083     }
00084 
00088     public String getProcessId() {
00089         return "oargrid_" + targetProcess.getProcessId();
00090     }
00091 
00095     public int getNodeNumber() {
00096         int num = 0;
00097 
00098         //if(oarsite==null) return 0;
00099         for (int i = 0; i < oarsite.length; i++) {
00100             if (oarsite[i].getNodes() == UniversalProcess.UNKNOWN_NODE_NUMBER) {
00101                 return UniversalProcess.UNKNOWN_NODE_NUMBER;
00102             }
00103             num += (oarsite[i].getNodes() * oarsite[i].getWeight());
00104         }
00105         return num;
00106     }
00107 
00111     public UniversalProcess getFinalProcess() {
00112         checkStarted();
00113         return targetProcess.getFinalProcess();
00114     }
00115 
00116     public String getWallTime() {
00117         return walltime;
00118     }
00119 
00125     public void setScriptLocation(String location) {
00126         checkStarted();
00127         //     if (location != null) {
00128         this.scriptLocation = location;
00129         //    }
00130     }
00131 
00138     public void setAccessProtocol(String accessProtocol) {
00139         this.accessProtocol = accessProtocol;
00140     }
00141 
00149     public void setResources(String resources) {
00150         checkStarted();
00151         if (resources != null) {
00152             ArrayList<OarSite> al = new ArrayList<OarSite>();
00153             this.resources = resources;
00154             String[] resTab = resources.split(",");
00155             for (int i = 0; i < resTab.length; i++)
00156                 al.add(new OarSite(resTab[i]));
00157 
00158             this.oarsite = al.toArray((new OarSite[] {  }));
00159         }
00160     }
00161 
00168     public void setQueueName(String queueName) {
00169         checkStarted();
00170         if (queueName == null) {
00171             throw new NullPointerException();
00172         }
00173         this.queueName = queueName;
00174     }
00175 
00176     public void setWallTime(String walltime) {
00177         checkStarted();
00178         if (walltime == null) {
00179             throw new NullPointerException();
00180         }
00181         this.walltime = walltime;
00182     }
00183 
00184     protected void internalStartProcess(String commandToExecute)
00185         throws java.io.IOException {
00186         ArrayList<String> al = new ArrayList<String>();
00187 
00188         //we divide the command into tokens
00189         //it's basically 3 blocks, the script path, the option and the rest
00190         Pattern p = Pattern.compile("(.*) .*(-c).*'(.*)'");
00191         Matcher m = p.matcher(command);
00192         if (!m.matches()) {
00193             System.err.println("Could not match command ");
00194             System.err.println(command);
00195         }
00196         for (int i = 1; i <= m.groupCount(); i++) {
00197             //            System.out.println(m.group(i));
00198             al.add(m.group(i));
00199         }
00200         String[] command = al.toArray(new String[] {  });
00201 
00202         try {
00203             externalProcess = Runtime.getRuntime().exec(command);
00204             java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(
00205                         externalProcess.getInputStream()));
00206             java.io.BufferedReader err = new java.io.BufferedReader(new java.io.InputStreamReader(
00207                         externalProcess.getErrorStream()));
00208             java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
00209                         externalProcess.getOutputStream()));
00210             handleProcess(in, out, err);
00211         } catch (java.io.IOException e) {
00212             isFinished = true;
00213             //throw e;
00214             e.printStackTrace();
00215         }
00216     }
00217 
00223     protected String internalBuildCommand() {
00224         StringBuilder oarsubCommand = new StringBuilder();
00225         oarsubCommand.append(
00226             "/bin/sh -c  'echo for i in \\`cat \\$OAR_NODEFILE\\` \\; do " +
00227             accessProtocol + " \\$i  ");
00228 
00229         oarsubCommand.append(targetProcess.getCommand());
00230         oarsubCommand.append(" \\&  done  \\; wait > ");
00231         oarsubCommand.append(scriptLocation).append(" ; ");
00232 
00233         //scp oarStartRuntuime.sh to selected clusters frontend
00234         for (int i = 0; i < oarsite.length; i++)
00235             oarsubCommand.append(getScpRunTimeStr(oarsite[i]));
00236 
00237         oarsubCommand.append(command_path);
00238         oarsubCommand.append(" -q ");
00239         oarsubCommand.append(this.queueName);
00240         oarsubCommand.append(" -w ");
00241         oarsubCommand.append(this.walltime);
00242         oarsubCommand.append(" -p ");
00243         oarsubCommand.append(this.scriptLocation);
00244 
00245         if (resources != null) {
00246             oarsubCommand.append(" ").append(resources).append(" ");
00247         }
00248         oarsubCommand.append("'");
00249 
00250         if (logger.isDebugEnabled()) {
00251             logger.debug("oarsub command is " + oarsubCommand.toString());
00252         }
00253         System.err.println(oarsubCommand);
00254         return oarsubCommand.toString();
00255     }
00256 
00266     private String getScpRunTimeStr(OarSite oarsite) {
00267         if (oarsite == null) {
00268             return "";
00269         }
00270         if ((oarsite.getClusterFrontEndName() == null) ||
00271                 (oarsite.getClusterFrontEndName() == "")) {
00272             return "";
00273         }
00274 
00275         StringBuilder str = new StringBuilder();
00276 
00277         str.append("scp -p ");
00278         str.append(this.scriptLocation);
00279         str.append(" ");
00280         str.append(oarsite.getClusterFrontEndName());
00281         str.append(":" + this.scriptLocation + " ; ");
00282         return str.toString();
00283     }
00284 
00285     public class OarSite implements Serializable {
00286         private String clusterName;
00287         private String clusterFrontEndName;
00288         private int nodes;
00289         private int weight;
00290 
00297         public OarSite(String resource) {
00298             setResource(resource);
00299             //Trying to guess oar site front end
00300             //TODO unharcode this
00301             if (clusterName.equalsIgnoreCase("idpot")) {
00302                 //clusterFrontEndName = "oar.grenoble.grid5000.fr";
00303                 clusterFrontEndName = "caddo.imag.fr";
00304             } else if (clusterName.equalsIgnoreCase("sophia")) {
00305                 clusterFrontEndName = "oar.sophia.grid5000.fr";
00306             } else if (clusterName.equalsIgnoreCase("gdx")) {
00307                 //clusterFrontEndName = "oar.orsay.grid5000.fr";
00308                 clusterFrontEndName = "devgdx002.orsay.grid5000.fr";
00309             } else if (clusterName.equalsIgnoreCase("lyon")) {
00310                 clusterFrontEndName = "oar.lyon.grid5000.fr";
00311             } else if (clusterName.equalsIgnoreCase("toulouse")) {
00312                 clusterFrontEndName = "oar.toulouse.grid5000.fr";
00313             } else if (clusterName.equalsIgnoreCase("parasol")) {
00314                 clusterFrontEndName = "oar.rennes.grid5000.fr";
00315             } else if (clusterName.equalsIgnoreCase("paraci")) {
00316                 //clusterFrontEndName = "paraci01.irisa.fr";
00317                 clusterFrontEndName = "dev-xeon.rennes.grid5000.fr";
00318             } else if (clusterName.equalsIgnoreCase("tartopom")) {
00319                 //clusterFrontEndName = "tartopom01.irisa.fr";
00320                 clusterFrontEndName = "dev-powerpc.rennes.grid5000.fr";
00321             } else if (clusterName.equalsIgnoreCase("icluster2")) {
00322                 clusterFrontEndName = "ita101.imag.fr";
00323             }
00324             //ToDo, handle Error case
00325             else {
00326                 System.out.println(
00327                     "clusterFrontEndName not found for cluster: " +
00328                     clusterName);
00329                 if (logger.isDebugEnabled()) {
00330                     logger.debug("clusterFrontEndName not found for cluster: " +
00331                         clusterName);
00332                 }
00333             }
00334         }
00335 
00344         public OarSite(String resource, String clusterFrontEndName) {
00345             this.clusterFrontEndName = clusterFrontEndName;
00346             setResource(resource);
00347         }
00348 
00355         public void setResource(String resource) {
00356             this.clusterName = "";
00357             this.clusterFrontEndName = "";
00358             nodes = OARGRIDSubProcess.DEFAULT_HOSTS_NUMBER;
00359             weight = OARGRIDSubProcess.DEFAULT_WEIGHT;
00360 
00361             String[] resTab = resource.split(":");
00362             for (int i = 0; i < resTab.length; i++) {
00363                 //System.out.println(resTab[i]);
00364                 if (resTab[i].indexOf("=") < 0) { //Clustername has no "="
00365                     clusterName = resTab[i];
00366                 } else if (resTab[i].indexOf("nodes=") >= 0) {
00367                     String count = resTab[i].substring(resTab[i].indexOf("=") +
00368                             1);
00369                     if (count.equals("all")) {
00370                         nodes = UniversalProcess.UNKNOWN_NODE_NUMBER;
00371                     } else {
00372                         nodes = Integer.parseInt(count);
00373                     }
00374                 } else if (resTab[i].indexOf("weight=") >= 0) {
00375                     weight = Integer.parseInt(resTab[i].substring(resTab[i].indexOf(
00376                                     "=") + 1));
00377                 }
00378             }
00379         }
00380 
00381         public String getResource() {
00382             return this.clusterName + ":nodes=" + this.nodes + ":weight=" +
00383             this.weight;
00384         }
00385 
00386         public String getClusterFrontEndName() {
00387             return clusterFrontEndName;
00388         }
00389 
00390         public void setClusterFrontEndName(String clusterFrontEndName) {
00391             this.clusterFrontEndName = clusterFrontEndName;
00392         }
00393 
00394         public String getClusterName() {
00395             return clusterName;
00396         }
00397 
00398         public void setClusterName(String clusterName) {
00399             this.clusterName = clusterName;
00400         }
00401 
00402         public int getNodes() {
00403             return nodes;
00404         }
00405 
00406         public void setNodes(int nodes) {
00407             this.nodes = nodes;
00408         }
00409 
00410         public int getWeight() {
00411             return weight;
00412         }
00413 
00414         public void setWeight(int weight) {
00415             this.weight = weight;
00416         }
00417     }
00418 
00419     public static void main(String[] args) {
00420         System.out.println("Testing OARGRIDSubProcess");
00421         JVMProcessImpl p = new JVMProcessImpl();
00422         OARGRIDSubProcess oargrid = new OARGRIDSubProcess(p);
00423         oargrid.setResources("sophia:nodes=2:weight=3,idpot:nodes=10");
00424         System.out.println(oargrid.buildCommand());
00425         System.out.println(oargrid.getCommand());
00426     }
00427 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1