Skip to content

Commit dbe6830

Browse files
authored
feat: add vacuum table result table (#14830)
* feat: add vacuum table result table * feat: add vacuum table result table * feat: add vacuum table result table, fix test fail
1 parent 6517e89 commit dbe6830

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

src/query/service/src/interpreters/interpreter_table_vacuum.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ use chrono::Duration;
1818
use databend_common_catalog::table::TableExt;
1919
use databend_common_exception::Result;
2020
use databend_common_expression::types::StringType;
21+
use databend_common_expression::types::UInt64Type;
2122
use databend_common_expression::DataBlock;
2223
use databend_common_expression::FromData;
2324
use databend_common_license::license::Feature::Vacuum;
2425
use databend_common_license::license_manager::get_license_manager;
2526
use databend_common_sql::plans::VacuumTablePlan;
2627
use databend_common_storages_fuse::FuseTable;
28+
use databend_common_storages_fuse::FUSE_TBL_BLOCK_PREFIX;
29+
use databend_common_storages_fuse::FUSE_TBL_SEGMENT_PREFIX;
30+
use databend_common_storages_fuse::FUSE_TBL_SNAPSHOT_PREFIX;
31+
use databend_common_storages_fuse::FUSE_TBL_XOR_BLOOM_INDEX_PREFIX;
2732
use databend_enterprise_vacuum_handler::get_vacuum_handler;
33+
use opendal::Metakey;
2834

2935
use crate::interpreters::Interpreter;
3036
use crate::pipelines::PipelineBuildResult;
@@ -36,10 +42,57 @@ pub struct VacuumTableInterpreter {
3642
plan: VacuumTablePlan,
3743
}
3844

45+
type FileStat = (u64, u64);
46+
47+
#[derive(Debug, Default)]
48+
struct Statistics {
49+
pub snapshot_files: FileStat,
50+
pub segment_files: FileStat,
51+
pub block_files: FileStat,
52+
pub index_files: FileStat,
53+
}
54+
3955
impl VacuumTableInterpreter {
4056
pub fn try_create(ctx: Arc<QueryContext>, plan: VacuumTablePlan) -> Result<Self> {
4157
Ok(VacuumTableInterpreter { ctx, plan })
4258
}
59+
60+
async fn get_statistics(&self, fuse_table: &FuseTable) -> Result<Statistics> {
61+
let operator = fuse_table.get_operator();
62+
let table_data_prefix = format!("/{}", fuse_table.meta_location_generator().prefix());
63+
64+
let mut snapshot_files = (0, 0);
65+
let mut segment_files = (0, 0);
66+
let mut block_files = (0, 0);
67+
let mut index_files = (0, 0);
68+
69+
let prefix_with_stats = vec![
70+
(FUSE_TBL_SNAPSHOT_PREFIX, &mut snapshot_files),
71+
(FUSE_TBL_SEGMENT_PREFIX, &mut segment_files),
72+
(FUSE_TBL_BLOCK_PREFIX, &mut block_files),
73+
(FUSE_TBL_XOR_BLOOM_INDEX_PREFIX, &mut index_files),
74+
];
75+
76+
for (dir_prefix, stat) in prefix_with_stats {
77+
for entry in operator
78+
.list_with(&format!("{}/{}/", table_data_prefix, dir_prefix))
79+
.metakey(Metakey::ContentLength)
80+
.await?
81+
{
82+
if entry.metadata().is_file() {
83+
stat.0 += 1;
84+
stat.1 += entry.metadata().content_length();
85+
}
86+
}
87+
}
88+
89+
Ok(Statistics {
90+
snapshot_files,
91+
segment_files,
92+
block_files,
93+
index_files,
94+
})
95+
}
4396
}
4497

4598
#[async_trait::async_trait]
@@ -83,7 +136,31 @@ impl Interpreter for VacuumTableInterpreter {
83136
.await?;
84137

85138
match purge_files_opt {
86-
None => return Ok(PipelineBuildResult::create()),
139+
None => {
140+
return {
141+
let stat = self.get_statistics(fuse_table).await?;
142+
let total_files = stat.snapshot_files.0
143+
+ stat.segment_files.0
144+
+ stat.block_files.0
145+
+ stat.index_files.0;
146+
let total_size = stat.snapshot_files.1
147+
+ stat.segment_files.1
148+
+ stat.block_files.1
149+
+ stat.index_files.1;
150+
PipelineBuildResult::from_blocks(vec![DataBlock::new_from_columns(vec![
151+
UInt64Type::from_data(vec![stat.snapshot_files.0]),
152+
UInt64Type::from_data(vec![stat.snapshot_files.1]),
153+
UInt64Type::from_data(vec![stat.segment_files.0]),
154+
UInt64Type::from_data(vec![stat.segment_files.1]),
155+
UInt64Type::from_data(vec![stat.block_files.0]),
156+
UInt64Type::from_data(vec![stat.block_files.1]),
157+
UInt64Type::from_data(vec![stat.index_files.0]),
158+
UInt64Type::from_data(vec![stat.index_files.1]),
159+
UInt64Type::from_data(vec![total_files]),
160+
UInt64Type::from_data(vec![total_size]),
161+
])])
162+
};
163+
}
87164
Some(purge_files) => PipelineBuildResult::from_blocks(vec![
88165
DataBlock::new_from_columns(vec![StringType::from_data(purge_files)]),
89166
]),

src/query/sql/src/planner/plans/ddl/table.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,18 @@ impl VacuumTablePlan {
114114
DataType::String,
115115
)]))
116116
} else {
117-
Arc::new(DataSchema::empty())
117+
Arc::new(DataSchema::new(vec![
118+
DataField::new("snapshot_files", DataType::Number(NumberDataType::UInt64)),
119+
DataField::new("snapshot_bytes", DataType::Number(NumberDataType::UInt64)),
120+
DataField::new("segments_files", DataType::Number(NumberDataType::UInt64)),
121+
DataField::new("segments_size", DataType::Number(NumberDataType::UInt64)),
122+
DataField::new("block_files", DataType::Number(NumberDataType::UInt64)),
123+
DataField::new("block_size", DataType::Number(NumberDataType::UInt64)),
124+
DataField::new("index_files", DataType::Number(NumberDataType::UInt64)),
125+
DataField::new("index_size", DataType::Number(NumberDataType::UInt64)),
126+
DataField::new("total_files", DataType::Number(NumberDataType::UInt64)),
127+
DataField::new("total_size", DataType::Number(NumberDataType::UInt64)),
128+
]))
118129
}
119130
}
120131
}

