Package org.glite.ce.creamapi.cmdmanagement

Examples of org.glite.ce.creamapi.cmdmanagement.CommandException


        BufferedOutputStream os = null;
        BufferedReader readErr = null;
        Process proc = null;

        if (!containsParameterKey(JOB_WRAPPER_TEMPLATE_PATH)) {
            throw new CommandException("parameter \"JOB_WRAPPER_TEMPLATE_PATH\" not defined!");
        }

        String cream_sandbox_dir = getParameterValueAsString("CREAM_SANDBOX_DIR");
        if (cream_sandbox_dir == null) {
            throw new CommandException("parameter \"CREAM_SANDBOX_DIR\" not defined!");
        }

        job.setCREAMSandboxBasePath(cream_sandbox_dir);

        if(!containsParameterKey("CREAM_CREATE_SANDBOX_BIN_PATH")) {
            throw new CommandException("parameter \"CREAM_CREATE_SANDBOX_BIN_PATH\" not defined!");
        }

        try {
            String[] cmd = new String[] { "sudo", "-S", "-n", "-u", job.getLocalUser(), getParameterValueAsString("CREAM_CREATE_SANDBOX_BIN_PATH"), cream_sandbox_dir, job.getUserId(), job.getId(), "true" };

            try {
                proc = runtime.exec(cmd);
            } catch (Throwable e) {
                logger.error("createJobSandboxDir: " + e.getMessage());
                throw (new IOException("Cannot create the sandbox for job " +  job.getId() + "! " + e.getMessage()));
            }

            in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            os = new BufferedOutputStream(proc.getOutputStream());
            readErr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

            try {
                job.setWorkingDirectory(in.readLine());

                if(job.getWorkingDirectory() == null) {
                    job.setWorkingDirectory(Job.NOT_AVAILABLE_VALUE);

                    throw new IOException();
                }
            } catch (IOException e) {
                throw new IOException("cannot create the job's working directory!");
            }

            if (gsiFTPcreamURL != null && job.getWorkingDirectory() != null) {
                job.setCREAMInputSandboxURI(gsiFTPcreamURL + job.getWorkingDirectory() + "/ISB");
                job.setCREAMOutputSandboxURI(gsiFTPcreamURL + job.getWorkingDirectory() + "/OSB");

                String dsUploadFile = null;
                if (job.containsVolatilePropertyKeys(Job.OUTPUT_DATA)) {
                    if (Job.NOT_AVAILABLE_VALUE.equals(job.getGridJobId()) || job.getGridJobId() == null) {               
                        dsUploadFile = "DSUpload_" + job.getId() + ".out";
                        job.getOutputFiles().add(dsUploadFile);
                        job.addVolatileProperty(Job.DS_UPLOAD_OUTPUT_FILE, dsUploadFile);
                    } else {
                        job.addVolatileProperty(Job.DS_UPLOAD_OUTPUT_FILE, "DSUpload_" + job.getGridJobId().substring(job.getGridJobId().lastIndexOf('/') +1) + ".out");
                    }
                }

                if (containsParameterKey(SANDBOX_TRANSFER_METHOD)) {
                    job.addVolatileProperty(SANDBOX_TRANSFER_METHOD, getParameterValueAsString(SANDBOX_TRANSFER_METHOD));
                } else {
                    job.addVolatileProperty(SANDBOX_TRANSFER_METHOD, GSIFTP_SANDBOX_TRANSFER_METHOD);
                }

                if (GSIFTP_SANDBOX_TRANSFER_METHOD.equalsIgnoreCase((String)job.getVolatileProperty(SANDBOX_TRANSFER_METHOD))) {
                    if (job.getOutputFiles() != null && job.getOutputFiles().size() > 0) {
                        if (job.getOutputSandboxBaseDestURI() != null) {
                            if ("gsiftp://localhost".equalsIgnoreCase(job.getOutputSandboxBaseDestURI())) {
                                job.setOutputSandboxBaseDestURI(job.getCREAMOutputSandboxURI());
                            }
                        } else {
                            int index = 0, i=0;
                            String outputFile = null;
                            ArrayList<String> outputSandboxDestURI = new ArrayList<String>(0);

                            if (dsUploadFile != null) {
                                job.getOutputSandboxDestURI().add(job.getCREAMOutputSandboxURI() + File.separator + dsUploadFile);
                            }

                            for (String file : job.getOutputSandboxDestURI()) {
                                if (file.startsWith("gsiftp://localhost")) {
                                    if (file.equals("gsiftp://localhost")) {
                                        outputFile = job.getOutputFiles().get(i);
                                    } else {
                                        outputFile = file.substring("gsiftp://localhost".length()+1);
                                    }

                                    index = outputFile.lastIndexOf("/");
                                    outputFile = index > 0? outputFile.substring(index + 1): outputFile;

                                    outputSandboxDestURI.add(job.getCREAMOutputSandboxURI() + outputFile);
                                } else {
                                    outputSandboxDestURI.add(file);
                                }
                           
                                i++;
                            }

                            job.setOutputSandboxDestURI(outputSandboxDestURI);
                        }
                    }
                } else if (LRMS_SANDBOX_TRANSFER_METHOD.equalsIgnoreCase((String)job.getVolatileProperty(SANDBOX_TRANSFER_METHOD))) {
                    int index=0;

                    if (job.getInputFiles() != null && job.getInputFiles().size() > 0) {
                        String inputFile = null;
                        StringBuffer transferInput = new StringBuffer();
                        ArrayList<String> inputFiles = new ArrayList<String>(0);
                        ArrayList<String> lrmsInputFiles = new ArrayList<String>(0);

                        for (String file : job.getInputFiles()) {
                            if (file.startsWith("/") || file.startsWith("file://") ||
                                    (job.getInputSandboxBaseURI() == null && !file.startsWith("gsiftp://") && !file.startsWith("https://"))) {
                                index = file.lastIndexOf("/");
                                inputFile = index > 0? file.substring(index + 1): file;

                                transferInput.append(inputFile).append(",");

                                lrmsInputFiles.add(inputFile);
                            } else {
                                inputFiles.add(file);
                            }
                        }

                        if (transferInput.length() > 0) {
                            transferInput.replace(transferInput.length()-1, transferInput.length(), "\"");
                            job.addExtraAttribute("TransferInput", "TransferInput=\"" + transferInput.toString());
                            job.addExtraAttribute("iwd", "iwd=\"" + job.getWorkingDirectory() + "/ISB\"");
                        }

                        if (inputFiles.size() == 0) {
                            job.setInputSandboxBaseURI(null);
                            job.setInputFiles(null);
                        } else {
                            job.setInputFiles(inputFiles);
                        }

                        if (lrmsInputFiles.size() > 0) {
                            job.addVolatileProperty(LRMS_INPUT_FILES, lrmsInputFiles);
                        }
                    }

                    if (job.getOutputFiles() != null && job.getOutputFiles().size() > 0) {
                        String outputFile = null;
                        String osbDir = job.getWorkingDirectory() + "/OSB/";
                        StringBuffer transferOutput = new StringBuffer();
                        StringBuffer transferOutputRemaps = new StringBuffer();
                        ArrayList<String> outputFiles = new ArrayList<String>(0);
                        ArrayList<String> lrmsOutputFiles = new ArrayList<String>(0);
                        ArrayList<String> outputSandboxDestURI = new ArrayList<String>(0);

                        if (job.getOutputSandboxBaseDestURI() != null) {
                            if ("gsiftp://localhost".equalsIgnoreCase(job.getOutputSandboxBaseDestURI())) {
                                for (String file : job.getOutputFiles()) {
                                    index = file.lastIndexOf("/");
                                    outputFile = index > 0? file.substring(index + 1): file;

                                    lrmsOutputFiles.add(outputFile);

                                    transferOutput.append(outputFile).append(",");
                                    transferOutputRemaps.append(outputFile).append("=").append(osbDir).append(outputFile).append(";");
                                }

                                job.setOutputFiles(null);
                                job.setOutputSandboxBaseDestURI(null);
                            }
                        } else {
                            int i=0;

                            if (dsUploadFile != null) {
                                job.getOutputSandboxDestURI().add("gsiftp://localhost");
                            }

                            for (String file : job.getOutputSandboxDestURI()) {
                                outputFile = job.getOutputFiles().get(i++);

                                if (file.startsWith("gsiftp://localhost")) {
                                    index = outputFile.lastIndexOf("/");
                                    outputFile = index > 0? outputFile.substring(index + 1): outputFile;

                                    lrmsOutputFiles.add(outputFile);

                                    transferOutput.append(outputFile).append(",");
                                    transferOutputRemaps.append(outputFile).append("=").append(osbDir);

                                    if (file.length() > "gsiftp://localhost".length()) {
                                        outputFile = file.substring("gsiftp://localhost".length()+1);
                                        index = outputFile.lastIndexOf("/");
                                        outputFile = index > 0? file.substring(index + 1): outputFile;
                                    }

                                    transferOutputRemaps.append(outputFile).append(";");
                                } else {
                                    outputFiles.add(outputFile);
                                    outputSandboxDestURI.add(file);                                   
                                }
                            }

                            job.setOutputFiles(outputFiles.size() > 0 ? outputFiles : null);   
                            job.setOutputSandboxDestURI(outputSandboxDestURI.size() > 0 ? outputSandboxDestURI : null);
                        }

                        if (transferOutput.length() > 0) {
                            transferOutput.replace(transferOutput.length()-1, transferOutput.length(), "\"");
                            transferOutputRemaps.replace(transferOutputRemaps.length()-1, transferOutputRemaps.length(), "\"");
                            job.addExtraAttribute("TransferOutput", "TransferOutput=\"" + transferOutput.toString());
                            job.addExtraAttribute("TransferOutputRemaps", "TransferOutputRemaps=\"" + transferOutputRemaps.toString());
                        }

                        if (lrmsOutputFiles.size() > 0) {
                            job.addVolatileProperty(LRMS_OUTPUT_FILES, lrmsOutputFiles);
                        }
                    }
                } else {
                    throw new CommandException("sandbox transfer mode \"" + job.getVolatileProperty(SANDBOX_TRANSFER_METHOD) + "\" not supported!");
                }
            } else {
                job.setCREAMInputSandboxURI(Job.NOT_AVAILABLE_VALUE);
                job.setCREAMOutputSandboxURI(Job.NOT_AVAILABLE_VALUE);
            }

            String id = "" + StrictMath.random();

            if (id.charAt(1) == '.') {
                id = id.substring(2);
            }

            if (id.length() > 20) {
                id = id.substring(0, 20);
            }

            if (gsiFTPcreamURL != null && (isEmptyField(job.getICEId()) || (!isEmptyField(job.getICEId()) && !isEmptyField(job.getMyProxyServer())))) {
                job.addExtraAttribute("PROXY_RENEWAL", "ENABLED");
                job.addVolatileProperty(DELEGATION_PROXY_CERT_SANDBOX_URI, gsiFTPcreamURL + job.getDelegationProxyCertPath());

                if(containsParameterKey(JOB_WRAPPER_DELEGATION_TIME_SLOT)) {
                    job.addVolatileProperty(JOB_WRAPPER_DELEGATION_TIME_SLOT, (String)getParameterValue(JOB_WRAPPER_DELEGATION_TIME_SLOT));
                }
            } else {
                job.addExtraAttribute("PROXY_RENEWAL", "DISABLED");
            }

            if (containsParameterKey(JOB_WRAPPER_COPY_PROXY_MIN_RETRY_WAIT)) {
                job.addVolatileProperty(JOB_WRAPPER_COPY_PROXY_MIN_RETRY_WAIT, (String)getParameterValue(JOB_WRAPPER_COPY_PROXY_MIN_RETRY_WAIT));
            }
           
            if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_COUNT_ISB)) {
                job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_COUNT_ISB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_COUNT_ISB));
            }

            if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_ISB)) {
                job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_ISB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_ISB));
            }

            if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_COUNT_OSB)) {
                job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_COUNT_OSB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_COUNT_OSB));
            }

            if (containsParameterKey(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_OSB)) {
                job.addVolatileProperty(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_OSB, (String)getParameterValue(JOB_WRAPPER_COPY_RETRY_FIRST_WAIT_OSB));
            }

            job.addVolatileProperty(JOB_WRAPPER_TEMPLATE_PATH, getParameterValueAsString(JOB_WRAPPER_TEMPLATE_PATH));
           
            String tmpWrapper = null;

            try {
                tmpWrapper = JobWrapper.buildWrapper(job);
            } catch (Throwable e) {
                proc.destroy();
                throw new CommandException("cannot generate the job wrapper! the problem seems to be related to the jdl: " + e.getMessage());
            }

            if (tmpWrapper == null) {
                throw new CommandException("cannot generate the job wrapper!");
            }

            try {
                os.write(tmpWrapper.getBytes());
                os.flush();
                os.close();
                os = null;
            } catch (Throwable e) {
                throw new CommandException("cannot write the job wrapper (jobId = " + job.getId() + ")!");
            }
        } catch (CommandException ce) {
            throw ce;
        } catch (Throwable exx) {
            if (proc != null) {
                proc.waitFor();

                if(proc.exitValue() != 0) {           
                    String procErrorMessage = null;

                    if (readErr.ready()) {
                        procErrorMessage = " [failure reason = \"" + readErr.readLine() + "\"]";
                    }

                    procErrorMessage += " [exit code = " + proc.exitValue() + "]";

                    throw new CommandException(exx.getMessage() + procErrorMessage);
                }
            } else {
                throw new CommandException(exx.getMessage());
            }
        } finally {
            if (proc != null) {
                proc.waitFor();
               
View Full Code Here


    }

    protected Job makeJobFromCmd(Command cmd) throws CommandException {
      String jdl = cmd.getParameterAsString("JDL");
      if (jdl == null) {
            throw new CommandException("JDL not defined!");
        }
      try {
            return JobFactory.makeJob(jdl);
        } catch (Throwable e) {
            logger.error(e.getMessage());
            throw new CommandException(e.getMessage());
        }
    }
View Full Code Here

    private void jobRegister(final Command cmd) throws CommandException, CommandExecutorException {
        logger.debug("BEGIN jobRegister");

        if (cmd == null) {
            throw new CommandException("command not defined!");
        }
       
        JobCommand jobCmd = new JobCommand(getCommandType(JobCommandConstant.JOB_REGISTER));
        jobCmd.setStatus(cmd.getStatus());
        jobCmd.setCreationTime(cmd.getCreationTime());
        jobCmd.setDescription(cmd.getDescription());
        jobCmd.setUserId(cmd.getUserId());
        jobCmd.setStartSchedulingTime(cmd.getStartProcessingTime());

        try {
            String userId = cmd.getUserId();
           
            if (userId == null) {
                throw new CommandException("userId not defined!");
            }

            Job job = makeJobFromCmd(cmd);
           
            JobStatus status = new JobStatus(JobStatus.REGISTERED, job.getId());

            job.setUserId(userId);
            job.setLocalUser(cmd.getParameterAsString("LOCAL_USER"));
            job.setJDL(cmd.getParameterAsString("JDL"));
            job.setICEId(cmd.getParameterAsString("ICE_ID"));
            job.addCommandHistory(jobCmd);
           
            if (cmd.containsParameterKey("USER_VO")) {
                job.setVirtualOrganization(cmd.getParameterAsString("USER_VO"));
            }

            if (isEmptyField(job.getBatchSystem())) {
                throw new CommandException("\"BatchSystem\" attribute not defined into the JDL");
            }

            if (isEmptyField(job.getQueue())) {
                throw new CommandException("\"QueueName\" attribute not defined into the JDL");
            }

            if (!isBatchSystemSupported(job.getBatchSystem())) {
                throw new CommandException("Batch System " + job.getBatchSystem() + " not supported!");
            }

            String cream_sandbox_dir = getParameterValueAsString("CREAM_SANDBOX_DIR");
            if (cream_sandbox_dir == null) {
                throw new CommandException("parameter \"CREAM_SANDBOX_DIR\" not defined!");
            }

            job.setCreamURL(cmd.getParameterAsString("CREAM_URL"));
            job.setDelegationProxyId(cmd.getParameterAsString("DELEGATION_PROXY_ID"));
            job.setDelegationProxyInfo(cmd.getParameterAsString("DELEGATION_PROXY_INFO"));
            job.setDelegationProxyCertPath(cmd.getParameterAsString("DELEGATION_PROXY_PATH"));
            job.setLRMSAbsLayerJobId(Job.NOT_AVAILABLE_VALUE);
            job.setLRMSJobId(Job.NOT_AVAILABLE_VALUE);
            job.setWorkerNode(Job.NOT_AVAILABLE_VALUE);
            job.setWorkingDirectory(Job.NOT_AVAILABLE_VALUE);

            if (cmd.containsParameterKey("USER_DN")) {
                job.addExtraAttribute("USER_DN", cmd.getParameterAsString("USER_DN").replaceAll("\\s+", "\\\\ "));
            }

            if (cmd.containsParameterKey("USER_DN_X500")) {
                job.addExtraAttribute("USER_DN_X500", cmd.getParameterAsString("USER_DN_X500").replaceAll("\\s+", "\\\\ "));
            }

            if (cmd.containsParameterKey("LOCAL_USER_GROUP")) {
                job.addExtraAttribute("LOCAL_USER_GROUP", cmd.getParameterAsString("LOCAL_USER_GROUP"));
            }

            if (cmd.containsParameterKey("USER_FQAN")) {
                List<String> fqanList = cmd.getParameterMultivalue("USER_FQAN");
               
                if (fqanList != null && fqanList.size() > 0) {                   
                    StringBuffer fqanBuffer = new StringBuffer();
                   
                    for (String fqan : fqanList) {
                        fqanBuffer.append("\\\"userFQAN=").append(fqan.replaceAll("\\s+", "\\\\ ")).append("\\\"\\ ");
                    }

                    fqanBuffer.deleteCharAt(fqanBuffer.length() - 1);
                    fqanBuffer.deleteCharAt(fqanBuffer.length() - 1);
                   
                    job.addExtraAttribute("USER_FQAN", fqanBuffer.toString());
                }
            }
            
            if (this.containsParameterKey("LRMS_EVENT_LISTENER_PORT")) {
                job.setLoggerDestURI(InetAddress.getLocalHost().getHostAddress() + ":" + getParameterValueAsString("LRMS_EVENT_LISTENER_PORT"));
            }

            if (job.getCreamURL() != null) {
                try {
                    URL url = new URL(job.getCreamURL());
                    job.setCeId(url.getHost() + ":" + url.getPort() + "/cream-" + job.getBatchSystem() + "-" + job.getQueue());
                } catch (MalformedURLException e) {
                }
            }

            if (cmd.containsParameterKey("LEASE_ID")) {
                String leaseId = cmd.getParameterAsString("LEASE_ID");

                if (leaseId != null && leaseId.length() > 0) {
                    Lease lease = jobDB.retrieveJobLease(leaseId, userId);
                    if (lease != null) {
                        logger.debug("found lease \"" + leaseId + "\" = " + lease.getLeaseTime().getTime());
                        job.setLease(lease);
                    } else {
                        throw new CommandException("lease id \"" + leaseId + "\" not found!");
                    }
                }
            }

            boolean jobInserted = false;
            int count = 0;

            while (!jobInserted && count < 5) {
                try {
                    jobDB.insert(job);
                    jobInserted = true;
                } catch (DatabaseException de) {
                    if (de.getMessage().indexOf("Duplicate entry") > -1) {
                        job.setId(job.generateJobId());
                        count++;
                    } else {
                        logger.error(de.getMessage());
                        throw new CommandException("database error occurred");
                    }
                } catch (IllegalArgumentException ie) {
                    throw new CommandException(ie.getMessage());
                }
            }

            if (!jobInserted) {
                throw new CommandException("Duplicate jobId error: cannot insert the new job (" + job.getId() + ") into the database");
            }

            jobCmd.setJobId(job.getId());
            jobCmd.setStatus(JobCommand.SUCCESSFULL);
          
            if (LBLogger.isEnabled()) {
                try {
                    LBLogger.getInstance().register(job);
                } catch (Throwable e) {
                    logger.warn("LBLogger.register() failed: " + e.getMessage());
                }

                try {
                    LBLogger.getInstance().accept(job);
                } catch (Throwable e) {
                    logger.warn("LBLogger.accept() failed: " + e.getMessage());
                }
            }

            try {
                createJobSandboxDir(job, cmd.getParameterAsString("GSI_FTP_CREAM_URL"));
            } catch (Throwable e) {
                jobCmd.setStatus(JobCommand.ERROR);
                jobCmd.setFailureReason(e.getMessage());

                status.setType(JobStatus.ABORTED);
                status.setFailureReason(e.getMessage());

                doOnJobStatusChanged(status, job);
               
                throw new CommandException(e.getMessage());
            } finally {
                jobDB.update(job);
                jobDB.updateJobCommand(jobCmd);
            }
View Full Code Here

                    try {
                        proc.getOutputStream().close();
                    } catch (IOException ioe) {}

                    if(errorMessage != null) {
                        throw new CommandException(errorMessage.toString());
                    }
                }
            }
        }

        try {
            jobDB.delete(job.getId(), job.getUserId());
            logger.info("purge: purged job " + job.getId());
        } catch (DatabaseException e) {
            logger.error(e.getMessage());
            throw new CommandException("database error occurred");
        }
    }
