-
Notifications
You must be signed in to change notification settings - Fork 0
Vendored/stoppable module #11
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
WeCodingNow
wants to merge
17
commits into
master
Choose a base branch
from
vendored/stoppable_module
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
17 commits
Select commit
Hold shift + click to select a range
961ca1d
add pop_many func for executor
oleggator aa83810
wip
oleggator 06fce20
remove retries
oleggator 32ac69b
handle executor clone error
oleggator f3b5c07
add fiber_standby_timeout parameter
oleggator f153c6d
remove redundant clone
oleggator befd794
fix scheduler dead lock
oleggator 0326cdb
change bench config
oleggator f34943b
fix TXChannelClosed (oneshot receiver was dropped)
oleggator f3d7193
change bench parameters
oleggator 2e3418f
added notifier parameter
500b568
wip
oleggator 2c6ea0b
fix waiters counter decrement
oleggator 4dca3ec
add waiters method for dispatcher and executor
oleggator 72650dd
Revert "Merge remote-tracking branch 'origin/notification_optimizatio…
007a044
Revert "add waiters method for dispatcher and executor"
f06b8a3
tracing
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,155 @@ | ||
| use std::{collections::LinkedList, rc::Rc, time::Duration}; | ||
|
|
||
| use crossbeam_channel::{unbounded, TryRecvError}; | ||
| use mlua::Lua; | ||
| use tarantool::fiber; | ||
| use tracing_opentelemetry::OpenTelemetrySpanExt; | ||
| // use tracing; | ||
|
|
||
| use crate::{ChannelError, Executor, ModuleConfig, Task, InstrumentedTask}; | ||
|
|
||
| struct SchedulerArgs<'a> { | ||
| lua: &'a Lua, | ||
| executor: Executor, | ||
| config: ModuleConfig, | ||
| } | ||
| fn scheduler_f(args: Box<SchedulerArgs>) -> i32 { | ||
| let SchedulerArgs { | ||
| lua, | ||
| executor, | ||
| config: | ||
| ModuleConfig { | ||
| max_batch, | ||
| coio_timeout, | ||
| fibers, | ||
| fiber_standby_timeout, | ||
| .. | ||
| }, | ||
| } = *args; | ||
|
|
||
| let cond = Rc::new(fiber::Cond::new()); | ||
| let (tx, rx) = unbounded::<InstrumentedTask>(); | ||
|
|
||
| let mut workers = LinkedList::new(); | ||
| for _ in 0..fibers { | ||
| let mut worker = fiber::Fiber::new("worker", &mut worker_f); | ||
| worker.set_joinable(true); | ||
| worker.start(WorkerArgs { | ||
| cond: cond.clone(), | ||
| lua, | ||
| rx: rx.clone(), | ||
| fiber_standby_timeout, | ||
| }); | ||
| workers.push_back(worker); | ||
| } | ||
|
|
||
| let result = loop { | ||
| let tasks = match executor.pop_many(max_batch, coio_timeout) { | ||
| Ok(tasks) => tasks, | ||
| Err(ChannelError::RXChannelClosed) => break Ok(()), | ||
| Err(err) => break Err(err), | ||
| }; | ||
|
|
||
| for task in tasks { | ||
| tx.send(task).unwrap(); // TODO: add error handling | ||
| cond.signal(); | ||
| } | ||
| }; | ||
|
|
||
| // gracefully kill fibers | ||
| drop(tx); | ||
| cond.broadcast(); | ||
|
|
||
| for worker in workers { | ||
| worker.join(); | ||
| } | ||
|
|
||
| match result { | ||
| Ok(_) => 0, | ||
| Err(_) => -1, | ||
| } | ||
| } | ||
|
|
||
| struct WorkerArgs<'a> { | ||
| cond: Rc<fiber::Cond>, | ||
| lua: &'a Lua, | ||
| rx: crossbeam_channel::Receiver<InstrumentedTask>, | ||
| fiber_standby_timeout: f64, | ||
| } | ||
| fn worker_f(args: Box<WorkerArgs>) -> i32 { | ||
| let WorkerArgs { | ||
| cond, | ||
| lua, | ||
| rx, | ||
| fiber_standby_timeout, | ||
| } = *args; | ||
| let fiber_standby_timeout = Duration::from_secs_f64(fiber_standby_timeout); | ||
|
|
||
| let thread_func = lua | ||
| .create_function(move |lua, _: ()| { | ||
| loop { | ||
| match rx.try_recv() { | ||
| Ok((func, span_ctx)) => match { | ||
| let span = tracing::span!(tracing::Level::TRACE, "fiber pool: exec"); | ||
| span.set_parent(span_ctx); | ||
| let _ = span.enter(); | ||
|
|
||
| func(lua, span.context()) | ||
| } { | ||
| Ok(()) => (), | ||
| Err(ChannelError::TXChannelClosed) => continue, | ||
| Err(err) => break Err(mlua::Error::external(err)), | ||
| }, | ||
| Err(TryRecvError::Disconnected) => break Ok(()), | ||
| Err(TryRecvError::Empty) => { | ||
| let signaled = cond.wait_timeout(fiber_standby_timeout); | ||
| // if !signaled { | ||
| // // kill fiber | ||
| // break Ok(()); | ||
| // } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| .unwrap(); | ||
| let thread = lua.create_thread(thread_func).unwrap(); | ||
| match thread.resume(()) { | ||
| Ok(()) => 0, | ||
| Err(_) => -1, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct FiberPool<'a> { | ||
| lua: &'a Lua, | ||
| executor: Executor, | ||
| config: ModuleConfig, | ||
|
|
||
| scheduler: fiber::Fiber<'a, SchedulerArgs<'a>>, | ||
| } | ||
|
|
||
| impl<'a> FiberPool<'a> { | ||
| pub fn new(lua: &'a Lua, executor: Executor, config: ModuleConfig) -> Self { | ||
| let mut scheduler = fiber::Fiber::new("scheduler", &mut scheduler_f); | ||
| scheduler.set_joinable(true); | ||
| Self { | ||
| lua, | ||
| executor, | ||
| config, | ||
| scheduler, | ||
| } | ||
| } | ||
|
|
||
| pub fn run(&mut self) -> std::io::Result<()> { | ||
| self.scheduler.start(SchedulerArgs { | ||
| lua: self.lua, | ||
| executor: self.executor.try_clone()?, | ||
| config: self.config.clone(), | ||
| }); | ||
| Ok(()) | ||
| } | ||
|
|
||
| // join will exit when all Dispatchers die | ||
| pub fn join(&self) { | ||
| self.scheduler.join(); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
может просто добавим generic чтобы можно было что угодно передавать?