Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/tracedecay-query/src/code_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ impl CodeIndexSearchCoverageV1 {

/// Build response coverage from the authenticated exact/lexical/graph
/// fallback receipt instead of assuming every admitted lane completed.
#[hotpath::measure(label = "query.code_search.map_fallback_coverage")]
pub fn from_fallback_lane_coverage(
fallback: &BTreeMap<
tracedecay_domain::RetrieverKind,
Expand Down Expand Up @@ -313,8 +314,12 @@ impl CodeIndexSearchCoverageV1 {
reason: CodeIndexSearchUnavailableReasonV1,
) -> std::result::Result<Self, CodeIndexSearchUnavailableReasonV1> {
if self.any_servable() {
if self.is_degraded() {
hotpath::gauge!("query.code_search.degraded_total").inc(1u64);
}
Ok(self)
} else {
hotpath::gauge!("query.code_search.unavailable_total").inc(1u64);
Err(reason)
}
}
Expand Down
31 changes: 18 additions & 13 deletions crates/tracedecay-query/src/retrieval/exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl ExactAdmissionValidator for CentralExactAdmissionAuthorityV1 {
}

impl ExactAdmissionAuthority for CentralExactAdmissionAuthorityV1 {
#[hotpath::measure(label = "query.lane.exact.parse_literals")]
fn parse_literals(
&self,
query_view: &EphemeralSanitizedQueryViewV1,
Expand Down Expand Up @@ -617,6 +618,7 @@ where
/// admission authority, then rebuild the committed deterministic prefix:
/// canonical order, sequential ordinals, deterministic fixed-point
/// scores, typed coverage, budget cutoff, and a checkpoint digest.
#[hotpath::measure(label = "query.lane.exact.enforce_batch")]
fn enforce_batch(
&self,
request: &ExactLaneRequest<'_>,
Expand Down Expand Up @@ -660,20 +662,23 @@ where
.map_err(contract_error)?;
// Only the central authority may mint a proof; re-admission
// binds this lane to proofs it can never construct itself.
let minted = self
.authority
.admit(
evidence.admission_proof.field,
&evidence.admission_proof.original_bytes,
&request.base,
)
.map_err(contract_error)?
.ok_or_else(|| {
RetrievalPortError::Contract(
"the central exact admission authority rejected the proof literal"
.to_owned(),
let minted = crate::hotpath_metrics::measure_frequent(
"query.lane.exact.readmit_row",
|| {
self.authority.admit(
evidence.admission_proof.field,
&evidence.admission_proof.original_bytes,
&request.base,
)
})?;
},
)
.map_err(contract_error)?
.ok_or_else(|| {
RetrievalPortError::Contract(
"the central exact admission authority rejected the proof literal"
.to_owned(),
)
})?;
if minted != evidence.admission_proof {
return Err(RetrievalPortError::Contract(
"exact admission proof was not minted by the central authority"
Expand Down
6 changes: 6 additions & 0 deletions crates/tracedecay-query/src/retrieval/fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl RetrievalCursorKeyringV1 {
keyed_mac(&material.secret, &bytes).map_err(map_cursor_key_error)
}

#[hotpath::measure(label = "query.stream.verify_cursor")]
pub(crate) fn verify_cursor(&self, cursor: &RetrievalCursor) -> Result<(), RetrievalError> {
let material = self
.key_material(&cursor.key_id, cursor.key_epoch)
Expand Down Expand Up @@ -474,6 +475,7 @@ impl CompositionLaneInput {
}
}

#[hotpath::measure(label = "query.fusion.compact_lane")]
fn compact_batch<E>(batch: RetrieverBatch<E>) -> Result<RetrieverBatch<()>, FusionStageError> {
batch.validate()?;
Ok(RetrieverBatch {
Expand Down Expand Up @@ -773,6 +775,7 @@ struct AdmittedLanes {
lane_checkpoints: Vec<RetrieverContinuation>,
}

#[hotpath::measure(label = "query.fusion.admit_lanes")]
fn admitted_lanes(
input: &FusionStageInput,
required_lanes: &[RetrieverKind],
Expand Down Expand Up @@ -1133,6 +1136,7 @@ fn decision_cmp(left: &RankingDecision, right: &RankingDecision) -> Ordering {
.then_with(|| left.detail.cmp(&right.detail))
}

#[hotpath::measure(label = "query.fusion.attach_decisions")]
fn attach_same_source_decisions(
candidates: &mut [FusedCandidate],
decisions: &[DedupeDecisionV1],
Expand Down Expand Up @@ -1185,6 +1189,7 @@ fn attach_same_source_decisions(
Ok(())
}

#[hotpath::measure(label = "query.stream.digest_set")]
pub fn digest_candidate_set(
candidates: &[RankedCandidate],
) -> Result<CandidateSetDigest, RetrievalError> {
Expand All @@ -1194,6 +1199,7 @@ pub fn digest_candidate_set(
}

#[allow(clippy::too_many_arguments)]
#[hotpath::measure(label = "query.stream.build_cursor")]
fn build_cursor(
request: &RetrievalRequest,
output: &CompositionOutputV1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3070,6 +3070,7 @@ fn sqlite_file_size(connection: &Connection) -> Result<u64, CodeLexicalArtifactE
})
}

#[hotpath::measure(label = "query.artifact.create.schema")]
fn create_schema(connection: &Connection) -> Result<(), CodeLexicalArtifactErrorV1> {
connection
.execute_batch(
Expand Down Expand Up @@ -4107,6 +4108,7 @@ fn finish_section(
/// counting/replaying every staged page on each bounded finalization wake.
/// The final section receipts validate the full source-page cardinality before
/// a sealed artifact is published.
#[hotpath::measure(label = "query.artifact.finalization.source_chain_verify")]
fn verify_staged_source_chain(
connection: &Connection,
source: &VerifiedSealedLexicalSourceReceiptV1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub(super) fn initial_base_section_receipt_fold()
Ok((row_counts, accumulators))
}

#[hotpath::measure(label = "query.artifact.finalization.receipt_absorb")]
pub(super) fn absorb_page_base_sections_receipt(
page_ordinal: u64,
bytes: &[u8],
Expand Down Expand Up @@ -321,6 +322,7 @@ fn validate_page_base_sections(
Ok(())
}

#[hotpath::measure(label = "query.artifact.index_verify")]
pub(super) fn verify_required_artifact_indexes(
connection: &Connection,
) -> Result<(), CodeLexicalArtifactErrorV1> {
Expand Down Expand Up @@ -541,6 +543,7 @@ pub(super) fn verify_artifact_table_layout(
Ok(())
}

#[hotpath::measure(label = "query.artifact.ngram.page_digest")]
pub(super) fn ngram_page_digest<'a>(
page_ordinal: u64,
rows: impl IntoIterator<Item = (i64, i64, &'a [u8], u64)>,
Expand Down Expand Up @@ -574,6 +577,14 @@ pub(super) fn ngram_page_digest<'a>(

pub(super) fn encode_ngram_bitmap(
bitmap: &RoaringBitmap,
) -> Result<Vec<u8>, CodeLexicalArtifactErrorV1> {
crate::hotpath_metrics::measure_frequent("query.artifact.ngram.encode_shard", || {
encode_ngram_bitmap_inner(bitmap)
})
}

fn encode_ngram_bitmap_inner(
bitmap: &RoaringBitmap,
) -> Result<Vec<u8>, CodeLexicalArtifactErrorV1> {
let cardinality = bitmap.len();
let mut range_count = 0u64;
Expand Down Expand Up @@ -650,6 +661,14 @@ pub(super) fn encode_ngram_bitmap(

pub(super) fn decode_ngram_bitmap(
encoded: &[u8],
) -> Result<RoaringBitmap, CodeLexicalArtifactErrorV1> {
crate::hotpath_metrics::measure_frequent("query.artifact.ngram.decode_shard", || {
decode_ngram_bitmap_inner(encoded)
})
}

fn decode_ngram_bitmap_inner(
encoded: &[u8],
) -> Result<RoaringBitmap, CodeLexicalArtifactErrorV1> {
let header = encoded.get(..16).ok_or_else(|| {
CodeLexicalArtifactErrorV1::Corrupt(
Expand Down Expand Up @@ -982,6 +1001,7 @@ pub(super) fn decode_padded_receipt(
/// Decode a fixed-size receipt while honoring the caller's canonical work
/// control. Reopen paths use this version so a corrupt or cold artifact never
/// turns an expired epoch into an unbounded padding scan.
#[hotpath::measure(label = "query.artifact.open.receipt_decode")]
pub(super) fn decode_padded_receipt_with_control(
bytes: &[u8],
control: &dyn CodeIndexExecutionControlV1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ fn reserve_ngram_scratch(
pub(super) fn document_ngrams(
bytes: &[u8],
control: &dyn CodeIndexExecutionControlV1,
) -> Result<Vec<u32>, CodeLexicalArtifactErrorV1> {
crate::hotpath_metrics::measure_frequent("query.artifact.ngram.project_document", || {
document_ngrams_inner(bytes, control)
})
}

fn document_ngrams_inner(
bytes: &[u8],
control: &dyn CodeIndexExecutionControlV1,
) -> Result<Vec<u32>, CodeLexicalArtifactErrorV1> {
let (_, scratch_bytes) = document_ngram_scratch(bytes.len())?;
if scratch_bytes > ARTIFACT_DOCUMENT_SCRATCH_LIMIT_BYTES {
Expand Down Expand Up @@ -103,6 +112,7 @@ pub(super) fn document_ngrams(
Ok(ngrams)
}

#[hotpath::measure(label = "query.artifact.ngram.project_query")]
pub(super) fn query_ngrams(bytes: &[u8]) -> BTreeSet<u32> {
let width = bytes.len().min(3);
if width == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub(super) struct PreparedTermPostingV1 {
pub(super) frequency: i64,
}

#[hotpath::measure(label = "query.artifact.prepare_page")]
pub(super) fn prepare_page(
metadata: &CodeLexicalProjectionMetadataV1,
page: &VerifiedSealedLexicalPageV1,
Expand Down Expand Up @@ -274,6 +275,7 @@ pub(super) fn prepare_page(
Ok(prepared)
}

#[hotpath::measure(label = "query.artifact.prepare_receipt")]
fn prepare_base_sections_receipt(
page_ordinal: u64,
imports: &[PreparedImportV1],
Expand Down Expand Up @@ -362,6 +364,18 @@ fn prepare_document(
chunk: &tracedecay_domain::CodeSearchChunkV1,
display: Option<&tracedecay_code_index::production::VerifiedSealedLexicalSymbolDisplayV1>,
control: &dyn CodeIndexExecutionControlV1,
) -> Result<(PreparedDocumentV1, Vec<(i64, i64)>), CodeLexicalArtifactErrorV1> {
crate::hotpath_metrics::measure_frequent("query.artifact.prepare_document", || {
prepare_document_inner(metadata, document_id, chunk, display, control)
})
}

fn prepare_document_inner(
metadata: &CodeLexicalProjectionMetadataV1,
document_id: i64,
chunk: &tracedecay_domain::CodeSearchChunkV1,
display: Option<&tracedecay_code_index::production::VerifiedSealedLexicalSymbolDisplayV1>,
control: &dyn CodeIndexExecutionControlV1,
) -> Result<(PreparedDocumentV1, Vec<(i64, i64)>), CodeLexicalArtifactErrorV1> {
u32::try_from(document_id).map_err(|_| {
CodeLexicalArtifactErrorV1::Contract(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,7 @@ impl<'a> ArtifactQueryV1<'a> {
decode_row(&bytes).map_err(map_query_artifact_error)
}

#[hotpath::measure(label = "query.lane.lexical.select_documents")]
fn lexical_documents(
&self,
request: &LexicalLaneRequest<'_>,
Expand Down Expand Up @@ -1586,6 +1587,7 @@ impl<'a> ArtifactQueryV1<'a> {
union_document_queries(sources)
}

#[hotpath::measure(label = "query.lane.exact.select_documents")]
fn exact_documents(
&self,
request: &ExactLaneRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ impl ByteNgramPostings {
document: u32,
bytes: &[u8],
budget: &mut ByteNgramBudget,
) -> Result<(), String> {
crate::hotpath_metrics::measure_frequent("query.artifact.ngram.insert_document", || {
self.insert_document_inner(document, bytes, budget)
})
}

fn insert_document_inner(
&mut self,
document: u32,
bytes: &[u8],
budget: &mut ByteNgramBudget,
) -> Result<(), String> {
let scratch_entries = (1..=bytes.len().min(3)).try_fold(0usize, |entries, width| {
entries
Expand Down Expand Up @@ -241,6 +252,7 @@ impl ByteNgramPostings {
Ok(postings)
}

#[hotpath::measure(label = "query.artifact.ngram.candidate_documents")]
pub(super) fn candidate_documents(&self, needle: &[u8]) -> RoaringBitmap {
let width = needle.len().min(3);
if width == 0 {
Expand Down Expand Up @@ -391,6 +403,7 @@ pub(super) struct FuzzySearchSlice {
}

impl FuzzyTermIndex {
#[hotpath::measure(label = "query.lane.fuzzy.build_index")]
pub(super) fn from_terms<I, S>(terms: I, deadline: Option<Instant>) -> Result<Self, String>
where
I: IntoIterator<Item = S>,
Expand All @@ -414,6 +427,7 @@ impl FuzzyTermIndex {
Ok(Self { terms })
}

#[hotpath::measure(label = "query.lane.fuzzy.terms_at_distance")]
pub(super) fn terms_at_distance(
&self,
query: &str,
Expand Down
15 changes: 15 additions & 0 deletions crates/tracedecay-query/src/retrieval/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub(crate) fn contract_error(error: impl std::fmt::Display) -> RetrievalPortErro
/// the payload differs between lanes, so only the payload is a lane's
/// business. Cursor bytes are ephemeral; live cursors minted before this
/// encoding are rejected as a set mismatch.
#[hotpath::measure(label = "query.lane.checkpoint_digest")]
pub(crate) fn checkpoint_digest<T>(payload: &T) -> Result<CursorPayloadDigest, RetrievalPortError>
where
T: Serialize,
Expand Down Expand Up @@ -149,6 +150,20 @@ pub(crate) fn lane_bound_evidence<'batch, E>(
lane: RetrieverKind,
rejections: &LaneEvidenceRejections,
) -> Result<&'batch E, RetrievalPortError>
where
E: LaneBoundEvidence,
{
crate::hotpath_metrics::measure_frequent("query.lane.bind_evidence", || {
lane_bound_evidence_inner(batch, candidate, lane, rejections)
})
}

fn lane_bound_evidence_inner<'batch, E>(
batch: &'batch RetrieverBatch<E>,
candidate: &CompactCandidate,
lane: RetrieverKind,
rejections: &LaneEvidenceRejections,
) -> Result<&'batch E, RetrievalPortError>
where
E: LaneBoundEvidence,
{
Expand Down
13 changes: 9 additions & 4 deletions crates/tracedecay-query/src/retrieval/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ where
/// are bit-identical to a flat scan's for every returned row. Coverage is
/// candidate-bounded and the continuation never claims exhaustion: an
/// index-bounded candidate set cannot prove no better row exists.
#[hotpath::measure(label = "query.lane.semantic.ann_rescore")]
fn retrieve_ann_exact_rescore(
&self,
request: &SemanticRetrievalRequestV1<'_>,
Expand Down Expand Up @@ -571,6 +572,7 @@ where
}

/// Exact-flat scan: visit and exactly score every published row.
#[hotpath::measure(label = "query.lane.semantic.exact_flat")]
fn retrieve_exact_flat(
&self,
request: &SemanticRetrievalRequestV1<'_>,
Expand Down Expand Up @@ -696,6 +698,7 @@ where
/// full sort followed by truncation), bind evidence, and validate the
/// batch. Shared by the exact-flat and ANN-rescore paths; only their
/// coverage accounting and exhaustion claims differ.
#[hotpath::measure(label = "query.lane.semantic.assemble")]
fn assemble_ranked_batch(
&self,
request: &SemanticRetrievalRequestV1<'_>,
Expand Down Expand Up @@ -776,10 +779,12 @@ where
self.control,
)));
}
let query = match self.embedder.embed_query(SemanticQueryEmbeddingRequestV1 {
query_digest: &request.query_digest,
query_view: request.query_view,
projection: request.projection,
let query = match hotpath::measure_block!("query.lane.semantic.embed_query", {
self.embedder.embed_query(SemanticQueryEmbeddingRequestV1 {
query_digest: &request.query_digest,
query_view: request.query_view,
projection: request.projection,
})
}) {
Ok(query) => query,
Err(error) => {
Expand Down
Loading