Skip to content

Commit 6f07d69

Browse files
zhongzcevenyag
andauthored
feat(mito): enable inverted index (#3158)
* feat(mito): enable inverted index Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * fix typos Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * fix typos Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * accidentally resolved the incorrect filtering issue within the Metric Engine Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * fix test Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * Update src/mito2/src/access_layer.rs * Update src/mito2/src/test_util/scheduler_util.rs Co-authored-by: Yingwen <realevenyag@gmail.com> * fix: format -> join_dir Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * refactor: move intermediate_manager from arg of write_and_upload_sst to field of WriteCache Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * refactor: add IndexerBuidler Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> * fix clippy Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> --------- Signed-off-by: Zhenchi <zhongzc_arch@outlook.com> Co-authored-by: Yingwen <realevenyag@gmail.com>
1 parent 816d948 commit 6f07d69

34 files changed

Lines changed: 916 additions & 235 deletions

config/datanode.example.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ parallel_scan_channel_size = 32
116116
# Whether to allow stale WAL entries read during replay.
117117
allow_stale_entries = false
118118

119+
[region_engine.mito.inverted_index]
120+
# Whether to create the index on flush.
121+
# - "auto": automatically
122+
# - "disable": never
123+
create_on_flush = "auto"
124+
# Whether to create the index on compaction.
125+
# - "auto": automatically
126+
# - "disable": never
127+
create_on_compaction = "auto"
128+
# Whether to apply the index on query
129+
# - "auto": automatically
130+
# - "disable": never
131+
apply_on_query = "auto"
132+
# Memory threshold for performing an external sort during index creation.
133+
# Setting to empty will disable external sorting, forcing all sorting operations to happen in memory.
134+
mem_threshold_on_create = "64MB"
135+
# File system path to store intermediate files for external sorting (default `{data_home}/index_intermediate`).
136+
intermediate_path = ""
137+
119138
# Log options, see `standalone.example.toml`
120139
# [logging]
121140
# dir = "/tmp/greptimedb/logs"

config/standalone.example.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,25 @@ parallel_scan_channel_size = 32
216216
# Whether to allow stale WAL entries read during replay.
217217
allow_stale_entries = false
218218

219+
[region_engine.mito.inverted_index]
220+
# Whether to create the index on flush.
221+
# - "auto": automatically
222+
# - "disable": never
223+
create_on_flush = "auto"
224+
# Whether to create the index on compaction.
225+
# - "auto": automatically
226+
# - "disable": never
227+
create_on_compaction = "auto"
228+
# Whether to apply the index on query
229+
# - "auto": automatically
230+
# - "disable": never
231+
apply_on_query = "auto"
232+
# Memory threshold for performing an external sort during index creation.
233+
# Setting to empty will disable external sorting, forcing all sorting operations to happen in memory.
234+
mem_threshold_on_create = "64M"
235+
# File system path to store intermediate files for external sorting (default `{data_home}/index_intermediate`).
236+
intermediate_path = ""
237+
219238
# Log options
220239
# [logging]
221240
# Specify logs directory.

src/datanode/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ impl DatanodeOptions {
284284
}
285285
}
286286

