diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-05 11:51:08 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-05 11:51:08 +0000 |
| commit | ff67b720f3d0c74739d8b8b657ac2e96bf171a1b (patch) | |
| tree | 611b7e39c97bf9168537835cd15267028a4e7f86 | |
| parent | b4ea4e3aaa9735c4d4342b98e3692cce52a40220 (diff) | |
| download | genenetwork4-ff67b720f3d0c74739d8b8b657ac2e96bf171a1b.tar.gz | |
gemma: key the gemma-wrapper cache dir by input set
The cache dir was keyed by chromosome only and shared across requests, so a request with different inputs (another trait, or another snps or genotype file) left a second *-gemma-cXX.tar.xz in the same chr-N dir. gemma-wrapper names those archives by a content hash, so the workflow's single-File glob output binding then matched more than one path and the step failed -- silently, since ravanan's user-error only logs when %log-level is set, which never happens inside a generated job script. Key the cache root by a hash of the input set (genotype path and size, snps contents, trait values, lmm) as well as by chromosome, so repeated identical requests still hit gemma-wrapper's cache while different input sets cannot see each other's archives. chromosomes_in reads the snps file, so it now takes the already-read contents (chromosomes_from) to avoid reading it twice. cwl/README.md: describe the two-part keying, and note that a node needs a writable HOME -- ravanan forwards the submitting process's HOME into the job (as sbatch would) and gemma-wrapper writes its lock file there.
| -rw-r--r-- | cwl/README.md | 30 | ||||
| -rw-r--r-- | src/gemma.rs | 84 |
2 files changed, 89 insertions, 25 deletions
diff --git a/cwl/README.md b/cwl/README.md index 186a689..9e5fe36 100644 --- a/cwl/README.md +++ b/cwl/README.md @@ -80,6 +80,7 @@ A node does need: | write access to its `data/`, `logs/` | the job writes its outputs record and its stdout/stderr there | | a writable **scratch** dir | the job's cwd is a temporary dir under `--scratch`: keep it node-local | | permission as the **job user** | identity comes from the JWT (e.g. `testuser`); these paths must be writable by it | +| a **writable `$HOME`** for the job user | ravanan forwards the submitting process's `HOME` into the job (as `sbatch` would); gemma-wrapper writes its lock file there (`$HOME/.<hash>.lck`) and dies with `undefined method '+' for nil` without it | | resolvable `NodeAddr` | Slurm's launch path needs the node address, not just a name | Practical split: `RAVANAN_CACHE_DIR` (ravanan's store + the @@ -92,7 +93,7 @@ only meant for single-box setups like the test container. | cache | lives in | scope | |-----------------------------|-------------------------------------------------------------------------|-------| | ravanan work reuse | `<store>/data/<hash>.json`, keyed by (script, inputs) | front end; a hit never touches Slurm | -| gemma-wrapper `--cache-dir` | one dir per chromosome under `<ravanan_cache_dir>/gemma-wrapper-cache/` | inside the job; nodes must see it | +| gemma-wrapper `--cache-dir` | one dir per chromosome under `<ravanan_cache_dir>/gemma-wrapper-cache/<input-key>/` | inside the job; nodes must see it | Two wrinkles specific to this endpoint: @@ -100,13 +101,26 @@ Two wrinkles specific to this endpoint: `/compute/gemma/ravanan`: `compute_ravanan` writes uniquely named pheno/inputs/CWL files on every call, so the (script, inputs) key is always new. The cache that actually saves work is gemma-wrapper's. -- the per-chromosome keying of `--cache-dir` is deliberate: - gemma-wrapper's cache-hit hash does not vary with `--chromosomes`, so - a *shared* cache directory would let chromosome 2 silently reuse - chromosome 1's result. A shared *parent* is fine; the per-chromosome - subdirectories are what matters — and two concurrent requests for the - same sample/chromosome can both write into the same subdirectory, - since it is keyed by chromosome only. +- the cache dir is keyed by input set *and* chromosome + (`<input-key>/chr-<N>`, see `gemma_wrapper_input_key` in + `src/gemma.rs`). Both parts are required: + - *per chromosome*, because gemma-wrapper's cache-hit hash does not + vary with `--chromosomes`, so one directory shared by all of them + would let chromosome 2 silently reuse chromosome 1's result; + - *per input set*, because gemma-wrapper names the archives it leaves + in `--cache-dir` by a **content** hash. Two different input sets + (another trait, another genotype or snps file) sharing a `chr-N` + directory leave two `*-gemma-cXX.tar.xz` files there, and the + workflow's single-`File` glob output binding + (`$(inputs.cache_dir)/*-gemma-cXX.tar.xz`) then matches more than + one path, so the step fails. Worse, `ravanan`'s `user-error` used to + swallow the message inside a job script, leaving only a non-zero + exit status; it now prints to stderr. + + Repeated requests with identical inputs land in the same + `<input-key>/chr-<N>` dirs and hit gemma-wrapper's cache (its logs say + `CACHE HIT`); requests with different inputs get their own tree and + cannot see each other's archives. ## Can the nodes rebuild the tools instead of sharing a store? diff --git a/src/gemma.rs b/src/gemma.rs index 42a7de9..3f8db70 100644 --- a/src/gemma.rs +++ b/src/gemma.rs @@ -497,16 +497,18 @@ fn ravanan_output_paths<'a>(outputs: &'a Value, id: &str) -> Result<Vec<&'a str> .collect() } -/// The distinct chromosome names in `snps_file` (whitespace-separated: -/// rs id, position, chromosome), in first-seen order -- reproduces -/// gemma-wrapper's own `get_chromosomes`, used here to build -/// `compute_ravanan`'s `chromosomes` scatter input (CWL's `scatter` -/// needs a concrete array at invocation time, which can't be +/// The distinct chromosome names in a SNP annotation file's contents +/// (whitespace-separated: rs id, position, chromosome), in first-seen +/// order -- reproduces gemma-wrapper's own `get_chromosomes`, used here +/// to build `compute_ravanan`'s `chromosomes` scatter input (CWL's +/// `scatter` needs a concrete array at invocation time, which can't be /// discovered by a step inside the workflow itself without another /// round of plumbing -- see `cwl/gemma/gemma-loco.scm`'s own doc /// comment on this input). -fn chromosomes_in(snps_file: &str) -> Result<Vec<String>, GemmaError> { - let contents = std::fs::read_to_string(snps_file).map_err(GemmaError::Io)?; +/// +/// Takes the contents, not the path: the caller also hashes them (see +/// [`gemma_wrapper_input_key`]) and should not read the file twice. +fn chromosomes_from(contents: &str) -> Vec<String> { let mut seen = std::collections::HashSet::new(); let mut chromosomes = Vec::new(); for line in contents.lines() { @@ -516,7 +518,42 @@ fn chromosomes_in(snps_file: &str) -> Result<Vec<String>, GemmaError> { } } } - Ok(chromosomes) + chromosomes +} + +/// A stable identity for one request's GEMMA inputs, used to give each +/// distinct input set its own gemma-wrapper cache root (see +/// `compute_ravanan`). +/// +/// The genotype file is identified by its path and size rather than its +/// contents: hashing a genome-sized file on every request would cost +/// more than the cache saves, and the path is stable in practice (it is +/// the caller's own file, interned verbatim into ravanan's store). +fn gemma_wrapper_input_key( + genotype_file: &str, + snps_contents: &str, + trait_values: &TraitValues, + lmm: u8, +) -> String { + let mut material = Vec::new(); + material.extend_from_slice(genotype_file.as_bytes()); + material.push(0); + if let Ok(metadata) = std::fs::metadata(genotype_file) { + material.extend_from_slice(&metadata.len().to_le_bytes()); + } + material.push(0); + material.extend_from_slice(snps_contents.as_bytes()); + material.push(0); + material.extend_from_slice(lmm.to_string().as_bytes()); + material.push(0); + for value in trait_values { + match value { + Some(value) => material.extend_from_slice(&value.to_bits().to_le_bytes()), + None => material.extend_from_slice(b"NA"), + } + material.push(b','); + } + crate::indexer::sha256::hex_digest(&material)[..16].to_string() } /// The same GEMMA `--loco` kinship-then-GWA computation as `compute`, @@ -578,7 +615,8 @@ pub async fn compute_ravanan( std::fs::create_dir_all(store_dir.join(subdir)).map_err(GemmaError::Io)?; } - let chromosomes = chromosomes_in(snps_file)?; + let snps_contents = std::fs::read_to_string(snps_file).map_err(GemmaError::Io)?; + let chromosomes = chromosomes_from(&snps_contents); if chromosomes.is_empty() { return Err(GemmaError::NoAssocFiles); } @@ -588,15 +626,27 @@ pub async fn compute_ravanan( .collect::<Vec<_>>() .join(", "); - // One persistent cache directory per chromosome, reused across - // requests, so gemma-wrapper's content-hash cache actually gets hit - // on repeated identical requests -- unlike "." (each job's own - // hermetic CWL working directory under ravanan's --store, fresh + // A persistent cache directory per (input set, chromosome), reused + // across requests, so gemma-wrapper's content-hash cache actually + // gets hit on repeated identical requests -- unlike "." (each job's + // own hermetic CWL working directory under ravanan's --store, fresh // every run), which never accumulates a cache at all. Keyed by - // chromosome, not shared, because gemma-wrapper's cache-hit hash - // does not vary with --chromosomes: a shared directory would let - // one chromosome's job silently reuse another's cached result. - let wrapper_cache_dir = Path::new(cache_dir).join("gemma-wrapper-cache"); + // chromosome as well as the input set, because gemma-wrapper's + // cache-hit hash does not vary with --chromosomes: a directory + // shared between chromosomes would let one chromosome's job + // silently reuse another's cached result. + // + // Keyed by the input set too, because gemma-wrapper names its + // archives by a *content* hash: two different input sets (another + // trait, or another snps/genotype file) sharing a `chr-N` directory + // leave two `*-gemma-cXX.tar.xz` files there, and the CWL's + // single-File glob output binding (`$(inputs.cache_dir)/*-gemma-cXX.tar.xz`) + // then matches more than one path and the step fails -- with an + // error that ravanan's `user-error` silently drops in a job script. + let input_key = gemma_wrapper_input_key(genotype_file, &snps_contents, trait_values, lmm); + let wrapper_cache_dir = Path::new(cache_dir) + .join("gemma-wrapper-cache") + .join(&input_key); let mut chr_cache_dirs = Vec::new(); for chr in &chromosomes { let chr_cache_dir = wrapper_cache_dir.join(format!("chr-{chr}")); |