tests/suites/5_ee/01_vacuum/01_0001_ee_vacuum_kill_randomly.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bash ../scripts/ci/deploy/databend-query-standalone.sh >/dev/null 2>&1
3030
# check if before and after vacuum table the table count matched
3131
old_count=$(echo "select * from test_vacuum.a order by c" | $BENDSQL_CLIENT_CONNECT)
3232

33-
echo "set data_retention_time_in_days=0; vacuum table test_vacuum.a" | $BENDSQL_CLIENT_CONNECT
33+
echo "set data_retention_time_in_days=0; vacuum table test_vacuum.a" | $BENDSQL_CLIENT_CONNECT >/dev/null
3434
#echo "optimize table test_vacuum.a all" | $BENDSQL_CLIENT_CONNECT
3535
count=$(echo "select * from test_vacuum.a order by c" | $BENDSQL_CLIENT_CONNECT)
3636

tests/suites/5_ee/01_vacuum/01_0002_ee_vacuum_drop_table.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ echo "select * from test_vacuum_drop_4.b" | $BENDSQL_CLIENT_CONNECT
9999
echo "set data_retention_time_in_days=0; vacuum drop table" | $BENDSQL_CLIENT_CONNECT
100100
echo "select * from test_vacuum_drop_4.b" | $BENDSQL_CLIENT_CONNECT
101101

102+
## test vacuum drop table output
103+
echo "create table test_vacuum_drop_4.c(c int)" | $BENDSQL_CLIENT_CONNECT
104+
echo "INSERT INTO test_vacuum_drop_4.c VALUES (1)" | $BENDSQL_CLIENT_CONNECT
105+
count=$(echo "set data_retention_time_in_days=0; vacuum table test_vacuum_drop_4.c" | $BENDSQL_CLIENT_CONNECT | awk '{print $9}')
106+
if [[ "$count" != "4" ]]; then
107+
echo "vacuum table, count:$count"
108+
exit 1
109+
fi
110+
102111
echo "drop database if exists test_vacuum_drop_4" | $BENDSQL_CLIENT_CONNECT
103112

104113
## Drop table

0 commit comments

Comments
 (0)