-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: able to handle concurrent region edit requests
- Loading branch information
1 parent
93be81c
commit 7f083ed
Showing
6 changed files
with
227 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use object_store::ObjectStore; | ||
use store_api::region_engine::RegionEngine; | ||
use store_api::region_request::RegionRequest; | ||
use store_api::storage::RegionId; | ||
use tokio::sync::Semaphore; | ||
use tokio::task::JoinSet; | ||
|
||
use crate::config::MitoConfig; | ||
use crate::engine::MitoEngine; | ||
use crate::manifest::action::RegionEdit; | ||
use crate::region::MitoRegionRef; | ||
use crate::sst::file::{FileId, FileMeta}; | ||
use crate::test_util::{CreateRequestBuilder, TestEnv}; | ||
|
||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)] | ||
async fn test_edit_region_concurrently() { | ||
const EDITS_PER_TASK: usize = 10; | ||
let tasks_count = 10; | ||
|
||
// A task that creates SST files and edits the region with them. | ||
struct Task { | ||
region: MitoRegionRef, | ||
ssts: Vec<FileMeta>, | ||
} | ||
|
||
impl Task { | ||
async fn create_ssts(&mut self, object_store: &ObjectStore) { | ||
for _ in 0..EDITS_PER_TASK { | ||
let file = FileMeta { | ||
region_id: self.region.region_id, | ||
file_id: FileId::random(), | ||
level: 0, | ||
..Default::default() | ||
}; | ||
object_store | ||
.write( | ||
&format!("{}/{}.parquet", self.region.region_dir(), file.file_id), | ||
b"x".as_slice(), | ||
) | ||
.await | ||
.unwrap(); | ||
self.ssts.push(file); | ||
} | ||
} | ||
|
||
async fn edit_region(self, engine: MitoEngine) { | ||
for sst in self.ssts { | ||
let edit = RegionEdit { | ||
files_to_add: vec![sst], | ||
files_to_remove: vec![], | ||
compaction_time_window: None, | ||
flushed_entry_id: None, | ||
flushed_sequence: None, | ||
}; | ||
engine | ||
.edit_region(self.region.region_id, edit) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
} | ||
|
||
let mut env = TestEnv::new(); | ||
let engine = env.create_engine(MitoConfig::default()).await; | ||
|
||
let region_id = RegionId::new(1, 1); | ||
engine | ||
.handle_request( | ||
region_id, | ||
RegionRequest::Create(CreateRequestBuilder::new().build()), | ||
) | ||
.await | ||
.unwrap(); | ||
let region = engine.get_region(region_id).unwrap(); | ||
|
||
let mut tasks = Vec::with_capacity(tasks_count); | ||
let object_store = env.get_object_store().unwrap(); | ||
for _ in 0..tasks_count { | ||
let mut task = Task { | ||
region: region.clone(), | ||
ssts: Vec::new(), | ||
}; | ||
task.create_ssts(&object_store).await; | ||
tasks.push(task); | ||
} | ||
|
||
let mut join_set = JoinSet::new(); | ||
// This semaphore is used to coordinate the tasks, making them started at roughly the same time. | ||
let s = Arc::new(Semaphore::new(0)); | ||
for task in tasks { | ||
join_set.spawn({ | ||
let s = s.clone(); | ||
let engine = engine.clone(); | ||
async move { | ||
let _permit = s.acquire().await.unwrap(); | ||
task.edit_region(engine).await; | ||
} | ||
}); | ||
} | ||
s.add_permits(tasks_count); | ||
while join_set.join_next().await.is_some() {} | ||
|
||
assert_eq!( | ||
region.version().ssts.levels()[0].files.len(), | ||
tasks_count * EDITS_PER_TASK | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters