Skip to content

Commit

Permalink
refactor: pass http method to metasrv http handler (#3667)
Browse files Browse the repository at this point in the history
* refactor: pass http method to metasrc http handler

Signed-off-by: tison <wander4096@gmail.com>

* update maintenance endpoint

Signed-off-by: tison <wander4096@gmail.com>

* fixup

Signed-off-by: tison <wander4096@gmail.com>

* Update src/meta-srv/src/service/admin.rs

Co-authored-by: dennis zhuang <killme2008@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
Co-authored-by: dennis zhuang <killme2008@gmail.com>
  • Loading branch information
tisonkun and killme2008 authored Apr 9, 2024
1 parent 183fccb commit 2896e1f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 21 deletions.
40 changes: 28 additions & 12 deletions src/meta-srv/src/service/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ pub fn make_admin_service(meta_srv: MetaSrv) -> Admin {
};
let router = router.route("/region-migration", handler);

let handler = maintenance::MaintenanceHandler {
kv_backend: meta_srv.kv_backend().clone(),
};
let router = router
.route("/maintenance", handler.clone())
.route("/maintenance/set", handler);

let router = router.route(
"/maintenance",
maintenance::MaintenanceHandler {
kv_backend: meta_srv.kv_backend().clone(),
},
);
let router = Router::nest("/admin", router);

Admin::new(router)
Expand All @@ -115,6 +114,7 @@ pub trait HttpHandler: Send + Sync {
async fn handle(
&self,
path: &str,
method: http::Method,
params: &HashMap<String, String>,
) -> crate::Result<http::Response<String>>;
}
Expand Down Expand Up @@ -163,7 +163,8 @@ where
})
.unwrap_or_default();
let path = req.uri().path().to_owned();
Box::pin(async move { router.call(&path, query_params).await })
let method = req.method().clone();
Box::pin(async move { router.call(&path, method, query_params).await })
}
}

