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

Try split engine store helper and proxy helper(failed) #274

Open
wants to merge 8 commits into
base: raftstore-proxy
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ members = [
"fuzz/fuzzer-honggfuzz",
"fuzz/fuzzer-libfuzzer",
"gen-proxy-ffi",
"proxy_ffi",
"proxy_tests",
"raftstore-proxy",
"tests",
Expand Down Expand Up @@ -388,6 +389,7 @@ engine_store_ffi = { path = "engine_store_ffi", default-features = false }
new-mock-engine-store = { path = "new-mock-engine-store", default-features = false }
proxy_server = { path = "proxy_server", default-features = false }
engine_tiflash = { path = "engine_tiflash", default-features = false }
proxy_ffi = { path = "proxy_ffi", default-features = false }

[profile.dev.package.grpcio-sys]
debug = false
Expand Down
2 changes: 1 addition & 1 deletion engine_store_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ collections = { workspace = true }
crossbeam = "0.8"
derivative = "2"
encryption = { workspace = true, default-features = false }

engine_rocks = { workspace = true, default-features = false }
# Should be [dev-dependencies] but we need to control the features
# https://github.com/rust-lang/cargo/issues/6915
Expand Down Expand Up @@ -67,6 +66,7 @@ portable-atomic = "0.3"
prometheus = { version = "0.13", features = ["nightly"] }
prometheus-static-metric = "0.5"
protobuf = { version = "2.8", features = ["bytes"] }
proxy_ffi = { workspace = true, default-features = false }
raft = { version = "0.7.0", default-features = false, features = ["protobuf-codec"] }
raft-proto = { version = "0.7.0", default-features = false }
raftstore = { workspace = true, default-features = false }
Expand Down
31 changes: 5 additions & 26 deletions engine_store_ffi/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,17 @@
/// All mods end up with `_impls` impl structs defined in interface.
/// Other mods which define and impl structs should not end up with name
/// `_impls`.

#[allow(dead_code)]
pub mod interfaces;
// All ffi impls that without raft domain.
pub mod basic_ffi_impls;
// All ffi impls that within raft domain, but without proxy helper context.
pub mod domain_impls;
// All ffi impls that within proxy helper context.
pub mod context_impls;
pub mod encryption_impls;
// FFI directly related with EngineStoreServerHelper.
pub mod engine_store_helper_impls;
pub(crate) mod lock_cf_reader;
// FFI directly related with RaftStoreProxyFFIHelper.
pub mod encryption_impls;
pub mod raftstore_proxy;
pub mod raftstore_proxy_helper_impls;
pub mod sst_reader_impls;
pub mod read_index_helper;

pub use engine_tiflash::EngineStoreConfig;
pub use proxy_ffi::*;

pub use self::{
basic_ffi_impls::*, domain_impls::*, encryption_impls::*, engine_store_helper_impls::*,
interfaces::root::DB as interfaces_ffi, lock_cf_reader::*, raftstore_proxy_helper_impls::*,
encryption_impls::*, lock_cf_reader::*, raftstore_proxy::*, raftstore_proxy_helper_impls::*,
sst_reader_impls::*,
};

#[allow(clippy::wrong_self_convention)]
pub trait UnwrapExternCFunc<T> {
unsafe fn into_inner(&self) -> &T;
}

impl<T> UnwrapExternCFunc<T> for std::option::Option<T> {
unsafe fn into_inner(&self) -> &T {
std::mem::transmute::<&Self, &T>(self)
}
}
92 changes: 92 additions & 0 deletions engine_store_ffi/src/ffi/raftstore_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

use std::sync::{
atomic::{AtomicU8, Ordering},
Arc,
};

use encryption::DataKeyManager;
use engine_traits::Peekable;

use super::{
interfaces_ffi::{ConstRawVoidPtr, RaftProxyStatus, RaftStoreProxyPtr},
raftstore_proxy_helper_impls::*,
read_index_helper,
};
use crate::TiFlashEngine;

pub struct RaftStoreProxy {
pub status: AtomicU8,
pub key_manager: Option<Arc<DataKeyManager>>,
pub read_index_client: Option<Box<dyn read_index_helper::ReadIndex>>,
pub kv_engine: std::sync::RwLock<Option<TiFlashEngine>>,
}

impl RaftStoreProxy {
pub fn new(
status: AtomicU8,
key_manager: Option<Arc<DataKeyManager>>,
read_index_client: Option<Box<dyn read_index_helper::ReadIndex>>,
kv_engine: std::sync::RwLock<Option<TiFlashEngine>>,
) -> Self {
RaftStoreProxy {
status,
key_manager,
read_index_client,
kv_engine,
}
}
}

impl RaftStoreProxyFFI<TiFlashEngine> for RaftStoreProxy {
fn set_kv_engine(&mut self, kv_engine: Option<TiFlashEngine>) {
let mut lock = self.kv_engine.write().unwrap();
*lock = kv_engine;
}

fn set_status(&mut self, s: RaftProxyStatus) {
self.status.store(s as u8, Ordering::SeqCst);
}

fn get_value_cf<F>(&self, cf: &str, key: &[u8], cb: F)
where
F: FnOnce(Result<Option<&[u8]>, String>),
{
let kv_engine_lock = self.kv_engine.read().unwrap();
let kv_engine = kv_engine_lock.as_ref();
if kv_engine.is_none() {
cb(Err("KV engine is not initialized".to_string()));
return;
}
let value = kv_engine.unwrap().get_value_cf(cf, key);
match value {
Ok(v) => {
if let Some(x) = v {
cb(Ok(Some(&x)));
} else {
cb(Ok(None));
}
}
Err(e) => {
cb(Err(format!("{}", e)));
}
}
}
}

impl RaftStoreProxyPtr {
pub unsafe fn as_ref(&self) -> &RaftStoreProxy {
&*(self.inner as *const RaftStoreProxy)
}
pub fn is_null(&self) -> bool {
self.inner.is_null()
}
}

impl From<&RaftStoreProxy> for RaftStoreProxyPtr {
fn from(ptr: &RaftStoreProxy) -> Self {
Self {
inner: ptr as *const _ as ConstRawVoidPtr,
}
}
}
Loading