org/objectweb/proactive/core/process/nordugrid/NGProcess.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.nordugrid;
00032 
00033 import java.io.BufferedReader;
00034 import java.io.BufferedWriter;
00035 import java.io.File;
00036 import java.io.FileReader;
00037 import java.io.FileWriter;
00038 import java.util.ArrayList;
00039 import java.util.Random;
00040 
00041 import org.apache.log4j.Logger;
00042 import org.objectweb.proactive.ProActive;
00043 import org.objectweb.proactive.core.ProActiveException;
00044 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
00045 import org.objectweb.proactive.core.process.AbstractExternalProcessDecorator;
00046 import org.objectweb.proactive.core.process.UniversalProcess;
00047 import org.objectweb.proactive.core.process.filetransfer.FileTransferDefinition;
00048 import org.objectweb.proactive.core.process.filetransfer.FileTransferDefinition.FileDescription;
00049 import org.objectweb.proactive.core.process.filetransfer.FileTransferWorkShop;
00050 import org.objectweb.proactive.core.util.log.Loggers;
00051 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00052 
00053 
00061 public class NGProcess extends AbstractExternalProcessDecorator {
00062     private static final String FILE_SEPARATOR = System.getProperty(
00063             "file.separator");
00064     private static final String DEFAULT_SCRIPT_LOCATION = System.getProperty(
00065             "user.home") + FILE_SEPARATOR + "ProActive" + FILE_SEPARATOR +
00066         "scripts" + FILE_SEPARATOR + "unix" + FILE_SEPARATOR + "cluster" +
00067         FILE_SEPARATOR + "ngStartRuntime.sh ";
00068     public final static String DEFAULT_NGPATH = "ngsub";
00069     protected String count = "1";
00070     protected String stderr = null;
00071     protected String stdout = null;
00072     protected String queue = null;
00073     protected String jobname = null;
00074     protected String executable_path = DEFAULT_SCRIPT_LOCATION;
00075     protected String tmp_executable;
00076     protected String DEFAULT_INPUT_FILE = "(inputfiles = ";
00077     protected String inputFiles;
00078     protected ArrayList<String> command_buffer;
00079 
00080     //===========================================================
00081     // Constructor
00082     //===========================================================
00083 
00087     public NGProcess() {
00088         super();
00089         setCompositionType(GIVE_COMMAND_AS_PARAMETER);
00090         this.command_path = DEFAULT_NGPATH;
00091         this.hostname = null;
00092         FILE_TRANSFER_DEFAULT_PROTOCOL = "nordugrid";
00093     }
00094 
00095     public void setCount(String count) {
00096         this.count = count;
00097     }
00098 
00099     public void setQueue(String queue) {
00100         this.queue = queue;
00101     }
00102 
00103     public void setStderr(String stderr) {
00104         this.stderr = stderr;
00105     }
00106 
00107     public void setStdout(String stdout) {
00108         this.stdout = stdout;
00109     }
00110 
00111     public void setJobname(String jobname) {
00112         this.jobname = jobname;
00113     }
00114 
00115     protected String internalBuildCommand() {
00116         buildExecutable();
00117         command_buffer = new ArrayList<String>();
00118         return buildNGSUBCommand() + " " + buildXRSLCommand();
00119     }
00120 
00121     protected void internalStartProcess(String xRslCommand)
00122         throws java.io.IOException {
00123         String[] ng_command = command_buffer.toArray(new String[] {  });
00124         int j = new Integer(count).intValue();
00125 
00126         for (int i = 0; i < j; i++) {
00127             //here we simulate the deployment of // jobs on multiple procs
00128             //indeed at this point Ng does support // executions on the site
00129             // we have access. This should change with // RTEs
00130             try {
00131                 externalProcess = Runtime.getRuntime().exec(ng_command);
00132                 java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(
00133                             externalProcess.getInputStream()));
00134                 java.io.BufferedReader err = new java.io.BufferedReader(new java.io.InputStreamReader(
00135                             externalProcess.getErrorStream()));
00136                 java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
00137                             externalProcess.getOutputStream()));
00138                 handleProcess(in, out, err);
00139             } catch (java.io.IOException e) {
00140                 isFinished = true;
00141                 //throw e;
00142                 e.printStackTrace();
00143             }
00144         }
00145     }
00146 
00147     protected boolean internalFileTransferDefaultProtocol() {
00148         FileTransferWorkShop fts = getFileTransferWorkShopDeploy();
00149         FileTransferDefinition[] ftDefinitions = fts.getAllFileTransferDefinitions();
00150         Logger fileTransferLogger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT_FILETRANSFER);
00151         StringBuilder sb = new StringBuilder();
00152         for (int i = 0; i < ftDefinitions.length; i++) {
00153             //Files and Dirs
00154             FileDescription[] files = ftDefinitions[i].getAll();
00155             for (int j = 0; j < files.length; j++) {
00156                 String fullfilename = fts.getAbsoluteSrcPath(files[j]);
00157 
00158                 //Skipping non existant local filenames, keep remote files
00159                 if (!FileTransferWorkShop.isLocalReadable(fullfilename) &&
00160                         !FileTransferWorkShop.isRemote(fullfilename)) {
00161                     logger.info(fullfilename);
00162                     if (fileTransferLogger.isDebugEnabled()) {
00163                         fileTransferLogger.debug(
00164                             "Skiping. Unreadable for FileTransfer:" +
00165                             fullfilename);
00166                     }
00167                     continue;
00168                 }
00169 
00170                 sb.append("(\"" + files[j].getDestName() + "\" \"" +
00171                     fullfilename + "\")");
00172             }
00173         }
00174         if (fileTransferLogger.isDebugEnabled()) {
00175             fileTransferLogger.debug("NorduGrid FileTransfer:" + sb.toString());
00176         }
00177         if (sb.length() > 0) {
00178             inputFiles = sb.toString();
00179         }
00180 
00181         //Because FileTransfer will be submited with the process,
00182         //we return success so no other protocols will be tried.
00183         return true;
00184     }
00185 
00186     public String getProcessId() {
00187         return "nordugrid_" + targetProcess.getProcessId();
00188     }
00189 
00190     public int getNodeNumber() {
00191         return (new Integer(count).intValue());
00192     }
00193 
00194     public UniversalProcess getFinalProcess() {
00195         checkStarted();
00196         return targetProcess.getFinalProcess();
00197     }
00198 
00202     public static void main(String[] args) {
00203         try {
00204             ProActiveDescriptor pad = ProActive.getProactiveDescriptor(
00205                     "/user/rquilici/home/ProActive/descriptors/nqueen.xml");
00206             pad.activateMappings();
00207         } catch (ProActiveException e) {
00208             // TODO Auto-generated catch block
00209             e.printStackTrace();
00210         }
00211     }
00212 
00213     public void setExecutable(String exec) {
00214         this.executable_path = exec;
00215     }
00216 
00217     private String buildNGSUBCommand() {
00218         command_buffer.add(command_path);
00219         command_buffer.add("-c");
00220         command_buffer.add(hostname);
00221         return command_path + " -c " + hostname;
00222     }
00223 
00224     private String buildXRSLCommand() {
00225         //It is always the temporary file that is sent to NG server
00226         //The original executable is kept unchanged
00227         String xRSL_command = "&(executable=" + tmp_executable + ")";
00228         if (jobname != null) {
00229             xRSL_command = xRSL_command + "(jobname=" + jobname + ")";
00230         }
00231         if (stdout != null) {
00232             xRSL_command = xRSL_command + "(stdout=" + stdout + ")";
00233         }
00234 
00235         if (stderr != null) {
00236             xRSL_command = xRSL_command + "(stderr=" + stderr + ")";
00237         }
00238 
00239         if (queue != null) {
00240             xRSL_command = xRSL_command + "(queue=" + queue + ")";
00241         }
00242         if (inputFiles != null) {
00243             xRSL_command = xRSL_command + DEFAULT_INPUT_FILE + inputFiles +
00244                 ")";
00245         }
00246 
00247         //following line should be uncommented in case parallel environment
00248         //        if (count != "1") {
00249         //            xRSL_command = xRSL_command + "(count=" + count + ")";
00250         //        }
00251         if (logger.isDebugEnabled()) {
00252             logger.debug(xRSL_command);
00253         }
00254         command_buffer.add(xRSL_command);
00255         return xRSL_command;
00256     }
00257 
00258     private void buildExecutable() {
00259         File tmp_executableFile;
00260         Random random = new Random();
00261         int index = executable_path.lastIndexOf("/");
00262         String executable = executable_path.substring(index + 1);
00263         this.tmp_executable = "tmp_" + executable + random.nextInt();
00264         String tmp_executable_path = executable_path.replace(executable,
00265                 tmp_executable);
00266 
00267         //first we build the temporary execuable, that will be sent
00268         try {
00269             tmp_executableFile = new File(tmp_executable_path);
00270             if (tmp_executableFile.exists()) {
00271                 tmp_executableFile.delete();
00272             }
00273             tmp_executableFile.deleteOnExit();
00274             BufferedReader reader = new BufferedReader(new FileReader(
00275                         executable_path));
00276 
00277             BufferedWriter writer = new BufferedWriter(new FileWriter(
00278                         tmp_executableFile));
00279             while (true) {
00280                 String line = reader.readLine();
00281                 if (line == null) {
00282                     break;
00283                 }
00284                 writer.write(line);
00285                 writer.newLine();
00286             }
00287             reader.close();
00288             //we append in the tmp file the java command
00289             writer.write(targetProcess.getCommand());
00290             writer.close();
00291         } catch (Exception e) {
00292             e.printStackTrace();
00293         }
00294         if (!tmp_executable_path.startsWith("file://")) {
00295             //check if it follows NG syntax: the local location should start
00296             // with file:///, so if not present, the local file must start with /: absolute path
00297             //First implementation !
00298             tmp_executable_path = "file://" + tmp_executable_path;
00299         }
00300 
00301         //then we append the executable in the inputfiles, to be transfered on the server
00302         if (inputFiles != null) {
00303             inputFiles = inputFiles + "(\"" + tmp_executable + "\" \"" +
00304                 tmp_executable_path + "\")";
00305         } else {
00306             inputFiles = "(\"" + tmp_executable + "\" \"" +
00307                 tmp_executable_path + "\")";
00308         }
00309     }
00310 }

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