-
Notifications
You must be signed in to change notification settings - Fork 78
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
pg
: Add ScopeName
-> Vec<GroupName>
mapping
#190
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
da3a1a4
Fix documentation and comments
leonqadirie 566791b
Make test logic more consistent
leonqadirie 94259de
Add `index` field to `PgState`
leonqadirie ec7c015
Add comment for `drop`
leonqadirie a3289cb
Remove stale comment
leonqadirie de5549e
Merge branch 'slawlor:main' into pg-scope-group-index
leonqadirie 387b4d0
Draft of simultaneous locks for both `DashMap`s
35fd146
Rename variables to avoid rebindings in the scope
leonqadirie a8e477c
Reduce race condition potential of `leave_*` functions
leonqadirie 95ec939
Merge branch 'slawlor:main' into pg-scope-group-index
leonqadirie 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 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 |
---|---|---|
|
@@ -90,6 +90,7 @@ | |
|
||
struct PgState { | ||
map: Arc<DashMap<ScopeGroupKey, HashMap<ActorId, ActorCell>>>, | ||
index: Arc<DashMap<ScopeName, Vec<GroupName>>>, | ||
listeners: Arc<DashMap<ScopeGroupKey, Vec<ActorCell>>>, | ||
} | ||
|
||
|
@@ -98,6 +99,7 @@ | |
fn get_monitor<'a>() -> &'a PgState { | ||
PG_MONITOR.get_or_init(|| PgState { | ||
map: Arc::new(DashMap::new()), | ||
index: Arc::new(DashMap::new()), | ||
listeners: Arc::new(DashMap::new()), | ||
}) | ||
} | ||
|
@@ -138,6 +140,18 @@ | |
vacancy.insert(map); | ||
} | ||
} | ||
match monitor.index.entry(scope.to_owned()) { | ||
Occupied(mut occupied) => { | ||
let oref = occupied.get_mut(); | ||
if !oref.contains(&group) { | ||
oref.push(group.to_owned()); | ||
} | ||
} | ||
Vacant(vacancy) => { | ||
vacancy.insert(vec![group.to_owned()]); | ||
} | ||
} | ||
|
||
// notify supervisors | ||
if let Some(listeners) = monitor.listeners.get(&key) { | ||
for listener in listeners.value() { | ||
|
@@ -186,10 +200,21 @@ | |
mut_ref.remove(&actor.get_id()); | ||
} | ||
|
||
// the scope and group tuple is empty, remove it | ||
// if the scope and group tuple is empty, remove it | ||
if mut_ref.is_empty() { | ||
occupied.remove(); | ||
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. so to the point above, i think we'd move this before the removal of the index? I still need to think a bit if there's a race that could occur here even with the drop operations release the locks sequentially |
||
|
||
// remove the group and possibly the scope from the monitor's index | ||
if let Some(mut groups_in_scope) = monitor.index.get_mut(&scope) { | ||
groups_in_scope.retain(|group_name| group_name != &group); | ||
if groups_in_scope.is_empty() { | ||
// drop the `RefMut` to prevent a `DashMap` deadlock | ||
drop(groups_in_scope); | ||
monitor.index.remove(&scope); | ||
} | ||
} | ||
} | ||
|
||
if let Some(listeners) = monitor.listeners.get(&key) { | ||
for listener in listeners.value() { | ||
let _ = listener.send_supervisor_evt(SupervisionEvent::ProcessGroupChanged( | ||
|
@@ -222,15 +247,15 @@ | |
let pg_monitor = get_monitor(); | ||
let map = pg_monitor.map.clone(); | ||
|
||
let mut empty_groups = vec![]; | ||
let mut empty_scope_group_keys = vec![]; | ||
let mut removal_events = HashMap::new(); | ||
|
||
for mut kv in map.iter_mut() { | ||
if let Some(actor_cell) = kv.value_mut().remove(&actor) { | ||
removal_events.insert(kv.key().clone(), actor_cell); | ||
} | ||
if kv.value().is_empty() { | ||
empty_groups.push(kv.key().clone()); | ||
empty_scope_group_keys.push(kv.key().clone()); | ||
} | ||
} | ||
|
||
|
@@ -267,8 +292,11 @@ | |
} | ||
|
||
// Cleanup empty groups | ||
for group in empty_groups { | ||
map.remove(&group); | ||
for scope_group_key in empty_scope_group_keys { | ||
map.remove(&scope_group_key); | ||
if let Some(mut groups_in_scope) = pg_monitor.index.get_mut(&scope_group_key.scope) { | ||
groups_in_scope.retain(|group| group != &scope_group_key.group); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -360,23 +388,15 @@ | |
/// in `scope` | ||
pub fn which_scoped_groups(scope: &ScopeName) -> Vec<GroupName> { | ||
let monitor = get_monitor(); | ||
monitor | ||
.map | ||
.iter() | ||
.filter_map(|kvp| { | ||
let key = kvp.key(); | ||
if key.scope == *scope { | ||
Some(key.group.to_owned()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>() | ||
match monitor.index.get(scope) { | ||
Some(groups) => groups.to_owned(), | ||
None => vec![], | ||
} | ||
} | ||
|
||
/// Returns a list of all known scope-group combinations. | ||
/// | ||
/// Returns a [`Vec<(ScopGroupKey)>`] representing all the registered | ||
/// Returns a [`Vec<ScopeGroupKey>`] representing all the registered | ||
/// combinations that form an identifying tuple | ||
pub fn which_scopes_and_groups() -> Vec<ScopeGroupKey> { | ||
let monitor = get_monitor(); | ||
|
This file contains 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.
so the problem that this approach takes (which is why it wasn't done this way in the beginning) is that this is technically a subtle race condition. We will probably need a global lock around both of the hash maps or something, but then there's going to be the benchmarking of the performance hit for taking a global lock.
Dashmaps are sharded maps so they can do concurrent updates from multiple threads, but what we'd be doing is locking around both maps with a singleton global lock.
Unless we can try and acquire both sharded locks, doing the updates, and then only releasing the locks simultaneously.
i.e.
It will be a little ugly with the scopes, but it's the best option for concurrent updates.
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.
You're right of course.
I hope to find the time to experiment and benchmark, can't currently promise a timeline, though :)
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.
Then again, as far as I understand we are only talking about the
map
andindex
fields which are only mutated in thejoin*
andleave*
functions.It duplicates a bit more code but I think just locking both fields and then nesting their updates (so the locks get dropped simultaneously when leaving scope) might work.