runtime: cut repository body observation latency
This commit is contained in:
parent
5d6a731607
commit
9d671aeef0
7 changed files with 252 additions and 22 deletions
143
epiphany-core/src/bin/epiphany-runtime-store-benchmark.rs
Normal file
143
epiphany-core/src/bin/epiphany-runtime-store-benchmark.rs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
use anyhow::{Context, Result, anyhow};
|
||||
use cultcache_rs::{
|
||||
CacheBackingStore, CultCacheEnvelope, RedbMessagePackBackingStore,
|
||||
SingleFileMessagePackBackingStore,
|
||||
};
|
||||
use epiphany_core::{EpiphanyRuntimeJob, RUNTIME_JOB_TYPE};
|
||||
use serde::Serialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::path::Path;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BenchmarkReceipt {
|
||||
schema_version: &'static str,
|
||||
store: String,
|
||||
job_id: String,
|
||||
job_created_at: String,
|
||||
selection_window_milliseconds: i64,
|
||||
selected_stored_ats: Vec<String>,
|
||||
selected_entry_count: usize,
|
||||
selected_identities: Vec<String>,
|
||||
selected_envelope_set_sha256: String,
|
||||
read_milliseconds: u128,
|
||||
cas_milliseconds: Vec<u128>,
|
||||
}
|
||||
|
||||
enum Store {
|
||||
Snapshot(SingleFileMessagePackBackingStore),
|
||||
Keyed(RedbMessagePackBackingStore),
|
||||
}
|
||||
|
||||
impl Store {
|
||||
fn open(path: &Path) -> Result<Self> {
|
||||
match path.extension().and_then(|value| value.to_str()) {
|
||||
Some("cc" | "msgpack") => {
|
||||
Ok(Self::Snapshot(SingleFileMessagePackBackingStore::new(path)))
|
||||
}
|
||||
Some("redb") => Ok(Self::Keyed(RedbMessagePackBackingStore::new(path)?)),
|
||||
extension => Err(anyhow!(
|
||||
"unsupported benchmark store extension {extension:?}"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn pull_all(&self) -> Result<Vec<CultCacheEnvelope>> {
|
||||
match self {
|
||||
Self::Snapshot(store) => store.pull_all(),
|
||||
Self::Keyed(store) => store.pull_all(),
|
||||
}
|
||||
}
|
||||
|
||||
fn compare_and_swap_batch(
|
||||
&self,
|
||||
expected: &[CultCacheEnvelope],
|
||||
replacements: Vec<CultCacheEnvelope>,
|
||||
) -> Result<bool> {
|
||||
match self {
|
||||
Self::Snapshot(store) => store.compare_and_swap_batch(expected, replacements),
|
||||
Self::Keyed(store) => store.compare_and_swap_batch(expected, replacements),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let store_path = args
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("usage: epiphany-runtime-store-benchmark <disposable-store> <historical-job-id> [repetitions]"))?;
|
||||
let job_id = args
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("missing historical job id"))?;
|
||||
let repetitions = args
|
||||
.next()
|
||||
.map(|value| value.parse::<usize>())
|
||||
.transpose()
|
||||
.context("invalid repetitions")?
|
||||
.unwrap_or(3);
|
||||
if repetitions == 0 || args.next().is_some() {
|
||||
return Err(anyhow!("repetitions must be positive and arguments exact"));
|
||||
}
|
||||
let path = Path::new(&store_path);
|
||||
let store = Store::open(path)?;
|
||||
let read_started = Instant::now();
|
||||
let entries = store.pull_all()?;
|
||||
let read_milliseconds = read_started.elapsed().as_millis();
|
||||
let job = entries
|
||||
.iter()
|
||||
.find(|entry| entry.r#type == RUNTIME_JOB_TYPE && entry.key == job_id)
|
||||
.ok_or_else(|| anyhow!("historical runtime job envelope is absent"))?;
|
||||
let runtime_job: EpiphanyRuntimeJob = rmp_serde::from_slice(&job.payload)?;
|
||||
let job_created_at = runtime_job.created_at;
|
||||
let job_created = chrono::DateTime::parse_from_rfc3339(&job_created_at)?;
|
||||
let selection_window_milliseconds = 2_000_i64;
|
||||
let mut selected = entries
|
||||
.into_iter()
|
||||
.filter(|entry| {
|
||||
chrono::DateTime::parse_from_rfc3339(&entry.stored_at)
|
||||
.map(|stored| {
|
||||
(stored.timestamp_millis() - job_created.timestamp_millis()).abs()
|
||||
<= selection_window_milliseconds
|
||||
})
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
selected.sort_by(|left, right| (&left.r#type, &left.key).cmp(&(&right.r#type, &right.key)));
|
||||
if selected.is_empty() {
|
||||
return Err(anyhow!("historical job commit selected no envelopes"));
|
||||
}
|
||||
let selected_identities = selected
|
||||
.iter()
|
||||
.map(|entry| format!("{}:{}", entry.r#type, entry.key))
|
||||
.collect::<Vec<_>>();
|
||||
let selected_stored_ats = selected
|
||||
.iter()
|
||||
.map(|entry| entry.stored_at.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let selected_envelope_set_sha256 =
|
||||
format!("{:x}", Sha256::digest(rmp_serde::to_vec_named(&selected)?));
|
||||
let mut cas_milliseconds = Vec::with_capacity(repetitions);
|
||||
for _ in 0..repetitions {
|
||||
let started = Instant::now();
|
||||
if !store.compare_and_swap_batch(&selected, selected.clone())? {
|
||||
return Err(anyhow!("exact historical batch CAS unexpectedly refused"));
|
||||
}
|
||||
cas_milliseconds.push(started.elapsed().as_millis());
|
||||
}
|
||||
let receipt = BenchmarkReceipt {
|
||||
schema_version: "epiphany.runtime_store_benchmark_receipt.v1",
|
||||
store: path.display().to_string(),
|
||||
job_id,
|
||||
job_created_at,
|
||||
selection_window_milliseconds,
|
||||
selected_stored_ats,
|
||||
selected_entry_count: selected.len(),
|
||||
selected_identities,
|
||||
selected_envelope_set_sha256,
|
||||
read_milliseconds,
|
||||
cas_milliseconds,
|
||||
};
|
||||
println!("{}", serde_json::to_string_pretty(&receipt)?);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -908,7 +908,14 @@ fn git_bytes(repo: &Path, envs: &[(&str, &str)], args: &[&str]) -> Result<Vec<u8
|
|||
Ok(output.stdout)
|
||||
}
|
||||
fn raw_manifest_entries(repo: &Path, index: &[u8]) -> Result<Vec<RepositoryBodyManifestEntry>> {
|
||||
let mut entries = Vec::new();
|
||||
#[derive(Clone)]
|
||||
struct IndexRecord {
|
||||
mode: String,
|
||||
oid: String,
|
||||
path: String,
|
||||
}
|
||||
|
||||
let mut records = Vec::new();
|
||||
for raw in index.split(|byte| *byte == 0).filter(|raw| !raw.is_empty()) {
|
||||
let tab = raw
|
||||
.iter()
|
||||
|
|
@ -930,22 +937,58 @@ fn raw_manifest_entries(repo: &Path, index: &[u8]) -> Result<Vec<RepositoryBodyM
|
|||
if fields.next().is_some() || stage != "0" {
|
||||
bail!("Git index contains an unresolved or malformed staged entry");
|
||||
}
|
||||
let path_buf = safe_worktree_path(repo, path)?;
|
||||
let entry = match mode {
|
||||
"100644" | "100755" => raw_regular_entry(&path_buf, path, mode)?,
|
||||
"120000" => raw_symlink_entry(&path_buf, path, mode)?,
|
||||
"160000" => RepositoryBodyManifestEntry {
|
||||
path: path.into(),
|
||||
git_mode: mode.into(),
|
||||
kind: "gitlink_nonrecursive".into(),
|
||||
raw_byte_length: 0,
|
||||
raw_sha256: String::new(),
|
||||
gitlink_oid: Some(oid.into()),
|
||||
},
|
||||
_ => bail!("unsupported Git index mode {mode:?} at {path:?}"),
|
||||
};
|
||||
entries.push(entry);
|
||||
records.push(IndexRecord {
|
||||
mode: mode.to_string(),
|
||||
oid: oid.to_string(),
|
||||
path: path.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let available = std::thread::available_parallelism()
|
||||
.map(usize::from)
|
||||
.unwrap_or(1);
|
||||
let worker_count = records.len().min((available * 4).clamp(1, 16));
|
||||
let chunk_size = records.len().div_ceil(worker_count.max(1));
|
||||
let mut entries = std::thread::scope(|scope| -> Result<Vec<_>> {
|
||||
let mut workers = Vec::new();
|
||||
for chunk in records.chunks(chunk_size.max(1)) {
|
||||
workers.push(scope.spawn(move || -> Result<Vec<RepositoryBodyManifestEntry>> {
|
||||
let mut local = Vec::with_capacity(chunk.len());
|
||||
for record in chunk {
|
||||
let path_buf = safe_worktree_path(repo, &record.path)?;
|
||||
let entry = match record.mode.as_str() {
|
||||
"100644" | "100755" => {
|
||||
raw_regular_entry(&path_buf, &record.path, &record.mode)?
|
||||
}
|
||||
"120000" => raw_symlink_entry(&path_buf, &record.path, &record.mode)?,
|
||||
"160000" => RepositoryBodyManifestEntry {
|
||||
path: record.path.clone(),
|
||||
git_mode: record.mode.clone(),
|
||||
kind: "gitlink_nonrecursive".into(),
|
||||
raw_byte_length: 0,
|
||||
raw_sha256: String::new(),
|
||||
gitlink_oid: Some(record.oid.clone()),
|
||||
},
|
||||
mode => bail!(
|
||||
"unsupported Git index mode {mode:?} at {:?}",
|
||||
record.path
|
||||
),
|
||||
};
|
||||
local.push(entry);
|
||||
}
|
||||
Ok(local)
|
||||
}));
|
||||
}
|
||||
let mut joined = Vec::with_capacity(records.len());
|
||||
for worker in workers {
|
||||
joined.extend(
|
||||
worker
|
||||
.join()
|
||||
.map_err(|_| anyhow!("repository Body manifest worker panicked"))??,
|
||||
);
|
||||
}
|
||||
Ok(joined)
|
||||
})?;
|
||||
entries.sort_by(|a, b| a.path.as_bytes().cmp(b.path.as_bytes()));
|
||||
if entries.windows(2).any(|pair| pair[0].path == pair[1].path) {
|
||||
bail!("duplicate path in repository Body manifest");
|
||||
|
|
|
|||
Reference in a new issue