| 1337 | } |
| 1338 | |
| 1339 | static application_attempt_status_t application_job_run_attempt(cbm_daemon_application_job_t *job, |
| 1340 | const char *marker_path, |
| 1341 | const char *quarantine_path, |
| 1342 | application_attempt_t *attempt) { |
| 1343 | application_attempt_init(attempt); |
| 1344 | cbm_daemon_application_t *application = job->application; |
| 1345 | if (application_job_cancel_requested(job)) { |
| 1346 | return APPLICATION_ATTEMPT_CANCELLED; |
| 1347 | } |
| 1348 | |
| 1349 | /* Decision 2 (#1997 #832): the slice is decided at spawn time from the jobs |
| 1350 | * active right now (this job included), so a lone worker may use the whole |
| 1351 | * aggregate and concurrent jobs split it. The divisor is logged so a |
| 1352 | * fail-whole verdict can be read against the budget the worker really had. */ |
| 1353 | size_t active_jobs = 0; |
| 1354 | cbm_mutex_lock(&application->mutex); |
| 1355 | size_t memory_budget_bytes = application_worker_memory_slice_locked(application, &active_jobs); |
| 1356 | size_t aggregate_memory_budget_bytes = application->aggregate_memory_budget_bytes; |
| 1357 | cbm_mutex_unlock(&application->mutex); |
| 1358 | if (memory_budget_bytes > 0) { |
| 1359 | char active_text[32]; |
| 1360 | char aggregate_text[32]; |
| 1361 | char slice_text[32]; |
| 1362 | (void)snprintf(active_text, sizeof(active_text), "%zu", active_jobs); |
| 1363 | (void)snprintf(aggregate_text, sizeof(aggregate_text), "%zu", |
| 1364 | aggregate_memory_budget_bytes / (1024U * 1024U)); |
| 1365 | (void)snprintf(slice_text, sizeof(slice_text), "%zu", |
| 1366 | memory_budget_bytes / (1024U * 1024U)); |
| 1367 | cbm_log_info("daemon.index.worker_budget", "project", job->project_key, "active_jobs", |
| 1368 | active_text, "aggregate_mb", aggregate_text, "slice_mb", slice_text); |
| 1369 | } |
| 1370 | |
| 1371 | cbm_daemon_application_worker_t worker = NULL; |
| 1372 | application_tmp_lock(); |
| 1373 | int start_result = |
| 1374 | application->worker_ops.start(application->worker_ops.context, job->args_json, |
| 1375 | memory_budget_bytes, marker_path, quarantine_path, &worker); |
| 1376 | application_tmp_unlock(); |
| 1377 | if (start_result != 0 || !worker) { |
| 1378 | return application_job_cancel_requested(job) ? APPLICATION_ATTEMPT_CANCELLED |
| 1379 | : APPLICATION_ATTEMPT_START_FAILED; |
| 1380 | } |
| 1381 | |
| 1382 | cbm_mutex_lock(&application->mutex); |
| 1383 | job->worker = worker; |
| 1384 | job->worker_started = true; |
| 1385 | bool cancel_now = job->cancel_requested || application->stopping; |
| 1386 | cbm_mutex_unlock(&application->mutex); |
| 1387 | if (cancel_now) { |
| 1388 | /* The worker thread owns this handle until destroy below. Invoke the |
| 1389 | * external supervisor without the application mutex held. */ |
| 1390 | (void)application->worker_ops.cancel(application->worker_ops.context, worker); |
| 1391 | } |
| 1392 | |
| 1393 | const cbm_index_worker_result_t *borrowed = NULL; |
| 1394 | for (;;) { |
| 1395 | cbm_index_worker_poll_t state = |
| 1396 | application->worker_ops.poll(application->worker_ops.context, worker, &borrowed); |
no test coverage detected