-
Notifications
You must be signed in to change notification settings - Fork 477
feat: admin http api #1026
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
Merged
Merged
feat: admin http api #1026
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7733c14
feat: catalog list
jun0315 1b12f69
feat: catalog list
jun0315 6a674d7
feat:api
jun0315 fdea7ab
feat: leader info
jun0315 e950e03
feat: use constant
jun0315 b66dabb
fix: ci
jun0315 e773fb4
feat: query heartbeat by ip
jun0315 ed173fd
ut: add test
jun0315 769cee7
fix: cr
jun0315 f302773
fix: cr
jun0315 9aae0e4
fix: cr
jun0315 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,43 @@ | ||
| // 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 snafu::ResultExt; | ||
| use tonic::codegen::http; | ||
|
|
||
| use crate::error::{self, Result}; | ||
| use crate::metasrv::ElectionRef; | ||
| use crate::service::admin::HttpHandler; | ||
|
|
||
| pub struct LeaderHandler { | ||
| pub election: Option<ElectionRef>, | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl HttpHandler for LeaderHandler { | ||
| async fn handle(&self, _: &str, _: &HashMap<String, String>) -> Result<http::Response<String>> { | ||
| if let Some(election) = &self.election { | ||
| let leader_addr = election.leader().await?.0; | ||
| http::Response::builder() | ||
| .status(http::StatusCode::OK) | ||
| .body(leader_addr) | ||
| .context(error::InvalidHttpBodySnafu) | ||
| } else { | ||
| http::Response::builder() | ||
| .status(http::StatusCode::OK) | ||
| .body("election info is None".to_string()) | ||
| .context(error::InvalidHttpBodySnafu) | ||
| } | ||
|
jun0315 marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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,212 @@ | ||
| // 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 api::v1::meta::{RangeRequest, RangeResponse}; | ||
| use catalog::helper::{CATALOG_KEY_PREFIX, SCHEMA_KEY_PREFIX, TABLE_GLOBAL_KEY_PREFIX}; | ||
| use snafu::ResultExt; | ||
| use tonic::codegen::http; | ||
|
|
||
| use crate::error::Result; | ||
| use crate::service::admin::HttpHandler; | ||
| use crate::service::store::ext::KvStoreExt; | ||
| use crate::service::store::kv::KvStoreRef; | ||
| use crate::{error, util}; | ||
|
|
||
| pub struct CatalogsHandler { | ||
| pub kv_store: KvStoreRef, | ||
| } | ||
|
|
||
| pub struct SchemasHandler { | ||
| pub kv_store: KvStoreRef, | ||
| } | ||
|
|
||
| pub struct TablesHandler { | ||
| pub kv_store: KvStoreRef, | ||
| } | ||
|
|
||
| pub struct TableHandler { | ||
| pub kv_store: KvStoreRef, | ||
| } | ||
|
shuiyisong marked this conversation as resolved.
|
||
|
|
||
| #[async_trait::async_trait] | ||
| impl HttpHandler for CatalogsHandler { | ||
| async fn handle(&self, _: &str, _: &HashMap<String, String>) -> Result<http::Response<String>> { | ||
| get_http_response_by_prefix(String::from(CATALOG_KEY_PREFIX), &self.kv_store).await | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl HttpHandler for SchemasHandler { | ||
| async fn handle( | ||
| &self, | ||
| _: &str, | ||
| params: &HashMap<String, String>, | ||
| ) -> Result<http::Response<String>> { | ||
| let catalog = match params.get("catalog_name") { | ||
| Some(catalog) => catalog, | ||
| None => { | ||
| return error::MissingRequiredParameterSnafu { | ||
| param: "catalog_name", | ||
| } | ||
| .fail(); | ||
| } | ||
| }; | ||
|
jun0315 marked this conversation as resolved.
Outdated
|
||
| let prefix = format!("{SCHEMA_KEY_PREFIX}-{catalog}",); | ||
| get_http_response_by_prefix(prefix, &self.kv_store).await | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl HttpHandler for TablesHandler { | ||
| async fn handle( | ||
| &self, | ||
| _: &str, | ||
| params: &HashMap<String, String>, | ||
| ) -> Result<http::Response<String>> { | ||
| let catalog = match params.get("catalog_name") { | ||
| Some(catalog) => catalog, | ||
| None => { | ||
| return error::MissingRequiredParameterSnafu { | ||
| param: "catalog_name", | ||
| } | ||
| .fail(); | ||
| } | ||
| }; | ||
|
|
||
| let schema = match params.get("schema_name") { | ||
| Some(schema) => schema, | ||
| None => { | ||
| return error::MissingRequiredParameterSnafu { | ||
| param: "schema_name", | ||
| } | ||
| .fail(); | ||
| } | ||
| }; | ||
|
jun0315 marked this conversation as resolved.
Outdated
|
||
| let prefix = format!("{TABLE_GLOBAL_KEY_PREFIX}-{catalog}-{schema}",); | ||
| get_http_response_by_prefix(prefix, &self.kv_store).await | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl HttpHandler for TableHandler { | ||
| async fn handle( | ||
| &self, | ||
| _: &str, | ||
| params: &HashMap<String, String>, | ||
| ) -> Result<http::Response<String>> { | ||
| let table_name = match params.get("full_table_name") { | ||
| Some(full_table_name) => full_table_name.replace('.', "-"), | ||
| None => { | ||
| return error::MissingRequiredParameterSnafu { | ||
| param: "full_table_name", | ||
| } | ||
| .fail(); | ||
| } | ||
| }; | ||
|
jun0315 marked this conversation as resolved.
Outdated
|
||
| let table_key = format!("{TABLE_GLOBAL_KEY_PREFIX}-{table_name}"); | ||
|
|
||
| let response = self.kv_store.get(table_key.into_bytes()).await?; | ||
| let mut value: String = "Not found result".to_string(); | ||
| if let Some(key_value) = response { | ||
| value = String::from_utf8(key_value.value).context(error::InvalidUtf8ValueSnafu)?; | ||
| } | ||
| http::Response::builder() | ||
| .status(http::StatusCode::OK) | ||
| .body(value) | ||
| .context(error::InvalidHttpBodySnafu) | ||
| } | ||
| } | ||
|
|
||
| /// Get kv_store's key list with http response format by prefix key | ||
| async fn get_http_response_by_prefix( | ||
| key_prefix: String, | ||
| kv_store: &KvStoreRef, | ||
| ) -> Result<http::Response<String>> { | ||
| let keys = get_keys_by_prefix(key_prefix, kv_store).await?; | ||
| let body = serde_json::to_string(&keys).context(error::SerializeToJsonSnafu { | ||
| input: format!("{keys:?}"), | ||
| })?; | ||
|
|
||
| http::Response::builder() | ||
| .status(http::StatusCode::OK) | ||
| .body(body) | ||
| .context(error::InvalidHttpBodySnafu) | ||
| } | ||
|
|
||
| /// Get kv_store's key list by prefix key | ||
| async fn get_keys_by_prefix(key_prefix: String, kv_store: &KvStoreRef) -> Result<Vec<String>> { | ||
| let key_prefix_u8 = key_prefix.clone().into_bytes(); | ||
| let range_end = util::get_prefix_end_key(&key_prefix_u8); | ||
| let req = RangeRequest { | ||
| key: key_prefix_u8, | ||
| range_end, | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let response: RangeResponse = kv_store.range(req).await?; | ||
|
|
||
| let kvs = response.kvs; | ||
| let mut values = Vec::with_capacity(kvs.len()); | ||
| for kv in kvs { | ||
| let value = String::from_utf8(kv.key).context(error::InvalidUtf8ValueSnafu)?; | ||
| let split_list = value.split(&key_prefix).collect::<Vec<&str>>(); | ||
| if let Some(v) = split_list.get(1) { | ||
| values.push(v.to_string()); | ||
| } | ||
| } | ||
| Ok(values) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use api::v1::meta::PutRequest; | ||
|
|
||
| use crate::service::admin::meta::get_keys_by_prefix; | ||
| use crate::service::store::kv::KvStoreRef; | ||
| use crate::service::store::memory::MemStore; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_get_list_by_prefix() { | ||
| let in_mem = Arc::new(MemStore::new()) as KvStoreRef; | ||
|
|
||
| in_mem | ||
| .put(PutRequest { | ||
| key: "test_key1".as_bytes().to_vec(), | ||
| value: "test_val1".as_bytes().to_vec(), | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| in_mem | ||
| .put(PutRequest { | ||
| key: "test_key2".as_bytes().to_vec(), | ||
| value: "test_val2".as_bytes().to_vec(), | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let keys = get_keys_by_prefix(String::from("test_key"), &in_mem) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| assert_eq!("1", keys[0]); | ||
| assert_eq!("2", keys[1]); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.