dLife Home Page

dlife.tools.xgrid
Class XgridBatch

java.lang.Object
  extended by dlife.tools.xgrid.XgridBatch

public class XgridBatch
extends Object

A collection of XgridCommands that can be submitted to the Xgrid as a batch. The jobs for each of the XgridCommands contained in a batch can be manipulated via the commands in this class making it easy to wait for all of the jobs to complete, get all of the results and delete all of the jobs.

Version:
Jun 7, 2010
Author:
Grant Braught, Dickinson College

Constructor Summary
XgridBatch(String host, String password, String grid)
          Create a new batch of XgridCommands.
 
Method Summary
 void addCommand(XgridCommand cmd)
          Add a new command to this batch of XgridCommands.
 void cancelAndResubmit()
          Cancel all unfinished or failed jobs and resubmit them.
 void deleteAll()
          Delete the xgrid jobs for each of the commands in this batch.
 ArrayList<XgridResult> getAllResults()
          Get the results for all of the xgrid jobs that were submitted for the commands in this batch.
 ArrayList<XgridCommand> getCommands()
          Get an ArrayList containing all of the XgridCommands that have been added to this batch.
 ArrayList<String> getJobIds()
          Get the xgrid id numbers that have been assigned to the jobs for all of the commands in this batch.
 XgridResult getResult(XgridCommand cmd)
          Get the results of the job for the specified XgridCommand.
 boolean hasFailures()
          Check if any of the jobs for this batch have failed.
 void resubmitFailedJobs()
          Cancel all failed jobs and resubmit them.
 boolean stillRunning()
          Check if any of the jobs for this batch are still running.
 void submit()
          Submit this batch of XgridCommands to the xgrid.
 void submitAndWait()
          Submit this batch of XgridCommands to the xgrid and wait for all of the jobs to complete (either successfully or unsuccessfully) before returning.
 void submitAndWait(int maxTime, int maxSubmits)
          Submit this batch of XGridCommands to the xgrid and wait for all of the jobs to complete successfully.
 boolean submitted()
          Return true if this batch of commands has already been submitted via the submit or the submitAndWait method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XgridBatch

public XgridBatch(String host,
                  String password,
                  String grid)
Create a new batch of XgridCommands.

Parameters:
host - the hostname of the grid controller to which to submit the commands. Can be null if the XGRID_CONTROLLER_HOSTNAME environment variable is set on the machine being used.
password - the password for the grid. Can be null if the XGRID_CONTROLLER_PASSWORD environment variable is set on the machine being used or the grid does not require a password.
grid - the name of the logical grid on which the command will be run. If this is null the command will be run on the default grid. Note: xgrid uses numeric gid's to identify the grids. This is not that id, it is the text name of the grid. This method will translate automatically to the xgrid gid value.
Method Detail

addCommand

public void addCommand(XgridCommand cmd)
Add a new command to this batch of XgridCommands. New commands can not be added after the batch has been submitted.

Parameters:
cmd - the XgridCommand to add to the batch
Throws:
IllegalStateException - if the batch has already been submitted.

submitted

public boolean submitted()
Return true if this batch of commands has already been submitted via the submit or the submitAndWait method.

Returns:
true if the batch has been submitted.

submit

public void submit()
Submit this batch of XgridCommands to the xgrid. This method does not wait for the jobs to complete before returning.

Throws:
IllegalStateException - if the batch has already been submitted.

submitAndWait

public void submitAndWait()
Submit this batch of XgridCommands to the xgrid and wait for all of the jobs to complete (either successfully or unsuccessfully) before returning. It is the caller's responsibility to check if the jobs have completed successfully or have failed.

Throws:
IllegalStateException - if the batch has already been submitted.

submitAndWait

public void submitAndWait(int maxTime,
                          int maxSubmits)
                   throws XGridFailureException
Submit this batch of XGridCommands to the xgrid and wait for all of the jobs to complete successfully. If all jobs complete successfully, this method will return at that point. If jobs either fail or do not complete within the specified maxTime, then those failed or incomplete jobs will be canceled and resubmitted to the xgrid up to maxSubmits times. If any job does not complete successfully after maxSubmits submissions (about maxTime * maxSubmits ms) then this method throws an XgridFailureException.

Parameters:
maxTime - the maximum time (in ms) to wait for all of the jobs to complete. If the jobs do not complete in this amount of time, then they will be resubmitted up to maxSubmits-1 times. This method also waits up to maxTime for each resubmission to complete.
maxSubmits - the maximum number of time to submit the jobs in this batch. This method will wait up to maxTime for the jobs to complete. If they do not complete within that time they will be resubmited up to maxSubmits-1 times.
Throws:
XGridFailureException - thrown if there are jobs that have not successfully completed after maxSubmits submissions.

getJobIds

public ArrayList<String> getJobIds()
Get the xgrid id numbers that have been assigned to the jobs for all of the commands in this batch. If the job has not yet been submitted the list of job ids that is returned will be empty.

Returns:
the xgrid ids of the commands in this batch.

stillRunning

public boolean stillRunning()
Check if any of the jobs for this batch are still running.

Returns:
true if any of the jobs are still running and false if either they have not been submitted or have all completed (either successfully or unsuccessfully).
Throws:
IllegalStateException - if any of the XgridCommands have already been deleted.

hasFailures

public boolean hasFailures()
Check if any of the jobs for this batch have failed.

Returns:
true if any of the jobs have failed and false if not.
Throws:
IllegalStateException - if any of the XgridCommands have already been deleted.

getCommands

public ArrayList<XgridCommand> getCommands()
Get an ArrayList containing all of the XgridCommands that have been added to this batch. They are store in the ArrayList in the order that they were added to the batch (e.g. the first one added appears at index 0).

Returns:
a list of the commands in the batch.

getResult

public XgridResult getResult(XgridCommand cmd)
Get the results of the job for the specified XgridCommand. This is simply a convenience method that returns the result of calling getResult on the cmd object.

Parameters:
cmd - the XgridCommand for which to retrieve the results.
Returns:
the results of the job. If the job is finished (i.e. its isFinished() method would return true) then the returned XgridResult will contain all of the results for the job and all files will be retrieved from the xgrid controller (as appropriate). If the job is not finished the returned XgridResult will contain the current attributes of the job but not the results.
Throws:
IllegalStateException - if this XgridCommand has not yet been submitted or the job has been deleted.

getAllResults

public ArrayList<XgridResult> getAllResults()
Get the results for all of the xgrid jobs that were submitted for the commands in this batch. This method simply aggregates the results returned by calling getResutls on each of the commands in this batch.

Returns:
a list of the XgridResult object for each of the commands in this batch. The order of the results corresponds to the order in which the commands were added to the batch (e.g. the result for the result for the first command added appear at index 0.)
Throws:
IllegalStateException - if this batch of commands has not yet been submitted or any of the jobs have been deleted.

deleteAll

public void deleteAll()
Delete the xgrid jobs for each of the commands in this batch.


cancelAndResubmit

public void cancelAndResubmit()
Cancel all unfinished or failed jobs and resubmit them. The unfinished or failed jobs are deleted from the grid.


resubmitFailedJobs

public void resubmitFailedJobs()
Cancel all failed jobs and resubmit them. The failed jobs are deleted from the grid. Jobs that are still running are unaffected by this method.


dLife Home Page