forked from rust-rocksdb/rust-rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
feat(eventlistener) support eventlistener interface #1
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
Open
ruojieranyishen
wants to merge
4
commits into
arana-db:master
Choose a base branch
from
ruojieranyishen:addeventlistener
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| // Copyright 2020 Tyler Neely, Alex Regueiro | ||
| // | ||
| // 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. | ||
|
|
||
| // /* Event listener */ | ||
| // typedef void (*on_flush_begin_cb)(void*, rocksdb_t*, | ||
| // const rocksdb_flushjobinfo_t*); | ||
| // typedef void (*on_flush_completed_cb)(void*, rocksdb_t*, | ||
| // const rocksdb_flushjobinfo_t*); | ||
|
|
||
| use crate::ffi; | ||
|
|
||
| use libc::{c_void, size_t}; | ||
|
|
||
| pub struct FlushJobInfo { | ||
| pub(crate) inner: *const ffi::rocksdb_flushjobinfo_t, | ||
| } | ||
|
|
||
| impl FlushJobInfo { | ||
| pub fn cf_name(&self) -> String { | ||
| unsafe { | ||
| let mut size: size_t = 0; | ||
| let ptr = ffi::rocksdb_flushjobinfo_cf_name(self.inner, &mut size); | ||
| let slice = std::slice::from_raw_parts(ptr as *const u8, size); | ||
| String::from_utf8_lossy(slice).into_owned() | ||
| } | ||
| } | ||
|
|
||
| pub fn file_path(&self) -> String { | ||
| unsafe { | ||
| let mut size: size_t = 0; | ||
| let ptr = ffi::rocksdb_flushjobinfo_file_path(self.inner, &mut size); | ||
| let slice = std::slice::from_raw_parts(ptr as *const u8, size); | ||
| String::from_utf8_lossy(slice).into_owned() | ||
| } | ||
| } | ||
|
|
||
| pub fn triggered_writes_slowdown(&self) -> bool { | ||
| unsafe { ffi::rocksdb_flushjobinfo_triggered_writes_slowdown(self.inner) != 0 } | ||
| } | ||
|
|
||
| pub fn triggered_writes_stop(&self) -> bool { | ||
| unsafe { ffi::rocksdb_flushjobinfo_triggered_writes_stop(self.inner) != 0 } | ||
| } | ||
|
|
||
| pub fn largest_seqno(&self) -> u64 { | ||
| unsafe { ffi::rocksdb_flushjobinfo_largest_seqno(self.inner) } | ||
| } | ||
|
|
||
| pub fn smallest_seqno(&self) -> u64 { | ||
| unsafe { ffi::rocksdb_flushjobinfo_smallest_seqno(self.inner) } | ||
| } | ||
| } | ||
|
|
||
| /// A handle to an EventListener that manages its lifetime. | ||
| pub struct EventListenerHandle { | ||
| inner: *mut ffi::rocksdb_eventlistener_t, | ||
| owned: bool, | ||
| } | ||
|
|
||
| unsafe impl Send for EventListenerHandle {} | ||
| unsafe impl Sync for EventListenerHandle {} | ||
|
|
||
| impl EventListenerHandle { | ||
| pub(crate) unsafe fn from_raw(ptr: *mut ffi::rocksdb_eventlistener_t) -> Self { | ||
| Self { | ||
| inner: ptr, | ||
| owned: true, | ||
| } | ||
| } | ||
| pub(crate) fn into_raw(mut self) -> *mut ffi::rocksdb_eventlistener_t { | ||
| self.owned = false; | ||
| self.inner | ||
| } | ||
| } | ||
| impl Drop for EventListenerHandle { | ||
| fn drop(&mut self) { | ||
| if self.owned && !self.inner.is_null() { | ||
| unsafe { | ||
| ffi::rocksdb_eventlistener_destroy(self.inner); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // WARNING: If an EventListener implementation panics, the panic will unwind across the C/FFI boundary, | ||
| // which is undefined behavior in Rust. Consider using std::panic::catch_unwind to wrap the callback body | ||
| // to prevent panic propagation to C code. Not fixing this issue for now. | ||
| pub trait EventListener: Send + Sync { | ||
| fn on_flush_begin(&self, _: &FlushJobInfo) {} | ||
| fn on_flush_completed(&self, _: &FlushJobInfo) {} | ||
| // fn on_compaction_begin(&self, _: &CompactionJobInfo) {} | ||
| // fn on_compaction_completed(&self, _: &CompactionJobInfo) {} | ||
| // fn on_subcompaction_begin(&self, _: &SubcompactionJobInfo) {} | ||
| // fn on_subcompaction_completed(&self, _: &SubcompactionJobInfo) {} | ||
| // fn on_external_file_ingested(&self, _: &IngestionInfo) {} | ||
| // fn on_background_error(&self, _: DBBackgroundErrorReason, _: MutableStatus) {} | ||
| // fn on_stall_conditions_changed(&self, _: &WriteStallInfo) {} | ||
| // fn on_memtable_sealed(&self, _: &MemTableInfo) {} | ||
| } | ||
|
|
||
| unsafe extern "C" fn destructor<E: EventListener>(ctx: *mut c_void) { | ||
| let _ = Box::from_raw(ctx as *mut E); | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_flush_begin<E: EventListener>( | ||
| ctx: *mut c_void, | ||
| _db: *mut ffi::rocksdb_t, | ||
| flush_job_info: *const ffi::rocksdb_flushjobinfo_t, | ||
| ) { | ||
| let listener = &*(ctx as *const E); | ||
| let info = FlushJobInfo { | ||
| inner: flush_job_info, | ||
| }; | ||
| listener.on_flush_begin(&info); | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_flush_completed<E: EventListener>( | ||
| ctx: *mut c_void, | ||
| _db: *mut ffi::rocksdb_t, | ||
| flush_job_info: *const ffi::rocksdb_flushjobinfo_t, | ||
| ) { | ||
| let listener = &*(ctx as *const E); | ||
| let info = FlushJobInfo { | ||
| inner: flush_job_info, | ||
| }; | ||
| listener.on_flush_completed(&info); | ||
| } | ||
ruojieranyishen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| unsafe extern "C" fn on_compaction_begin<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _db: *mut ffi::rocksdb_t, | ||
| _compaction_job_info: *const ffi::rocksdb_compactionjobinfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_compaction_completed<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _db: *mut ffi::rocksdb_t, | ||
| _compaction_job_info: *const ffi::rocksdb_compactionjobinfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_subcompaction_begin<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _sub_compaction_job_info: *const ffi::rocksdb_subcompactionjobinfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_subcompaction_completed<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _sub_compaction_job_info: *const ffi::rocksdb_subcompactionjobinfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_external_file_ingested<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _db: *mut ffi::rocksdb_t, | ||
| _external_file_ingestion_info: *const ffi::rocksdb_externalfileingestioninfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_background_error<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _reason: u32, | ||
| _status_ptr: *mut ffi::rocksdb_status_ptr_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_stall_conditions_changed<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _writestall_info: *const ffi::rocksdb_writestallinfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| unsafe extern "C" fn on_memtable_sealed<E: EventListener>( | ||
| _ctx: *mut c_void, | ||
| _info: *const ffi::rocksdb_memtableinfo_t, | ||
| ) { | ||
| // TODO | ||
| } | ||
|
|
||
| pub fn new_event_listener<E: EventListener>(e: E) -> EventListenerHandle { | ||
| let p = Box::new(e); | ||
| let ptr = unsafe { | ||
| ffi::rocksdb_eventlistener_create( | ||
| Box::into_raw(p) as *mut c_void, | ||
| Some(destructor::<E>), | ||
| Some(on_flush_begin::<E>), | ||
| Some(on_flush_completed::<E>), | ||
| Some(on_compaction_begin::<E>), | ||
| Some(on_compaction_completed::<E>), | ||
| Some(on_subcompaction_begin::<E>), | ||
| Some(on_subcompaction_completed::<E>), | ||
| Some(on_external_file_ingested::<E>), | ||
| Some(on_background_error::<E>), | ||
| Some(on_stall_conditions_changed::<E>), | ||
| Some(on_memtable_sealed::<E>), | ||
| ) | ||
| }; | ||
| unsafe { EventListenerHandle::from_raw(ptr) } | ||
| } | ||
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,64 @@ | ||
| // Copyright 2020 Tyler Neely | ||
| // | ||
| // 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. | ||
|
|
||
| mod util; | ||
|
|
||
| use rocksdb::{new_event_listener, EventListener, FlushJobInfo, FlushOptions, Options, DB}; | ||
| use std::sync::atomic::{AtomicI32, Ordering}; | ||
| use std::sync::Arc; | ||
| use util::DBPath; | ||
|
|
||
| struct TestEventListener { | ||
| flush_begin_count: Arc<AtomicI32>, | ||
| flush_completed_count: Arc<AtomicI32>, | ||
| } | ||
|
|
||
| impl EventListener for TestEventListener { | ||
| fn on_flush_begin(&self, info: &FlushJobInfo) { | ||
| println!("Flush file: {}", info.file_path()); | ||
| self.flush_begin_count.fetch_add(1, Ordering::Relaxed); | ||
| } | ||
|
|
||
| fn on_flush_completed(&self, _: &FlushJobInfo) { | ||
| self.flush_completed_count.fetch_add(1, Ordering::Relaxed); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_flush_callback() { | ||
| let path = DBPath::new("_test_flush_callback"); | ||
| let flush_begin_count = Arc::new(AtomicI32::new(0)); | ||
| let flush_completed_count = Arc::new(AtomicI32::new(0)); | ||
| { | ||
| let mut opts = Options::default(); | ||
| opts.create_if_missing(true); | ||
|
|
||
| let listener = TestEventListener { | ||
| flush_begin_count: flush_begin_count.clone(), | ||
| flush_completed_count: flush_completed_count.clone(), | ||
| }; | ||
| let listener_ptr = new_event_listener(listener); | ||
| opts.add_event_listener(listener_ptr); | ||
| let db = DB::open(&opts, &path).unwrap(); | ||
| db.put(b"k1", b"v1").unwrap(); | ||
|
|
||
| let mut flush_opts = FlushOptions::default(); | ||
| flush_opts.set_wait(true); | ||
| db.flush_opt(&flush_opts).unwrap(); | ||
|
|
||
| // Verify callback was called | ||
| assert_eq!(flush_begin_count.load(Ordering::Relaxed), 1); | ||
| assert_eq!(flush_completed_count.load(Ordering::Relaxed), 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.