-
Notifications
You must be signed in to change notification settings - Fork 160
Use ArcSwap for aggregate fn registry #8072
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
robert3005
wants to merge
3
commits into
develop
Choose a base branch
from
rk/aggregatearcswap
base: develop
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
3 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
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 |
|---|---|---|
|
|
@@ -4,11 +4,10 @@ | |
| use std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| use parking_lot::RwLock; | ||
| use arc_swap::ArcSwap; | ||
| use vortex_session::Ref; | ||
| use vortex_session::SessionExt; | ||
| use vortex_session::SessionVar; | ||
| use vortex_session::registry::Registry; | ||
| use vortex_utils::aliases::hash_map::HashMap; | ||
|
|
||
| use crate::aggregate_fn::AggregateFnId; | ||
|
|
@@ -43,16 +42,13 @@ use crate::arrays::dict::compute::is_constant::DictIsConstantKernel; | |
| use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel; | ||
| use crate::arrays::dict::compute::min_max::DictMinMaxKernel; | ||
|
|
||
| /// Registry of aggregate function vtables. | ||
| pub type AggregateFnRegistry = Registry<AggregateFnPluginRef>; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we have a pattern like this for ArcSwap? |
||
|
|
||
| /// Session state for aggregate function vtables. | ||
| #[derive(Debug)] | ||
| pub struct AggregateFnSession { | ||
| registry: AggregateFnRegistry, | ||
|
robert3005 marked this conversation as resolved.
|
||
| registry: ArcSwap<HashMap<AggregateFnId, AggregateFnPluginRef>>, | ||
|
|
||
| pub(super) kernels: RwLock<HashMap<KernelKey, &'static dyn DynAggregateKernel>>, | ||
| pub(super) grouped_kernels: RwLock<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>, | ||
| kernels: ArcSwap<HashMap<KernelKey, &'static dyn DynAggregateKernel>>, | ||
| grouped_kernels: ArcSwap<HashMap<KernelKey, &'static dyn DynGroupedAggregateKernel>>, | ||
| } | ||
|
|
||
| impl SessionVar for AggregateFnSession { | ||
|
|
@@ -70,9 +66,9 @@ type KernelKey = (ArrayId, Option<AggregateFnId>); | |
| impl Default for AggregateFnSession { | ||
| fn default() -> Self { | ||
| let this = Self { | ||
| registry: AggregateFnRegistry::default(), | ||
| kernels: RwLock::new(HashMap::default()), | ||
| grouped_kernels: RwLock::new(HashMap::default()), | ||
| registry: ArcSwap::from_pointee(HashMap::default()), | ||
| kernels: ArcSwap::from_pointee(HashMap::default()), | ||
| grouped_kernels: ArcSwap::from_pointee(HashMap::default()), | ||
| }; | ||
|
|
||
| // Register the built-in aggregate functions | ||
|
|
@@ -106,16 +102,35 @@ impl Default for AggregateFnSession { | |
| } | ||
|
|
||
| impl AggregateFnSession { | ||
| /// Returns the aggregate function registry. | ||
| pub fn registry(&self) -> &AggregateFnRegistry { | ||
| &self.registry | ||
| /// Find plugin in the registry for the given id | ||
| pub fn find_plugin(&self, id: &AggregateFnId) -> Option<AggregateFnPluginRef> { | ||
| self.registry.load().get(id).cloned() | ||
| } | ||
|
|
||
| /// Register an aggregate function vtable in the session, replacing any existing vtable with | ||
| /// the same ID. | ||
| pub fn register<V: AggregateFnVTable>(&self, vtable: V) { | ||
| self.registry | ||
| .register(vtable.id(), Arc::new(vtable) as AggregateFnPluginRef); | ||
| let id = vtable.id(); | ||
| let pluginref = Arc::new(vtable) as AggregateFnPluginRef; | ||
| self.registry.rcu(move |registry| { | ||
| let mut existing = registry.as_ref().clone(); | ||
| existing.insert(id, Arc::clone(&pluginref)); | ||
| existing | ||
| }); | ||
| } | ||
|
|
||
| pub fn find_aggregate_kernel( | ||
| &self, | ||
| array_id: impl Into<ArrayId>, | ||
| agg_fn_id: impl Into<AggregateFnId>, | ||
| ) -> Option<&'static dyn DynAggregateKernel> { | ||
| let loaded = self.kernels.load(); | ||
| let id = array_id.into(); | ||
| let fn_id = agg_fn_id.into(); | ||
| loaded | ||
| .get(&(id, Some(fn_id))) | ||
| .or_else(|| loaded.get(&(id, None))) | ||
| .copied() | ||
| } | ||
|
|
||
| /// Register an aggregate function kernel for a specific aggregate function and array type. | ||
|
|
@@ -125,9 +140,26 @@ impl AggregateFnSession { | |
| agg_fn_id: Option<impl Into<AggregateFnId>>, | ||
| kernel: &'static dyn DynAggregateKernel, | ||
| ) { | ||
| self.kernels | ||
| .write() | ||
| .insert((array_id.into(), agg_fn_id.map(|id| id.into())), kernel); | ||
| let id = (array_id.into(), agg_fn_id.map(|id| id.into())); | ||
| self.kernels.rcu(move |registry| { | ||
| let mut existing = registry.as_ref().clone(); | ||
| existing.insert(id, kernel); | ||
| existing | ||
| }); | ||
| } | ||
|
|
||
| pub fn find_grouped_kernel( | ||
| &self, | ||
| array_id: impl Into<ArrayId>, | ||
| agg_fn_id: impl Into<AggregateFnId>, | ||
| ) -> Option<&'static dyn DynGroupedAggregateKernel> { | ||
| let loaded = self.grouped_kernels.load(); | ||
| let id = array_id.into(); | ||
| let fn_id = agg_fn_id.into(); | ||
| loaded | ||
| .get(&(id, Some(fn_id))) | ||
| .or_else(|| loaded.get(&(id, None))) | ||
| .copied() | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
❤️