mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 05:34:02 +00:00
fix(mito2): allow table compaction publication after adding nullable sparse tags (#10681)
* fix(mito2): allow table compaction publication after adding nullable sparse tags Signed-off-by: WenyXu <wenymedia@gmail.com> * fix(mito2): publish table compaction across schema changes Signed-off-by: WenyXu <wenymedia@gmail.com> --------- Signed-off-by: WenyXu <wenymedia@gmail.com>
This commit is contained in:
@@ -17,7 +17,7 @@ mod tests;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use snafu::{OptionExt, ResultExt, ensure};
|
||||
use store_api::metadata::RegionMetadataRef;
|
||||
use store_api::metadata::{RegionMetadata, RegionMetadataRef};
|
||||
use store_api::storage::RegionId;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
@@ -57,6 +57,18 @@ pub enum PublicationOutcome {
|
||||
StaleInputs,
|
||||
}
|
||||
|
||||
/// Returns whether both snapshots describe the same table-compaction target range.
|
||||
/// ALTER may change the schema while compaction runs. Publication verifies the
|
||||
/// output footer against the captured schema and checks the input files separately.
|
||||
pub fn is_table_compaction_metadata_compatible(
|
||||
captured: &RegionMetadata,
|
||||
current: &RegionMetadata,
|
||||
) -> bool {
|
||||
captured.region_id == current.region_id
|
||||
&& captured.partition_expr_version == current.partition_expr_version
|
||||
&& captured.partition_expr == current.partition_expr
|
||||
}
|
||||
|
||||
impl MitoEngine {
|
||||
pub fn table_compaction_snapshot(
|
||||
&self,
|
||||
|
||||
@@ -13,20 +13,24 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Weak};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use api::v1::Rows;
|
||||
use api::v1::{Rows, SemanticType};
|
||||
use bytes::Bytes;
|
||||
use common_base::Plugins;
|
||||
use common_recordbatch::RecordBatches;
|
||||
use common_time::Timestamp;
|
||||
use common_time::timestamp::TimeUnit;
|
||||
use datatypes::prelude::ConcreteDataType;
|
||||
use datatypes::schema::ColumnSchema;
|
||||
use parquet::file::metadata::ParquetMetaData;
|
||||
use store_api::metadata::ColumnMetadata;
|
||||
use store_api::region_engine::{RegionEngine, RegionRole};
|
||||
use store_api::region_request::{
|
||||
AlterKind, RegionAlterRequest, RegionRequest, RegionTruncateRequest, SetRegionOption,
|
||||
AddColumn, AlterKind, RegionAlterRequest, RegionRequest, RegionTruncateRequest,
|
||||
SetRegionOption, UnsetRegionOption,
|
||||
};
|
||||
use store_api::storage::{FileId, RegionId};
|
||||
|
||||
use super::{PublicationOutcome, TableCompactionSnapshot};
|
||||
use super::{PublicationOutcome, TableCompactionSnapshot, is_table_compaction_metadata_compatible};
|
||||
use crate::config::MitoConfig;
|
||||
use crate::engine::MitoEngine;
|
||||
use crate::engine::lease_test::promote_to_await_leader_lease;
|
||||
@@ -36,8 +40,10 @@ use crate::manifest::action::{RegionEdit, RegionMetaAction, RegionMetaActionList
|
||||
use crate::region::MitoRegionRef;
|
||||
use crate::sst::file::{ColumnIndexMetadata, FileMeta, IndexType, LEVEL_SHARED, SHARED_LEVEL_SLOT};
|
||||
use crate::sst::location;
|
||||
use crate::test_util::sst_util::new_sparse_primary_key;
|
||||
use crate::test_util::{
|
||||
CreateRequestBuilder, TestEnv, build_rows, flush_region, put_rows, reopen_region, rows_schema,
|
||||
sparse_metric_create_request, sparse_metric_put,
|
||||
};
|
||||
|
||||
fn config() -> MitoConfig {
|
||||
@@ -105,12 +111,20 @@ async fn seed_shared_inputs(
|
||||
.await;
|
||||
flush_region(engine, region_id, None).await;
|
||||
}
|
||||
promote_flushed_files(engine, region_id, row_ranges.len()).await
|
||||
}
|
||||
|
||||
async fn promote_flushed_files(
|
||||
engine: &MitoEngine,
|
||||
region_id: RegionId,
|
||||
file_count: usize,
|
||||
) -> (TableCompactionSnapshot, Vec<FileMeta>) {
|
||||
let region = engine.get_region(region_id).unwrap();
|
||||
let originals = region.version().ssts.levels()[0]
|
||||
.files()
|
||||
.map(|file| file.meta_ref().clone())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(row_ranges.len(), originals.len());
|
||||
assert_eq!(file_count, originals.len());
|
||||
let mut shared = Vec::with_capacity(originals.len());
|
||||
for original in &originals {
|
||||
shared.push(copy_sst(®ion, original, LEVEL_SHARED).await);
|
||||
@@ -127,7 +141,7 @@ async fn seed_shared_inputs(
|
||||
.await
|
||||
.unwrap();
|
||||
let snapshot = engine.table_compaction_snapshot(region_id).unwrap();
|
||||
assert_eq!(row_ranges.len(), snapshot.inputs.len());
|
||||
assert_eq!(file_count, snapshot.inputs.len());
|
||||
let mut outputs = Vec::with_capacity(snapshot.inputs.len());
|
||||
for input in &snapshot.inputs {
|
||||
outputs.push(copy_sst(®ion, input, 0).await);
|
||||
@@ -1284,3 +1298,145 @@ async fn test_failed_verification_does_not_notify_hook() {
|
||||
);
|
||||
assert_eq!(6, row_count(&engine, region_id).await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_table_compaction_publication_after_nullable_tag_added() {
|
||||
let mut env = TestEnv::new().await;
|
||||
let engine = env.create_engine(config()).await;
|
||||
let region_id = RegionId::new(1, 1);
|
||||
engine
|
||||
.handle_request(
|
||||
region_id,
|
||||
RegionRequest::Create(sparse_metric_create_request("flat")),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let region = engine.get_region(region_id).unwrap();
|
||||
let key = new_sparse_primary_key(&["old"], ®ion.metadata(), 42, 7);
|
||||
engine
|
||||
.handle_request(region_id, sparse_metric_put(key, 1000, Some(1.0)))
|
||||
.await
|
||||
.unwrap();
|
||||
flush_region(&engine, region_id, None).await;
|
||||
let (snapshot, outputs) = promote_flushed_files(&engine, region_id, 1).await;
|
||||
engine
|
||||
.handle_request(
|
||||
region_id,
|
||||
RegionRequest::Alter(RegionAlterRequest {
|
||||
kind: AlterKind::AddColumns {
|
||||
columns: vec![AddColumn {
|
||||
column_metadata: ColumnMetadata {
|
||||
column_schema: ColumnSchema::new(
|
||||
"new_tag",
|
||||
ConcreteDataType::string_datatype(),
|
||||
true,
|
||||
),
|
||||
semantic_type: SemanticType::Tag,
|
||||
column_id: 3,
|
||||
},
|
||||
location: None,
|
||||
}],
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// The second publication models a lost acknowledgement after installing the output.
|
||||
for _ in 0..2 {
|
||||
assert_eq!(
|
||||
PublicationOutcome::Applied,
|
||||
engine
|
||||
.publish_table_compaction(region_id, snapshot.clone(), outputs.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
let batches = RecordBatches::try_collect(
|
||||
engine
|
||||
.scan_to_stream(region_id, Default::default())
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
1,
|
||||
batches.iter().map(|batch| batch.num_rows()).sum::<usize>()
|
||||
);
|
||||
for batch in batches.iter() {
|
||||
let tag = batch.column_by_name("new_tag").unwrap();
|
||||
assert_eq!(tag.len(), tag.null_count());
|
||||
}
|
||||
engine.stop().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_publication_after_drop_field_and_option_changes() {
|
||||
let mut env = TestEnv::new().await;
|
||||
let engine = env.create_engine(config()).await;
|
||||
let region_id = RegionId::new(1, 1);
|
||||
let (snapshot, output) = seed(&engine, region_id).await;
|
||||
let alter =
|
||||
|kind| engine.handle_request(region_id, RegionRequest::Alter(RegionAlterRequest { kind }));
|
||||
alter(AlterKind::DropColumns {
|
||||
names: vec!["field_0".into()],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
alter(AlterKind::SetRegionOptions {
|
||||
options: vec![SetRegionOption::CompactionOutputParallelism(Some(2))],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let scan = || async {
|
||||
RecordBatches::try_collect(
|
||||
engine
|
||||
.scan_to_stream(region_id, Default::default())
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
};
|
||||
let before = scan().await;
|
||||
assert_eq!(
|
||||
3,
|
||||
before.iter().map(|batch| batch.num_rows()).sum::<usize>()
|
||||
);
|
||||
assert!(
|
||||
before
|
||||
.iter()
|
||||
.all(|batch| batch.column_by_name("field_0").is_none())
|
||||
);
|
||||
let publish =
|
||||
|| engine.publish_table_compaction(region_id, snapshot.clone(), vec![output.clone()]);
|
||||
assert_eq!(PublicationOutcome::Applied, publish().await.unwrap());
|
||||
alter(AlterKind::UnsetRegionOptions {
|
||||
keys: vec![UnsetRegionOption::CompactionOutputParallelism],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(PublicationOutcome::Applied, publish().await.unwrap());
|
||||
assert_eq!(
|
||||
before.pretty_print().unwrap(),
|
||||
scan().await.pretty_print().unwrap()
|
||||
);
|
||||
|
||||
let mut changed = (*snapshot.metadata).clone();
|
||||
changed.set_partition_expr(Some("changed".into()));
|
||||
assert!(!is_table_compaction_metadata_compatible(
|
||||
&snapshot.metadata,
|
||||
&changed
|
||||
));
|
||||
let mut stale = snapshot;
|
||||
stale.metadata = Arc::new(changed);
|
||||
assert_eq!(
|
||||
PublicationOutcome::StaleInputs,
|
||||
engine
|
||||
.publish_table_compaction(region_id, stale, vec![output])
|
||||
.await
|
||||
.unwrap()
|
||||
);
|
||||
engine.stop().await.unwrap();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@ use std::collections::HashSet;
|
||||
|
||||
use snafu::ensure;
|
||||
|
||||
use crate::engine::table_compaction::{PublicationContext, output};
|
||||
use crate::engine::table_compaction::{
|
||||
PublicationContext, is_table_compaction_metadata_compatible, output,
|
||||
};
|
||||
use crate::error::{InvalidRequestSnafu, RegionBusySnafu, Result, StaleCompactionExecutionSnafu};
|
||||
use crate::manifest::action::{RegionEdit, RegionManifest, RegionMetaAction, RegionMetaActionList};
|
||||
use crate::region::{MitoRegionRef, RegionLeaderState, RegionRoleState};
|
||||
@@ -112,7 +114,8 @@ fn is_replay(
|
||||
) -> Result<bool> {
|
||||
let region_id = region.region_id;
|
||||
ensure!(
|
||||
context.metadata == manifest.metadata && !edit.files_to_remove.is_empty(),
|
||||
is_table_compaction_metadata_compatible(&context.metadata, &manifest.metadata)
|
||||
&& !edit.files_to_remove.is_empty(),
|
||||
StaleCompactionExecutionSnafu { region_id }
|
||||
);
|
||||
let matches =
|
||||
|
||||
Reference in New Issue
Block a user