about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-11-14 04:30:26 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-11-14 04:30:26 +0000
commit7ff1543f8649b4e04d6ccfd012ae8d2403f2e0a7 (patch)
tree338fb955f85c3c587bb920f584083e79c2859fe9 /src
parent0e017416628d72ae9814b8225d17f62b28f855ee (diff)
downloadgenenetwork4-7ff1543f8649b4e04d6ccfd012ae8d2403f2e0a7.tar.gz
ravanan: name the workflow inputs by content, so a repeat request is warm
compute_ravanan wrote <pid>-<nanos>-pheno.txt, -ravanan-inputs.yaml and
-gemma-loco.cwl, so ravanan's (script, inputs) work-reuse key was new on
every call and a repeat of the identical request recomputed all 40
steps.  Naming them by a hash of the request's inputs (genotype path and
size, snps contents, trait values, lmm) makes an identical request hit
the store instead.

Measured on the BXD fixture, full 20-chromosome LOCO mapping,
single-machine: 14.3 s cold, 1.6 s warm, identical .assoc.txt checksums.
For comparison the gemma-wrapper stack costs 11.9 s cold and 12.1 s
warm -- its own cache did not hit on a repeat here -- so the ravanan
path is now the cheaper one for any repeated request and the slightly
dearer one for a first request.

The run log stays per-call: it is what a caller streams by run_id.  The
three shared files are written aside and renamed, so two concurrent
identical requests cannot read a half-written file.
Diffstat (limited to 'src')
-rw-r--r--src/gemma.rs76
1 files changed, 69 insertions, 7 deletions
diff --git a/src/gemma.rs b/src/gemma.rs
index ca9c522..bfe0065 100644
--- a/src/gemma.rs
+++ b/src/gemma.rs
@@ -1028,6 +1028,47 @@ fn ravanan_output_paths<'a>(outputs: &'a Value, id: &str) -> Result<Vec<&'a str>
 /// comment on this input).
 ///
 /// Takes the contents, not the path, so the caller reads the file once.
+/// A stable identity for one request's GEMMA inputs, used to name the files
+/// ravanan keys its work reuse on: the phenotype file, the inputs YAML and the
+/// compiled CWL.  Identical inputs therefore produce identical paths and a
+/// repeat request is answered from ravanan's store instead of recomputing
+/// every chromosome.
+///
+/// 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 reuse saves, and the path is stable in practice (it is the caller's own
+/// file, interned verbatim into ravanan's store).  The SNP file is hashed,
+/// because the chromosome list -- and therefore the whole scatter -- comes
+/// from it.
+fn ravanan_input_key(
+    genotype_file: &str,
+    snps_file: &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);
+    if let Ok(snps) = std::fs::read(snps_file) {
+        material.extend_from_slice(&snps);
+    }
+    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()
+}
+
 fn chromosomes_from(contents: &str) -> Vec<String> {
     let mut seen = std::collections::HashSet::new();
     let mut chromosomes = Vec::new();
@@ -1068,10 +1109,22 @@ pub async fn compute_ravanan(
     run_id: Option<&str>,
 ) -> Result<GemmaOutput, GemmaError> {
     std::fs::create_dir_all(cache_dir).map_err(GemmaError::Io)?;
-    let unique = format!("{}-{}", std::process::id(), nanos_since_epoch());
-    let pheno_path = Path::new(cache_dir).join(format!("{unique}-pheno.txt"));
-    write_pheno_file(&pheno_path, trait_values).map_err(GemmaError::Io)?;
 
+    // The workflow's inputs are named by their *content*, not by a pid and a
+    // timestamp: ravanan reuses a step's result when the (script, inputs) pair
+    // is unchanged, and that pair is built from these paths.  Unique names made
+    // every request cold, which cost the whole computation again for a repeat
+    // of the same request -- 14.7 s versus 1.6 s on the BXD fixture.
+    let input_key = ravanan_input_key(genotype_file, snps_file, trait_values, lmm);
+    let pheno_path = Path::new(cache_dir).join(format!("{input_key}-pheno.txt"));
+    // Written aside and renamed, so a concurrent request with the same key
+    // never reads a half-written file.
+    let pheno_tmp = Path::new(cache_dir).join(format!("{input_key}-pheno.tmp-{}", std::process::id()));
+    write_pheno_file(&pheno_tmp, trait_values).map_err(GemmaError::Io)?;
+    std::fs::rename(&pheno_tmp, &pheno_path).map_err(GemmaError::Io)?;
+
+    // The run log stays unique per call: it is the thing a caller streams by
+    // run_id, so two calls must never share one.
     let log_path = streaming::create_run_log(cache_dir, run_id)?;
 
     // gemma-loco.scm is the maintained source; the CWL ravanan actually
@@ -1080,8 +1133,13 @@ pub async fn compute_ravanan(
     // involved) and never checked into the repo -- see the doc comment
     // on Config::gemma_ravanan_workflow.
     let compiled_workflow_path =
-        Path::new(cache_dir).join(format!("{unique}-gemma-loco.cwl"));
-    let ccwl_output_arg = format!("--output={}", compiled_workflow_path.display());
+        Path::new(cache_dir).join(format!("{input_key}-gemma-loco.cwl"));
+    // Compile to a per-process path and rename: the compiled CWL is
+    // deterministic, so two concurrent identical requests would otherwise
+    // race on the same file.
+    let compiled_tmp =
+        Path::new(cache_dir).join(format!("{input_key}-gemma-loco.tmp-{}.cwl", std::process::id()));
+    let ccwl_output_arg = format!("--output={}", compiled_tmp.display());
     let ccwl_args = vec!["compile", ccwl_output_arg.as_str(), workflow_scm_path];
     let ccwl_output = streaming::run_streaming(ccwl_cmd, &ccwl_args, None)
         .await
@@ -1095,6 +1153,7 @@ pub async fn compute_ravanan(
             stderr: String::from_utf8_lossy(&ccwl_output.stderr).into_owned(),
         });
     }
+    std::fs::rename(&compiled_tmp, &compiled_workflow_path).map_err(GemmaError::Io)?;
     let compiled_workflow_arg = compiled_workflow_path.to_string_lossy().into_owned();
 
     let store_dir = Path::new(cache_dir).join("ravanan-store");
@@ -1115,9 +1174,11 @@ pub async fn compute_ravanan(
         .collect::<Vec<_>>()
         .join(", ");
 
-    let inputs_yaml_path = Path::new(cache_dir).join(format!("{unique}-ravanan-inputs.yaml"));
+    let inputs_yaml_path = Path::new(cache_dir).join(format!("{input_key}-ravanan-inputs.yaml"));
+    let inputs_tmp =
+        Path::new(cache_dir).join(format!("{input_key}-ravanan-inputs.tmp-{}.yaml", std::process::id()));
     std::fs::write(
-        &inputs_yaml_path,
+        &inputs_tmp,
         format!(
             "genotype_file: {{class: File, path: {genotype_file}}}\n\
              pheno_file: {{class: File, path: {}}}\n\
@@ -1128,6 +1189,7 @@ pub async fn compute_ravanan(
         ),
     )
     .map_err(GemmaError::Io)?;
+    std::fs::rename(&inputs_tmp, &inputs_yaml_path).map_err(GemmaError::Io)?;
 
     let store_arg = format!("--store={}", store_dir.display());
     let manifest_arg = format!("--guix-manifest={manifest_path}");