Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

perf(pipeline): parallel rehydration; skip the outgoing quick_check on the delta route

Martin Vogela33a82f3e023
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index bdfc574d7..4aa721afd 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1675,7 +1675,7 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
- return cbm_pipeline_publish_staged(stage_path, generation, true);
+ return cbm_pipeline_publish_staged(stage_path, generation, true, false);
}
/* Complete and publish an already-materialized staging database: metadata
@@ -1685,7 +1685,7 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
* because its patch step already wrote row-level FTS inserts for exactly
* the nodes it created. */
int cbm_pipeline_publish_staged(char *stage_path, const cbm_pipeline_generation_t *generation,
- bool fts_wholesale) {
+ bool fts_wholesale, bool destination_known_healthy) {
struct timespec t_pub;
cbm_clock_gettime(CLOCK_MONOTONIC, &t_pub);
cbm_store_t *store = cbm_store_open_path(stage_path);
@@ -1761,8 +1761,8 @@ int cbm_pipeline_publish_staged(char *stage_path, const cbm_pipeline_generation_
free(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
}
- int fin_rc = cbm_pipeline_finalize_staged_generation(stage_path, generation->final_db_path,
- generation->cancelled);
+ int fin_rc = cbm_pipeline_finalize_staged_generation(
+ stage_path, generation->final_db_path, generation->cancelled, destination_known_healthy);
free(stage_path);
return fin_rc;
}
@@ -1781,7 +1781,9 @@ void cbm_pipeline_discard_stage(const char *stage_path) {
* every failure path. The store handle must already be CLOSED — sidecar
* removal and rename act on the bare file. */
int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_db_path,
- atomic_int *cancelled) {
+ atomic_int *cancelled, bool destination_known_healthy) {
+ struct timespec t_fin;
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t_fin);
if (cbm_remove_db_sidecars(stage_path) != 0) {
discard_generation_stage(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
@@ -1790,10 +1792,25 @@ int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_
discard_generation_stage(stage_path);
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
+ cbm_log_info("finalize.timing", "block", "stage_sidecars", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t_fin)));
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t_fin);
cbm_replacement_prepare_t prepared = {0};
- /* The destination here is the caller's staging file, not the live database:
- * the publishing wrapper owns quarantining that. */
- if (prepare_existing_generation_for_replace(final_db_path, &prepared, false) != 0) {
+ /* destination_known_healthy: the delta route CLONED this same file and
+ * ran complete transactions against the clone minutes ago -- reaching
+ * this point is structural-health evidence, and the quick_check the
+ * prepare would run is a full-database page scan (measured 35.5s on a
+ * kernel-scale generation). A corrupt live DB can never take the delta
+ * route: every earlier step fails it into the dump path, whose
+ * finalize keeps the check and the quarantine semantics. Sidecars are
+ * still removed either way: a replaced DB must never inherit the old
+ * generation's WAL. */
+ if (destination_known_healthy) {
+ if (cbm_remove_db_sidecars(final_db_path) != 0) {
+ cbm_pipeline_discard_stage(stage_path);
+ return CBM_PIPELINE_PERSIST_FAILED;
+ }
+ } else if (prepare_existing_generation_for_replace(final_db_path, &prepared, false) != 0) {
discard_generation_stage(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
}
@@ -1807,11 +1824,16 @@ int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_
discard_generation_stage(stage_path);
return rollback_rc == 0 ? CBM_PIPELINE_ABORT_PRESERVE_DB : CBM_PIPELINE_PERSIST_FAILED;
}
+ cbm_log_info("finalize.timing", "block", "prepare_live", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t_fin)));
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t_fin);
if (cbm_rename_replace(stage_path, final_db_path) != 0) {
(void)rollback_quarantined_generation(final_db_path, &prepared);
discard_generation_stage(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
}
+ cbm_log_info("finalize.timing", "block", "rename", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t_fin)));
return 0;
}
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index b9b58bba7..c364a8d90 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -1097,6 +1097,11 @@ typedef struct {
int fresh_count;
char **def_modules;
int def_module_count;
+ /* Parallel rehydration: per-worker arenas owning the deserialized
+ * def strings. The resolvers and the patch borrow those strings, so
+ * the arenas live exactly as long as cr.arena itself. */
+ CBMArena *rehydrate_arenas;
+ int rehydrate_arena_count;
} closure_resolve_t;
typedef struct {
@@ -1495,6 +1500,32 @@ static int dump_and_persist(cbm_gbuf_t *gbuf, const char *db_path, const char *p
return 0;
}
+/* Parallel base-def rehydration: rows are independent, so the JSON decode
+ * fans out across workers on a stride; assembly back into one array stays
+ * in ROW ORDER, so the registration input is byte-for-byte the sequence
+ * the serial loop produced. Measured 35s serial at kernel scale (2.37M
+ * defs) -- the single largest delta block after the query-plan fixes. */
+typedef struct {
+ const cbm_lsp_surface_row_t *const *rows;
+ int row_count;
+ int worker_id;
+ int worker_count;
+ CBMArena *arena; /* this worker's arena (owned by cr) */
+ CBMLSPDef **row_defs; /* per-row result pointers */
+ int *row_counts; /* per-row def counts; -1 = decode failure */
+} rehydrate_worker_t;
+
+static void *rehydrate_worker(void *arg) {
+ rehydrate_worker_t *w = (rehydrate_worker_t *)arg;
+ for (int i = w->worker_id; i < w->row_count; i += w->worker_count) {
+ CBMLSPDef *defs = NULL;
+ int count = cbm_lsp_surface_defs_from_json(w->arena, w->rows[i]->defs_json, &defs);
+ w->row_defs[i] = defs;
+ w->row_counts[i] = count;
+ }
+ return NULL;
+}
+
/* Probe-extract a set of files into a throwaway graph to compute their
* FRESH surfaces without touching the real state. Parses the changed files
* once more than strictly necessary (the executor re-extracts them into the
@@ -1916,10 +1947,12 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
goto out;
}
cbm_log_info("delta.clone", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
staging = cbm_store_open_path(stage);
if (!staging) {
goto out;
}
+ cbm_log_info("delta.staging_open", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
/* Purge set = closure files + deleted files. */
int purge_count = ci + deleted_count;
@@ -1977,42 +2010,103 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
}
cbm_arena_init(&cr.arena);
cr_arena_live = true;
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
{
- int cap = 0;
+ /* Eligible rows first (skip everything being re-parsed or deleted),
+ * then fan the JSON decode out across workers. */
+ const cbm_lsp_surface_row_t **elig = (const cbm_lsp_surface_row_t **)calloc(
+ (size_t)(plan->stored_count ? plan->stored_count : 1), sizeof(*elig));
+ if (!elig) {
+ goto out;
+ }
+ int elig_count = 0;
for (int i = 0; i < plan->stored_count; i++) {
const cbm_lsp_surface_row_t *row = &plan->stored_rows[i];
- if (cbm_ht_get(stale_surface_set, row->rel_path)) {
- continue;
+ if (!cbm_ht_get(stale_surface_set, row->rel_path)) {
+ elig[elig_count++] = row;
}
- CBMLSPDef *row_defs = NULL;
- int row_count = cbm_lsp_surface_defs_from_json(&cr.arena, row->defs_json, &row_defs);
- if (row_count < 0) {
- cbm_log_error("delta.rehydrate_failed", "path", row->rel_path);
- goto out;
+ }
+ int workers = cbm_default_worker_count(true);
+ if (workers < 1) {
+ workers = 1;
+ }
+ if (workers > elig_count) {
+ workers = elig_count ? elig_count : 1;
+ }
+ CBMLSPDef **row_defs =
+ (CBMLSPDef **)calloc((size_t)(elig_count ? elig_count : 1), sizeof(*row_defs));
+ int *row_counts = (int *)calloc((size_t)(elig_count ? elig_count : 1), sizeof(int));
+ cr.rehydrate_arenas = (CBMArena *)calloc((size_t)workers, sizeof(CBMArena));
+ cbm_thread_t *threads = (cbm_thread_t *)calloc((size_t)workers, sizeof(cbm_thread_t));
+ rehydrate_worker_t *wargs = (rehydrate_worker_t *)calloc((size_t)workers, sizeof(*wargs));
+ if (!row_defs || !row_counts || !cr.rehydrate_arenas || !threads || !wargs) {
+ free(elig);
+ free(row_defs);
+ free(row_counts);
+ free(threads);
+ free(wargs);
+ goto out;
+ }
+ cr.rehydrate_arena_count = workers;
+ for (int w = 0; w < workers; w++) {
+ cbm_arena_init(&cr.rehydrate_arenas[w]);
+ }
+ int spawned = 0;
+ for (int w = 0; w < workers; w++) {
+ wargs[w] = (rehydrate_worker_t){.rows = elig,
+ .row_count = elig_count,
+ .worker_id = w,
+ .worker_count = workers,
+ .arena = &cr.rehydrate_arenas[w],
+ .row_defs = row_defs,
+ .row_counts = row_counts};
+ if (cbm_thread_create(&threads[w], 0, rehydrate_worker, &wargs[w]) != 0) {
+ break;
}
- if (row_count > 0) {
- if (cr.base_def_count + row_count > cap) {
- int ncap = cap ? cap * PAIR_LEN : CBM_SZ_256;
- while (ncap < cr.base_def_count + row_count) {
- ncap *= PAIR_LEN;
- }
- CBMLSPDef *grown =
- (CBMLSPDef *)cbm_arena_alloc(&cr.arena, (size_t)ncap * sizeof(CBMLSPDef));
- if (!grown) {
- goto out;
- }
- if (cr.base_def_count > 0) {
- memcpy(grown, cr.base_defs, (size_t)cr.base_def_count * sizeof(CBMLSPDef));
+ spawned++;
+ }
+ for (int w = spawned; w < workers; w++) {
+ (void)rehydrate_worker(&wargs[w]);
+ }
+ for (int w = 0; w < spawned; w++) {
+ cbm_thread_join(&threads[w]);
+ }
+ free(threads);
+ free(wargs);
+ bool decode_ok = true;
+ int64_t total = 0;
+ for (int i = 0; i < elig_count; i++) {
+ if (row_counts[i] < 0) {
+ cbm_log_error("delta.rehydrate_failed", "path", elig[i]->rel_path);
+ decode_ok = false;
+ break;
+ }
+ total += row_counts[i];
+ }
+ if (decode_ok && total > 0) {
+ cr.base_defs =
+ (CBMLSPDef *)cbm_arena_alloc(&cr.arena, (size_t)total * sizeof(CBMLSPDef));
+ if (!cr.base_defs) {
+ decode_ok = false;
+ } else {
+ for (int i = 0; i < elig_count; i++) {
+ if (row_counts[i] > 0) {
+ memcpy(cr.base_defs + cr.base_def_count, row_defs[i],
+ (size_t)row_counts[i] * sizeof(CBMLSPDef));
+ cr.base_def_count += row_counts[i];
}
- cr.base_defs = grown;
- cap = ncap;
}
- memcpy(cr.base_defs + cr.base_def_count, row_defs,
- (size_t)row_count * sizeof(CBMLSPDef));
- cr.base_def_count += row_count;
}
}
+ free(elig);
+ free(row_defs);
+ free(row_counts);
+ if (!decode_ok) {
+ goto out;
+ }
}
+ cbm_log_info("delta.rehydrate", "defs", itoa_buf(cr.base_def_count), "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t)));
char **excluded_dirs = NULL;
int excluded_count = 0;
@@ -2168,8 +2262,10 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
}
/* Committed counts for the #334 plausibility gate. */
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
cbm_pipeline_set_committed_counts(p, cbm_store_count_nodes(staging, project),
cbm_store_count_edges(staging, project));
+ cbm_log_info("delta.committed_counts", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
{
int index_mode = cbm_pipeline_get_mode(p);
@@ -2201,7 +2297,9 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
};
cbm_store_close(staging);
staging = NULL;
- result = cbm_pipeline_publish_staged(stage, &generation, false);
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ result = cbm_pipeline_publish_staged(stage, &generation, false, true);
+ cbm_log_info("delta.publish_total", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
stage = NULL; /* publish_staged owns and frees it on every path */
}
cbm_log_info("incremental.done_delta", "elapsed_ms", itoa_buf((int)elapsed_ms(t0)));
@@ -2228,6 +2326,10 @@ out:
free(cr.def_modules[i]);
}
free(cr.def_modules);
+ for (int i = 0; i < cr.rehydrate_arena_count; i++) {
+ cbm_arena_destroy(&cr.rehydrate_arenas[i]);
+ }
+ free(cr.rehydrate_arenas);
cbm_arena_destroy(&cr.arena);
}
cbm_pipeline_free_semantic_manifest(manifest, manifest_count);
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index ab0e57116..50efd3ba5 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -717,13 +717,13 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
* must be complete and sealed with its store handle closed. Discards the
* stage on every failure. Does NOT free stage_path. */
int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_db_path,
- atomic_int *cancelled);
+ atomic_int *cancelled, bool destination_known_healthy);
/* Metadata writes + FTS policy + integrity + seal + finalize for an
* already-materialized staging DB. Takes ownership of stage_path. The dump
* path passes fts_wholesale=true; the delta path passes false (its patch
* wrote row-level FTS inserts). generation->gbuf is not read here. */
int cbm_pipeline_publish_staged(char *stage_path, const cbm_pipeline_generation_t *generation,
- bool fts_wholesale);
+ bool fts_wholesale, bool destination_known_healthy);
/* mkstemp-minted staging sibling of final_path (exported for the delta
* executor; the dump path uses it internally). malloc'd, caller frees. */