Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(storage): limit the scan threads if there are small parts #10226

Merged
merged 5 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/query/storages/common/table-meta/src/meta/v2/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::collections::HashMap;
use std::ops::Range;
use std::sync::Arc;

use common_arrow::native::ColumnMeta as NativeColumnMeta;
Expand Down Expand Up @@ -114,6 +115,38 @@ impl ColumnMeta {
ColumnMeta::Native(v) => (v.offset, v.pages.iter().map(|page| page.length).sum()),
}
}

pub fn read_rows(&self, range: &Option<Range<usize>>) -> u64 {
match self {
ColumnMeta::Parquet(v) => v.num_values,
ColumnMeta::Native(v) => match range {
Some(range) => v
.pages
.iter()
.skip(range.start)
.take(range.end - range.start)
.map(|page| page.num_values)
.sum(),
None => v.pages.iter().map(|page| page.num_values).sum(),
},
}
}

pub fn read_bytes(&self, range: &Option<Range<usize>>) -> u64 {
match self {
ColumnMeta::Parquet(v) => v.len,
ColumnMeta::Native(v) => match range {
Some(range) => v
.pages
.iter()
.skip(range.start)
.take(range.end - range.start)
.map(|page| page.length)
.sum(),
None => v.pages.iter().map(|page| page.length).sum(),
},
}
}
}

impl SegmentInfo {
Expand Down
35 changes: 32 additions & 3 deletions src/query/storages/fuse/src/operations/read/fuse_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use common_pipeline_core::Pipeline;
use common_pipeline_core::SourcePipeBuilder;
use tracing::info;

use crate::fuse_part::FusePartInfo;
use crate::io::BlockReader;
use crate::operations::read::native_data_source_deserializer::NativeDeserializeDataTransform;
use crate::operations::read::native_data_source_reader::ReadNativeDataSource;
Expand All @@ -41,7 +42,8 @@ pub fn build_fuse_native_source_pipeline(
topk: Option<TopK>,
mut max_io_requests: usize,
) -> Result<()> {
(max_threads, max_io_requests) = adjust_threads_and_request(max_threads, max_io_requests, plan);
(max_threads, max_io_requests) =
adjust_threads_and_request(true, max_threads, max_io_requests, plan);

if topk.is_some() {
max_threads = max_threads.min(16);
Expand Down Expand Up @@ -122,7 +124,8 @@ pub fn build_fuse_parquet_source_pipeline(
mut max_threads: usize,
mut max_io_requests: usize,
) -> Result<()> {
(max_threads, max_io_requests) = adjust_threads_and_request(max_threads, max_io_requests, plan);
(max_threads, max_io_requests) =
adjust_threads_and_request(false, max_threads, max_io_requests, plan);

let mut source_builder = SourcePipeBuilder::create();

Expand Down Expand Up @@ -220,12 +223,38 @@ pub fn dispatch_partitions(
}

pub fn adjust_threads_and_request(
is_native: bool,
mut max_threads: usize,
mut max_io_requests: usize,
plan: &DataSourcePlan,
) -> (usize, usize) {
if !plan.parts.is_lazy {
let block_nums = plan.parts.partitions.len().max(1);
let mut block_nums = plan.parts.partitions.len();

// If the read bytes of a partition is small enough, less than 16k rows
// we will not use an extra heavy thread to process it.
// now only works for native reader
static MIN_ROWS_READ_PER_THREAD: u64 = 16 * 1024;
if is_native {
plan.parts.partitions.iter().for_each(|part| {
if let Some(part) = part.as_any().downcast_ref::<FusePartInfo>() {
let to_read_rows = part
.columns_meta
.values()
.map(|meta| meta.read_rows(&part.range))
.find(|rows| *rows > 0)
.unwrap_or(part.nums_rows as u64);

if to_read_rows < MIN_ROWS_READ_PER_THREAD {
block_nums -= 1;
}
}
});
}

// At least max(1/8 of the original parts, 1), in case of too many small partitions but io threads is just one.
block_nums = std::cmp::max(block_nums, plan.parts.partitions.len() / 8);
block_nums = std::cmp::max(block_nums, 1);

max_threads = std::cmp::min(max_threads, block_nums);
max_io_requests = std::cmp::min(max_io_requests, block_nums);
Expand Down