View Full Code Here

        }

        Calendar now = Calendar.getInstance();

        if (leaseTime.before(now)) {
            throw new CommandException("lease time (" + leaseTime.getTime() + ") expired!");
        }

        Long nowInMillis = now.getTimeInMillis();
        Long leaseInMills = leaseTime.getTimeInMillis();
        Long diffTime = (leaseInMills - nowInMillis) / 1000; // diff in sec
View Full Code Here

        synchronized (mutex) {
            try {
                executor.getJobDB().deleteJobLease(leaseId, userId);
            } catch (DatabaseException e) {
                logger.error("job lease deletion failed! " + e.getMessage());
                throw new CommandException("job lease deletion failed due to some database problems");
            }
        }
        logger.debug("After synchronized (mutex)");
        logger.info("Lease Removed: leaseId = " + leaseId + " and userId = " + userId);
        logger.debug("End deleteLease");
View Full Code Here

                    executor.getJobDB().updateJobLease(lease);
                    logger.info("Lease updated: leaseId = " + lease.getLeaseId() + " leaseTime = " + new java.sql.Timestamp(lease.getLeaseTime().getTimeInMillis()).toString()
                            + " userId = " + lease.getUserId());
                } catch (DatabaseException de2) {
                    logger.error("Problem to update/insert jobLease. " + de2.getMessage());
                    throw new CommandException("Problem to update/insert jobLease due to some database problems");
                }
            }
        }
        logger.debug("After synchronized (mutex)");
        logger.debug("End setLease");
View Full Code Here

        synchronized (mutex) {
            try {
                executor.getJobDB().setLeaseId(leaseId, jobId, userId);
            } catch (DatabaseException e) {
                logger.error(e.getMessage());
                throw new CommandException("setLeaseId failed due to some database problems");
            }
        }
        logger.debug("After synchronized (mutex)");
        logger.info("LeaseId field has been set for  jobId = " + jobId + " leaseId = " + leaseId + " userId = " + userId);
        logger.debug("End setJobLeaseId. jobId = " + jobId + " leaseId = " + leaseId + " userId = " + userId);
View Full Code Here

TOP

Related Classes of org.glite.ce.creamapi.cmdmanagement.CommandException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.