|
52 | 52 | HOST_HTTP_RETRY_INITIAL_MS = 1000, |
53 | 53 | HOST_HTTP_RETRY_MAX_MS = 30000, |
54 | 54 | HOST_WATCH_INTERVAL_MS = 5000, |
| 55 | + /* Age-based temp cleaners need days of inactivity (macOS tmp_cleaner: 3); |
| 56 | + * a minute bounds the blind window after a long sleep (#2178). */ |
| 57 | + HOST_COORDINATION_TOUCH_MS = 60000, |
55 | 58 | HOST_CONFLICT_LOG_CAP = 1024 * 1024, |
56 | 59 | HOST_OPERATION_LOG_CAP = 5 * 1024 * 1024, |
57 | 60 | HOST_PATH_CAP = 4096, |
@@ -868,10 +871,42 @@ static bool host_background_start(host_state_t *host) { |
868 | 871 | return true; |
869 | 872 | } |
870 | 873 |
|
| 874 | +/* Coordination handles the host retains for the generation's whole lifetime. */ |
| 875 | +typedef struct { |
| 876 | + const cbm_daemon_ipc_endpoint_t *endpoint; |
| 877 | + cbm_version_cohort_lease_t *cohort_lease; |
| 878 | + cbm_version_cohort_daemon_claim_t *daemon_claim; |
| 879 | + cbm_daemon_ipc_participant_guard_t *participant_guard; |
| 880 | +} host_coordination_t; |
| 881 | + |
| 882 | +/* Refresh every long-held runtime file and name the first one found lost, or |
| 883 | + * return NULL. A held lock whose path now names another inode coordinates |
| 884 | + * nothing: forked index workers read the daemon as uncoordinated and a peer |
| 885 | + * can start a second generation (#2178). */ |
| 886 | +static const char *host_coordination_touch(const host_coordination_t *coordination, |
| 887 | + cbm_daemon_runtime_service_t *service) { |
| 888 | + if (!cbm_version_cohort_daemon_claim_touch(coordination->daemon_claim)) { |
| 889 | + return "cohort_daemon_claim"; |
| 890 | + } |
| 891 | + if (!cbm_version_cohort_lease_touch(coordination->cohort_lease)) { |
| 892 | + return "cohort_lease"; |
| 893 | + } |
| 894 | + if (!cbm_daemon_ipc_participant_guard_touch(coordination->endpoint, |
| 895 | + coordination->participant_guard)) { |
| 896 | + return "participant_guard"; |
| 897 | + } |
| 898 | + if (!cbm_daemon_runtime_service_touch_listener(service)) { |
| 899 | + return "listener"; |
| 900 | + } |
| 901 | + return NULL; |
| 902 | +} |
| 903 | + |
871 | 904 | static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service, |
872 | | - atomic_int *stop_requested, host_state_t *host, bool permanent) { |
| 905 | + atomic_int *stop_requested, host_state_t *host, bool permanent, |
| 906 | + const host_coordination_t *coordination) { |
873 | 907 | uint64_t initial_deadline = cbm_now_ms() + HOST_INITIAL_CLIENT_TIMEOUT_MS; |
874 | 908 | uint64_t stopping_deadline = 0; |
| 909 | + uint64_t next_touch = 0; |
875 | 910 | for (;;) { |
876 | 911 | cbm_daemon_runtime_service_state_t state = cbm_daemon_runtime_service_state(service); |
877 | 912 | if (state == CBM_DAEMON_RUNTIME_SERVICE_EXITED) { |
@@ -903,6 +938,17 @@ static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service, |
903 | 938 | stop ? "stop_requested" : "initial_window_expired"); |
904 | 939 | return cbm_daemon_runtime_service_stop(service, HOST_RUNTIME_SHUTDOWN_MS); |
905 | 940 | } |
| 941 | + /* Permanent generations too: exiting lets the next client start a |
| 942 | + * coordinated generation, while re-claiming would race that client. */ |
| 943 | + if (cbm_now_ms() >= next_touch) { |
| 944 | + next_touch = cbm_now_ms() + HOST_COORDINATION_TOUCH_MS; |
| 945 | + const char *lost = host_coordination_touch(coordination, service); |
| 946 | + if (lost) { |
| 947 | + cbm_log_warn("daemon.lifetime_end", "reason", "coordination_file_lost", "file", |
| 948 | + lost); |
| 949 | + return cbm_daemon_runtime_service_stop(service, HOST_RUNTIME_SHUTDOWN_MS); |
| 950 | + } |
| 951 | + } |
906 | 952 | host_http_reconcile_at(host, cbm_now_ms(), false); |
907 | 953 | /* Retire an ephemeral generation that lingered for cold-storm cohort |
908 | 954 | * participants once they drain, or once its bounded linger elapses. |
@@ -1123,7 +1169,14 @@ int cbm_daemon_host_run(const cbm_daemon_host_config_t *config) { |
1123 | 1169 | : "unavailable", |
1124 | 1170 | "memory_budget_bytes", memory_budget, "physical_job_limit", physical_job_limit, |
1125 | 1171 | "worker_memory_budget_bytes", worker_memory_budget); |
1126 | | - if (!host_wait_for_lifetime(service, config->stop_requested, &host, config->permanent)) { |
| 1172 | + host_coordination_t coordination = { |
| 1173 | + .endpoint = config->endpoint, |
| 1174 | + .cohort_lease = cohort_lease, |
| 1175 | + .daemon_claim = daemon_claim, |
| 1176 | + .participant_guard = participant_guard, |
| 1177 | + }; |
| 1178 | + if (!host_wait_for_lifetime(service, config->stop_requested, &host, config->permanent, |
| 1179 | + &coordination)) { |
1127 | 1180 | host_force_terminate("runtime"); |
1128 | 1181 | } |
1129 | 1182 |
|
|
0 commit comments