{section: How To Run Self-Checkpointing Jobs} This HOWTO describes how to use the features of HTCondor to run self-checkpointing jobs. This HOWTO is not about to checkpoint a given job; that's up to you or your software provider. This HOWTO focuses on using =+WantFTOnCheckpoint=, a submit command which causes HTCondor to do file transfer when a job indicates that it has taken a checkpoint by exiting in a specific way. While there are other approaches, we recommend this one because it allows jobs to checkpoint and resume across as many different types of interruptions as possible, and because among the techniques that make the same claim, it's the simplest. The =+WantFTOnCheckpoint= submit command makes a number of assumptions about the job, detailed in the 'Assumptions' section. These assumptions may not be true for your job, but in many cases, you will be able to modify your job (by adding a wrapper script or altering an existing one) to make them true. If you can not, consult the _Working Around the Assumptions_ section and/or the _Other Options_ section. {subsection: General Idea} The general idea of checkpointing is, of course, for your job to save its progress in such a way that it can pick up where it left off after being interrupted. Interruptions in the HTCondor system generally take three different forms: temporary (eviction and preemption), permanent (=condor_rm=), and recoverable failures (network outages, software faults, hardware problems, being held). Of course, no checkpoint system can recover from permanent failures, but =+WantFTOnCheckpoint= -- unlike many choices in the _Other Options_ section -- readily allows recovery from the other types of interruptions. File transfer on checkpoint works in the following way: 1: The job exits after taking a checkpoint with a unique exit code. For example, the following Python script exits with code 77. {file: 77.py} import sys print("Now exiting with code 77.") sys.exit(77) {endfile} 2: HTCondor recognizes the unique exit code and does file transfer; this file transfer presently behaves as it would if the job were being evicted. Thus, your job must set =when_to_transfer_output= to =ON_EXIT_OR_EVICT=. 3: After the job's =transfer_output_files= are successfully sent to the submit node (and are stored in the schedd's spool, as normal for file transfer on eviction), HTCondor restarts the job exactly as it started it the first time. 4: If something interrupts the job, HTCondor reschedules it as normal. As for any job with =ON_EXIT_OR_EVICT= set, HTCondor will restart the job with the files stored in the schedd's spool. By storing the job's checkpoint on the submit node as soon as it is created, this method allows the job to resume even if the execute node becomes unavailable. Transferring the checkpoint before the job is interrupted is also more reliable, not just because not all interruptions permit file transfer, but because job lifetimes are typically much longer than eviction deadlines, so slower transfers are much more likely to complete. {subsection: Assumptions} 1: Your job exits after taking a checkpoint with an exit code it does not otherwise use. *:: If your job does not exit when it takes a checkpoint, HTCondor can not (currently) transfer its checkpoint. If your job exits normally when it takes a checkpoint, HTCondor will not be able to tell the difference between taking a checkpoint and actually finishing; if the checkpoint code and the terminal exit code are the same, your job will never finish. For example, the following Python script exits with code 77 half of the time, and code 0 half of the time. {file: 77-time.py} import sys import time if time.time() % 2 > 1: sys.exit(77) else: sys.exit(0) {endfile} 2: When restarted, your job determines on its own if a checkpoint is available, and if so, uses it. *:: If your job does not look for a checkpoint each time it starts up, it will start from scratch each time; HTCondor does not run a different command line when restarting a job which has taken a checkpoint. 3: Starting your job up from a checkpoint is relatively quick. *:: If starting your job up from a checkpoint is relatively slow, your job may not run efficiently enough to be useful, depending on the frequency of checkpoints and interruptions. 4: Your job atomically updates its checkpoint file(s). *:: Because eviction may occur at any time, if your job does not update its checkpoints atomically, HTCondor may transfer a partially-updated checkpoint when your job is evicted. {subsection: Using +WantFTOnCheckpoint} Given the following toy Python script: {file: vuc.py} #!/usr/bin/env python import sys import time value = 0 try: with open('vuc.ckpt', 'r') as f: value = int(f.read()) except IOError: pass print("Starting from {0}".format(value)) for i in range(value,10): print("Computing timestamp {0}".format(value)) time.sleep(1) value += 1 with open('vuc.ckpt', 'w') as f: f.write("{0}".format(value)) if value%3 == 0: sys.exit(77) print("Computation complete") sys.exit(0) {endfile} the following submit file will transfer the file 'vuc.ckpt' back to the submit at timesteps 3, 6, and 9. If interrupted, it will resume from the most recent of those checkpoints. {file: vuc.submit} # These first three lines are the magic ones. when_to_transfer_output = ON_EXIT_OR_EVICT +WantFTOnCheckpoint = TRUE +SuccessCheckpointExitCode = 77 executable = vuc.py arguments = should_transfer_files = yes transfer_output_files = vuc.ckpt output = vuc.out error = vuc.err log = vuc.log queue 1 {endfile} This script/submit file combination does not remove the "checkpoint file" generated for timestep 9 when the job completes. This could be done in 'vuc.py' immediately before it exits, but that would cause the final file transfer to fail. The script could instead remove the file and then re-create it empty, it desired. {subsection: How Frequently to Checkpoint} Ballpark, your job should aim to checkpoint once an hour. However, this depends on a number of factors: *: How frequently your job "naturally" checkpoints. For instance, a particular simulation may compute a time step roughly every five minutes on an average machine. If the simulator can be configured to write a checkpoint every ten time-steps, that gives the simulator and HTCondor ten minutes to write and then transfer the checkpoint. (Of course, if it actually takes that long, your job will take a sixth longer, which may be too slow; see below.) If instead each job is a batch of smaller tasks (e.g., run six different computations on the same input data), it may be most convenient to checkpoint after each computation completes. If that takes about forty-five minutes for some and ninety for others, that's fine. If your job writes checkpoints very frequently, you may only want to exit every tenth (or whatever) checkpoint. *: How frequently your job is interrupted. For instance, if the max job lifetime on your pool is six hours, taking a checkpoint every three hours could easily result in losing almost (but not quite actually) three hours of work. *: How long it takes to make and transfer checkpoints. Measuring the first may be difficult (how do you know when the job started taking a checkpoint?), but the second, in practice, can only be done experimentally. (HTCondor versions 8.9.1 and later record file transfer events in the user job log (the =log= submit command), including for file transfers on checkpoint, so this duration should be easy to determine.) Generally, when checkpoints take longer to make and/or transfer, you want to checkpoint less frequently, so as to maintain efficient job progress. However, the longer between intervals, the more progress the job will lose when interrupted. *: Your appetite for deadline risk versus your desire for fast turn-arounds. Generally, the longer you go between checkpoints, the sooner the job will complete (because taking checkpoints and transferring them takes time). On the other hand, if the job is interrupted, you'll wait longer because the job will have to recompute more. {subsection: Debugging Self-Checkpointing Jobs} Because a job may be interrupted at any time, it's valid to interrupt the job at any time and see if a valid checkpoint is transferred. To do so, use =condor_vacate_job= to evict the job. When it's done transferring (watch the user log), use =condor_hold= to put it on hold, so that it can't restart while you're looking at the checkpoint (and potentially, overwrite it). Finally, to obtain the checkpoint file(s) themselves, use =condor_config_val= to ask where the SPOOL is, and then look in appropriate subdirectory. For example, if your job is ID 635.0, and is logging to the file 'job.log', you can copy the files in the checkpoint to a subdirectory of the current as follows: {code} condor_vacate_job 635.0 # Wait for the job to finish being evicted; hit CTRL-C when you see 'Job was evicted.' tail --follow job.log condor_hold 635.0 # Copy the checkpoint files from the spool. # Note that _condor_stderr and _condor_stdout are the files corresponding to the job's # output and error submit commands; they aren't named correctly until the the job finishes. cp -a `condor_config_val SPOOL`/635/0/cluster635.proc0.subproc0 . # Then examine the checkpoint files to see if they look right. # When you're done, release the job to see if it actually works right. condor_release 635.0 condor_ssh_to_job 635.0 {endcode} {subsection: Working Around the Assumptions} 1: If your job can be made to exit after taking a checkpoint, but does not return a unique exit code when doing so, a wrapper script for the job may be able to inspect the sandbox after the job exits and determine if a checkpoint was successfully created. If so, this wrapper script could then return a unique value. If the job can return literally any value at all, HTCondor can also regard being killed by a particular (unique) signal as a sign of a successful checkpoint; set =+SuccessCheckpointExitBySignal= to =TRUE= and =+SuccessCheckpointExitSignal= to the particular signal. If your job can not be made to exit after taking a checkpoint, a wrapper script may be able to determine when a successful checkpoint has been taken and kill the job. 2: If your job requires different arguments to start from a checkpoint, you can wrap your job in a script which checks for the presence of a checkpoint and runs the jobs with the corresponding arguments. 3: If it takes too long to start your job up from a checkpoint, you will need to take checkpoints less often to make quicker progress. This, of course, increases the risk of losing a substantial amount of work when your job is interrupted. See also the 'Delayed and Manual Transfers' section. 4: If a job does not update its checkpoints atomically, you can use a wrapper script to atomically update some other file(s) and treat those as the checkpoints. More specifically, if your job writes a checkpoint incrementally to a file called 'checkpoint', but exits with code 17 when it's finished doing so, your wrapper script can check for exit code 17 and then rename 'checkpoint' to 'checkpoint.atomic'. Because rename is an atomic operation, this will prevent HTCondor from transferring a partial checkpoint file, even if the job is interrupted in the middle of taking a checkpoint. Your wrapper script will also have to copy 'checkpoint.atomic' to 'checkpoint' before starting the job, so that the job (a) uses the safe checkpoint file and (b) doesn't corrupt that checkpoint file if interrupted at a bad time. The following Python script implements the logic above: {file: 77-atomic.py} FIXME {endfile} Future version of HTCondor may remove the requirement for job to set =when_to_transfer_output= to =ON_EXIT_OR_EVICT=. Doing so would relax this requirement; the job would only have to ensure that its checkpoint was complete and consistent (if stored in multiple files) when it exited. (HTCondor does not partially update the sandbox stored in spool: either every file succesfully transfers back, or none of them do.) Future versions of HTCondor may provide for explicit coordination between the job and HTCondor. Modifying a job to explicitly coordinate with HTCondor would substantially alter the assumptions. {subsection: Other Options} The preceding sections of this HOWTO explain how a job meeting this HOWTO's assumptions can take checkpoints at arbitrary intervals and transfer them back to the submit node. Although this is the method of operation most likely to result in an interrupted job continuing from a valid checkpoint, other, less reliable options exist. {subsubsection: Delayed and Manual Transfers} If your job takes checkpoints but can not exit with a unique code when it does, you have two options. The first is much simpler, but only preserves progress when your job is evicted (e.g., not when the machine shuts down or the network fails). To ensure that the checkpoint(s) a job has already taken are transferred when evicted, set =when_to_transfer_output= to =ON_EXIT_OR_EVICT=, and include the checkpoint file(s) in =transfer_output_files=. All the other assumptions still apply, except that quick restarts may be less important if eviction is infrequent in your pool. If your job can determine when it has successfully taken a checkpoint, but it can not stop when it does, or doing so is too expensive, it could instead transfer its checkpoints without HTCondor's help. In most cases, this will involve using =condor_chirp= (by setting =+WantIOProxy= to =TRUE= and calling the =condor_chirp= command-line tool). Your job would be responsible for fetching its own checkpoint file(s) on start-up. (You could also create an empty checkpoint file and list it as part of =transfer_input_files=.) {subsubsection: Signals} If your job can not spontaneously exit with a unique exit code after taking a checkpoint, but can take a checkpoint when sent a particular signal and _then_ exit in a unique way, you may set =+WantCheckpointSignal= to =TRUE=, and =+CheckpointSig= to the particular signal. HTCondor will send this signal to the job at interval set by the administrator of the execute machine; if the job exits as specified by the =SuccessCheckpoint= job attributes, its files will be transferred and the job restarted, as usual. {subsubsection: Reactive Checkpoints} Instead of taking a checkpoint at some interval, it is possible, for some types of interruption, to instead take a checkpoint when interrupted. Specifically, if your execution resources are generally reliable, and your job's checkpoints both quick to take and small, your job may be able to generate a checkpoint, and transfer it back to the submit node, at the time your job is evicted. This works like the previous section, except that you set =when_to_transfer_output= to =ON_EXIT_OR_EVICT= and =KillSig= to the particular signal (that causes your job to checkpoint), and the signal is only sent when your job is preempted. The administrator of the execute machine determines the maximum amount of time is allowed to run after receiving its =KillSig=; a job may request a longer delay than the machine's default by setting =JobMaxVacateTime= (but this will be capped by the administrator's setting). You should probably only use this method of operation if your job runs on an HTCondor pool too old to support =+WantFTOnCheckpoint=, or the pool administrator has disallowed use of the feature (because it can be resource-intensive). {subsubsection: Early Checkpoint Exits} If your job's natural checkpoint interval is half or more of your pool's max job runtime, it may make sense to checkpoint and then immediately ask to be rescheduled, rather than lower your user priority doing work you know will be thrown away. In this case, you can use the =OnExitRemove= job attribute to determine if your job should be rescheduled after exiting. Don't set =ON_EXIT_OR_EVICT=, and don't set =+WantFTOnCheckpoint=; just have the job exit with a unique code after its checkpoint.