library(rush)
branin = function(x1, x2) {
(x2 - 5.1 / (4 * pi^2) * x1^2 + 5 / pi * x1 - 6)^2 + 10 * (1 - 1 / (8 * pi)) * cos(x1) + 10
}
wl_random_search = function(rush, branin) {
while (rush$n_finished_tasks < 100) {
xs = list(x1 = runif(1, -5, 10), x2 = runif(1, 0, 15))
key = rush$push_running_tasks(xss = list(xs))
tryCatch({
if (runif(1) < 0.5) stop("Random Error")
ys = list(y = branin(xs$x1, xs$x2))
rush$finish_tasks(key, yss = list(ys))
}, error = function(e) {
condition = list(message = e$message)
rush$fail_tasks(key, conditions = list(condition))
})
}
}rush provides error-handling mechanisms for two failure modes: standard R errors during task evaluation and unexpected worker failures such as crashes or lost connections. If errors cannot be caught automatically, the worker loop can be debugged locally.
Simple R Errors
We use the random search example from the tutorial and introduce a random error with 50% probability. Within the worker loop, users are responsible for catching errors and marking the corresponding task as "failed" using the $fail_tasks() method.
We initialize the network and start the workers.
rush = rsh(
network = "test-simple-error",
config = redux::redis_config())
mirai::daemons(4)
rush$start_workers(
worker_loop = wl_random_search,
n_workers = 4,
branin = branin)When an error occurs, the task is marked as "failed" and the error message is stored in the "message" column. This ensures that errors do not interrupt the overall execution and allows subsequent inspection and reevaluation of failed tasks.
rush$fetch_failed_tasks() x1 x2 worker_id condition keys
<num> <num> <char> <list> <char>
1: 2.572199 2.482924 refreshful... <list[1]> ee687727-b...
2: -1.986177 11.923412 contradict... <list[1]> 7d35c7a0-d...
3: 3.711689 1.905521 refreshful... <list[1]> cab9b5b9-a...
4: -4.653192 4.163906 contradict... <list[1]> b025b725-a...
5: 9.342368 13.138830 refreshful... <list[1]> 02fc5dc3-a...
---
106: 4.669072 10.166325 contradict... <list[1]> 3bc37bb0-9...
107: 4.027866 13.918090 kainolopho... <list[1]> 95c8f7ec-2...
108: -2.995506 8.620931 archaistic... <list[1]> e29d093e-e...
109: 3.637736 11.293786 kainolopho... <list[1]> 3c845975-2...
110: 9.868069 12.899744 archaistic... <list[1]> 550dd0c2-b...
Handling Failing Workers
When a worker fails due to a crash or lost connection, its tasks may remain in the "running" state indefinitely. We simulate a segmentation fault by terminating the worker process.
wl_failed_worker = function(rush) {
xs = list(x1 = runif(1, -5, 10), x2 = runif(1, 0, 15))
key = rush$push_running_tasks(xss = list(xs))
tools::pskill(Sys.getpid(), tools::SIGKILL)
}
rush = rsh(
network = "test-failed-workers",
config = redux::redis_config())
mirai::daemons(2)
worker_ids = rush$start_workers(
worker_loop = wl_failed_worker,
n_workers = 2)The $detect_lost_workers() method identifies such workers and updates their state to "terminated". For workers started with $start_local_workers() or $start_workers(), lost worker detection works automatically by checking process status. Workers started via $worker_script() require an additional heartbeat mechanism (see the manager vignette).
rush$detect_lost_workers()[1] "brainsick_alligatorgar_f28d6e57"
[2] "diamantiferous_cleanerwrasse_7c33246e"
When a worker fails, the state of any task it was evaluating is set to "failed".
rush$fetch_failed_tasks() x1 x2 worker_id condition keys
<num> <num> <char> <list> <char>
1: 0.03488415 13.06521 brainsick_... <list[1]> 016f71a5-e...
2: -3.41829768 10.73486 diamantife... <list[1]> c6eb9af2-c...
Debugging
When the worker loop fails due to an uncaught error, the loop can be executed locally to reproduce and inspect the failure. Consider the following worker loop, which generates an error for large values of x1.
To debug the worker loop locally, a RushWorker instance is instantiated manually and passed as argument to the worker loop.
rush_worker = RushWorker$new(network_id = "test-error")
wl_error(rush_worker)Error in `wl_error()`:
! Unexpected error
When an error is raised in the main process, traceback() can be called to examine the stack trace and breakpoints can be set within the worker loop to inspect the program state. Note that certain errors such as missing packages may not be reproducible locally but can be identified by running the worker loop in a separate process and using $detect_lost_workers().
rush = rsh(
network = "test-error",
config = redux::redis_config())
mirai::daemons(1)
rush$start_workers(
worker_loop = wl_error,
n_workers = 1)
rush$detect_lost_workers()[1] "reflectible_anteater_58458f9b"
Output and message logs can be written to files via the message_log and output_log arguments.
rush = rsh(
network = "test-error",
config = redux::redis_config())
message_log = tempdir()
output_log = tempdir()
mirai::daemons(1)
worker_ids = rush$start_workers(
worker_loop = wl_error,
n_workers = 1,
message_log = message_log,
output_log = output_log)
Sys.sleep(5)
readLines(file.path(message_log, sprintf("message_%s.log", worker_ids[1])))[1] "Debug message logging on worker volumed_bluefintuna_e0febfc7 started"
[1] "[1] \"Debug output logging on worker volumed_bluefintuna_e0febfc7 started\""