-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[scheduler] Even more resilient perl beanstalkd annotation worker #545
[scheduler] Even more resilient perl beanstalkd annotation worker #545
Conversation
…in client-side timeout
…ecessary, because we properly touch() jobs
…stro do not run and produce outputs in the same directory
@@ -51,7 +52,7 @@ has config => ( | |||
is => 'ro', | |||
isa => 'Str', | |||
coerce => 1, | |||
required => 1, | |||
required => 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer required, so that we can defer the check of whether we have config
until we've seen whether we have json_config
, which may define config
.
@@ -41,15 +42,46 @@ | |||
logger = logging.getLogger(__name__) | |||
|
|||
|
|||
class FunctionWrapper: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python ProcessPoolExecutor cannot serialize (to send to other processes) functions that are not defined as top-level functions (e.g. defined in this module, or imported from another module); the functions we're executing are instead passed to the listen() function.
To get around that, we use a custom serializer: cloud pickle, which is the serializer used by Ray, for the same purpose.
# Signal handler function | ||
def kill_child_processes(parent_pid=os.getpid(), kill_parent=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this to ensure that the process thathandler_fn
is running in, as well as any child processes handler_fn spawns are cleaned up either on program exit, or if touching the job fails with NOT_FOUND (or another exception), because if that happens the present worker no longer owns the job, and therefore the job is free to be picked up by another worker (applies when multiple workers are found; but even with a single worker we can get behavior that appears like multiple workers, because event message lifetimes are decoupled from submission message lifetimes).
Since the main comment is busy:
|
perl/lib/Seq.pm
Outdated
unlink($lockPath); | ||
} | ||
catch { | ||
$self->log( 'warn', "Failed to close and delete lock file: $_" ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to STDERR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Walked through the overall process changes in miro (https://miro.com/app/board/uXjVKzUcjLM=/?share_link_id=838501974882), recommended to add workflow to documentation in the future.
Overall, lgtm!
This change makes it possible to have multiple annotation workers competing for jobs, even in the presence of network instability, and removes race conditions that could result in double submission and parallel workers writing to the same directory in the case of such instability. This change also makes Python beanstalkd workers more robust to network communication issues, such as application load balancer enforced connection interruptions on idle TCP connections.
Enables annotation jobs to run indefinitely, but while also making those jobs' TTR (time to run, see https://github.com/beanstalkd/beanstalkd/blob/master/doc/protocol.txt) potentially as short as a few seconds. We now use a keep-alive mechanism on job submissions. This makes it possible to retry jobs faster if the worker that picked up the job times out.
bystro-annotate.pl and Seq/ packages now consistently output debug messages to STDERR, and you can now pipe the output from bystro-annotate.pl for downstream processing; the output is a JSON message containing the result paths and result summary:
Semaphores to make double writes impossible: Seq.pm takes an exclusive lock on
bystro_annotation.lock
file in the target output directory. If it cannot, annotation fails. This file lock is kept until annotation is completed.bystron_annotation.completed
file is written before the exclusive lock is released. Presence of this file is checked before annotation begins (after exclusive lock is acquired), to ensure that we do not accidentally re-attempt processing of an annotation after completion. The reason this is important is that in the case of submissions through the Bystro API server / UI, after the job is completed, the output files are used for automatic ancestry, search, and (potentially) PRS calculations. If an annotation worker re-attempts to write the annotation while indexing/ancestry/prs (and in the future) other workers are processing data, we have introduced a race condition that potentially leads to corruption (the annotation files could be in an incomplete state just before the indexer attempts to read them, or during read they could be modified, for instance).Remove Perl Bystro Annotation server (bystro-server.pl) in favor of calling the bystro-annotate.pl command line program from the Python beanstalkd worker. This is done to consolidate beanstalkd communication code, to improve ease of debugging.
python/beanstalkd/worker.py: now runs
handler_fn
using ProcessPoolExecutor (instead of ThreadPoolExecutor), so as to not conflict with Ray's signal handler, and to make it possible to kill all handler_fn child processes.python/beanstalkd/worker.py: kills the child processes associated with
handler_fn
if the job touch() fails with NOT_FOUND while thehandler_fn
child processes are still running.NOT_FOUND
indicates that the job is no longer available to the worker for processing: at this point the worker should attempt to stop job processing (whatever is running in handler_fn), and must ensure that any other workers of the same kind that pick up the job would supersede it.python/beanstalkd/worker.py: finer-grained error handling, and will not die on client timeout errors. Instead we sleep for 1s and re-attempt connection, indefinitely (or until the process is killed).
Example of new perl/bin/bystro-annotate.pl output:
Additional Background and Motivation:
Perl MCE (Many Cores Engine), at least in our hands, could not launch a 2nd parallel process for touching jobs (would conflict with launching the process pool for annotation), making it necessary to fork the annotation process anyway.
By having the beanstalkd worker run bystro-annotate.pl, rather than calling Seq.pm directly, we ensure our command line interface gets use, and that we discover usability improvements, as we have done in this PR.
Annotation jobs required long TTR (time to run) leases, because the annotation workers would not periodically touch the job to refresh the lease. If the client became unresponsive, say due to network outage, such that the job would not be completed or failed (or communication from the worker to beanstalkd server during delete/release operations failed), the job would only be retried after the TTR lease expired. Currently that lease is 48 hours. With this change jobs can run as long as needed, even with short TTRs, so that retrying the job after unresponsive client happened much faster (we could set the TTR to say 30 minutes).