@@ -90,6 +90,7 @@ impl ScopeGroupKey {
90
90
91
91
struct PgState {
92
92
map : Arc < DashMap < ScopeGroupKey , HashMap < ActorId , ActorCell > > > ,
93
+ index : Arc < DashMap < ScopeName , Vec < GroupName > > > ,
93
94
listeners : Arc < DashMap < ScopeGroupKey , Vec < ActorCell > > > ,
94
95
}
95
96
@@ -98,6 +99,7 @@ static PG_MONITOR: OnceCell<PgState> = OnceCell::new();
98
99
fn get_monitor < ' a > ( ) -> & ' a PgState {
99
100
PG_MONITOR . get_or_init ( || PgState {
100
101
map : Arc :: new ( DashMap :: new ( ) ) ,
102
+ index : Arc :: new ( DashMap :: new ( ) ) ,
101
103
listeners : Arc :: new ( DashMap :: new ( ) ) ,
102
104
} )
103
105
}
@@ -138,6 +140,18 @@ pub fn join_scoped(scope: ScopeName, group: GroupName, actors: Vec<ActorCell>) {
138
140
vacancy. insert ( map) ;
139
141
}
140
142
}
143
+ match monitor. index . entry ( scope. to_owned ( ) ) {
144
+ Occupied ( mut occupied) => {
145
+ let oref = occupied. get_mut ( ) ;
146
+ if !oref. contains ( & group) {
147
+ oref. push ( group. to_owned ( ) ) ;
148
+ }
149
+ }
150
+ Vacant ( vacancy) => {
151
+ vacancy. insert ( vec ! [ group. to_owned( ) ] ) ;
152
+ }
153
+ }
154
+
141
155
// notify supervisors
142
156
if let Some ( listeners) = monitor. listeners . get ( & key) {
143
157
for listener in listeners. value ( ) {
@@ -189,7 +203,17 @@ pub fn leave_scoped(scope: ScopeName, group: GroupName, actors: Vec<ActorCell>)
189
203
// if the scope and group tuple is empty, remove it
190
204
if mut_ref. is_empty ( ) {
191
205
occupied. remove ( ) ;
206
+
207
+ // remove the group and possibly the scope from the monitor's index
208
+ if let Some ( mut groups_in_scope) = monitor. index . get_mut ( & scope) {
209
+ groups_in_scope. retain ( |group_name| group_name != & group) ;
210
+ if groups_in_scope. is_empty ( ) {
211
+ drop ( groups_in_scope) ;
212
+ monitor. index . remove ( & scope) ;
213
+ }
214
+ }
192
215
}
216
+
193
217
if let Some ( listeners) = monitor. listeners . get ( & key) {
194
218
for listener in listeners. value ( ) {
195
219
let _ = listener. send_supervisor_evt ( SupervisionEvent :: ProcessGroupChanged (
@@ -221,16 +245,17 @@ pub fn leave_scoped(scope: ScopeName, group: GroupName, actors: Vec<ActorCell>)
221
245
pub ( crate ) fn leave_all ( actor : ActorId ) {
222
246
let pg_monitor = get_monitor ( ) ;
223
247
let map = pg_monitor. map . clone ( ) ;
248
+ // let index = pg_monitor.index.clone();
224
249
225
- let mut empty_groups = vec ! [ ] ;
250
+ let mut empty_scope_group_keys = vec ! [ ] ;
226
251
let mut removal_events = HashMap :: new ( ) ;
227
252
228
253
for mut kv in map. iter_mut ( ) {
229
254
if let Some ( actor_cell) = kv. value_mut ( ) . remove ( & actor) {
230
255
removal_events. insert ( kv. key ( ) . clone ( ) , actor_cell) ;
231
256
}
232
257
if kv. value ( ) . is_empty ( ) {
233
- empty_groups . push ( kv. key ( ) . clone ( ) ) ;
258
+ empty_scope_group_keys . push ( kv. key ( ) . clone ( ) ) ;
234
259
}
235
260
}
236
261
@@ -267,8 +292,11 @@ pub(crate) fn leave_all(actor: ActorId) {
267
292
}
268
293
269
294
// Cleanup empty groups
270
- for group in empty_groups {
271
- map. remove ( & group) ;
295
+ for scope_group_key in empty_scope_group_keys {
296
+ map. remove ( & scope_group_key) ;
297
+ if let Some ( mut groups_in_scope) = pg_monitor. index . get_mut ( & scope_group_key. scope ) {
298
+ groups_in_scope. retain ( |group| group != & scope_group_key. group ) ;
299
+ }
272
300
}
273
301
}
274
302
@@ -360,18 +388,10 @@ pub fn which_groups() -> Vec<GroupName> {
360
388
/// in `scope`
361
389
pub fn which_scoped_groups ( scope : & ScopeName ) -> Vec < GroupName > {
362
390
let monitor = get_monitor ( ) ;
363
- monitor
364
- . map
365
- . iter ( )
366
- . filter_map ( |kvp| {
367
- let key = kvp. key ( ) ;
368
- if key. scope == * scope {
369
- Some ( key. group . to_owned ( ) )
370
- } else {
371
- None
372
- }
373
- } )
374
- . collect :: < Vec < _ > > ( )
391
+ match monitor. index . get ( scope) {
392
+ Some ( groups) => groups. to_owned ( ) ,
393
+ None => vec ! [ ] ,
394
+ }
375
395
}
376
396
377
397
/// Returns a list of all known scope-group combinations.
0 commit comments