Search in sources :

Example 6 with Job

use of org.gridlab.gat.resources.Job in project compss by bsc-wdc.

the class SshLsfResourceBrokerAdaptor method sshLsfSubmission.

/*private java.io.File createJobScript(JobDescription description)
			throws GATInvocationException {

		java.io.File temp;

		SoftwareDescription sd = description.getSoftwareDescription();

		try {
			temp = java.io.File.createTempFile("lsf-sub", null);
		} catch (IOException e) {
			throw new GATInvocationException("Cannot create file", e);
		}
		PrintWriter job = null;
		try {
			job = new PrintWriter(new BufferedWriter(new FileWriter(temp)));

			job.print("#!/bin/sh\n");
			job.print("# job script\n");

			// Support DIRECTORY
			String dir = sd.getStringAttribute(SoftwareDescription.DIRECTORY, null);
			if (dir != null) {
				job.print("cd " + dir + "\n");
			}

			// Support environment.
			Map<String, Object> env = sd.getEnvironment();
			if (env != null) {
				Set<String> s = env.keySet();
				Object[] keys = s.toArray();

				for (int i = 0; i < keys.length; i++) {
					String val = (String) env.get(keys[i]);
					job.print(keys[i] + "=" + val + " && export " + keys[i] + "\n");
				}
			}

			// Construct command.
			StringBuffer cmd = new StringBuffer();

			cmd.append(sd.getExecutable().toString());
			if (sd.getArguments() != null) {
				String[] args = sd.getArguments();
				for (int i = 0; i < args.length; ++i) {
					cmd.append(" ");
					cmd.append(args[i]);
				}
			}
			job.print(cmd.toString() + "\n");
			//job.print("exit $?\n");
		} catch (Throwable e) {
			throw new GATInvocationException(
					"Cannot create temporary job script file "
							+ temp.getAbsolutePath(), e);
		} finally {
			if (job != null)
				job.close();
		}
		return temp;
	}*/
