feat(mito2): support index plugins in compaction (#10333)

* feat(mito2): build index plugins during compaction

Signed-off-by: WenyXu <wenymedia@gmail.com>

* feat(mito2): support index plugins in multi-region compaction

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(mito2): expose primary key runs to index providers

Signed-off-by: WenyXu <wenymedia@gmail.com>

* perf(mito2): reuse compaction index inputs with matching partition scopes

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito2): compare interned partition scopes during index reuse

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test(mito2): align compaction registration error context

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test(mito2): implement explicit compaction handler validation

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito2): preserve compaction indexes across optional providers

Allow compaction to continue with a warning when sketch providers are unavailable. Build active sketch and builtin creators together and avoid a later builtin-only replacement of the shared Puffin.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito2): cancel compaction index input probes

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
This commit is contained in:
Weny Xu
2026-11-17 08:02:06 +00:00
committed by GitHub
parent 416f08de0f
commit ecee34fc5a
21 changed files with 1532 additions and 151 deletions
+3
View File
@@ -87,6 +87,9 @@ pending. DDL cancellation retains its existing scheduling fence and priority.
`tests/compatibility/manifest/table_compaction/` and
`tests/compatibility/manifest/pending_tails/`.
- **SST/Parquet layout** (`sst/`): readers must stay compatible with existing files.
- **Index extensions**: shared contracts and provider validation live in `sst/index/registry.rs`;
typed sketch options are enterprise-gated. Lifecycle and failure tests live in
`engine/index_build_test.rs` and `compaction/`.
- **Series-index disk limit**: tasks estimate usage from entry sizes in open-region snapshots
and defer new builds when the shared estimate is full. Cleanup still prunes obsolete
range entries and expires series indexes to reclaim published usage. Concurrent builds may
+6 -1
View File
@@ -375,11 +375,16 @@ impl AccessLayer {
async fn write_sst_inner(
&self,
request: SstWriteRequest,
mut request: SstWriteRequest,
write_opts: &WriteOptions,
metrics: &mut Metrics,
context: Option<SstWriteContext>,
) -> Result<SstInfoArray> {
// Rollover can create multiple SSTs from this request, so no output may
// reuse summaries covering the complete input set.
if let Some(provider) = &mut request.index_build_inputs {
provider.can_merge_inputs &= write_opts.max_file_size.is_none();
}
let context = context.unwrap_or_default();
let limits = context.limits;
let guard = context.guard.clone();
+614 -67
View File
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashSet;
use std::num::NonZeroU64;
use std::sync::Arc;
use std::time::Duration;
@@ -21,6 +22,7 @@ use common_meta::key::SchemaMetadataManagerRef;
use common_telemetry::{info, warn};
use common_time::TimeToLive;
use either::Either;
use futures::{StreamExt, stream};
use itertools::Itertools;
use object_store::manager::ObjectStoreManagerRef;
use partition::expr::PartitionExpr;
@@ -53,11 +55,15 @@ use crate::region::version::VersionRef;
use crate::region::{ManifestContext, RegionLeaderState, RegionRoleState};
use crate::schedule::scheduler::LocalScheduler;
use crate::sst::FormatType;
use crate::sst::file::{self, FileHandle, FileMeta, UncommittedSsts};
use crate::sst::file::{self, FileHandle, FileMeta, RegionFileId, UncommittedSsts};
use crate::sst::file_metadata::{FileMetadataInterner, FileMetadataInternerRef};
use crate::sst::file_purger::LocalFilePurger;
use crate::sst::index::IndexBuildType;
use crate::sst::index::build::IndexBuildInputs;
use crate::sst::index::handler::{IndexKindProviderRef, PlanContext};
use crate::sst::index::intermediate::IntermediateManager;
use crate::sst::index::puffin_manager::PuffinManagerFactory;
use crate::sst::index::registry::{IndexBuildPlan, IndexRegistry};
use crate::sst::location::region_dir_from_table_dir;
use crate::sst::parquet::metadata::extract_primary_key_range;
use crate::sst::parquet::parallel_writer::EncodeRuntime;
@@ -145,11 +151,26 @@ pub struct CompactionContext {
pub(crate) access_layer: AccessLayerRef,
manifest_ctx: Arc<ManifestContext>,
plugins: Plugins,
index_plan: Arc<IndexBuildPlan>,
index_registry: Arc<IndexRegistry>,
existing_index_inputs: Option<Arc<HashSet<RegionFileId>>>,
}
impl From<&CompactionRegion> for CompactionContext {
fn from(region: &CompactionRegion) -> Self {
Self {
impl TryFrom<&CompactionRegion> for CompactionContext {
type Error = error::Error;
fn try_from(region: &CompactionRegion) -> Result<Self> {
let registry = IndexRegistry::from_config(
&region.engine_config,
region.plugins.get::<IndexKindProviderRef>(),
)?;
let index_plan = Arc::new(registry.plan(&PlanContext {
metadata: &region.region_metadata,
index_options: &region.current_version.options.index_options,
build_type: IndexBuildType::Compact,
existing_indexes: &[],
}));
Ok(Self {
region_id: region.region_id,
region_options: region.region_options.clone(),
version_options: region.current_version.options.clone(),
@@ -159,7 +180,10 @@ impl From<&CompactionRegion> for CompactionContext {
access_layer: region.access_layer.clone(),
manifest_ctx: region.manifest_ctx.clone(),
plugins: region.plugins.clone(),
}
index_plan,
index_registry: Arc::new(registry),
existing_index_inputs: None,
})
}
}
@@ -392,57 +416,118 @@ impl CompactionRegion {
/// before this unit's [`Compactor::update_manifest`] so its committed edit
/// can be associated with the SST metadata reported here.
pub async fn invoke_sst_hook(&self, merge_output: &MergeOutput) {
CompactionContext::from(self)
.invoke_sst_hook(merge_output)
.await;
invoke_sst_hook(
&self.plugins,
self.region_id,
&self.region_metadata,
merge_output,
)
.await;
}
}
impl CompactionContext {
/// Reports this unit's output metadata before publishing its manifest edit.
pub(crate) async fn invoke_sst_hook(&self, merge_output: &MergeOutput) {
let Some(hook) = self.plugins.get::<RegionHookRef>() else {
return;
};
async fn invoke_sst_hook(
plugins: &Plugins,
region_id: RegionId,
metadata: &RegionMetadataRef,
merge_output: &MergeOutput,
) {
let Some(hook) = plugins.get::<RegionHookRef>() else {
return;
};
// Remote compaction deserializes `MergeOutput` over gRPC, where
// `sst_infos` is `#[serde(skip)]`. Rebuild each `SstInfo` from the
// matching `FileMeta` so the hook observes real ids/rows/sizes rather
// than zeros — see [`sst_info_from_file_meta`] for what stays default.
let synthesized: Vec<SstInfo>;
let infos: &[SstInfo] = if merge_output.sst_infos.is_empty() {
synthesized = merge_output
.files_to_add
.iter()
.map(sst_info_from_file_meta)
.collect();
&synthesized
} else {
// `sst_infos` and `files_to_add` are documented as 1:1. If they
// ever diverge, `zip` below would silently truncate; warn so the
// mismatch is observable rather than dropping files from the hook.
if merge_output.sst_infos.len() != merge_output.files_to_add.len() {
warn!(
"sst_infos length ({}) does not match files_to_add length ({}) for region {}",
merge_output.sst_infos.len(),
merge_output.files_to_add.len(),
self.region_id
);
}
&merge_output.sst_infos
};
let files: Vec<SstFileInfo<'_>> = merge_output
// Remote compaction deserializes `MergeOutput` over gRPC, where
// `sst_infos` is `#[serde(skip)]`. Rebuild each `SstInfo` from the
// matching `FileMeta` so the hook observes real ids/rows/sizes rather
// than zeros — see [`sst_info_from_file_meta`] for what stays default.
let synthesized: Vec<SstInfo>;
let infos: &[SstInfo] = if merge_output.sst_infos.is_empty() {
synthesized = merge_output
.files_to_add
.iter()
.zip(infos)
.map(|(meta, info)| SstFileInfo {
sst_info_ref: info,
file_meta: meta,
})
.map(sst_info_from_file_meta)
.collect();
hook.on_sst_files_written(self.region_id, &self.region_metadata, &files)
.await;
&synthesized
} else {
// `sst_infos` and `files_to_add` are documented as 1:1. If they
// ever diverge, `zip` below would silently truncate; warn so the
// mismatch is observable rather than dropping files from the hook.
if merge_output.sst_infos.len() != merge_output.files_to_add.len() {
warn!(
"sst_infos length ({}) does not match files_to_add length ({}) for region {}",
merge_output.sst_infos.len(),
merge_output.files_to_add.len(),
region_id
);
}
&merge_output.sst_infos
};
let files: Vec<SstFileInfo<'_>> = merge_output
.files_to_add
.iter()
.zip(infos)
.map(|(meta, info)| SstFileInfo {
sst_info_ref: info,
file_meta: meta,
})
.collect();
hook.on_sst_files_written(region_id, metadata, &files).await;
}
impl CompactionContext {
/// Missing sketch implementations must not block compaction after a downgrade.
fn warn_missing_index_handlers<'a>(&self, inputs: impl Iterator<Item = &'a FileHandle>) {
self.index_registry.warn_missing_compaction_handlers(
&self.region_metadata,
inputs.map(FileHandle::meta_ref),
);
}
/// Checks each physical input once before output tasks begin. Missing inputs
/// disable imported summaries without changing the reader's tolerant behavior.
async fn prepare_index_inputs<'a>(
&mut self,
inputs: impl Iterator<Item = &'a FileHandle>,
cancellation: &CancellationToken,
) -> Result<()> {
if !self.index_plan.needs_input_reuse() {
return Ok(());
}
let ids: HashSet<_> = inputs.map(FileHandle::file_id).collect();
let access_layer = &self.access_layer;
let existing = stream::iter(ids)
.map(|id| async move {
let path = crate::sst::location::sst_file_path(
access_layer.table_dir(),
id,
access_layer.path_type(),
);
matches!(access_layer.object_store().exists(&path).await, Ok(true)).then_some(id)
})
.buffer_unordered(8)
.filter_map(|id| async move { id })
.collect::<HashSet<_>>();
// Read-only probes can be dropped without waiting for writer cleanup.
let existing = tokio::select! {
biased;
_ = cancellation.cancelled() => return error::CompactionCancelledSnafu.fail(),
existing = existing => existing,
};
// FileHandles protect ordinary purge; external object-store deletion can race.
self.existing_index_inputs = Some(Arc::new(existing));
Ok(())
}
/// Reports this unit's output metadata before publishing its manifest edit.
pub(crate) async fn invoke_sst_hook(&self, merge_output: &MergeOutput) {
invoke_sst_hook(
&self.plugins,
self.region_id,
&self.region_metadata,
merge_output,
)
.await;
}
/// Publishes outputs through the existing manifest validation and hook path.
@@ -600,6 +685,33 @@ fn known_max_input_sequence(inputs: &[FileHandle]) -> Option<NonZeroU64> {
crate::sst::file::known_max_input_sequence(inputs.iter().map(FileHandle::meta_ref))
}
/// Whole-input summaries require a complete output without file rollover.
fn can_reuse_output_inputs(output: &CompactionOutput, has_rollover: bool) -> bool {
!has_rollover && output.output_time_range.is_none()
}
/// Whole-file summaries are usable only when reading needs no partition filtering.
fn output_matches_partition(
output: &CompactionOutput,
partition_expr: Option<&PartitionExpr>,
) -> bool {
partition_expr.is_none()
|| output
.inputs
.iter()
.all(|input| input.meta_ref().partition_expr.as_deref() == partition_expr)
}
fn reusable_index_inputs(
outputs: &[CompactionOutput],
has_rollover: bool,
) -> impl Iterator<Item = &FileHandle> {
outputs
.iter()
.filter(move |output| can_reuse_output_inputs(output, has_rollover))
.flat_map(|output| output.inputs.iter())
}
fn validate_region_compaction_output(region_id: RegionId, output: &CompactionOutput) -> Result<()> {
ensure!(
output.output_level < file::LEVEL_SHARED
@@ -687,6 +799,42 @@ impl DefaultSstMerger {
merge_mode,
read_parallelism: write_opts.encode_parallelism,
};
// Matching file scopes need no additional region filtering. Rollover and
// time slicing still require output-scoped accumulation.
let partition_expr = match context.region_metadata.partition_expr.as_deref() {
None | Some("") => None,
Some(expr) => {
PartitionExpr::from_json_str(expr).with_context(|_| InvalidPartitionExprSnafu {
expr: expr.to_string(),
})?
}
};
let can_merge_inputs = can_reuse_output_inputs(&output, write_opts.max_file_size.is_some())
&& output_matches_partition(&output, partition_expr.as_ref())
&& context
.existing_index_inputs
.as_ref()
.is_some_and(|existing| {
output
.inputs
.iter()
.all(|input| existing.contains(&input.file_id()))
});
let index_build_inputs = Some(IndexBuildInputs {
output_rows: None,
plan: context.index_plan.clone(),
memory_limit: None,
inputs: if context.index_plan.needs_input_reuse() {
output.inputs.clone().into()
} else {
Arc::from([])
},
input_puffin_manager: context
.access_layer
.build_puffin_manager()
.with_puffin_metadata_cache(context.cache_manager.puffin_metadata_cache().cloned()),
can_merge_inputs,
});
let source = tokio::select! {
biased;
_ = cancellation.cancelled() => return error::CompactionCancelledSnafu.fail(),
@@ -700,7 +848,7 @@ impl DefaultSstMerger {
.access_layer
.write_sst_with_context(
SstWriteRequest {
index_build_inputs: None,
index_build_inputs,
op_type: OperationType::Compact,
metadata: region_metadata.clone(),
source,
@@ -730,17 +878,6 @@ impl DefaultSstMerger {
},
)
.await?;
// Convert partition expression once outside the map
let partition_expr = match &region_metadata.partition_expr {
None => None,
Some(json_str) if json_str.is_empty() => None,
Some(json_str) => PartitionExpr::from_json_str(json_str).with_context(|_| {
InvalidPartitionExprSnafu {
expr: json_str.clone(),
}
})?,
};
let output_files = sst_infos
.iter()
.map(|sst_info| {
@@ -843,6 +980,14 @@ impl<M: SstMerger> DefaultCompactor<M> {
context: &CompactionContext,
unit: &CompactionUnit,
) -> Result<MergeOutput> {
context.warn_missing_index_handlers(unit.inputs.iter());
let mut context = context.clone();
context
.prepare_index_inputs(
reusable_index_inputs(&unit.outputs, unit.max_file_size.is_some()),
&self.cancel_token,
)
.await?;
let mut result = MergeOutput {
files_to_remove: unit.inputs.iter().map(|f| f.meta_ref().clone()).collect(),
compaction_time_window: Some(unit.time_window_size),
@@ -915,7 +1060,22 @@ where
let internal_parallelism = compaction_region.max_parallelism.max(1);
let compaction_time_window = picker_output.time_window_size;
let region_id = compaction_region.region_id;
let context = CompactionContext::from(compaction_region);
let mut context = CompactionContext::try_from(compaction_region)?;
context.warn_missing_index_handlers(
picker_output
.outputs
.iter()
.flat_map(|output| output.inputs.iter()),
);
context
.prepare_index_inputs(
reusable_index_inputs(
&picker_output.outputs,
picker_output.max_file_size.is_some(),
),
&self.cancel_token,
)
.await?;
// Build tasks along with their input file metas so we can track which
// inputs correspond to each task.
@@ -1040,7 +1200,7 @@ where
compaction_region: &CompactionRegion,
merge_output: MergeOutput,
) -> Result<(RegionEdit, ManifestVersion)> {
CompactionContext::from(compaction_region)
CompactionContext::try_from(compaction_region)?
.update_manifest(merge_output)
.await
}
@@ -1052,6 +1212,9 @@ mod tests {
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;
use common_error::ext::BoxedError;
use common_time::Timestamp;
use common_time::range::TimestampRange;
use datatypes::arrow::record_batch::RecordBatch;
use object_store::ObjectStore;
use object_store::layers::mock::{self, MockLayerBuilder};
@@ -1067,6 +1230,10 @@ mod tests {
use crate::error::Result;
use crate::sst::file::FileHandle;
use crate::sst::file_purger::NoopFilePurger;
use crate::sst::index::handler::{
BuildContext, IndexCreator, IndexKindHandler, IndexKindProvider, PlanEntry, PlannedIndex,
};
use crate::sst::index::kind::{ExtensionIndexKind, IndexKind};
use crate::test_util::sst_util::new_record_batch_by_range;
fn dummy_file_meta() -> FileMeta {
@@ -1150,7 +1317,7 @@ mod tests {
..Default::default()
};
let compactor = DefaultCompactor::with_merger(DefaultSstMerger);
let context = CompactionContext::from(&region);
let context = CompactionContext::try_from(&region).unwrap();
let mut merge = Box::pin(async {
if merge_unit {
let unit = CompactionUnit::from_picker(picker_output)
@@ -1181,6 +1348,272 @@ mod tests {
}
}
#[derive(Debug)]
struct InputReusePlan;
impl IndexKindHandler for InputReusePlan {
fn validate(&self, _context: &PlanContext<'_>) -> std::result::Result<(), BoxedError> {
Ok(())
}
fn kinds(&self) -> &[IndexKind] {
&[IndexKind::Extension(ExtensionIndexKind::Hll)]
}
fn plan(
&self,
_context: &PlanContext<'_>,
) -> std::result::Result<Option<Arc<dyn PlannedIndex>>, BoxedError> {
Ok(Some(Arc::new(Self)))
}
}
#[async_trait::async_trait]
impl PlannedIndex for InputReusePlan {
fn kinds(&self) -> &[IndexKind] {
IndexKindHandler::kinds(self)
}
fn entries(&self) -> &[PlanEntry] {
&[]
}
fn needs_input_reuse(&self) -> bool {
true
}
async fn build(
&self,
_context: &BuildContext<'_>,
) -> std::result::Result<Option<Box<dyn IndexCreator>>, BoxedError> {
Ok(None)
}
fn is_current(&self, _file: &FileMeta, _context: &PlanContext<'_>) -> bool {
true
}
}
impl IndexKindProvider for InputReusePlan {
fn handlers(&self) -> Vec<Arc<dyn IndexKindHandler>> {
vec![Arc::new(Self)]
}
}
#[rstest::rstest]
#[case::unit(true)]
#[case::outputs(false)]
#[tokio::test]
async fn test_index_input_probes_observe_cancellation(
#[case] merge_unit: bool,
#[values(false, true)] cancelled_before_start: bool,
) {
let mut region = new_test_compaction_region().await;
region
.plugins
.insert::<IndexKindProviderRef>(Arc::new(InputReusePlan));
let started = Arc::new(Notify::new());
let probes = Arc::new(AtomicUsize::new(0));
let signal = started.clone();
let observed = probes.clone();
let layer = MockLayerBuilder::default()
.stat_interceptor(Arc::new(move |_| {
let signal = signal.clone();
let observed = observed.clone();
Box::pin(async move {
observed.fetch_add(1, Ordering::Relaxed);
signal.notify_one();
std::future::pending().await
})
}))
.build()
.unwrap();
let store = ObjectStore::new(Memory::default()).unwrap().layer(layer);
let access = &region.access_layer;
region.access_layer = Arc::new(AccessLayer::new(
access.table_dir(),
access.path_type(),
store,
access.puffin_manager_factory().clone(),
access.intermediate_manager().clone(),
));
let picker_output = PickerOutput {
outputs: vec![CompactionOutput {
output_level: 1,
inputs: vec![new_file_handle(dummy_file_meta())],
filter_deleted: false,
output_time_range: None,
}],
..Default::default()
};
let compactor = DefaultCompactor::with_merger(DefaultSstMerger);
let context = CompactionContext::try_from(&region).unwrap();
let mut merge = Box::pin(async {
if merge_unit {
let unit = CompactionUnit::from_picker(picker_output)
.unwrap()
.pop()
.unwrap();
compactor.merge_unit(&context, &unit).await
} else {
compactor.merge_ssts(&region, picker_output).await
}
});
if !cancelled_before_start {
tokio::select! {
_ = started.notified() => {},
result = &mut merge => panic!("merge returned before cancellation: {result:?}"),
_ = tokio::time::sleep(Duration::from_secs(10)) => panic!("merge did not reach input probes"),
}
}
compactor.cancel_token.cancel();
let result = timeout(Duration::from_secs(10), merge).await.unwrap();
assert!(matches!(
result,
Err(error::Error::CompactionCancelled { .. })
));
assert_eq!(
probes.load(Ordering::Relaxed),
usize::from(!cancelled_before_start)
);
assert!(context.existing_index_inputs.is_none());
}
#[derive(Debug)]
struct DuplicateIndexProvider;
impl IndexKindProvider for DuplicateIndexProvider {
fn handlers(&self) -> Vec<Arc<dyn IndexKindHandler>> {
vec![Arc::new(InputReusePlan), Arc::new(InputReusePlan)]
}
}
#[tokio::test]
async fn test_context_rejects_duplicate_provider_before_merge() {
let region = new_test_compaction_region().await;
region
.plugins
.insert::<IndexKindProviderRef>(Arc::new(DuplicateIndexProvider));
assert!(CompactionContext::try_from(&region).is_err());
}
#[test]
fn test_reusable_inputs_exclude_sliced_outputs_and_rollover() {
let whole = new_file_handle(dummy_file_meta());
let sliced = new_file_handle(dummy_file_meta());
let outputs = vec![
CompactionOutput {
output_level: 1,
inputs: vec![whole.clone()],
filter_deleted: true,
output_time_range: None,
},
CompactionOutput {
output_level: 1,
inputs: vec![sliced],
filter_deleted: false,
output_time_range: TimestampRange::new(
Timestamp::new_second(0),
Timestamp::new_second(1),
),
},
];
assert_eq!(
reusable_index_inputs(&outputs, false)
.map(FileHandle::file_id)
.collect::<Vec<_>>(),
vec![whole.file_id()],
);
assert_eq!(reusable_index_inputs(&outputs, true).count(), 0);
}
#[test]
fn test_reuse_requires_all_input_partition_scopes_to_match() {
use datatypes::value::Value;
use partition::expr::{RestrictedOp, col};
let scope = PartitionExpr::new(col("tag_0"), RestrictedOp::GtEq, Value::UInt32(10).into());
let other = PartitionExpr::new(col("tag_0"), RestrictedOp::GtEq, Value::UInt32(20).into());
for (scopes, expected) in [
(vec![Some(scope.clone()), Some(scope.clone())], true),
(vec![Some(scope.clone()), None], false),
(vec![Some(scope.clone()), Some(other)], false),
] {
let output = CompactionOutput {
output_level: 1,
inputs: scopes
.into_iter()
.map(|partition_expr| {
let mut meta = dummy_file_meta();
meta.partition_expr = partition_expr.map(Into::into);
new_file_handle(meta)
})
.collect(),
filter_deleted: true,
output_time_range: None,
};
assert_eq!(output_matches_partition(&output, Some(&scope)), expected);
assert!(output_matches_partition(&output, None));
}
}
#[tokio::test]
async fn test_index_input_preparation_tracks_physical_files() {
let mut region = new_test_compaction_region().await;
Arc::make_mut(&mut region.region_metadata).column_metadatas[0]
.column_schema
.set_inverted_index(true);
let mut context = CompactionContext::try_from(&region).unwrap();
assert!(context.existing_index_inputs.is_none());
// Builtin plans do not probe physical inputs solely for optional reuse.
context
.prepare_index_inputs(std::iter::empty(), &CancellationToken::new())
.await
.unwrap();
assert!(context.existing_index_inputs.is_none());
let mut registry =
IndexRegistry::new(Default::default(), Default::default(), Default::default());
registry.register(Arc::new(InputReusePlan)).unwrap();
context.index_plan = Arc::new(registry.plan(&PlanContext {
metadata: &context.region_metadata,
index_options: &context.version_options.index_options,
build_type: IndexBuildType::Compact,
existing_indexes: &[],
}));
let present = new_file_handle(dummy_file_meta());
let missing = new_file_handle(dummy_file_meta());
let path = crate::sst::location::sst_file_path(
context.access_layer.table_dir(),
present.file_id(),
context.access_layer.path_type(),
);
context
.access_layer
.object_store()
.write(&path, vec![1])
.await
.unwrap();
// Duplicate references are checked as one physical input. Only successfully
// probed files can contribute imported summaries to an output.
context
.prepare_index_inputs(
[&present, &missing, &present].into_iter(),
&CancellationToken::new(),
)
.await
.unwrap();
assert_eq!(
context.existing_index_inputs.as_deref(),
Some(&HashSet::from([present.file_id()]))
);
let prepared = context.clone();
assert!(Arc::ptr_eq(
context.existing_index_inputs.as_ref().unwrap(),
prepared.existing_index_inputs.as_ref().unwrap()
));
assert!(Arc::ptr_eq(&context.index_plan, &prepared.index_plan));
}
#[test]
fn test_effective_target_trust_and_max_sequence() {
let target = RegionId::new(1, 1);
@@ -1311,8 +1744,10 @@ mod tests {
for preserve in [false, true] {
region.region_options.preserve_row_sequence = preserve;
for (inputs, known_bound, inputs_trusted) in &cases {
let metadata =
output_sequence_metadata(&CompactionContext::from(&region), inputs);
let metadata = output_sequence_metadata(
&CompactionContext::try_from(&region).unwrap(),
inputs,
);
let trusted = preserve && *inputs_trusted;
assert_eq!(trusted, metadata.exact_sequence_trusted);
assert_eq!(
@@ -1558,6 +1993,13 @@ mod tests {
access_layer: region.access_layer.clone(),
manifest_ctx: region.manifest_ctx.clone(),
plugins: Plugins::new(),
index_plan: Arc::default(),
index_registry: Arc::new(IndexRegistry::new(
Default::default(),
Default::default(),
Default::default(),
)),
existing_index_inputs: None,
};
region
.access_layer
@@ -1651,7 +2093,7 @@ mod tests {
#[tokio::test]
async fn test_compaction_unit_group_failure_is_atomic() {
let context = CompactionContext::from(&new_test_compaction_region().await);
let context = CompactionContext::try_from(&new_test_compaction_region().await).unwrap();
let input = new_file_handle(dummy_file_meta());
let unit = CompactionUnit::from_picker(PickerOutput {
outputs: (0..2)
@@ -1699,6 +2141,111 @@ mod tests {
}
}
#[tokio::test]
async fn test_missing_index_handler_does_not_block_builtin_compaction() {
use api::v1::SemanticType;
use datatypes::prelude::ConcreteDataType;
use datatypes::schema::{
ColumnSchema, FulltextOptions, SkippingIndexOptions, SkippingIndexType,
};
use store_api::metadata::{ColumnMetadata, RegionMetadataBuilder};
use crate::sst::file::{ColumnIndexMetadata, IndexType};
#[derive(Debug, PartialEq, Eq)]
enum RequiredBy {
None,
Schema,
File,
Column,
}
for required_by in [
RequiredBy::None,
RequiredBy::Schema,
RequiredBy::File,
RequiredBy::Column,
] {
let mut region = new_test_compaction_region().await;
Arc::make_mut(&mut region.region_metadata).column_metadatas[0]
.column_schema
.set_inverted_index(true);
let mut metadata =
RegionMetadataBuilder::from_existing((*region.region_metadata).clone());
metadata.push_column_metadata(ColumnMetadata {
column_schema: ColumnSchema::new("text", ConcreteDataType::string_datatype(), true)
.with_fulltext_options(FulltextOptions {
enable: true,
..Default::default()
})
.unwrap()
.with_skipping_options(SkippingIndexOptions::new_unchecked(
42,
0.01,
SkippingIndexType::BloomFilter,
))
.unwrap(),
semantic_type: SemanticType::Field,
column_id: 5,
});
region.region_metadata = Arc::new(metadata.build().unwrap());
let mut input = dummy_file_meta();
match required_by {
RequiredBy::Schema => {
Arc::make_mut(&mut region.region_metadata).column_metadatas[0]
.column_schema
.mut_metadata()
.insert(
datatypes::schema::HLL_KEY.into(),
r#"{"precision":10}"#.into(),
);
}
RequiredBy::File => input.available_indexes.push(IndexType::HllIndex),
RequiredBy::Column => input.indexes.push(ColumnIndexMetadata {
column_id: 0,
created_indexes: [IndexType::HllIndex].into_iter().collect(),
..Default::default()
}),
_ => {}
}
let context = CompactionContext::try_from(&region).unwrap();
assert_eq!(
context
.index_plan
.planned()
.iter()
.flat_map(|plan| plan.kinds().iter().copied())
.collect::<Vec<_>>(),
[IndexKind::Inverted, IndexKind::Fulltext, IndexKind::Bloom]
);
let output = PickerOutput {
outputs: vec![CompactionOutput {
output_level: 1,
inputs: vec![new_file_handle(input)],
filter_deleted: false,
output_time_range: None,
}],
..Default::default()
};
for local in [false, true] {
let merger = MockMerger::new(vec![Ok(Vec::new())]);
let calls = merger.call_idx.clone();
let compactor = DefaultCompactor::with_merger(merger);
let result = if local {
let context = CompactionContext::try_from(&region).unwrap();
let unit = CompactionUnit::from_picker(output.clone())
.unwrap()
.pop()
.unwrap();
compactor.merge_unit(&context, &unit).await
} else {
compactor.merge_ssts(&region, output.clone()).await
};
assert!(result.is_ok(), "{required_by:?}, local={local}: {result:?}");
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
}
}
#[tokio::test]
async fn test_partial_merge_failure_collects_only_successful_outputs() {
common_telemetry::init_default_ut_logging();
+16 -1
View File
@@ -38,9 +38,12 @@ use tokio_util::sync::CancellationToken;
use crate::access_layer::AccessLayerRef;
use crate::cache::CacheManagerRef;
use crate::config::MitoConfig;
use crate::error::CompactionCancelledSnafu;
use crate::error::{InvalidMultiRegionCompactionSnafu, Result};
use crate::region::options::RegionOptions;
use crate::sst::file::{FileMeta, RegionFileId};
use crate::sst::index::handler::IndexKindProviderRef;
use crate::sst::index::registry::IndexRegistry;
use crate::sst::parquet::SstInfo;
use crate::sst::parquet::writer::SstWriteGuard;
@@ -78,6 +81,8 @@ pub struct MultiRegionCompactionPlan {
/// Shared storage services and attempt-owned output resources.
pub struct MultiRegionCompactionContext {
/// Optional enterprise index implementation. Each routed output builds its own state.
pub index_kind_provider: Option<IndexKindProviderRef>,
pub access_layer: AccessLayerRef,
pub cache: CacheManagerRef,
pub config: Arc<MitoConfig>,
@@ -127,8 +132,18 @@ pub async fn compact_regions(
context: MultiRegionCompactionContext,
) -> Result<MultiRegionCompactionOutput> {
let metadata = validate_plan(&plan)?;
// Invalid registrations still fail, but unavailable sketches only warn so
// compaction can continue after a downgrade.
let registry =
IndexRegistry::from_config(&context.config, context.index_kind_provider.clone())?;
for target in plan.targets.values() {
registry.warn_missing_compaction_handlers(
&target.metadata,
plan.sources.iter().chain(target.inputs.values()),
);
}
if context.cancellation.is_cancelled() {
return crate::error::CompactionCancelledSnafu.fail();
return CompactionCancelledSnafu.fail();
}
let cancellation = context.cancellation.clone();
let execution = async move {
@@ -43,6 +43,8 @@ pub(crate) struct MemoryLimits {
pub(crate) batch: usize,
pub(crate) group: usize,
pub(crate) index: usize,
/// Reuses index allocation only when the optional working set fits admission.
pub(crate) extension_index_memory_limit: Option<usize>,
pub(crate) writer: SstWriteLimits,
pub(crate) estimated: u64,
}
@@ -60,6 +62,7 @@ impl MemoryLimits {
batch,
group: plan.memory_limit / 16,
index: plan.memory_limit / (64 * plan.targets.len()),
extension_index_memory_limit: None,
writer: SstWriteLimits {
// Parquet 59 preallocates a 1 MiB bit writer for each of the
// timestamp/sequence delta encoders. Dictionary interner tables
@@ -78,6 +81,7 @@ impl MemoryLimits {
plan: &MultiRegionCompactionPlan,
context: &MultiRegionCompactionContext,
footers: &[Arc<ParquetMetaData>],
extension_targets: usize,
) -> Result<()> {
self.check_upload_minimum(context)?;
let mut extra_delta = 0;
@@ -105,7 +109,12 @@ impl MemoryLimits {
),
}
);
self.estimated = bytes;
// Optional indexes must not turn an admitted data compaction into a
// resource failure. Reserve all resident target states together or skip.
let admission =
admit_extension_indexes(bytes, plan.memory_limit, self.index, extension_targets);
self.extension_index_memory_limit = admission.limit;
self.estimated = admission.estimated;
Ok(())
}
@@ -212,10 +221,83 @@ fn reader_bytes(footer: &ParquetMetaData) -> u64 {
.iter()
.map(|group| {
(group.total_byte_size().max(0) as u64)
.saturating_mul(2)
.saturating_mul(RETAINED_AND_FINISH_SCRATCH_FACTOR)
.saturating_add(group.compressed_size().max(0) as u64)
})
.max()
.unwrap_or(0);
pages.saturating_add(footer.memory_size() as u64)
}
/// Retained state and temporary finish allocations share the admission budget.
const RETAINED_AND_FINISH_SCRATCH_FACTOR: u64 = 2;
#[derive(Debug, PartialEq, Eq)]
struct ExtensionIndexAdmission {
limit: Option<usize>,
estimated: u64,
}
/// Reserves all target states together, or preserves the admitted data budget.
fn admit_extension_indexes(
base: u64,
budget: usize,
index: usize,
targets: usize,
) -> ExtensionIndexAdmission {
let with_extension_indexes = base.saturating_add(
(index as u64)
.saturating_mul(RETAINED_AND_FINISH_SCRATCH_FACTOR)
.saturating_mul(targets as u64),
);
if targets > 0 && index > 0 && with_extension_indexes <= budget as u64 {
ExtensionIndexAdmission {
limit: Some(index),
estimated: with_extension_indexes,
}
} else {
ExtensionIndexAdmission {
limit: None,
estimated: base,
}
}
}
#[cfg(test)]
mod tests {
use super::{ExtensionIndexAdmission, admit_extension_indexes};
#[test]
fn test_extension_indexes_do_not_reject_admitted_data() {
// Both target writers are resident. Insufficient extra room leaves the
// already-admitted data budget unchanged and explicitly disables optional indexes.
assert_eq!(
admit_extension_indexes(900, 1000, 30, 2),
ExtensionIndexAdmission {
limit: None,
estimated: 900
}
);
assert_eq!(
admit_extension_indexes(880, 1000, 30, 2),
ExtensionIndexAdmission {
limit: Some(30),
estimated: 1000
}
);
assert_eq!(
admit_extension_indexes(900, 1000, 30, 0),
ExtensionIndexAdmission {
limit: None,
estimated: 900
}
);
assert_eq!(
admit_extension_indexes(900, 1000, 0, 2),
ExtensionIndexAdmission {
limit: None,
estimated: 900
}
);
}
}
@@ -15,6 +15,7 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use arrow_schema::extension::EXTENSION_TYPE_METADATA_KEY;
use common_base::readable_size::ReadableSize;
use common_time::Timestamp;
use datatypes::arrow::array::{
Array, ArrayRef, BinaryArray, GenericByteArray, GenericByteDictionaryBuilder, UInt64Array,
@@ -38,9 +39,10 @@ use crate::compaction::multi_region::memory::{BATCH_SIZE, MemoryLimits};
use crate::compaction::multi_region::{
MultiRegionCompactionContext, MultiRegionCompactionPlan, MultiRegionTarget, SourceCounters,
};
use crate::config::MemoryThreshold;
use crate::error::{
ComputeArrowSnafu, InvalidMultiRegionCompactionSnafu, MultiRegionCompactionResourceSnafu,
NewRecordBatchSnafu, Result,
ComputeArrowSnafu, ExternalSnafu, InvalidMultiRegionCompactionSnafu,
MultiRegionCompactionResourceSnafu, NewRecordBatchSnafu, Result,
};
use crate::read::compat::FlatCompatBatch;
use crate::read::flat_merge::FlatMergeReader;
@@ -49,6 +51,9 @@ use crate::read::read_columns::ReadColumns;
use crate::read::{BoxedRecordBatchStream, timestamp_array_to_i64_slice};
use crate::sst::file::{FileHandle, FileMeta};
use crate::sst::file_purger::NoopFilePurger;
use crate::sst::index::IndexBuildType;
use crate::sst::index::handler::PlanContext;
use crate::sst::index::registry::{IndexBuildPlan, IndexRegistry};
use crate::sst::parquet::flat_format::{
primary_key_column_index, sequence_column_index, time_index_column_index,
};
@@ -108,6 +113,7 @@ pub(crate) struct SharedSource {
partition_rule: PartitionRuleRef,
window: (Timestamp, Timestamp),
pub(crate) limits: MemoryLimits,
pub(crate) index_plans: BTreeMap<RegionId, Arc<IndexBuildPlan>>,
peak_group_bytes: AtomicU64,
cancellation: CancellationToken,
stop: CancellationToken,
@@ -205,7 +211,47 @@ impl SharedSource {
)?;
let schema = mapper.input_arrow_schema(false);
let mut limits = MemoryLimits::new(plan, &schema)?;
limits.admit(plan, context, &footers)?;
let mut inverted = context.config.inverted_index.clone();
inverted.mem_threshold_on_create =
admitted_index_threshold(inverted.mem_threshold_on_create(), limits.index);
let mut fulltext = context.config.fulltext_index.clone();
fulltext.mem_threshold_on_create =
admitted_index_threshold(Some(fulltext.mem_threshold_on_create()), limits.index);
let mut bloom = context.config.bloom_filter_index.clone();
bloom.mem_threshold_on_create =
admitted_index_threshold(bloom.mem_threshold_on_create(), limits.index);
let registry = IndexRegistry::new(inverted, fulltext, bloom)
.with_provider(context.index_kind_provider.clone())
.with_context(|_| ExternalSnafu {
context: "registering index handlers".to_string(),
})?;
let mut index_plans = plan
.targets
.iter()
.map(|(id, target)| {
let work = registry.plan(&PlanContext {
metadata: &target.metadata,
index_options: &target.options.index_options,
build_type: IndexBuildType::Compact,
existing_indexes: &[],
});
(*id, Arc::new(work))
})
.collect::<BTreeMap<_, _>>();
limits.admit(
plan,
context,
&footers,
index_plans
.values()
.filter(|work| work.entries().any(|entry| !entry.kind.is_builtin()))
.count(),
)?;
if limits.extension_index_memory_limit.is_none() {
for (region_id, work) in &mut index_plans {
Arc::make_mut(work).omit_extensions_for_budget(*region_id);
}
}
// Compute routing/version annotations on bounded reader batches, but
// keep physical __sequence as the merge key. The annotations are not
// persisted and fixed-position time/PK/sequence/op columns stay last.
@@ -233,6 +279,7 @@ impl SharedSource {
Timestamp::new_second(plan.window_start + plan.window_seconds),
),
limits,
index_plans,
peak_group_bytes: AtomicU64::new(0),
cancellation: context.cancellation.clone(),
stop: CancellationToken::new(),
@@ -752,3 +799,7 @@ fn key_at(batch: &RecordBatch, row: usize) -> Result<&[u8]> {
);
Ok(values.value(index))
}
fn admitted_index_threshold(configured: Option<usize>, limit: usize) -> MemoryThreshold {
MemoryThreshold::Size(ReadableSize(configured.unwrap_or(limit).min(limit) as u64))
}
@@ -15,6 +15,7 @@ use std::sync::{Arc, Mutex};
use std::time::Duration;
use api::v1::{OpType, SemanticType};
use common_error::ext::BoxedError;
use datatypes::arrow::array::{
Array, ArrayRef, BinaryArray, BinaryDictionaryBuilder, BooleanArray, DictionaryArray,
StringArray, StringDictionaryBuilder, TimestampMillisecondArray, UInt8Array, UInt64Array,
@@ -48,7 +49,9 @@ use crate::error::InvalidMultiRegionCompactionSnafu;
use crate::read::FlatSource;
use crate::sst::file::{FileHandle, RegionFileId};
use crate::sst::file_purger::NoopFilePurger;
use crate::sst::index::handler::{IndexKindHandler, IndexKindProvider, PlanContext, PlannedIndex};
use crate::sst::index::intermediate::IntermediateManager;
use crate::sst::index::kind::{ExtensionIndexKind, IndexKind};
use crate::sst::index::puffin_manager::PuffinManagerFactory;
use crate::sst::parquet::WriteOptions;
use crate::sst::parquet::flat_format::{
@@ -273,6 +276,7 @@ impl Fixture {
});
(
MultiRegionCompactionContext {
index_kind_provider: None,
access_layer: self.layer.clone(),
cache: Arc::new(CacheManager::default()),
config: self.config.clone(),
@@ -2327,6 +2331,7 @@ async fn test_limited_writer_fails_instead_of_publishing_over_budget(#[case] kin
&context,
&limits,
plan.max_output_file_size,
Arc::default(),
)
.write(source)
.await
@@ -2455,3 +2460,166 @@ async fn test_invalid_plan_is_rejected_before_creating_outputs(
);
assert!(protection.files.lock().unwrap().is_empty());
}
#[derive(Debug)]
struct InvalidOptionalIndexHandler;
impl IndexKindProvider for InvalidOptionalIndexHandler {
fn handlers(&self) -> Vec<Arc<dyn IndexKindHandler>> {
vec![Arc::new(Self)]
}
}
impl IndexKindHandler for InvalidOptionalIndexHandler {
fn validate(&self, context: &PlanContext<'_>) -> std::result::Result<(), BoxedError> {
self.plan(context).map(|_| ())
}
fn kinds(&self) -> &[IndexKind] {
&[IndexKind::Extension(ExtensionIndexKind::Hll)]
}
fn plan(
&self,
context: &PlanContext<'_>,
) -> std::result::Result<Option<Arc<dyn PlannedIndex>>, BoxedError> {
assert!(
context.metadata.column_metadatas[0]
.column_schema
.hll_options()
.is_err()
);
Err(BoxedError::new(
crate::error::InvalidConfigSnafu {
reason: "invalid optional index configuration",
}
.build(),
))
}
}
#[derive(Debug, Default)]
struct ConflictingRegistrationProvider(std::sync::atomic::AtomicUsize);
impl IndexKindProvider for ConflictingRegistrationProvider {
fn handlers(&self) -> Vec<Arc<dyn IndexKindHandler>> {
// Preflight succeeds; source preparation must propagate its own failure too.
let calls = self.0.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let handler: Arc<dyn IndexKindHandler> = Arc::new(InvalidOptionalIndexHandler);
if calls == 0 {
vec![handler]
} else {
vec![handler.clone(), handler]
}
}
}
#[tokio::test]
async fn test_source_index_registration_failure_does_not_fall_back() {
let fixture = Fixture::new(PrimaryKeyEncoding::Dense, FormatType::Flat, 0).await;
let (mut context, protection) = fixture.context(None);
context.index_kind_provider = Some(Arc::new(ConflictingRegistrationProvider::default()));
let error = compact_regions(fixture.plan(2), context)
.await
.err()
.expect("conflicting registration must fail");
assert!(
format!("{error:?}").contains("registering index handlers"),
"unexpected registration error: {error:?}"
);
assert!(protection.files.lock().unwrap().is_empty());
}
#[tokio::test]
async fn test_invalid_optional_index_options_do_not_block_routed_compaction() {
let fixture = Fixture::new(PrimaryKeyEncoding::Dense, FormatType::Flat, 0).await;
for invalid_value in ["missing-column", "bad-capacity"] {
let mut plan = fixture.plan(2);
for target in plan.targets.values_mut() {
Arc::make_mut(&mut target.metadata).column_metadatas[0]
.column_schema
.mut_metadata()
.insert(datatypes::schema::HLL_KEY.into(), invalid_value.into());
}
let (mut context, _) = fixture.context(None);
context.index_kind_provider = Some(Arc::new(InvalidOptionalIndexHandler));
let output = compact_regions(plan, context).await.unwrap();
assert_eq!(output.stats.source_rows, output.stats.output_rows);
assert!(output.stats.output_rows > 0);
assert!(
output
.files
.values()
.flatten()
.all(|file| file
.indexes
.iter()
.all(|index| index.created_indexes.iter().all(|kind| !matches!(
kind,
crate::sst::file::IndexType::HllIndex
| crate::sst::file::IndexType::KllIndex
| crate::sst::file::IndexType::MgIndex
))))
);
}
}
#[tokio::test]
async fn test_missing_index_handler_does_not_block_routed_compaction() {
use crate::sst::file::{ColumnIndexMetadata, IndexType};
let fixture = Fixture::new(PrimaryKeyEncoding::Dense, FormatType::Flat, 0).await;
for required_by in ["schema", "file", "column", "target"] {
let mut plan = fixture.plan(2);
match required_by {
"schema" => {
for target in plan.targets.values_mut() {
Arc::make_mut(&mut target.metadata).column_metadatas[0]
.column_schema
.set_hll_options(&datatypes::schema::HllOptions::default())
.unwrap();
}
}
"file" => plan.sources[0].available_indexes.push(IndexType::HllIndex),
"column" => plan.sources[0].indexes.push(ColumnIndexMetadata {
column_id: 0,
created_indexes: [IndexType::HllIndex].into_iter().collect(),
..Default::default()
}),
_ => {
plan.targets
.values_mut()
.next()
.unwrap()
.inputs
.values_mut()
.next()
.unwrap()
.available_indexes
.push(IndexType::HllIndex);
}
}
let (context, _) = fixture.context(None);
let output = compact_regions(plan, context)
.await
.unwrap_or_else(|error| panic!("{required_by}: {error}"));
assert_eq!(output.stats.source_rows, output.stats.output_rows);
assert!(output.stats.output_rows > 0);
assert!(!output.files.is_empty());
for file in output.files.values().flatten() {
assert!(file.num_rows > 0);
assert!(file.available_indexes.iter().all(|kind| !matches!(
kind,
IndexType::HllIndex | IndexType::KllIndex | IndexType::MgIndex
)));
assert!(
file.indexes
.iter()
.all(|index| index.created_indexes.iter().all(|kind| !matches!(
kind,
IndexType::HllIndex | IndexType::KllIndex | IndexType::MgIndex
)))
);
}
}
}
@@ -55,6 +55,14 @@ pub(crate) async fn execute(
senders.insert(id, sender);
let schema = source.schema();
let limits = source.limits.clone();
let index_plan =
source
.index_plans
.get(&id)
.cloned()
.context(InvalidMultiRegionCompactionSnafu {
reason: "Missing index plan for compaction target",
})?;
let plan = plan.clone();
let context = context.clone();
workers.push(AbortOnDropHandle::new(common_runtime::spawn_compact(
@@ -76,6 +84,7 @@ pub(crate) async fn execute(
&context,
&limits,
plan.max_output_file_size,
index_plan,
)
.write(input)
.await?;
+19 -20
View File
@@ -21,11 +21,12 @@ use tokio_util::sync::CancellationToken;
use crate::access_layer::{Metrics, OperationType, SstWriteRequest, WriteType};
use crate::compaction::multi_region::memory::{self, MemoryLimits};
use crate::compaction::multi_region::{MultiRegionCompactionContext, MultiRegionTarget};
use crate::config::MemoryThreshold;
use crate::error::{ComputeArrowSnafu, Result};
use crate::read::FlatSource;
use crate::sst::FormatType;
use crate::sst::file::FileMeta;
use crate::sst::index::build::IndexBuildInputs;
use crate::sst::index::registry::IndexBuildPlan;
use crate::sst::parquet::metadata::extract_primary_key_range;
use crate::sst::parquet::parallel_writer::EncodeRuntime;
use crate::sst::parquet::writer::SstWriteContext;
@@ -35,6 +36,7 @@ pub(crate) struct TargetWriter<'a> {
target: &'a MultiRegionTarget,
context: &'a MultiRegionCompactionContext,
limits: &'a MemoryLimits,
index_plan: Arc<IndexBuildPlan>,
max_file_size: usize,
sequence: Option<NonZeroU64>,
preserve_row_sequence: bool,
@@ -46,6 +48,7 @@ impl<'a> TargetWriter<'a> {
context: &'a MultiRegionCompactionContext,
limits: &'a MemoryLimits,
max_file_size: usize,
index_plan: Arc<IndexBuildPlan>,
) -> Self {
let sequence = crate::sst::file::known_max_input_sequence(target.inputs.values());
let preserve_row_sequence = target.options.preserve_row_sequence
@@ -57,6 +60,7 @@ impl<'a> TargetWriter<'a> {
target,
context,
limits,
index_plan,
max_file_size,
sequence,
preserve_row_sequence,
@@ -99,16 +103,19 @@ impl<'a> TargetWriter<'a> {
fn request(&self, source: FlatSource) -> Result<SstWriteRequest> {
let config = &self.context.config;
let mut inverted = config.inverted_index.clone();
inverted.mem_threshold_on_create = self.index_threshold(inverted.mem_threshold_on_create());
let mut fulltext = config.fulltext_index.clone();
fulltext.mem_threshold_on_create =
self.index_threshold(Some(fulltext.mem_threshold_on_create()));
let mut bloom = config.bloom_filter_index.clone();
bloom.mem_threshold_on_create = self.index_threshold(bloom.mem_threshold_on_create());
// The shared source plan already applies the admitted index thresholds.
let source = output_projection(self.target, source, self.context.cancellation.clone())?;
let index_build_inputs = Some(IndexBuildInputs {
output_rows: None,
plan: self.index_plan.clone(),
input_puffin_manager: self.context.access_layer.build_puffin_manager(),
// Routing and file rollover change coverage. Consume only actual output rows.
inputs: Arc::from([]),
can_merge_inputs: false,
memory_limit: self.limits.extension_index_memory_limit,
});
Ok(SstWriteRequest {
index_build_inputs: None,
index_build_inputs,
op_type: OperationType::Compact,
metadata: self.target.metadata.clone(),
source,
@@ -124,20 +131,12 @@ impl<'a> TargetWriter<'a> {
preserve_row_sequence: self.preserve_row_sequence,
index_options: self.target.options.index_options.clone(),
index_config: config.index.clone(),
inverted_index_config: inverted,
fulltext_index_config: fulltext,
bloom_filter_index_config: bloom,
inverted_index_config: config.inverted_index.clone(),
fulltext_index_config: config.fulltext_index.clone(),
bloom_filter_index_config: config.bloom_filter_index.clone(),
})
}
fn index_threshold(&self, configured: Option<usize>) -> MemoryThreshold {
MemoryThreshold::Size(ReadableSize(
configured
.unwrap_or(self.limits.index)
.min(self.limits.index) as u64,
))
}
fn options(&self) -> WriteOptions {
WriteOptions {
write_buffer_size: ReadableSize(self.limits.batch as u64),
+4 -4
View File
@@ -31,7 +31,7 @@ use crate::compaction::scheduler::{
};
use crate::compaction::task::CompactionTask;
use crate::compaction::unit::CompactionUnit;
use crate::error::{self, Error};
use crate::error::{self, Error, Result};
use crate::metrics::INFLIGHT_COMPACTION_COUNT;
use crate::region::ManifestContextRef;
use crate::request::{
@@ -260,7 +260,7 @@ impl CompactionScheduler {
region: CompactionRegion,
units: Vec<CompactionUnit>,
reserved: CompactingFiles,
) -> CompactionPhase {
) -> Result<CompactionPhase> {
// Memory admission may have removed oversized units from the picker output.
// Report only the inputs that this local cycle will actually execute.
if let Some(info) = self
@@ -286,7 +286,7 @@ impl CompactionScheduler {
made_progress: false,
cancellation_requested: false,
};
let context = CompactionContext::from(&region);
let context = CompactionContext::try_from(&region)?;
// Planning is complete; unit tasks must not pin the other units' inputs.
drop(region);
for unit in units {
@@ -320,7 +320,7 @@ impl CompactionScheduler {
});
}
local.unit_states = local.units.values().cloned().collect();
CompactionPhase::Units(local)
Ok(CompactionPhase::Units(local))
}
/// Returns local unit state only while the region is in a local execution phase.
@@ -629,7 +629,7 @@ impl CompactionScheduler {
compaction_region,
units,
files,
)))
)?))
}
fn exceeds_compaction_memory_limit(&self, estimated_bytes: u64) -> Option<u64> {
+1 -1
View File
@@ -240,7 +240,7 @@ mod tests {
let mut task = CompactionTask {
state: CancellableTaskState::new(),
execution: CompactionExecution::for_test(7),
compaction_context: CompactionContext::from(&region),
compaction_context: CompactionContext::try_from(&region).unwrap(),
request_sender,
listener: WorkerListener::new(Some(Arc::new(PanicOnUnitMergeBegin))),
unit: CompactionUnit {
+405 -8
View File
@@ -2830,7 +2830,7 @@ impl IndexCreator for RebuildSketchCreator {
#[case(false)]
#[case(true)]
#[tokio::test]
async fn test_async_compaction_builds_only_builtin_indexes(#[case] registered: bool) {
async fn test_async_compaction_respects_optional_sketch_provider(#[case] registered: bool) {
let mut env = TestEnv::new().await;
let provider = Arc::new(RebuildSketchProvider::default());
let plugins = Plugins::new();
@@ -2867,8 +2867,18 @@ async fn test_async_compaction_builds_only_builtin_indexes(#[case] registered: b
.handle_request(region_id, RegionRequest::Create(request))
.await
.unwrap();
put_and_flush(&engine, region_id, &schema, 0..2).await;
put_and_flush(&engine, region_id, &schema, 2..4).await;
for start in [0, 2] {
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: build_rows(start, start + 2),
},
)
.await;
flush_region(&engine, region_id, None).await;
}
let inputs = engine
.get_region(region_id)
.unwrap()
@@ -2908,11 +2918,18 @@ async fn test_async_compaction_builds_only_builtin_indexes(#[case] registered: b
&& !inputs.iter().any(|input| input.file_id == output.file_id)
&& output.available_indexes.contains(&IndexType::InvertedIndex)
{
assert_eq!(
output.available_indexes.as_slice(),
let expected: &[IndexType] = if registered {
&[IndexType::InvertedIndex, IndexType::HllIndex]
} else {
&[IndexType::InvertedIndex]
);
};
assert_eq!(output.available_indexes.as_slice(), expected);
assert_eq!(output.num_rows, 4);
assert_tag_inverted_index(&region.access_layer, output.index_id()).await;
if registered {
assert_eq!(output.index_version, 0);
assert_sketch_rows(&region.access_layer, output.index_id(), 4).await;
}
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
@@ -2920,8 +2937,388 @@ async fn test_async_compaction_builds_only_builtin_indexes(#[case] registered: b
})
.await
.expect("async compaction did not publish built-in indexes");
assert_eq!(provider.compact_plans.load(Ordering::Relaxed), 0);
assert_eq!(provider.creates.load(Ordering::Relaxed), 0);
if registered {
assert!(provider.compact_plans.load(Ordering::Relaxed) > 0);
assert_eq!(provider.creates.load(Ordering::Relaxed), 1);
} else {
assert_eq!(provider.compact_plans.load(Ordering::Relaxed), 0);
assert_eq!(provider.creates.load(Ordering::Relaxed), 0);
}
engine.stop().await.unwrap();
}
/// Reads the rebuilt inverted blob rather than trusting its manifest descriptor.
async fn assert_tag_inverted_index(
layer: &crate::access_layer::AccessLayer,
index_id: RegionIndexId,
) {
use index::inverted_index::format::reader::{InvertedIndexBlobReader, InvertedIndexReader};
use puffin::puffin_manager::{PuffinManager, PuffinReader};
let manager = layer.build_puffin_manager();
let reader = manager.reader(&index_id).await.unwrap();
let blob = reader
.blob(crate::sst::index::inverted_index::INDEX_BLOB_TYPE)
.await
.unwrap()
.reader()
.await
.unwrap();
let reader = InvertedIndexBlobReader::new(blob);
let metadata = reader.metadata(None).await.unwrap();
let target = index::target::IndexTarget::ColumnId(0);
let meta = &metadata.metas[&target.encode()];
assert_eq!(metadata.total_row_count, 4);
let fst = reader
.fst(
meta.base_offset + u64::from(meta.relative_fst_offset),
meta.fst_size,
None,
)
.await
.unwrap();
assert_eq!(fst.len(), 4);
for value in ["0", "1", "2", "3"] {
let mut encoded = Vec::new();
mito_codec::index::IndexValueCodec::encode_nonnull_value(
datatypes::value::ValueRef::String(value),
&mito_codec::row_converter::SortField::new(ConcreteDataType::string_datatype()),
&mut encoded,
)
.unwrap();
assert!(fst.get(&encoded).is_some(), "missing tag {value}");
}
}
async fn assert_sketch_rows(
layer: &crate::access_layer::AccessLayer,
index_id: RegionIndexId,
expected_rows: usize,
) {
use common_base::range_read::RangeReader;
let manager = layer.build_puffin_manager();
let reader = manager.reader(&index_id).await.unwrap();
let blob = reader.blob("test.sketch").await.unwrap();
let reader = blob.reader().await.unwrap();
let metadata = reader.metadata().await.unwrap();
let bytes = reader.read(0..metadata.content_length).await.unwrap();
assert_eq!(bytes.as_ref(), expected_rows.to_le_bytes().as_slice());
}
#[tokio::test]
async fn test_async_compaction_builds_and_manual_rebuild_maintains_sketch() {
use common_base::Plugins;
let mut env = TestEnv::new().await;
let plugins = Plugins::new();
let provider = Arc::new(RebuildSketchProvider::default());
plugins.insert::<IndexKindProviderRef>(provider.clone());
let engine = env
.create_engine_with_plugins(async_build_mode_config(false), plugins)
.await;
let region_id = RegionId::new(1, 1);
let mut request = CreateRequestBuilder::new()
.insert_option("sst_format", "flat")
.build();
request.column_metadatas[0]
.column_schema
.set_inverted_index(true);
request
.column_metadatas
.iter_mut()
.find(|column| column.column_id == 0)
.unwrap()
.column_schema
.mut_metadata()
.insert(
datatypes::schema::HLL_KEY.into(),
r#"{"precision":10}"#.into(),
);
let schema = rows_schema(&request);
engine
.handle_request(region_id, RegionRequest::Create(request))
.await
.unwrap();
for start in [0, 2] {
let rows = build_rows(start, start + 2);
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows,
},
)
.await;
flush_region(&engine, region_id, None).await;
}
engine
.handle_request(
region_id,
RegionRequest::BuildIndex(RegionBuildIndexRequest::default()),
)
.await
.unwrap();
assert_eq!(
0,
provider.creates.load(Ordering::Relaxed),
"manual rebuild must not introduce sketches on flush files"
);
engine
.handle_request(
region_id,
RegionRequest::Compact(RegionCompactRequest::default()),
)
.await
.unwrap();
let region = engine.get_region(region_id).unwrap();
tokio::time::timeout(Duration::from_secs(10), async {
loop {
let files = region.all_manifest_files().await.0;
if files.len() == 1
&& files[0].indexes.iter().any(|column| {
column.created_indexes.iter().any(|kind| {
matches!(
kind,
crate::sst::file::IndexType::HllIndex
| crate::sst::file::IndexType::KllIndex
| crate::sst::file::IndexType::MgIndex
)
})
})
{
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.unwrap();
let before = region.all_manifest_files().await.0[0].clone();
assert_eq!(before.index_version, 0);
assert_tag_inverted_index(&region.access_layer, before.index_id()).await;
assert_sketch_rows(&region.access_layer, before.index_id(), 4).await;
let before_inputs = provider.readable_inputs.lock().unwrap().len();
let before_creates = provider.creates.load(Ordering::Relaxed);
engine
.handle_request(
region_id,
RegionRequest::BuildIndex(RegionBuildIndexRequest::default()),
)
.await
.unwrap();
let files = region.all_manifest_files().await.0;
assert_eq!(before_creates + 1, provider.creates.load(Ordering::Relaxed));
assert!(files[0].index_version > before.index_version);
assert_eq!(
&provider.readable_inputs.lock().unwrap()[before_inputs..],
&[(before.index_id(), true)],
"manual rebuild must read the current manifest's committed Puffin version"
);
assert!(files[0].indexes.iter().any(|column| {
column
.created_indexes
.contains(&crate::sst::file::IndexType::HllIndex)
}));
assert_tag_inverted_index(&region.access_layer, files[0].index_id()).await;
assert_sketch_rows(&region.access_layer, files[0].index_id(), 4).await;
engine.stop().await.unwrap();
}
#[tokio::test]
async fn test_rebuild_column_only_index_advances_zero_generation() {
use crate::sst::file::{ColumnIndexMetadata, IndexType};
use common_base::Plugins;
let mut env = TestEnv::new().await;
let plugins = Plugins::new();
let provider = Arc::new(RebuildSketchProvider::default());
plugins.insert::<IndexKindProviderRef>(provider.clone());
let engine = env
.create_engine_with_plugins(MitoConfig::default(), plugins)
.await;
let region_id = RegionId::new(1, 1);
let mut request = CreateRequestBuilder::new().build();
request.column_metadatas[0]
.column_schema
.mut_metadata()
.insert(
datatypes::schema::HLL_KEY.into(),
r#"{"precision":10}"#.into(),
);
let table_dir = request.table_dir.clone();
let schema = rows_schema(&request);
engine
.handle_request(region_id, RegionRequest::Create(request))
.await
.unwrap();
for start in [0, 2] {
put_and_flush(&engine, region_id, &schema, start..start + 2).await;
}
engine
.handle_request(
region_id,
RegionRequest::Compact(RegionCompactRequest::default()),
)
.await
.unwrap();
let region = engine.get_region(region_id).unwrap();
let mut files = region.all_manifest_files().await.0;
assert_eq!(files.len(), 1);
let mut before = files.pop().unwrap();
assert_eq!(before.index_version, 0);
assert!(before.index_file_size > 0);
before.available_indexes.clear();
before.indexes = vec![ColumnIndexMetadata {
column_id: 0,
created_indexes: [IndexType::HllIndex].into_iter().collect(),
..Default::default()
}];
let old_path = location::index_file_path(
region.access_layer.table_dir(),
before.index_id(),
region.access_layer.path_type(),
);
let store = region.access_layer.object_store().clone();
let old_bytes = store.read(&old_path).await.unwrap().to_vec();
engine
.edit_region(
region_id,
RegionEdit {
files_to_add: vec![before.clone()],
..Default::default()
},
)
.await
.unwrap();
reopen_region(&engine, region_id, table_dir, true, HashMap::new()).await;
let inputs_before = provider.readable_inputs.lock().unwrap().len();
engine
.handle_request(
region_id,
RegionRequest::BuildIndex(RegionBuildIndexRequest::default()),
)
.await
.unwrap();
let region = engine.get_region(region_id).unwrap();
let files = region.all_manifest_files().await.0;
assert_eq!(files.len(), 1);
assert_eq!(files[0].file_id, before.file_id);
assert_eq!(files[0].index_version, 1);
assert!(
files[0]
.indexes
.iter()
.any(|column| column.created_indexes.contains(&IndexType::HllIndex))
);
assert_eq!(
&provider.readable_inputs.lock().unwrap()[inputs_before..],
&[(before.index_id(), true)]
);
let new_path = location::index_file_path(
region.access_layer.table_dir(),
files[0].index_id(),
region.access_layer.path_type(),
);
assert_ne!(old_path, new_path);
assert!(store.exists(&new_path).await.unwrap());
assert_eq!(store.read(&old_path).await.unwrap().to_vec(), old_bytes);
engine.stop().await.unwrap();
}
#[tokio::test]
async fn test_compaction_without_provider_drops_historical_sketch_indexes() {
use crate::sst::file::{ColumnIndexMetadata, IndexType};
let mut env = TestEnv::new().await;
let engine = env.create_engine(MitoConfig::default()).await;
let region_id = RegionId::new(1, 1);
let request = CreateRequestBuilder::new().build_with_index();
let schema = rows_schema(&request);
let table_dir = request.table_dir.clone();
engine
.handle_request(region_id, RegionRequest::Create(request))
.await
.unwrap();
for start in [0, 2] {
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: build_rows(start, start + 2),
},
)
.await;
flush_region(&engine, region_id, None).await;
}
let region = engine.get_region(region_id).unwrap();
let mut inputs = region.all_manifest_files().await.0;
for file in &mut inputs {
file.available_indexes.push(IndexType::HllIndex);
file.indexes.push(ColumnIndexMetadata {
column_id: 0,
created_indexes: [IndexType::HllIndex].into_iter().collect(),
..Default::default()
});
}
engine
.edit_region(
region_id,
RegionEdit {
files_to_add: inputs,
..Default::default()
},
)
.await
.unwrap();
// Reload handles so compaction sees the persisted descriptors as well as the manifest.
reopen_region(&engine, region_id, table_dir, true, HashMap::new()).await;
let region = engine.get_region(region_id).unwrap();
let version = region.version();
let files: Vec<_> = version
.ssts
.levels()
.iter()
.flat_map(|level| level.files())
.collect();
assert_eq!(files.len(), 2);
assert!(files.iter().all(|file| {
file.meta_ref()
.available_indexes
.contains(&IndexType::HllIndex)
}));
drop(version);
let before: HashMap<_, _> = region
.all_manifest_files()
.await
.0
.into_iter()
.map(|file| (file.file_id, file))
.collect();
engine
.handle_request(
region_id,
RegionRequest::Compact(RegionCompactRequest::default()),
)
.await
.unwrap();
let outputs = region.all_manifest_files().await.0;
assert_eq!(outputs.len(), 1);
let output = &outputs[0];
assert!(!before.contains_key(&output.file_id));
assert_eq!(output.num_rows, 4);
assert_eq!(
output.available_indexes.as_slice(),
&[IndexType::InvertedIndex]
);
assert!(
output
.indexes
.iter()
.all(|column| !column.created_indexes.contains(&IndexType::HllIndex))
);
assert_tag_inverted_index(&region.access_layer, output.index_id()).await;
engine.stop().await.unwrap();
}
+9 -1
View File
@@ -23,7 +23,7 @@ mod indexer;
pub mod intermediate;
pub(crate) mod inverted_index;
pub mod kind;
mod primary_key;
pub mod primary_key;
pub mod puffin_manager;
pub mod registry;
#[cfg(test)]
@@ -292,6 +292,11 @@ impl Indexer {
self.creators.is_empty()
}
/// Whether this output has an active extension creator.
pub(crate) fn has_extension_creators(&self) -> bool {
self.creators.iter().any(|active| !active.is_builtin())
}
#[cfg(test)]
fn has_kind(&self, kind: kind::IndexKind) -> bool {
self.creators
@@ -2800,6 +2805,9 @@ mod tests {
);
assert!(!first.is_empty());
assert!(!second.is_empty());
// Deferring the built-in indexes must retain the sketch creator.
first.abort_builtin_creators().await;
assert!(!first.creators.is_empty());
assert_eq!(first.creators[0].creator.memory_usage(), 17);
first.abort().await;
assert!(first.creators.is_empty());
+14
View File
@@ -19,6 +19,20 @@ use crate::sst::file::{RegionFileId, RegionIndexId};
use crate::sst::index::Indexer;
impl Indexer {
/// Aborts builtin creators while retaining synchronous extension creators.
pub(crate) async fn abort_builtin_creators(&mut self) {
self.dense_pk_decoder = None;
let mut position = 0;
while position < self.creators.len() {
if self.creators[position].is_builtin() {
self.abort_creator(position).await;
} else {
position += 1;
}
}
self.flush_mem_metrics();
}
pub(crate) async fn abort_creator(&mut self, position: usize) {
let mut active = self.creators.remove(position);
if let Err(err) = active.creator.abort().await {
+3 -2
View File
@@ -21,13 +21,14 @@ use crate::sst::parquet::flat_format::primary_key_column_index;
use crate::sst::parquet::format::PrimaryKeyArray;
/// Iterates consecutive dictionary-key runs, preserving their row order without decoding.
pub(crate) struct PrimaryKeyRuns<'a> {
pub struct PrimaryKeyRuns<'a> {
keys: &'a [u32],
values: &'a BinaryArray,
}
impl<'a> PrimaryKeyRuns<'a> {
pub(crate) fn try_new(batch: &'a RecordBatch) -> Result<Self> {
/// Borrows the encoded primary keys of a flat batch without decoding values.
pub fn try_new(batch: &'a RecordBatch) -> Result<Self> {
let pk = batch
.column(primary_key_column_index(batch.num_columns()))
.as_any()
+61 -2
View File
@@ -19,11 +19,11 @@ use std::sync::Arc;
use common_error::ext::BoxedError;
use common_telemetry::warn;
use snafu::IntoError;
use snafu::{IntoError, ResultExt};
use store_api::metadata::RegionMetadataRef;
use store_api::storage::RegionId;
use crate::config::{BloomFilterConfig, FulltextIndexConfig, InvertedIndexConfig};
use crate::config::{BloomFilterConfig, FulltextIndexConfig, InvertedIndexConfig, MitoConfig};
use crate::error::{Error, ExternalSnafu, InvalidConfigSnafu};
use crate::metrics::{INDEX_CREATE_DISCARDED_TOTAL, INDEX_DISCARD_ADMISSION_REJECTED};
use crate::sst::file::{FileMeta, IndexType};
@@ -59,6 +59,22 @@ impl IndexRegistry {
}
}
/// Assembles the configured built-ins and the injected provider.
pub(crate) fn from_config(
config: &MitoConfig,
provider: Option<IndexKindProviderRef>,
) -> crate::error::Result<Self> {
Self::new(
config.inverted_index.clone(),
config.fulltext_index.clone(),
config.bloom_filter_index.clone(),
)
.with_provider(provider)
.with_context(|_| ExternalSnafu {
context: "registering index handlers".to_string(),
})
}
/// Adds the embedding application's handlers after the built-in configuration.
pub fn with_provider(
mut self,
@@ -102,6 +118,49 @@ impl IndexRegistry {
self.validate_file_kinds(files)
}
/// Reports unavailable sketches without blocking compaction after a downgrade.
/// Logs each missing kind at most once for this input group.
pub(crate) fn warn_missing_compaction_handlers<'a>(
&self,
metadata: &RegionMetadataRef,
files: impl IntoIterator<Item = &'a FileMeta>,
) {
let missing = ExtensionIndexKind::ALL
.map(|kind| !self.has_handler(IndexKind::Extension(kind).index_type()));
if !missing.iter().any(|missing| *missing) {
return;
}
let mut requested = ExtensionIndexKind::ALL.map(|kind| {
metadata.column_metadatas.iter().any(|column| {
column
.column_schema
.metadata()
.contains_key(kind.required_metadata_key())
})
});
for file in files {
for (position, kind) in ExtensionIndexKind::ALL.iter().enumerate() {
if missing[position] && !requested[position] {
let index_type = IndexKind::Extension(*kind).index_type();
requested[position] = file.available_indexes.contains(&index_type)
|| file
.indexes
.iter()
.any(|column| column.created_indexes.contains(&index_type));
}
}
}
for (position, kind) in ExtensionIndexKind::ALL.iter().enumerate() {
if missing[position] && requested[position] {
warn!(
"Compacting without unavailable sketch provider, region_id: {}, kind: {}; output will omit this sketch",
metadata.region_id,
kind.as_str()
);
}
}
}
/// Rejects unavailable implementations for indexes being replaced.
fn validate_file_kinds<'a>(
&self,
+20 -24
View File
@@ -56,7 +56,7 @@ use crate::error::{
};
use crate::read::FlatSource;
use crate::sst::file::RegionFileId;
use crate::sst::index::{IndexOutput, Indexer, IndexerBuilder};
use crate::sst::index::{Indexer, IndexerBuilder};
use crate::sst::parquet::flat_format::{
FlatWriteFormat, primary_key_column_index, time_index_column_index,
};
@@ -531,18 +531,7 @@ where
// Finish indexer and writer.
// safety: writer and index can only be both present or not.
let mut index_output = IndexOutput::default();
match self.index_config.build_mode {
IndexBuildMode::Sync => {
index_output = self.current_indexer.as_mut().unwrap().finish().await;
}
IndexBuildMode::Async => {
debug!(
"Index for file {} will be built asynchronously later",
self.current_file
);
}
}
let index_output = self.current_indexer.as_mut().unwrap().finish().await;
self.check_cancelled()?;
self.writer
.as_mut()
@@ -773,16 +762,15 @@ where
self.check_cancelled()?;
stats.update_flat(batch)?;
if matches!(self.index_config.build_mode, IndexBuildMode::Sync) {
let start = Instant::now();
// safety: self.current_indexer must be set when first batch has been written.
self.current_indexer
.as_mut()
.unwrap()
.update_flat(batch)
.await;
self.metrics.update_index += start.elapsed();
}
let start = Instant::now();
// The indexer exists once the first batch has been written. Outputs
// with extensions build all indexes together, including in async mode.
self.current_indexer
.as_mut()
.unwrap()
.update_flat(batch)
.await;
self.metrics.update_index += start.elapsed();
Ok(())
}
.await;
@@ -896,7 +884,7 @@ where
};
self.writer = Some(arrow_writer);
let indexer = self
let mut indexer = self
.indexer_builder
.build(
RegionFileId::new(self.metadata.region_id, self.current_file),
@@ -904,6 +892,14 @@ where
Some(opts.row_group_size),
)
.await;
if matches!(self.index_config.build_mode, IndexBuildMode::Async)
&& !indexer.has_extension_creators()
{
indexer.abort_builtin_creators().await;
}
// Extensions share one Puffin with built-in indexes. When an extension
// is active, finish all creators here instead of replacing its output
// with a built-in-only Puffin in the background.
self.current_indexer = Some(indexer);
// safety: self.writer is assigned above
+2 -11
View File
@@ -68,7 +68,7 @@ use crate::cache::{CacheManager, CacheManagerRef, WriteCacheUploadStoreWrapperRe
use crate::compaction::memory_manager::{CompactionMemoryManager, new_compaction_memory_manager};
use crate::compaction::{CancelCompactionResult, CompactionCoordinator, CompactionScheduler};
use crate::config::MitoConfig;
use crate::error::{self, CreateDirSnafu, ExternalSnafu, JoinSnafu, Result, WorkerStoppedSnafu};
use crate::error::{self, CreateDirSnafu, JoinSnafu, Result, WorkerStoppedSnafu};
use crate::flush::{FlushScheduler, WriteBufferManagerImpl, WriteBufferManagerRef};
use crate::gc::{GcLimiter, GcLimiterRef};
use crate::lease::{GrantedLeases, NodeLease};
@@ -689,16 +689,7 @@ fn index_registry(
config: &MitoConfig,
provider: Option<IndexKindProviderRef>,
) -> Result<IndexRegistryRef> {
IndexRegistry::new(
config.inverted_index.clone(),
config.fulltext_index.clone(),
config.bloom_filter_index.clone(),
)
.with_provider(provider)
.map(Arc::new)
.with_context(|_| ExternalSnafu {
context: "registering index handlers".to_string(),
})
IndexRegistry::from_config(config, provider).map(Arc::new)
}
/// Worker start config.
+26 -3
View File
@@ -27,9 +27,24 @@ use crate::request::{
BuildIndexRequest, CompactionFailed, CompactionFinished, CompactionUnitNotification, OnFailure,
OptionOutputTx,
};
use crate::sst::file::FileMeta;
use crate::sst::index::IndexBuildType;
use crate::sst::index::kind::{ExtensionIndexKind, IndexKind};
use crate::worker::RegionWorkerLoop;
/// Completed extensions prove the writer built this output's indexes together.
/// A background replacement would otherwise erase those extension blobs.
fn needs_background_indexes(file: &FileMeta) -> bool {
!ExtensionIndexKind::ALL.iter().any(|kind| {
let index_type = IndexKind::Extension(*kind).index_type();
file.available_indexes.contains(&index_type)
|| file
.indexes
.iter()
.any(|column| column.created_indexes.contains(&index_type))
})
}
fn made_progress(files_to_add: usize, files_to_remove: usize) -> bool {
files_to_add > 0 || files_to_remove > files_to_add
}
@@ -71,14 +86,19 @@ impl<S> RegionWorkerLoop<S> {
plan_id,
Ok(progress),
);
let files_to_index = edit
.files_to_add
.into_iter()
.filter(needs_background_indexes)
.collect::<Vec<_>>();
if self.config.index.build_mode == IndexBuildMode::Async
&& !edit.files_to_add.is_empty()
&& !files_to_index.is_empty()
{
self.handle_rebuild_index(
BuildIndexRequest {
region_id,
build_type: IndexBuildType::Compact,
file_metas: edit.files_to_add,
file_metas: files_to_index,
result_senders: Vec::new(),
},
OptionOutputTx::new(None),
@@ -231,7 +251,10 @@ impl<S> RegionWorkerLoop<S> {
region.file_purger.clone(),
);
let index_build_file_metas = std::mem::take(&mut request.edit.files_to_add);
let index_build_file_metas = std::mem::take(&mut request.edit.files_to_add)
.into_iter()
.filter(needs_background_indexes)
.collect::<Vec<_>>();
// compaction finished.
request.on_success();
+13
View File
@@ -13,6 +13,8 @@
// limitations under the License.
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use derive_builder::Builder;
@@ -26,6 +28,9 @@ pub type MockWriterFactory = Arc<dyn Fn(&str, OpWrite, oio::Writer) -> oio::Writ
pub type MockReaderFactory = Arc<dyn Fn(&str, OpRead, oio::Reader) -> oio::Reader + Send + Sync>;
pub type MockListerFactory = Arc<dyn Fn(&str, OpList, oio::Lister) -> oio::Lister + Send + Sync>;
pub type MockDeleterFactory = Arc<dyn Fn(oio::Deleter) -> oio::Deleter + Send + Sync>;
pub type StatInterceptor =
Arc<dyn Fn(&str) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;
pub type CopyInterceptor = Arc<dyn Fn(&str, &str, OpCopy) -> Option<Result<()>> + Send + Sync>;
#[derive(Builder)]
@@ -40,6 +45,8 @@ pub struct MockLayer {
deleter_factory: Option<MockDeleterFactory>,
#[builder(setter(strip_option), default)]
copy_interceptor: Option<CopyInterceptor>,
#[builder(setter(strip_option), default)]
stat_interceptor: Option<StatInterceptor>,
}
impl Clone for MockLayer {
@@ -50,6 +57,7 @@ impl Clone for MockLayer {
lister_factory: self.lister_factory.clone(),
deleter_factory: self.deleter_factory.clone(),
copy_interceptor: self.copy_interceptor.clone(),
stat_interceptor: self.stat_interceptor.clone(),
}
}
}
@@ -69,6 +77,7 @@ impl Layer for MockLayer {
lister_factory: self.lister_factory.clone(),
deleter_factory: self.deleter_factory.clone(),
copy_interceptor: self.copy_interceptor.clone(),
stat_interceptor: self.stat_interceptor.clone(),
})
}
}
@@ -80,6 +89,7 @@ struct MockService {
lister_factory: Option<MockListerFactory>,
deleter_factory: Option<MockDeleterFactory>,
copy_interceptor: Option<CopyInterceptor>,
stat_interceptor: Option<StatInterceptor>,
}
impl Debug for MockService {
@@ -121,6 +131,9 @@ impl Service for MockService {
path: &str,
args: opendal::raw::OpStat,
) -> Result<opendal::raw::RpStat> {
if let Some(interceptor) = &self.stat_interceptor {
interceptor(path).await;
}
self.inner.stat(ctx, path, args).await
}