Skip to content

Commit 5c54201

Browse files
committed
fix(watcher): pace re-indexes by the cost of the last successful run
After a successful re-index the next poll waits max(interval, 3 x index duration), capped at 30 min, counted from when the index finished. A project whose full rebuild takes minutes no longer re-indexes back-to-back under continuous writes; indexing spends at most ~1/4 of wall time. Sub-second indexes keep the existing cadence; failure backoff and busy-skip are unchanged. Logs watcher.cooldown when the delay is extended. Refs #2835 Signed-off-by: Shane McCarron <[email protected]>
1 parent a3831c9 commit 5c54201

3 files changed

Lines changed: 172 additions & 4 deletions

File tree

‎src/watcher/watcher.c‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ struct cbm_watcher {
154154
* daemon log names the stuck project instead of only repeating the warning. */
155155
#define INDEX_FAIL_SUSTAINED 10
156156

157+
/* Success cooldown (#2835): after a successful re-index the next poll waits
158+
* at least INDEX_COOLDOWN_FACTOR x the index's own duration, counted from when
159+
* the index finished, so the watcher spends at most ~1/(K+1) of wall time
160+
* indexing one project — a 5-minute rebuild idles 15 minutes. Sub-second
161+
* indexes stay at the plain cadence. The ceiling bounds staleness after one
162+
* pathological run; it is NOT INDEX_FAIL_CEILING_MS (5 min), which would cut
163+
* the cooldown of any rebuild over 100 s below K x its duration. */
164+
#define INDEX_COOLDOWN_FACTOR 3
165+
#define INDEX_COOLDOWN_CEILING_MS 1800000 /* 30 min */
166+
157167
/* Stale-root pruning (#286): a watched project whose root directory stays
158168
* missing is pruned — its cached DB is deleted and the watch entry removed.
159169
* Deletion is destructive (the DB can hold user-authored data such as the
@@ -204,6 +214,20 @@ int cbm_watcher_index_backoff_ms(int interval_ms, int consecutive_failures) {
204214
return (int)(delay < interval_ms ? interval_ms : delay);
205215
}
206216

217+
int cbm_watcher_success_delay_ms(int interval_ms, int64_t index_elapsed_ms) {
218+
if (interval_ms < 0) {
219+
interval_ms = 0;
220+
}
221+
if (index_elapsed_ms <= 0) {
222+
return interval_ms;
223+
}
224+
/* Divide instead of multiply so a huge elapsed cannot overflow. */
225+
int64_t cooldown = index_elapsed_ms > INDEX_COOLDOWN_CEILING_MS / INDEX_COOLDOWN_FACTOR
226+
? INDEX_COOLDOWN_CEILING_MS
227+
: index_elapsed_ms * INDEX_COOLDOWN_FACTOR;
228+
return (int)(cooldown > interval_ms ? cooldown : interval_ms);
229+
}
230+
207231
int cbm_watcher_poll_interval_ms(int file_count) {
208232
int ms = POLL_BASE_MS + ((file_count / POLL_FILE_STEP) * CBM_MSEC_PER_SEC);
209233
if (ms > POLL_MAX_MS) {
@@ -1693,12 +1717,22 @@ static void poll_project(const char *key, void *val, void *ud) {
16931717
}
16941718
cbm_log_info("watcher.changed", "project", s->project_name, "strategy",
16951719
s->is_git ? "git" : "tree");
1720+
int delay_ms = -1; /* < 0: use the failure-backoff cadence below */
1721+
int64_t anchor_ns = ctx->now; /* success re-anchors to when the index finished */
16961722
if (ctx->w->index_fn) {
1723+
int64_t index_start = now_ns();
16971724
int rc = ctx->w->index_fn(s->project_name, s->root_path, ctx->w->user_data);
16981725
if (rc == 0) {
16991726
ctx->reindexed++;
17001727
s->index_failures = 0;
17011728
commit_baselines(ctx->w, s);
1729+
anchor_ns = now_ns();
1730+
delay_ms =
1731+
cbm_watcher_success_delay_ms(s->interval_ms, (anchor_ns - index_start) / US_PER_MS);
1732+
if (delay_ms > s->interval_ms) {
1733+
cbm_log_info("watcher.cooldown", "project", s->project_name, "delay_ms",
1734+
itoa_buf(delay_ms));
1735+
}
17021736
} else if (rc > 0) {
17031737
/* Busy-skip: baseline stays uncommitted, next poll retries. */
17041738
cbm_log_info("watcher.index.retry", "project", s->project_name);
@@ -1721,10 +1755,12 @@ static void poll_project(const char *key, void *val, void *ud) {
17211755
}
17221756
}
17231757

1724-
/* Failures back off; success and busy-skip keep the adaptive cadence. */
1725-
s->next_poll_ns =
1726-
ctx->now +
1727-
((int64_t)cbm_watcher_index_backoff_ms(s->interval_ms, s->index_failures) * US_PER_MS);
1758+
/* Success pays back the index's cost; failures back off; busy-skip keeps
1759+
* the adaptive cadence. */
1760+
if (delay_ms < 0) {
1761+
delay_ms = cbm_watcher_index_backoff_ms(s->interval_ms, s->index_failures);
1762+
}
1763+
s->next_poll_ns = anchor_ns + ((int64_t)delay_ms * US_PER_MS);
17281764
}
17291765

17301766
/* Callback to snapshot project state pointers into an array. */

‎src/watcher/watcher.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ int cbm_watcher_poll_interval_ms(int file_count);
195195
* Negative inputs are treated as zero. */
196196
int cbm_watcher_index_backoff_ms(int interval_ms, int consecutive_failures);
197197

198+
/* Return the delay (ms) before the next poll after a SUCCESSFUL re-index that
199+
* took `index_elapsed_ms`: max(interval_ms, INDEX_COOLDOWN_FACTOR (3) x
200+
* index_elapsed_ms), with the cooldown term capped at INDEX_COOLDOWN_CEILING_MS
201+
* (30 min). The caller counts it from when the index finished, so the watcher
202+
* spends at most ~1/4 of wall time indexing a project; sub-second indexes keep
203+
* `interval_ms`.
204+
* Never below `interval_ms`; overflow-safe; negative inputs act as zero. */
205+
int cbm_watcher_success_delay_ms(int interval_ms, int64_t index_elapsed_ms);
206+
198207
/* Classify a stat() errno observed on a watched project root: returns true
199208
* only for values that mean the root itself is gone (ENOENT, ENOTDIR) and
200209
* may count toward stale-root pruning (#286). Any other failure (EACCES,

‎tests/test_watcher.c‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,124 @@ TEST(watcher_sustained_failure_logs_once_issue2015) {
22602260
PASS();
22612261
}
22622262

2263+
/* ── Success cooldown (#2835) ─────────────────────────────────────── */
2264+
2265+
TEST(success_delay_fast_index_keeps_interval) {
2266+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 0), 5000);
2267+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 1), 5000);
2268+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 1666), 5000); /* 3x < interval */
2269+
ASSERT_EQ(cbm_watcher_success_delay_ms(60000, 20000), 60000);
2270+
PASS();
2271+
}
2272+
2273+
TEST(success_delay_slow_index_scales_by_factor) {
2274+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 1667), 5001);
2275+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 10000), 30000);
2276+
/* 5-minute rebuild → next poll 15 min after it finishes. */
2277+
ASSERT_EQ(cbm_watcher_success_delay_ms(60000, 300000), 900000);
2278+
PASS();
2279+
}
2280+
2281+
TEST(success_delay_clamped_and_degenerate) {
2282+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 600000), 1800000);
2283+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, 600001), 1800000);
2284+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, INT64_MAX), 1800000);
2285+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, -1), 5000);
2286+
ASSERT_EQ(cbm_watcher_success_delay_ms(5000, INT64_MIN), 5000);
2287+
ASSERT_EQ(cbm_watcher_success_delay_ms(-1, 0), 0);
2288+
/* Never below the caller's own cadence, even above the ceiling. */
2289+
ASSERT_EQ(cbm_watcher_success_delay_ms(INT_MAX, INT64_MAX), INT_MAX);
2290+
PASS();
2291+
}
2292+
2293+
TEST(success_delay_is_monotonic) {
2294+
int previous = cbm_watcher_success_delay_ms(5000, 0);
2295+
for (int64_t e = 0; e <= 2000000; e += 997) {
2296+
int delay = cbm_watcher_success_delay_ms(5000, e);
2297+
ASSERT_TRUE(delay >= previous);
2298+
ASSERT_TRUE(delay >= 5000 && delay <= 1800000);
2299+
previous = delay;
2300+
}
2301+
PASS();
2302+
}
2303+
2304+
/* Wire-up: the cooldown must be measured around the real index_fn call. An
2305+
* instant callback keeps the plain cadence (no cooldown line); a callback
2306+
* slower than interval/3 (5000/3 ms) extends it and logs once. */
2307+
static int cooldown_log_hits = 0;
2308+
static void cooldown_log_sink(const char *line) {
2309+
if (line && strstr(line, "watcher.cooldown") != NULL) {
2310+
cooldown_log_hits++;
2311+
}
2312+
}
2313+
static int slow_index_sleep_ms = 0;
2314+
static int slow_index_calls = 0;
2315+
static int slow_index_callback(const char *name, const char *path, void *ud) {
2316+
(void)name;
2317+
(void)path;
2318+
(void)ud;
2319+
slow_index_calls++;
2320+
if (slow_index_sleep_ms > 0) {
2321+
cbm_usleep((unsigned)slow_index_sleep_ms * 1000U);
2322+
}
2323+
return 0;
2324+
}
2325+
2326+
TEST(watcher_success_cooldown_measures_index_issue2835) {
2327+
char tmpdir[256];
2328+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_cool_XXXXXX");
2329+
if (!cbm_mkdtemp(tmpdir))
2330+
FAIL("cbm_mkdtemp failed");
2331+
if (wt_git(tmpdir, "init -q") != 0) {
2332+
th_rmtree(tmpdir);
2333+
FAIL("git init failed");
2334+
}
2335+
char p[300];
2336+
th_write_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "hello\n");
2337+
wt_git(tmpdir, "add file.txt");
2338+
wt_git(tmpdir, "commit -q -m init");
2339+
2340+
cbm_store_t *store = cbm_store_open_memory();
2341+
cbm_watcher_t *w = cbm_watcher_new(store, slow_index_callback, NULL);
2342+
cbm_watcher_watch(w, "cool-repo", tmpdir);
2343+
slow_index_calls = 0;
2344+
slow_index_sleep_ms = 0;
2345+
cbm_watcher_poll_once(w); /* baseline */
2346+
2347+
CBMLogLevel old_level = cbm_log_get_level();
2348+
cbm_log_set_level(CBM_LOG_INFO);
2349+
cooldown_log_hits = 0;
2350+
cbm_log_set_sink(cooldown_log_sink);
2351+
2352+
/* Instant index: no cooldown. Loop on the attempt count (a transient git
2353+
* probe failure makes a poll a no-op, see the sustained-failure test). */
2354+
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "a\n");
2355+
while (slow_index_calls < 1) {
2356+
cbm_watcher_touch(w, "cool-repo");
2357+
cbm_watcher_poll_once(w);
2358+
}
2359+
int fast_hits = cooldown_log_hits;
2360+
2361+
/* 1.8 s index → 3 x 1800 = 5400 ms > 5000 ms interval: cooldown. */
2362+
slow_index_sleep_ms = 1800;
2363+
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "b\n");
2364+
while (slow_index_calls < 2) {
2365+
cbm_watcher_touch(w, "cool-repo");
2366+
cbm_watcher_poll_once(w);
2367+
}
2368+
2369+
cbm_log_set_sink(NULL);
2370+
cbm_log_set_level(old_level);
2371+
2372+
ASSERT_EQ(fast_hits, 0);
2373+
ASSERT_EQ(cooldown_log_hits, 1);
2374+
2375+
cbm_watcher_free(w);
2376+
cbm_store_close(store);
2377+
th_rmtree(tmpdir);
2378+
PASS();
2379+
}
2380+
22632381
TEST(watcher_index_failure_count_unknown_project) {
22642382
cbm_store_t *store = cbm_store_open_memory();
22652383
cbm_watcher_t *w = cbm_watcher_new(store, index_callback, NULL);
@@ -4043,6 +4161,11 @@ SUITE(watcher) {
40434161
RUN_TEST(index_backoff_never_schedules_sooner_than_the_interval);
40444162
RUN_TEST(watcher_index_failure_streak_increments_and_resets_issue2015);
40454163
RUN_TEST(watcher_index_failure_backoff_gates_repolling_issue2015);
4164+
RUN_TEST(success_delay_fast_index_keeps_interval);
4165+
RUN_TEST(success_delay_slow_index_scales_by_factor);
4166+
RUN_TEST(success_delay_clamped_and_degenerate);
4167+
RUN_TEST(success_delay_is_monotonic);
4168+
RUN_TEST(watcher_success_cooldown_measures_index_issue2835);
40464169
RUN_TEST(watcher_sustained_failure_logs_once_issue2015);
40474170
RUN_TEST(watcher_index_failure_count_unknown_project);
40484171
RUN_TEST(watcher_project_info_publishes_scan_issue2167);

0 commit comments

Comments
 (0)