private String sshLsfSubmission(SshLSFJob lsfJob, JobDescription description, java.io.File bsubFile, ResourceBroker subBroker, Sandbox sandbox) throws GATInvocationException {
    java.io.File slurmResultFile = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("***** Doing sandbox prestage " + sandbox.getSandboxPath());
        }
        sandbox.prestage();
        if (logger.isDebugEnabled()) {
            logger.debug("***** Sandbox prestage done " + sandbox.getSandboxPath());
        }
        // Create sbatch job
        SoftwareDescription sd = new SoftwareDescription();
        sd.setExecutable("sh");
        sd.setArguments("-c", "bsub < " + bsubFile.getName() + " 2>submit.err");
        // + " && rm -rf " + bsubFile.getName() + " submit.err");
        sd.setAttributes(description.getSoftwareDescription().getAttributes());
        sd.addAttribute(SoftwareDescription.SANDBOX_USEROOT, "true");
        slurmResultFile = java.io.File.createTempFile("GAT", "tmp");
        try {
            sd.setStdout(GAT.createFile(gatContext, new URI(slurmResultFile.toURI())));
            sd.addPreStagedFile(GAT.createFile(gatContext, new URI(bsubFile.toURI())));
        } catch (Throwable e1) {
            try {
                sandbox.removeSandboxDir();
            } catch (Throwable e) {
            // ignore
            }
            throw new GATInvocationException("Could not create GAT object for temporary " + slurmResultFile.getAbsolutePath(), e1);
        }
        // sd.addAttribute(SoftwareDescription.DIRECTORY, sd.getStringAttribute(SoftwareDescription, defaultVal)));
        JobDescription jd = new JobDescription(sd);
        if (logger.isDebugEnabled()) {
            logger.debug("Submitting lsf job: " + sd);
        }
        Job job = subBroker.submitJob(jd, this, "job.status");
        if (logger.isDebugEnabled()) {
            logger.debug("Job submitted.");
        }
        synchronized (job) {
            while (job.getState() != Job.JobState.STOPPED && job.getState() != Job.JobState.SUBMISSION_ERROR) {
                try {
                    job.wait();
                } catch (InterruptedException e) {
                // ignore
                }
            }
        }
        if (job.getState() != Job.JobState.STOPPED || job.getExitStatus() != 0) {
            try {
                sandbox.removeSandboxDir();
            } catch (Throwable e) {
            // ignore
            }
            logger.debug("jobState = " + job.getState() + ", exit status = " + job.getExitStatus());
            throw new GATInvocationException("Could not submit LSF job");
        }
        // submit success.
        BufferedReader in = new BufferedReader(new FileReader(slurmResultFile.getAbsolutePath()));
        String result = in.readLine();
        if (logger.isDebugEnabled()) {
            logger.debug("bsub result line = " + result);
        }
        // Check for LSF bsub result ...
        // TODO Check if LSF return the same
        String job_prefix = "Job <";
        if (result.contains(job_prefix)) {
            int i = result.indexOf(job_prefix);
            result = result.substring(i + job_prefix.length(), result.indexOf(">", i));
        }
        return result;
    } catch (IOException e) {
        try {
            sandbox.removeSandboxDir();
        } catch (Throwable e1) {
        // ignore
        }
        throw new GATInvocationException("Got IOException", e);
    } finally {
        slurmResultFile.delete();
        bsubFile.delete();
    }
}
Also used : IOException(java.io.IOException) URI(org.gridlab.gat.URI) File(java.io.File) SoftwareDescription(org.gridlab.gat.resources.SoftwareDescription) JobDescription(org.gridlab.gat.resources.JobDescription) WrapperJobDescription(org.gridlab.gat.resources.WrapperJobDescription) AbstractJobDescription(org.gridlab.gat.resources.AbstractJobDescription) GATInvocationException(org.gridlab.gat.GATInvocationException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Job(org.gridlab.gat.resources.Job)

Example 7 with Job

use of org.gridlab.gat.resources.Job in project compss by bsc-wdc.

the class ResourceBrokerAdaptorTest method submitJobEnvironment.

private AdaptorTestResultEntry submitJobEnvironment(GATContext gatContext, Preferences preferences, String host) {
    SoftwareDescription sd = new SoftwareDescription();
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("JAVAGAT_TEST_KEY", "javagat-test-value");
    sd.setEnvironment(env);
    sd.setExecutable("/usr/bin/env");
    try {
        sd.setStdout(GAT.createFile(gatContext, preferences, "stdout"));
    } catch (GATObjectCreationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    JobDescription jd = new JobDescription(sd);
    ResourceBroker broker;
    try {
        broker = GAT.createResourceBroker(gatContext, preferences, new URI(host));
    } catch (GATObjectCreationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    } catch (URISyntaxException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    long start = System.currentTimeMillis();
    Job job;
    try {
        job = broker.submitJob(jd, this, "job.status");
    } catch (GATInvocationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    waitForJob();
    try {
        Map<String, Object> info = job.getInfo();
        Throwable ex = (Throwable) info.get("poststage.exception");
        if (ex != null) {
            return new AdaptorTestResultEntry(false, 0L, ex);
        }
    } catch (GATInvocationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    long stop = System.currentTimeMillis();
    boolean success = false;
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new java.io.FileInputStream("stdout")));
        while (true) {
            String result = reader.readLine();
            if (result == null) {
                break;
            }
            if (result.contains("JAVAGAT_TEST_KEY") && result.contains("javagat-test-value")) {
                success = true;
            }
        }
        reader.close();
    } catch (Exception e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    return new AdaptorTestResultEntry(success, (stop - start), null);
}
Also used : GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ResourceBroker(org.gridlab.gat.resources.ResourceBroker) URISyntaxException(java.net.URISyntaxException) URI(org.gridlab.gat.URI) SoftwareDescription(org.gridlab.gat.resources.SoftwareDescription) URISyntaxException(java.net.URISyntaxException) GATInvocationException(org.gridlab.gat.GATInvocationException) IOException(java.io.IOException) GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) JobDescription(org.gridlab.gat.resources.JobDescription) GATInvocationException(org.gridlab.gat.GATInvocationException) BufferedReader(java.io.BufferedReader) Job(org.gridlab.gat.resources.Job)

Example 8 with Job

use of org.gridlab.gat.resources.Job in project compss by bsc-wdc.

the class ResourceBrokerAdaptorTest method submitJobGetInfo.

private AdaptorTestResultEntry submitJobGetInfo(GATContext gatContext, Preferences preferences, String host) {
    SoftwareDescription sd = new SoftwareDescription();
    sd.setExecutable("/bin/sleep");
    sd.setArguments("2");
    JobDescription jd = new JobDescription(sd);
    ResourceBroker broker;
    try {
        broker = GAT.createResourceBroker(gatContext, preferences, new URI(host));
    } catch (GATObjectCreationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    } catch (URISyntaxException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    Job job = null;
    Exception exception = null;
    long start = System.currentTimeMillis();
    try {
        job = broker.submitJob(jd);
    } catch (GATInvocationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    while (job.getState() != Job.JobState.STOPPED) {
        Map<String, Object> info = null;
        try {
            info = job.getInfo();
        } catch (GATInvocationException e) {
            exception = e;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
            // ignored
            }
            continue;
        }
        if (info == null) {
            exception = new Exception("getInfo returns null");
        } else {
            if (!info.containsKey("state")) {
                exception = new Exception("getInfo doesn't contain a key 'state'");
            }
            if (!info.containsKey("hostname")) {
                exception = new Exception("getInfo doesn't contain a key 'hostname'");
            } else {
                if (info.get("state").equals(Job.JobState.RUNNING) && info.get("hostname") == null) {
                    exception = new Exception("inconsistent getInfo: state=RUNNING, hostname=null");
                }
            }
            if (!info.containsKey("submissiontime")) {
                exception = new Exception("getInfo doesn't contain a key 'submissiontime'");
            }
            if (!info.containsKey("starttime")) {
                exception = new Exception("getInfo doesn't contain a key 'starttime'");
            }
            if (!info.containsKey("stoptime")) {
                exception = new Exception("getInfo doesn't contain a key 'stoptime'");
            }
            if (!info.containsKey("poststage.exception")) {
                exception = new Exception("getInfo doesn't contain a key 'poststage.exception'");
            }
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            exception = e;
        }
    }
    long stop = System.currentTimeMillis();
    return new AdaptorTestResultEntry(exception == null, (stop - start), exception);
}
Also used : GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) ResourceBroker(org.gridlab.gat.resources.ResourceBroker) URISyntaxException(java.net.URISyntaxException) URI(org.gridlab.gat.URI) SoftwareDescription(org.gridlab.gat.resources.SoftwareDescription) URISyntaxException(java.net.URISyntaxException) GATInvocationException(org.gridlab.gat.GATInvocationException) IOException(java.io.IOException) GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) JobDescription(org.gridlab.gat.resources.JobDescription) GATInvocationException(org.gridlab.gat.GATInvocationException) Job(org.gridlab.gat.resources.Job)

Example 9 with Job

use of org.gridlab.gat.resources.Job in project compss by bsc-wdc.

the class ResourceBrokerAdaptorTest method submitJobStderr.

private AdaptorTestResultEntry submitJobStderr(GATContext gatContext, Preferences preferences, String host) {
    SoftwareDescription sd = new SoftwareDescription();
    sd.setExecutable("/bin/ls");
    sd.setArguments("floep");
    try {
        sd.setStderr(GAT.createFile(gatContext, preferences, "stderr"));
    } catch (GATObjectCreationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    JobDescription jd = new JobDescription(sd);
    ResourceBroker broker;
    try {
        broker = GAT.createResourceBroker(gatContext, preferences, new URI(host));
    } catch (GATObjectCreationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    } catch (URISyntaxException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    long start = System.currentTimeMillis();
    Job job;
    try {
        job = broker.submitJob(jd, this, "job.status");
    } catch (GATInvocationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    waitForJob();
    try {
        Map<String, Object> info = job.getInfo();
        Throwable ex = (Throwable) info.get("poststage.exception");
        if (ex != null) {
            return new AdaptorTestResultEntry(false, 0L, ex);
        }
    } catch (GATInvocationException e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    long stop = System.currentTimeMillis();
    String result;
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new java.io.FileInputStream("stderr")));
        result = reader.readLine();
        reader.close();
    } catch (Exception e) {
        return new AdaptorTestResultEntry(false, 0L, e);
    }
    return new AdaptorTestResultEntry(result != null && result.endsWith("floep: No such file or directory"), (stop - start), null);
}
Also used : GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) InputStreamReader(java.io.InputStreamReader) ResourceBroker(org.gridlab.gat.resources.ResourceBroker) URISyntaxException(java.net.URISyntaxException) URI(org.gridlab.gat.URI) SoftwareDescription(org.gridlab.gat.resources.SoftwareDescription) URISyntaxException(java.net.URISyntaxException) GATInvocationException(org.gridlab.gat.GATInvocationException) IOException(java.io.IOException) GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) JobDescription(org.gridlab.gat.resources.JobDescription) GATInvocationException(org.gridlab.gat.GATInvocationException) BufferedReader(java.io.BufferedReader) Job(org.gridlab.gat.resources.Job)