287+
#[allow(clippy::large_enum_variant)]
287288
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
288289
pub enum RegionEngineConfig {
289290
#[serde(rename = "mito")]

src/datanode/src/datanode.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use metric_engine::engine::MetricEngine;
4141
use mito2::config::MitoConfig;
4242
use mito2::engine::MitoEngine;
4343
use object_store::manager::{ObjectStoreManager, ObjectStoreManagerRef};
44-
use object_store::util::{join_dir, normalize_dir};
44+
use object_store::util::normalize_dir;
4545
use query::QueryEngineFactory;
4646
use servers::export_metrics::ExportMetricsTask;
4747
use servers::server::{start_server, ServerHandlers};
@@ -374,27 +374,21 @@ impl DatanodeBuilder {
374374
async fn build_mito_engine(
375375
opts: &DatanodeOptions,
376376
object_store_manager: ObjectStoreManagerRef,
377-
mut config: MitoConfig,
377+
config: MitoConfig,
378378
) -> Result<MitoEngine> {
379-
// Sets write cache path if it is empty.
380-
if config.experimental_write_cache_path.is_empty() {
381-
config.experimental_write_cache_path = join_dir(&opts.storage.data_home, "write_cache");
382-
info!(
383-
"Sets write cache path to {}",
384-
config.experimental_write_cache_path
385-
);
386-
}
387-
388379
let mito_engine = match &opts.wal {
389380
WalConfig::RaftEngine(raft_engine_config) => MitoEngine::new(
381+
&opts.storage.data_home,
390382
config,
391383
Self::build_raft_engine_log_store(&opts.storage.data_home, raft_engine_config)
392384
.await?,
393385
object_store_manager,
394386
)
395387
.await
396388
.context(BuildMitoEngineSnafu)?,
389+
397390
WalConfig::Kafka(kafka_config) => MitoEngine::new(
391+
&opts.storage.data_home,
398392
config,
399393
Self::build_kafka_log_store(kafka_config).await?,
400394
object_store_manager,

src/index/src/inverted_index/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::inverted_index::BytesRef;
2323

2424
/// `InvertedIndexCreator` provides functionality to construct an inverted index
2525
#[async_trait]
26-
pub trait InvertedIndexCreator {
26+
pub trait InvertedIndexCreator: Send {
2727
/// Adds a value to the named index. A `None` value represents an absence of data (null)
2828
///
2929
/// - `index_name`: Identifier for the index being built

src/index/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@
1313
// limitations under the License.
1414

1515
#![feature(iter_partition_in_place)]
16-
// TODO(zhongzc): remove once further code is added
17-
#![allow(dead_code)]
1816

1917
pub mod inverted_index;

src/mito2/src/access_layer.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use crate::cache::CacheManagerRef;
2525
use crate::error::{CleanDirSnafu, DeleteIndexSnafu, DeleteSstSnafu, OpenDalSnafu, Result};
2626
use crate::read::Source;
2727
use crate::sst::file::{FileHandle, FileId, FileMeta};
28+
use crate::sst::index::intermediate::IntermediateManager;
29+
use crate::sst::index::IndexerBuilder;
2830
use crate::sst::location;
2931
use crate::sst::parquet::reader::ParquetReaderBuilder;
3032
use crate::sst::parquet::writer::ParquetWriter;
@@ -37,6 +39,8 @@ pub struct AccessLayer {
3739
region_dir: String,
3840
/// Target object store.
3941
object_store: ObjectStore,
42+
/// Intermediate manager for inverted index.
43+
intermediate_manager: IntermediateManager,
4044
}
4145

4246
impl std::fmt::Debug for AccessLayer {
@@ -49,10 +53,15 @@ impl std::fmt::Debug for AccessLayer {
4953

5054
impl AccessLayer {
5155
/// Returns a new [AccessLayer] for specific `region_dir`.
52-
pub fn new(region_dir: impl Into<String>, object_store: ObjectStore) -> AccessLayer {
56+
pub fn new(
57+
region_dir: impl Into<String>,
58+
object_store: ObjectStore,
59+
intermediate_manager: IntermediateManager,
60+
) -> AccessLayer {
5361
AccessLayer {
5462
region_dir: region_dir.into(),
5563
object_store,
64+
intermediate_manager,
5665
}
5766
}
5867

@@ -105,16 +114,15 @@ impl AccessLayer {
105114
let file_path = location::sst_file_path(&self.region_dir, request.file_id);
106115
let index_file_path = location::index_file_path(&self.region_dir, request.file_id);
107116
let region_id = request.metadata.region_id;
117+
let file_id = request.file_id;
118+
let cache_manager = request.cache_manager.clone();
108119

109-
let sst_info = if let Some(write_cache) = request.cache_manager.write_cache() {
120+
let sst_info = if let Some(write_cache) = cache_manager.write_cache() {
110121
// Write to the write cache.
111122
write_cache
112123
.write_and_upload_sst(
124+
request,
113125
SstUploadRequest {
114-
file_id: request.file_id,
115-
metadata: request.metadata,
116-
source: request.source,
117-
storage: request.storage,
118126
upload_path: file_path,
119127
index_upload_path: index_file_path,
120128
remote_store: self.object_store.clone(),
@@ -124,19 +132,30 @@ impl AccessLayer {
124132
.await?
125133
} else {
126134
// Write cache is disabled.
127-
let mut writer =
128-
ParquetWriter::new(file_path, request.metadata, self.object_store.clone());
135+
let indexer = IndexerBuilder {
136+
create_inverted_index: request.create_inverted_index,
137+
mem_threshold_index_create: request.mem_threshold_index_create,
138+
file_id,
139+
file_path: index_file_path,
140+
metadata: &request.metadata,
141+
row_group_size: write_opts.row_group_size,
142+
object_store: self.object_store.clone(),
143+
intermediate_manager: self.intermediate_manager.clone(),
144+
}
145+
.build();
146+
let mut writer = ParquetWriter::new(
147+
file_path,
148+
request.metadata,
149+
self.object_store.clone(),
150+
indexer,
151+
);
129152
writer.write_all(request.source, write_opts).await?
130153
};
131154

132155
// Put parquet metadata to cache manager.
133156
if let Some(sst_info) = &sst_info {
134157
if let Some(parquet_metadata) = &sst_info.file_metadata {
135-
request.cache_manager.put_parquet_meta_data(
136-
region_id,
137-
request.file_id,
138-
parquet_metadata.clone(),
139-
)
158+
cache_manager.put_parquet_meta_data(region_id, file_id, parquet_metadata.clone())
140159
}
141160
}
142161

@@ -150,7 +169,12 @@ pub(crate) struct SstWriteRequest {
150169
pub(crate) metadata: RegionMetadataRef,
151170
pub(crate) source: Source,
152171
pub(crate) cache_manager: CacheManagerRef,
172+
#[allow(dead_code)]
153173
pub(crate) storage: Option<String>,
174+
/// Whether to create inverted index.
175+
pub(crate) create_inverted_index: bool,
176+
/// The threshold of memory size to create inverted index.
177+
pub(crate) mem_threshold_index_create: Option<usize>,
154178
}
155179

156180
/// Creates a fs object store with atomic write dir.

0 commit comments

Comments
 (0)