-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH]: move materialization into operator (#3357)
## Description of changes Log materialization is now in its own operator. Having materialization in its own operator unlocks two main benefits: - allows us to pipeline log applications across segment types (currently unrealized) - we can easily bail for any partition with an empty set of materialized records before constructing writers ## Test plan *How are these changes tested?* - [x] Tests pass locally with `pytest` for python, `yarn test` for js, `cargo test` for rust Tested locally with SciDocs as well. ## Documentation Changes *Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the [docs repository](https://github.com/chroma-core/docs)?*
- Loading branch information
1 parent
6145277
commit 19e1971
Showing
5 changed files
with
390 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use crate::execution::operator::Operator; | ||
use crate::segment::record_segment::RecordSegmentReaderCreationError; | ||
use crate::segment::{materialize_logs, record_segment::RecordSegmentReader}; | ||
use crate::segment::{LogMaterializerError, MaterializeLogsResult}; | ||
use async_trait::async_trait; | ||
use chroma_blockstore::provider::BlockfileProvider; | ||
use chroma_error::ChromaError; | ||
use chroma_types::{Chunk, LogRecord, Segment}; | ||
use futures::TryFutureExt; | ||
use std::sync::atomic::AtomicU32; | ||
use std::sync::Arc; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum MaterializeLogOperatorError { | ||
#[error("Could not create record segment reader: {0}")] | ||
RecordSegmentReaderCreationFailed(#[from] RecordSegmentReaderCreationError), | ||
#[error("Log materialization failed: {0}")] | ||
LogMaterializationFailed(#[from] LogMaterializerError), | ||
} | ||
|
||
impl ChromaError for MaterializeLogOperatorError { | ||
fn code(&self) -> chroma_error::ErrorCodes { | ||
match self { | ||
MaterializeLogOperatorError::RecordSegmentReaderCreationFailed(e) => e.code(), | ||
MaterializeLogOperatorError::LogMaterializationFailed(e) => e.code(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct MaterializeLogOperator {} | ||
|
||
impl MaterializeLogOperator { | ||
pub fn new() -> Box<Self> { | ||
Box::new(MaterializeLogOperator {}) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct MaterializeLogInput { | ||
logs: Chunk<LogRecord>, | ||
provider: BlockfileProvider, | ||
record_segment: Segment, | ||
offset_id: Arc<AtomicU32>, | ||
} | ||
|
||
impl MaterializeLogInput { | ||
pub fn new( | ||
logs: Chunk<LogRecord>, | ||
provider: BlockfileProvider, | ||
record_segment: Segment, | ||
offset_id: Arc<AtomicU32>, | ||
) -> Self { | ||
MaterializeLogInput { | ||
logs, | ||
provider, | ||
record_segment, | ||
offset_id, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Operator<MaterializeLogInput, MaterializeLogsResult> for MaterializeLogOperator { | ||
type Error = MaterializeLogOperatorError; | ||
|
||
async fn run(&self, input: &MaterializeLogInput) -> Result<MaterializeLogsResult, Self::Error> { | ||
tracing::debug!("Materializing {} log entries", input.logs.total_len()); | ||
|
||
let record_segment_reader = | ||
match RecordSegmentReader::from_segment(&input.record_segment, &input.provider).await { | ||
Ok(reader) => Some(reader), | ||
Err(e) => { | ||
match *e { | ||
// Uninitialized segment is fine and means that the record | ||
// segment is not yet initialized in storage. | ||
RecordSegmentReaderCreationError::UninitializedSegment => None, | ||
err => { | ||
tracing::error!("Error creating record segment reader: {:?}", err); | ||
return Err( | ||
MaterializeLogOperatorError::RecordSegmentReaderCreationFailed(err), | ||
); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
materialize_logs( | ||
&record_segment_reader, | ||
input.logs.clone(), | ||
Some(input.offset_id.clone()), | ||
) | ||
.map_err(MaterializeLogOperatorError::LogMaterializationFailed) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.