forked from GreptimeTeam/greptimedb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: define region server and related requests (GreptimeTeam#2160)
* define region server and related requests Signed-off-by: Ruihang Xia <[email protected]> * fill request body Signed-off-by: Ruihang Xia <[email protected]> * change mito2's request type Signed-off-by: Ruihang Xia <[email protected]> * fix clippy Signed-off-by: Ruihang Xia <[email protected]> * chore: bump greptime-proto to d9167cab (row insert/delete) Signed-off-by: Ruihang Xia <[email protected]> * fix test compile Signed-off-by: Ruihang Xia <[email protected]> * remove name_to_index Signed-off-by: Ruihang Xia <[email protected]> * address cr comments Signed-off-by: Ruihang Xia <[email protected]> * finilise Signed-off-by: Ruihang Xia <[email protected]> --------- Signed-off-by: Ruihang Xia <[email protected]>
- Loading branch information
Showing
25 changed files
with
479 additions
and
193 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,103 @@ | ||
// 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::collections::HashMap; | ||
|
||
use common_query::Output; | ||
use common_telemetry::info; | ||
use dashmap::DashMap; | ||
use snafu::{OptionExt, ResultExt}; | ||
use store_api::region_engine::RegionEngineRef; | ||
use store_api::region_request::RegionRequest; | ||
use store_api::storage::RegionId; | ||
|
||
use crate::error::{ | ||
HandleRegionRequestSnafu, RegionEngineNotFoundSnafu, RegionNotFoundSnafu, Result, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct RegionServer { | ||
engines: HashMap<String, RegionEngineRef>, | ||
region_map: DashMap<RegionId, RegionEngineRef>, | ||
} | ||
|
||
impl RegionServer { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
pub fn register_engine(&mut self, engine: RegionEngineRef) { | ||
let engine_name = engine.name(); | ||
self.engines.insert(engine_name.to_string(), engine); | ||
} | ||
|
||
pub async fn handle_request( | ||
&self, | ||
region_id: RegionId, | ||
request: RegionRequest, | ||
) -> Result<Output> { | ||
// TODO(ruihang): add some metrics | ||
|
||
let region_change = match &request { | ||
RegionRequest::Create(create) => RegionChange::Register(create.engine.clone()), | ||
RegionRequest::Open(open) => RegionChange::Register(open.engine.clone()), | ||
RegionRequest::Close(_) | RegionRequest::Drop(_) => RegionChange::Deregisters, | ||
RegionRequest::Write(_) | ||
| RegionRequest::Read(_) | ||
| RegionRequest::Delete(_) | ||
| RegionRequest::Alter(_) | ||
| RegionRequest::Flush(_) | ||
| RegionRequest::Compact(_) => RegionChange::None, | ||
}; | ||
|
||
let engine = match ®ion_change { | ||
RegionChange::Register(engine_type) => self | ||
.engines | ||
.get(engine_type) | ||
.with_context(|| RegionEngineNotFoundSnafu { name: engine_type })? | ||
.clone(), | ||
RegionChange::None | RegionChange::Deregisters => self | ||
.region_map | ||
.get(®ion_id) | ||
.with_context(|| RegionNotFoundSnafu { region_id })? | ||
.clone(), | ||
}; | ||
let engine_type = engine.name(); | ||
|
||
let result = engine | ||
.handle_request(region_id, request) | ||
.await | ||
.with_context(|_| HandleRegionRequestSnafu { region_id })?; | ||
|
||
match region_change { | ||
RegionChange::None => {} | ||
RegionChange::Register(_) => { | ||
info!("Region {region_id} is registered to engine {engine_type}"); | ||
self.region_map.insert(region_id, engine); | ||
} | ||
RegionChange::Deregisters => { | ||
info!("Region {region_id} is deregistered from engine {engine_type}"); | ||
self.region_map.remove(®ion_id); | ||
} | ||
} | ||
|
||
Ok(result) | ||
} | ||
} | ||
|
||
enum RegionChange { | ||
None, | ||
Register(String), | ||
Deregisters, | ||
} |
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
Oops, something went wrong.