Example 10 with Job

use of org.gridlab.gat.resources.Job in project compss by bsc-wdc.

the class ResourceBrokerPollingExample method start.

public void start(String brokerURI) {
    ResourceBroker broker = null;
    try {
        broker = GAT.createResourceBroker(new URI(brokerURI));
    } catch (GATObjectCreationException e) {
        System.err.println("Failed to create resource broker at location '" + brokerURI + "': " + e);
        return;
    } catch (URISyntaxException e) {
        System.err.println("Wrong uri '" + brokerURI + "': " + e);
        return;
    }
    SoftwareDescription sd = new SoftwareDescription();
    sd.setExecutable("/bin/hostname");
    try {
        sd.setStdout(GAT.createFile("hostname.txt"));
    } catch (GATObjectCreationException e) {
        System.err.println("Failed to create the stdout file 'hostname.txt': " + e);
        return;
    }
    JobDescription jd = new JobDescription(sd);
    Job job = null;
    try {
        job = broker.submitJob(jd);
    } catch (GATInvocationException e) {
        System.err.println("Failed to submit the job: " + e);
        return;
    }
    while (job.getState() != JobState.STOPPED) {
        System.out.println("job is in state: " + job.getState());
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
        // ignore
        }
    }
}
Also used : JobDescription(org.gridlab.gat.resources.JobDescription) GATObjectCreationException(org.gridlab.gat.GATObjectCreationException) GATInvocationException(org.gridlab.gat.GATInvocationException) ResourceBroker(org.gridlab.gat.resources.ResourceBroker) URISyntaxException(java.net.URISyntaxException) Job(org.gridlab.gat.resources.Job) URI(org.gridlab.gat.URI) SoftwareDescription(org.gridlab.gat.resources.SoftwareDescription)

Aggregations

Job (org.gridlab.gat.resources.Job)27 JobDescription (org.gridlab.gat.resources.JobDescription)25 SoftwareDescription (org.gridlab.gat.resources.SoftwareDescription)24 URI (org.gridlab.gat.URI)23 GATInvocationException (org.gridlab.gat.GATInvocationException)20 ResourceBroker (org.gridlab.gat.resources.ResourceBroker)19 GATObjectCreationException (org.gridlab.gat.GATObjectCreationException)14 URISyntaxException (java.net.URISyntaxException)13 IOException (java.io.IOException)10 BufferedReader (java.io.BufferedReader)7 Preferences (org.gridlab.gat.Preferences)6 File (org.gridlab.gat.io.File)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 AbstractJobDescription (org.gridlab.gat.resources.AbstractJobDescription)4 WrapperJobDescription (org.gridlab.gat.resources.WrapperJobDescription)4 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3 GATContext (org.gridlab.gat.GATContext)3 Metric (org.gridlab.gat.monitoring.Metric)3