Skip to content
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

enhance interface fn_handle_http_request & add interface fn_get_config #34

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions components/raftstore/src/engine_store_ffi/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ pub mod root {
pub fn_handle_http_request: ::std::option::Option<
unsafe extern "C" fn(
arg1: *mut root::DB::EngineStoreServerWrap,
arg2: root::DB::BaseBuffView,
path: root::DB::BaseBuffView,
query: root::DB::BaseBuffView,
body: root::DB::BaseBuffView,
) -> root::DB::HttpRequestRes,
>,
pub fn_check_http_uri_available:
Expand All @@ -355,8 +357,14 @@ pub mod root {
pub fn_set_server_info_resp: ::std::option::Option<
unsafe extern "C" fn(arg1: root::DB::BaseBuffView, arg2: root::DB::RawVoidPtr),
>,
pub fn_get_config: ::std::option::Option<
unsafe extern "C" fn(
arg1: *mut root::DB::EngineStoreServerWrap,
full: u8,
) -> root::DB::CppStrWithView,
>,
}
pub const RAFT_STORE_PROXY_VERSION: u64 = 2118434012412631151;
pub const RAFT_STORE_PROXY_VERSION: u64 = 17680868848344786018;
pub const RAFT_STORE_PROXY_MAGIC_NUMBER: u32 = 324508639;
}
}
30 changes: 28 additions & 2 deletions components/raftstore/src/engine_store_ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,30 @@ impl EngineStoreServerHelper {
}
}

