use super::*;
use crate::error::SearchProjectionRebuildRefusal;
use crate::search::{
SearchDocumentSetDigest, SearchMutationSortStepLimits, SearchOutOfCoreGenerationBuildReport,
SearchOutOfCoreGenerationWriter, SearchProjectionScanBudget, SearchUnorderedMutationLimits,
SearchUnorderedMutationReport,
};
use crate::SearchProjectionRow;
use std::num::{NonZeroU64, NonZeroUsize};
use std::sync::atomic::AtomicU64;
#[derive(Debug, Clone)]
pub struct SearchProjectionRebuildOptions {
pub maintenance: IncrementalSearchMaintenanceOptions,
pub priority: RuntimeWorkPriority,
pub hydration_batch_rows: NonZeroUsize,
pub sorting: SearchUnorderedMutationLimits,
pub sort_step: SearchMutationSortStepLimits,
}
impl Default for SearchProjectionRebuildOptions {
fn default() -> Self {
Self {
maintenance: IncrementalSearchMaintenanceOptions::default(),
priority: RuntimeWorkPriority::Background,
hydration_batch_rows: NonZeroUsize::new(1024).unwrap(),
sorting: SearchUnorderedMutationLimits {
max_buffer_records: NonZeroUsize::new(16_384).unwrap(),
max_buffer_payload_bytes: NonZeroUsize::new(32 * 1024 * 1024).unwrap(),
max_input_operations: NonZeroUsize::new(100_000_000).unwrap(),
max_spill_bytes: NonZeroU64::new(4 * 1024 * 1024 * 1024 * 1024).unwrap(),
max_read_bytes: NonZeroU64::new(16 * 1024 * 1024 * 1024 * 1024).unwrap(),
max_merge_visits: NonZeroUsize::new(usize::MAX >> 1).unwrap(),
max_sort_work: NonZeroUsize::new(usize::MAX >> 1).unwrap(),
},
sort_step: SearchMutationSortStepLimits::default(),
}
}
}
pub trait SearchProjectionRebuildSource {
fn hydrate_graph_rows(
&mut self,
_snapshot: &mut DatabaseReadTransaction,
_rows: &mut Vec<SearchProjectionRow>,
) -> Result<()> {
Ok(())
}
fn visit_host_rows(
&mut self,
_snapshot: &mut DatabaseReadTransaction,
_emit: &mut dyn FnMut(SearchProjectionRow) -> Result<()>,
) -> Result<()> {
Ok(())
}
fn progress(&mut self, _progress: &SearchProjectionRebuildProgress) {}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GraphOnlySearchProjectionRebuild;
impl SearchProjectionRebuildSource for GraphOnlySearchProjectionRebuild {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchProjectionRebuildPhase {
Scan,
Build,
Verify,
Install,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SearchProjectionRebuildProgress {
pub phase: SearchProjectionRebuildPhase,
pub source_graph_commit_epoch: u64,
pub scanned_nodes: u64,
pub graph_documents: u64,
pub host_documents: u64,
pub sorting: SearchUnorderedMutationReport,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SearchProjectionRebuildVerification {
pub graph: SearchDocumentSetDigest,
pub generation: SearchDocumentSetDigest,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchProjectionRebuildReport {
pub target_path: PathBuf,
pub source_graph_commit_epoch: u64,
pub scanned_nodes: u64,
pub graph_documents: u64,
pub host_documents: u64,
pub sorting: SearchUnorderedMutationReport,
pub build: SearchOutOfCoreGenerationBuildReport,
pub verification: SearchProjectionRebuildVerification,
pub working_memory_bytes: u64,
pub elapsed_micros: u64,
}
struct BuiltStage {
sorting: SearchUnorderedMutationReport,
build: SearchOutOfCoreGenerationBuildReport,
verification: SearchProjectionRebuildVerification,
counts: StreamCounts,
epoch: u64,
}
#[derive(Debug, Default, Clone, Copy)]
struct StreamCounts {
scanned_nodes: u64,
graph_documents: u64,
host_documents: u64,
}
impl EmbeddedStoreHandle {
pub fn rebuild_out_of_core_search_projection(
&self,
target: impl AsRef<Path>,
options: SearchProjectionRebuildOptions,
source: &mut dyn SearchProjectionRebuildSource,
task: RuntimeTaskContext,
) -> Result<SearchProjectionRebuildReport> {
let started = Instant::now();
options.maintenance.validate()?;
let target = target.as_ref();
let stage = RebuildStage::create(target)?;
let working = options.maintenance.working_memory_bytes.get();
let permit = {
let store = self.read_store()?;
store.graph.check_runtime_context(&task)?;
store.graph.try_admit_runtime(
RuntimeWorkRequest::new(options.priority, RuntimeWorkKind::Maintenance)
.with_cpu_slots(1)
.with_memory_bytes(working)
.with_io_slots(1)
.with_blocking(true),
)?
};
let task = permit
.bind_task_context(task)
.with_memory_reservation(hawdb_core::RuntimeMemoryReservation::new(working, 0));
let owned = Arc::clone(&self.read_store()?.externally_owned_search_kinds);
let carry_over_available =
!owned.is_empty() && self.carry_over_unavailable(&owned).is_none();
let ownership = Ownership {
handle: self,
owned: &owned,
carry_over: carry_over_available,
};
let result = self
.rebuild_into_stage(&stage, &options, source, &ownership, &task)
.and_then(|built| {
if !owned.is_empty() {
record_ownership(&stage.path, &owned)?;
}
Ok(built)
});
let BuiltStage {
sorting,
build,
verification,
counts,
epoch,
} = match result {
Ok(value) => value,
Err(error) => {
drop(permit);
return Err(error);
}
};
source.progress(&SearchProjectionRebuildProgress {
phase: SearchProjectionRebuildPhase::Install,
source_graph_commit_epoch: epoch,
scanned_nodes: counts.scanned_nodes,
graph_documents: counts.graph_documents,
host_documents: counts.host_documents,
sorting,
});
let installed = stage.install(&task);
permit.release();
installed?;
Ok(SearchProjectionRebuildReport {
target_path: target.to_path_buf(),
source_graph_commit_epoch: epoch,
scanned_nodes: counts.scanned_nodes,
graph_documents: counts.graph_documents,
host_documents: counts.host_documents,
sorting,
build,
verification,
working_memory_bytes: working,
elapsed_micros: u64::try_from(started.elapsed().as_micros()).unwrap_or(u64::MAX),
})
}
fn rebuild_into_stage(
&self,
stage: &RebuildStage,
options: &SearchProjectionRebuildOptions,
source: &mut dyn SearchProjectionRebuildSource,
ownership: &Ownership<'_>,
task: &RuntimeTaskContext,
) -> Result<BuiltStage> {
let scan = {
let store = self.read_store()?;
store
.graph
.database()
.begin_read_transaction_with_context(task)?
};
let mut host = scan.fork_at_same_snapshot(Some(task.clone()))?;
let epoch = scan.commit_epoch();
let maintenance = &options.maintenance;
let mut build_options = maintenance.build_options.clone();
build_options.source_graph_commit_epoch = Some(epoch);
let mut writer =
SearchOutOfCoreGenerationWriter::create_unordered_replacement_with_context(
&stage.path,
build_options,
&maintenance.reader_config,
options.sorting,
task.clone(),
)?;
let sort_step = options.sort_step;
let mut progress = SearchProjectionRebuildProgress {
phase: SearchProjectionRebuildPhase::Scan,
source_graph_commit_epoch: epoch,
scanned_nodes: 0,
graph_documents: 0,
host_documents: 0,
sorting: writer.report(),
};
let counts = stream_documents(
&scan,
&mut host,
source,
ownership,
Pass::Build,
options.hydration_batch_rows,
task,
&mut progress,
&mut |document, progress| {
writer.queue_upsert(document)?;
while !writer.ready_for_input() {
writer.advance_ordering(sort_step)?;
}
progress.sorting = writer.report();
Ok(())
},
)?;
writer.finish_input()?;
progress.phase = SearchProjectionRebuildPhase::Build;
while !writer.ordering_complete() {
writer.advance_ordering(sort_step)?;
progress.sorting = writer.report();
source.progress(&progress);
}
let (sorting, candidate) = writer.prepare_publication()?;
let build = candidate.publish()?;
progress.phase = SearchProjectionRebuildPhase::Verify;
progress.sorting = sorting;
source.progress(&progress);
let projection =
OutOfCoreSearchProjection::open_incremental(&stage.path, maintenance.clone())?;
if projection.reader.source_graph_commit_epoch() != Some(epoch) {
return Err(refused(
SearchProjectionRebuildRefusal::VerificationMismatch,
&stage.target,
format!(
"rebuilt search projection is stamped at {:?}, expected graph snapshot epoch {epoch}; nothing was installed",
projection.reader.source_graph_commit_epoch()
),
));
}
let build_options = &maintenance.build_options;
let budget = SearchProjectionScanBudget {
max_rows: build_options.max_documents.get(),
max_payload_bytes: usize::try_from(build_options.max_logical_document_bytes.get())
.unwrap_or(usize::MAX),
max_encoded_record_bytes: usize::try_from(build_options.max_record_bytes.get())
.unwrap_or(usize::MAX),
};
let mut generation = SearchDocumentSetDigest::default();
let mut generation_total = 0usize;
projection
.reader
.visit_projection_documents(budget, task, &mut |document| {
generation_total += 1;
if !ownership.owns_document(document) {
generation.add(document);
}
Ok(())
})?;
drop(projection);
let mut graph = SearchDocumentSetDigest::default();
let mut verify_progress = progress;
stream_documents(
&scan,
&mut host,
source,
ownership,
Pass::Verify,
options.hydration_batch_rows,
task,
&mut verify_progress,
&mut |document, _| {
graph.add(&document);
Ok(())
},
)?;
if graph != generation || generation_total != build.document_count {
return Err(refused(
SearchProjectionRebuildRefusal::VerificationMismatch,
&stage.target,
format!(
"rebuilt search projection differs from graph snapshot {epoch}: graph {} documents ({}), generation {} documents ({}), build reported {}; nothing was installed",
graph.document_count(),
graph.content_digest_hex(),
generation.document_count(),
generation.content_digest_hex(),
build.document_count,
),
));
}
Ok(BuiltStage {
sorting,
build,
verification: SearchProjectionRebuildVerification { graph, generation },
counts,
epoch,
})
}
}
const CARRY_OVER_MAX_BATCH_BYTES: usize = 64 * 1024 * 1024;
#[cfg(test)]
thread_local! {
pub(super) static FAIL_CARRY_OVER_PROBE: std::cell::Cell<bool> =
const { std::cell::Cell::new(false) };
}
struct Ownership<'a> {
handle: &'a EmbeddedStoreHandle,
owned: &'a BTreeSet<SearchProjectionKind>,
carry_over: bool,
}
impl Ownership<'_> {
fn owns(&self, kind: SearchProjectionKind) -> bool {
self.owned.contains(&kind)
}
fn owns_document(&self, document: &SearchDocument) -> bool {
document
.id
.split_once(':')
.and_then(|(kind, _)| SearchProjectionKind::from_name(kind))
.is_some_and(|kind| self.owns(kind))
}
fn carry(&self, owned: Vec<SearchProjectionRow>) -> Result<Vec<SearchProjectionRow>> {
if owned.is_empty() || !self.carry_over {
return Ok(Vec::new());
}
let ids = owned
.iter()
.map(|row| format!("{}:{}", row.kind.as_str(), row.external_id))
.collect::<Vec<_>>();
let mut by_id = self
.handle
.lookup_previous(&ids)?
.into_iter()
.map(|document| (document.id.clone(), document))
.collect::<BTreeMap<_, _>>();
Ok(owned
.into_iter()
.zip(ids)
.filter_map(|(row, id)| {
by_id
.remove(&id)
.map(|document| row_from_carried(row, document))
})
.collect())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Pass {
Build,
Verify,
}
fn record_ownership(stage: &Path, owned: &BTreeSet<SearchProjectionKind>) -> Result<()> {
use crate::search::projection_ownership::{raise_rows_required, write_record};
raise_rows_required(stage)?;
write_record(stage, owned)
}
fn row_from_carried(row: SearchProjectionRow, document: SearchDocument) -> SearchProjectionRow {
let SearchDocument {
title,
content,
embedding,
mut metadata,
..
} = document;
metadata.remove("kind");
metadata.remove("external_id");
let source_id = metadata.remove("source_id");
SearchProjectionRow {
kind: row.kind,
external_id: row.external_id,
title,
body: content,
embedding,
source_id,
metadata,
}
}
impl EmbeddedStoreHandle {
fn carry_over_unavailable(&self, owned: &BTreeSet<SearchProjectionKind>) -> Option<String> {
#[cfg(test)]
if FAIL_CARRY_OVER_PROBE.with(std::cell::Cell::get) {
return Some("injected carry-over failure".into());
}
let kind = owned.first()?.as_str();
self.lookup_previous(&[format!("{kind}:")])
.err()
.map(|error| error.to_string())
}
fn lookup_previous(&self, ids: &[String]) -> Result<Vec<SearchDocument>> {
let budget = crate::EmbeddedReadSnapshotBudget {
max_rows: ids.len(),
max_payload_bytes: CARRY_OVER_MAX_BATCH_BYTES,
};
self.with_bounded_read_snapshot(budget, |snapshot| {
snapshot
.lookup_search_projection_documents(ids)
.map(|lookup| lookup.documents)
})
}
}
#[allow(clippy::too_many_arguments)]
fn stream_documents(
scan: &DatabaseReadTransaction,
host: &mut DatabaseReadTransaction,
source: &mut dyn SearchProjectionRebuildSource,
ownership: &Ownership<'_>,
pass: Pass,
batch_rows: NonZeroUsize,
task: &RuntimeTaskContext,
progress: &mut SearchProjectionRebuildProgress,
sink: &mut dyn FnMut(SearchDocument, &mut SearchProjectionRebuildProgress) -> Result<()>,
) -> Result<StreamCounts> {
progress.scanned_nodes = 0;
progress.graph_documents = 0;
progress.host_documents = 0;
let mut batch = Vec::with_capacity(batch_rows.get());
let mut flush = |batch: &mut Vec<SearchProjectionRow>,
source: &mut dyn SearchProjectionRebuildSource,
host: &mut DatabaseReadTransaction,
progress: &mut SearchProjectionRebuildProgress|
-> Result<()> {
let (owned, mut rows): (Vec<_>, Vec<_>) =
batch.drain(..).partition(|row| ownership.owns(row.kind));
source.hydrate_graph_rows(host, &mut rows)?;
if pass == Pass::Build {
rows.extend(ownership.carry(owned)?);
}
for row in rows {
if pass == Pass::Verify && ownership.owns(row.kind) {
continue;
}
sink(row.into_document(), progress)?;
progress.graph_documents += 1;
}
source.progress(progress);
Ok(())
};
let scanned = scan.visit_search_projection_rows(task, &mut |row| {
batch.push(row);
if batch.len() == batch_rows.get() {
flush(&mut batch, source, host, progress)?;
}
Ok(())
})?;
progress.scanned_nodes = scanned;
if !batch.is_empty() {
flush(&mut batch, source, host, progress)?;
}
let mut host_documents = 0u64;
source.visit_host_rows(host, &mut |row| {
task.checkpoint().map_err(HawDBError::RuntimeTaskStopped)?;
if pass == Pass::Verify && ownership.owns(row.kind) {
return Ok(());
}
sink(row.into_document(), progress)?;
host_documents += 1;
Ok(())
})?;
progress.host_documents = host_documents;
source.progress(progress);
Ok(StreamCounts {
scanned_nodes: scanned,
graph_documents: progress.graph_documents,
host_documents,
})
}
fn refused(reason: SearchProjectionRebuildRefusal, target: &Path, message: String) -> HawDBError {
HawDBError::SearchProjectionRebuildRefused {
reason,
target: target.to_path_buf(),
message,
}
}
struct RebuildStage {
path: PathBuf,
target: PathBuf,
installed: bool,
}
impl RebuildStage {
fn create(target: &Path) -> Result<Self> {
if std::fs::symlink_metadata(target).is_ok() {
return Err(refused(
SearchProjectionRebuildRefusal::TargetExists,
target,
format!(
"search projection rebuild target {} already exists; the rebuild writes only to a new directory",
target.display()
),
));
}
let name = target.file_name().ok_or_else(|| {
refused(
SearchProjectionRebuildRefusal::TargetInvalid,
target,
format!(
"search projection rebuild target {} has no final path component",
target.display()
),
)
})?;
let parent = match target.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent.to_path_buf(),
_ => PathBuf::from("."),
};
if !parent.is_dir() {
return Err(refused(
SearchProjectionRebuildRefusal::TargetParentMissing,
target,
format!(
"search projection rebuild parent directory {} does not exist",
parent.display()
),
));
}
static SEQUENCE: AtomicU64 = AtomicU64::new(0);
let nonce = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_nanos());
let path = parent.join(format!(
".{}.hawdb-search-rebuild-{}-{nonce}-{}",
name.to_string_lossy(),
std::process::id(),
SEQUENCE.fetch_add(1, Ordering::Relaxed)
));
hawdb_storage::file_io::create_dir(&path).map_err(|error| {
HawDBError::or_stop(&error, || {
HawDBError::Storage(format!(
"create search projection rebuild stage {}: {error}",
path.display()
))
})
})?;
Ok(Self {
path,
target: target.to_path_buf(),
installed: false,
})
}
fn install(mut self, task: &RuntimeTaskContext) -> Result<()> {
task.checkpoint().map_err(HawDBError::RuntimeTaskStopped)?;
crate::sync_bootstrap_directory(&self.path)?;
#[cfg(test)]
hooks::before_install(&self.path, &self.target);
if let Err(error) = hawdb_storage::file_io::rename_directory_new(&self.path, &self.target) {
if std::fs::symlink_metadata(&self.path).is_ok() {
let message = format!(
"search projection rebuild target {} could not be installed ({error}); nothing was installed",
self.target.display()
);
return Err(if error.kind() == std::io::ErrorKind::AlreadyExists {
refused(
SearchProjectionRebuildRefusal::TargetAppeared,
&self.target,
message,
)
} else {
HawDBError::Storage(message)
});
}
self.installed = true;
return Err(self.outcome_unknown(format!("the move reported {error}")));
}
self.installed = true;
let parent = match self.target.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent,
_ => Path::new("."),
};
crate::sync_bootstrap_directory(&self.target)
.and_then(|()| crate::sync_bootstrap_directory(parent))
.map_err(|error| {
self.outcome_unknown(format!(
"the directory barrier after the move failed: {error}"
))
})
}
fn outcome_unknown(&self, message: String) -> HawDBError {
HawDBError::PublishOutcomeUnknown {
destination: self.target.clone(),
message: format!("search projection rebuild: {message}"),
}
}
}
impl Drop for RebuildStage {
fn drop(&mut self) {
if !self.installed {
let _ = hawdb_storage::file_io::remove_dir_all(&self.path);
}
}
}
#[cfg(test)]
pub(super) mod hooks {
use std::cell::RefCell;
use std::path::Path;
type BeforeInstall = Box<dyn FnOnce(&Path, &Path)>;
thread_local! {
static BEFORE_INSTALL: RefCell<Option<BeforeInstall>> = const { RefCell::new(None) };
}
pub(crate) fn set_before_install(hook: impl FnOnce(&Path, &Path) + 'static) {
BEFORE_INSTALL.with(|slot| *slot.borrow_mut() = Some(Box::new(hook)));
}
pub(super) fn before_install(stage: &Path, target: &Path) {
let hook = BEFORE_INSTALL.with(|slot| slot.borrow_mut().take());
if let Some(hook) = hook {
hook(stage, target);
}
}
}