Expand Down Expand Up @@ -202,6 +203,7 @@ impl Router {
pub async fn call(
&self,
path: &str,
method: http::Method,
params: HashMap<String, String>,
) -> Result<http::Response<BoxBody>, Infallible> {
let handler = match self.handlers.get(path) {
Expand All @@ -214,7 +216,7 @@ impl Router {
}
};

let res = match handler.handle(path, &params).await {
let res = match handler.handle(path, method, &params).await {
Ok(res) => res.map(boxed),
Err(e) => http::Response::builder()
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
Expand Down Expand Up @@ -250,6 +252,7 @@ mod tests {
async fn handle(
&self,
_: &str,
_: http::Method,
_: &HashMap<String, String>,
) -> crate::Result<http::Response<String>> {
Ok(http::Response::builder()
Expand All @@ -265,6 +268,7 @@ mod tests {
async fn handle(
&self,
_: &str,
_: http::Method,
_: &HashMap<String, String>,
) -> crate::Result<http::Response<String>> {
error::EmptyKeySnafu {}.fail()
Expand Down Expand Up @@ -300,7 +304,11 @@ mod tests {
let router = Router::nest("/test_root", router);

let res = router
.call("/test_root/test_node", HashMap::default())
.call(
"/test_root/test_node",
http::Method::GET,
HashMap::default(),
)
.await
.unwrap();

Expand All @@ -312,7 +320,11 @@ mod tests {
let router = Router::new();

let res = router
.call("/test_root/test_node", HashMap::default())
.call(
"/test_root/test_node",
http::Method::GET,
HashMap::default(),
)
.await
.unwrap();

Expand All @@ -326,7 +338,11 @@ mod tests {
let router = Router::nest("/test_root", router);

let res = router
.call("/test_root/test_node", HashMap::default())
.call(
"/test_root/test_node",
http::Method::GET,
HashMap::default(),
)
.await
.unwrap();

Expand Down
12 changes: 10 additions & 2 deletions src/meta-srv/src/service/admin/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ pub struct HealthHandler;

#[async_trait::async_trait]
impl HttpHandler for HealthHandler {
async fn handle(&self, _: &str, _: &HashMap<String, String>) -> Result<http::Response<String>> {
async fn handle(
&self,
_: &str,
_: http::Method,
_: &HashMap<String, String>,
) -> Result<http::Response<String>> {
Ok(http::Response::builder()
.status(http::StatusCode::OK)
.body(HTTP_OK.to_owned())
Expand All @@ -42,7 +47,10 @@ mod tests {
let health_handler = HealthHandler {};
let path = "any";
let params = HashMap::default();
let res = health_handler.handle(path, &params).await.unwrap();
let res = health_handler
.handle(path, http::Method::GET, &params)
.await
.unwrap();

assert!(res.status().is_success());
assert_eq!(HTTP_OK.to_owned(), res.body().clone());
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/src/service/admin/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl HttpHandler for HeartBeatHandler {
async fn handle(
&self,
path: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
if path.ends_with("/help") {
Expand Down
7 changes: 6 additions & 1 deletion src/meta-srv/src/service/admin/leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ pub struct LeaderHandler {

#[async_trait::async_trait]
impl HttpHandler for LeaderHandler {
async fn handle(&self, _: &str, _: &HashMap<String, String>) -> Result<http::Response<String>> {
async fn handle(
&self,
_: &str,
_: http::Method,
_: &HashMap<String, String>,
) -> Result<http::Response<String>> {
if let Some(election) = &self.election {
let leader_addr = election.leader().await?.0;
return http::Response::builder()
Expand Down
15 changes: 10 additions & 5 deletions src/meta-srv/src/service/admin/maintenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tonic::codegen::http::Response;

use crate::error::{
InvalidHttpBodySnafu, KvBackendSnafu, MissingRequiredParameterSnafu, ParseBoolSnafu,
UnsupportedSnafu,
};
use crate::service::admin::HttpHandler;

Expand Down Expand Up @@ -91,13 +92,17 @@ impl MaintenanceHandler {
impl HttpHandler for MaintenanceHandler {
async fn handle(
&self,
path: &str,
_: &str,
method: http::Method,
params: &HashMap<String, String>,
) -> crate::Result<Response<String>> {
if path.ends_with("/set") {
self.set_maintenance(params).await
} else {
self.get_maintenance().await
match method {
http::Method::GET => self.get_maintenance().await,
http::Method::PUT => self.set_maintenance(params).await,
_ => UnsupportedSnafu {
operation: format!("http method {method}"),
}
.fail(),
}
}
}
10 changes: 9 additions & 1 deletion src/meta-srv/src/service/admin/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ pub struct TableHandler {

#[async_trait::async_trait]
impl HttpHandler for CatalogsHandler {
async fn handle(&self, _: &str, _: &HashMap<String, String>) -> Result<http::Response<String>> {
async fn handle(
&self,
_: &str,
_: http::Method,
_: &HashMap<String, String>,
) -> Result<http::Response<String>> {
let stream = self
.table_metadata_manager
.catalog_manager()
Expand All @@ -69,6 +74,7 @@ impl HttpHandler for SchemasHandler {
async fn handle(
&self,
path: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
if path.ends_with("/help") {
Expand Down Expand Up @@ -100,6 +106,7 @@ impl HttpHandler for TablesHandler {
async fn handle(
&self,
path: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
if path.ends_with("/help") {
Expand Down Expand Up @@ -135,6 +142,7 @@ impl HttpHandler for TableHandler {
async fn handle(
&self,
path: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
if path.ends_with("/help") {
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/src/service/admin/node_lease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl HttpHandler for NodeLeaseHandler {
async fn handle(
&self,
_: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
let cluster_id = util::extract_cluster_id(params)?;
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/src/service/admin/region_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl HttpHandler for SubmitRegionMigrationTaskHandler {
async fn handle(
&self,
_: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
let request = SubmitRegionMigrationTaskRequest::try_from(params)?;
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/src/service/admin/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl HttpHandler for RouteHandler {
async fn handle(
&self,
path: &str,
_: http::Method,
params: &HashMap<String, String>,
) -> Result<http::Response<String>> {
if path.ends_with("/help") {
Expand Down

0 comments on commit 2896e1f

Please sign in to comment.