pub fn handle_http_request(&self, path: &str) -> HttpRequestRes {
pub fn handle_http_request(
&self,
path: &str,
query: Option<&str>,
body: &[u8],
) -> HttpRequestRes {
debug_assert!(self.fn_handle_http_request.is_some());

unsafe { (self.fn_handle_http_request.into_inner())(self.inner, path.as_bytes().into()) }
let query = if let Some(s) = query {
s.as_bytes().into()
} else {
BaseBuffView {
data: std::ptr::null(),
len: 0,
}
};
unsafe {
(self.fn_handle_http_request.into_inner())(
self.inner,
path.as_bytes().into(),
query,
body.into(),
)
}
}

pub fn check_http_uri_available(&self, path: &str) -> bool {
Expand All @@ -752,6 +772,12 @@ impl EngineStoreServerHelper {

unsafe { (self.fn_set_server_info_resp.into_inner())(res, ptr) }
}

pub fn get_config(&self, full: bool) -> Vec<u8> {
debug_assert!(self.fn_get_config.is_some());
let config = unsafe { (self.fn_get_config.into_inner())(self.inner, full.into()) };
config.view.to_slice().to_vec()
}
}

impl Clone for SSTReaderPtr {
Expand Down
1 change: 1 addition & 0 deletions mock-engine-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub fn gen_engine_store_server_helper(
fn_gc_raw_cpp_ptr: Some(ffi_gc_raw_cpp_ptr),
fn_insert_batch_read_index_resp: None,
fn_set_server_info_resp: None,
fn_get_config: None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion raftstore-proxy/ffi/src/RaftStoreProxyFFI/@version
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once
#include <cstdint>
namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 2118434012412631151ull; }
namespace DB { constexpr uint64_t RAFT_STORE_PROXY_VERSION = 17680868848344786018ull; }
5 changes: 4 additions & 1 deletion raftstore-proxy/ffi/src/RaftStoreProxyFFI/ProxyFFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ struct EngineStoreServerHelper {
void (*fn_apply_pre_handled_snapshot)(EngineStoreServerWrap *, RawVoidPtr,
RawCppPtrType);
HttpRequestRes (*fn_handle_http_request)(EngineStoreServerWrap *,
BaseBuffView);
BaseBuffView path,
BaseBuffView query,
BaseBuffView body);
uint8_t (*fn_check_http_uri_available)(BaseBuffView);
void (*fn_gc_raw_cpp_ptr)(RawVoidPtr, RawCppPtrType);
void (*fn_insert_batch_read_index_resp)(RawVoidPtr, BaseBuffView, uint64_t);
void (*fn_set_server_info_resp)(BaseBuffView, RawVoidPtr);
CppStrWithView (*fn_get_config)(EngineStoreServerWrap *, uint8_t full);
};
} // namespace DB
72 changes: 50 additions & 22 deletions src/server/status_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ where
async fn get_config(
req: Request<Body>,
cfg_controller: &ConfigController,
engine_store_server_helper: &'static raftstore::engine_store_ffi::EngineStoreServerHelper,
) -> hyper::Result<Response<Body>> {
let mut full = false;
if let Some(query) = req.uri().query() {
Expand All @@ -279,12 +280,30 @@ where
// Filter hidden config
serde_json::to_string(&cfg_controller.get_current().get_encoder())
};
Ok(match encode_res {
Ok(json) => Response::builder()

let engine_store_config = engine_store_server_helper.get_config(full);
let engine_store_config =
unsafe { String::from_utf8_unchecked(engine_store_config) }.parse::<toml::Value>();

let engine_store_config = match engine_store_config {
Ok(c) => serde_json::to_string(&c),
Err(e) => {
return Ok(StatusServer::err_response(
StatusCode::INTERNAL_SERVER_ERROR,
"Internal Server Error: fail to parse config from engine-store",
));
}
};

Ok(match (encode_res, engine_store_config) {
(Ok(json), Ok(store_config)) => Response::builder()
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(json))
.body(Body::from(format!(
"{{\"raftstore-proxy\":{},\"engine-store\":{}}}",
json, store_config,
)))
.unwrap(),
Err(_) => StatusServer::err_response(
_ => StatusServer::err_response(
StatusCode::INTERNAL_SERVER_ERROR,
"Internal Server Error",
),
Expand Down Expand Up @@ -696,25 +715,33 @@ where
req: Request<Body>,
engine_store_server_helper: &'static raftstore::engine_store_ffi::EngineStoreServerHelper,
) -> hyper::Result<Response<Body>> {
fn err_resp(
status_code: StatusCode,
msg: impl Into<Body>,
) -> hyper::Result<Response<Body>> {
Ok(StatusServer::err_response(status_code, msg))
}

let res = engine_store_server_helper.handle_http_request(req.uri().path());
if res.status != raftstore::engine_store_ffi::HttpRequestStatus::Ok {
return err_resp(
StatusCode::BAD_REQUEST,
format!("error uri path: {}", req.uri().path()),
);
}
let (head, body) = req.into_parts();
let body = hyper::body::to_bytes(body).await;

match body {
Ok(s) => {
let res = engine_store_server_helper.handle_http_request(
head.uri.path(),
head.uri.query(),
&s,
);
if res.status != raftstore::engine_store_ffi::HttpRequestStatus::Ok {
return Ok(StatusServer::err_response(
StatusCode::INTERNAL_SERVER_ERROR,
"engine-store fails to build response".to_string(),
));
}

let data = res.res.view.to_slice().to_vec();
let data = res.res.view.to_slice().to_vec();

match Response::builder().body(hyper::Body::from(data)) {
Ok(resp) => Ok(resp),
match Response::builder().body(hyper::Body::from(data)) {
Ok(resp) => Ok(resp),
Err(err) => Ok(StatusServer::err_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("fails to build response: {}", err),
)),
}
}
Err(err) => Ok(StatusServer::err_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("fails to build response: {}", err),
Expand Down Expand Up @@ -782,7 +809,8 @@ where
Self::dump_prof_to_resp(req).await
}
(Method::GET, "/config") => {
Self::get_config(req, &cfg_controller).await
Self::get_config(req, &cfg_controller, engine_store_server_helper)
.await
}
(Method::POST, "/config") => {
Self::update_config(cfg_controller.clone(), req).await
Expand Down