@@ -84,7 +84,7 @@ use bao_tree::{
84
84
} ;
85
85
use bytes:: Bytes ;
86
86
use delete_set:: { BaoFilePart , ProtectHandle } ;
87
- use entity_manager:: { EntityManager , Options as EntityManagerOptions } ;
87
+ use entity_manager:: { EntityManager , Options as EntityManagerOptions , SpawnArg } ;
88
88
use entry_state:: { DataLocation , OutboardLocation } ;
89
89
use gc:: run_gc;
90
90
use import:: { ImportEntry , ImportSource } ;
@@ -201,11 +201,10 @@ impl TaskContext {
201
201
}
202
202
}
203
203
204
- #[ derive( Debug , Clone , Default ) ]
205
- struct EntityState ;
206
-
207
- impl entity_manager:: Reset for EntityState {
208
- fn reset ( & mut self ) { }
204
+ impl entity_manager:: Reset for Slot {
205
+ fn reset ( & mut self ) {
206
+ self . 0 = Arc :: new ( tokio:: sync:: Mutex :: new ( None ) ) ;
207
+ }
209
208
}
210
209
211
210
#[ derive( Debug ) ]
@@ -216,7 +215,7 @@ impl entity_manager::Params for EmParams {
216
215
217
216
type GlobalState = Arc < TaskContext > ;
218
217
219
- type EntityState = EntityState ;
218
+ type EntityState = Slot ;
220
219
221
220
async fn on_shutdown (
222
221
state : entity_manager:: ActiveEntityState < Self > ,
@@ -235,11 +234,7 @@ struct Actor {
235
234
fs_cmd_rx : tokio:: sync:: mpsc:: Receiver < InternalCommand > ,
236
235
// Tasks for import and export operations.
237
236
tasks : JoinSet < ( ) > ,
238
- // Running tasks
239
- running : HashSet < Id > ,
240
- // handles
241
- handles : HashMap < Hash , Slot > ,
242
-
237
+ // Entity handler
243
238
handles2 : EntityManager < EmParams > ,
244
239
// temp tags
245
240
temp_tags : TempTags ,
@@ -293,14 +288,6 @@ impl HashContext {
293
288
self . db ( ) . set ( hash, state) . await
294
289
}
295
290
296
- pub async fn get_maybe_create ( & self , hash : Hash , create : bool ) -> api:: Result < BaoFileHandle > {
297
- if create {
298
- self . get_or_create ( hash) . await
299
- } else {
300
- self . get ( hash) . await
301
- }
302
- }
303
-
304
291
pub async fn get ( & self , hash : Hash ) -> api:: Result < BaoFileHandle > {
305
292
if hash == Hash :: EMPTY {
306
293
return Ok ( self . ctx . empty . clone ( ) ) ;
@@ -433,17 +420,12 @@ impl Actor {
433
420
434
421
fn spawn ( & mut self , fut : impl Future < Output = ( ) > + Send + ' static ) {
435
422
let span = tracing:: Span :: current ( ) ;
436
- let id = self . tasks . spawn ( fut. instrument ( span) ) . id ( ) ;
437
- self . running . insert ( id) ;
423
+ self . tasks . spawn ( fut. instrument ( span) ) ;
438
424
}
439
425
440
- fn log_task_result ( & mut self , res : Result < ( Id , ( ) ) , JoinError > ) {
426
+ fn log_task_result ( & mut self , res : Result < ( ) , JoinError > ) {
441
427
match res {
442
- Ok ( ( id, _) ) => {
443
- // println!("task {id} finished");
444
- self . running . remove ( & id) ;
445
- // println!("{:?}", self.running);
446
- }
428
+ Ok ( _) => { }
447
429
Err ( e) => {
448
430
error ! ( "task failed: {e}" ) ;
449
431
}
@@ -459,26 +441,6 @@ impl Actor {
459
441
tx. send ( tt) . await . ok ( ) ;
460
442
}
461
443
462
- async fn clear_dead_handles ( & mut self ) {
463
- let mut to_remove = Vec :: new ( ) ;
464
- for ( hash, slot) in & self . handles {
465
- if !slot. is_live ( ) . await {
466
- to_remove. push ( * hash) ;
467
- }
468
- }
469
- for hash in to_remove {
470
- if let Some ( slot) = self . handles . remove ( & hash) {
471
- // do a quick check if the handle has become alive in the meantime, and reinsert it
472
- let guard = slot. 0 . lock ( ) . await ;
473
- let is_live = guard. as_ref ( ) . map ( |x| !x. is_dead ( ) ) . unwrap_or_default ( ) ;
474
- if is_live {
475
- drop ( guard) ;
476
- self . handles . insert ( hash, slot) ;
477
- }
478
- }
479
- }
480
- }
481
-
482
444
async fn handle_command ( & mut self , cmd : Command ) {
483
445
let span = cmd. parent_span ( ) ;
484
446
let _entered = span. enter ( ) ;
@@ -513,7 +475,6 @@ impl Actor {
513
475
}
514
476
Command :: ClearProtected ( cmd) => {
515
477
trace ! ( "{cmd:?}" ) ;
516
- self . clear_dead_handles ( ) . await ;
517
478
self . db ( ) . send ( cmd. into ( ) ) . await . ok ( ) ;
518
479
}
519
480
Command :: BlobStatus ( cmd) => {
@@ -569,40 +530,112 @@ impl Actor {
569
530
}
570
531
Command :: ExportPath ( cmd) => {
571
532
trace ! ( "{cmd:?}" ) ;
572
- let ctx = self . hash_context ( cmd. hash ) ;
573
- self . spawn ( export_path ( cmd, ctx) ) ;
533
+ let ctx = self . context . clone ( ) ;
534
+ self . handles2
535
+ . spawn ( cmd. hash , |state| async move {
536
+ match state {
537
+ SpawnArg :: Active ( state) => {
538
+ let ctx = HashContext {
539
+ slot : state. state ,
540
+ ctx,
541
+ } ;
542
+ export_path ( cmd, ctx) . await
543
+ }
544
+ _ => { }
545
+ }
546
+ } )
547
+ . await
548
+ . ok ( ) ;
549
+ // let ctx = self.hash_context(cmd.hash);
550
+ // self.spawn(export_path(cmd, ctx));
574
551
}
575
552
Command :: ExportBao ( cmd) => {
576
553
trace ! ( "{cmd:?}" ) ;
577
- let ctx = self . hash_context ( cmd. hash ) ;
578
- self . spawn ( export_bao ( cmd, ctx) ) ;
554
+ let ctx = self . context . clone ( ) ;
555
+ self . handles2
556
+ . spawn ( cmd. hash , |state| async move {
557
+ match state {
558
+ SpawnArg :: Active ( state) => {
559
+ let ctx = HashContext {
560
+ slot : state. state ,
561
+ ctx,
562
+ } ;
563
+ export_bao ( cmd, ctx) . await
564
+ }
565
+ _ => { }
566
+ }
567
+ } )
568
+ . await
569
+ . ok ( ) ;
570
+ // let ctx = self.hash_context(cmd.hash);
571
+ // self.spawn(export_bao(cmd, ctx));
579
572
}
580
573
Command :: ExportRanges ( cmd) => {
581
574
trace ! ( "{cmd:?}" ) ;
582
- let ctx = self . hash_context ( cmd. hash ) ;
583
- self . spawn ( export_ranges ( cmd, ctx) ) ;
575
+ let ctx = self . context . clone ( ) ;
576
+ self . handles2
577
+ . spawn ( cmd. hash , |state| async move {
578
+ match state {
579
+ SpawnArg :: Active ( state) => {
580
+ let ctx = HashContext {
581
+ slot : state. state ,
582
+ ctx,
583
+ } ;
584
+ export_ranges ( cmd, ctx) . await
585
+ }
586
+ _ => { }
587
+ }
588
+ } )
589
+ . await
590
+ . ok ( ) ;
591
+ // let ctx = self.hash_context(cmd.hash);
592
+ // self.spawn(export_ranges(cmd, ctx));
584
593
}
585
594
Command :: ImportBao ( cmd) => {
586
595
trace ! ( "{cmd:?}" ) ;
587
- let ctx = self . hash_context ( cmd. hash ) ;
588
- self . spawn ( import_bao ( cmd, ctx) ) ;
596
+ let ctx = self . context . clone ( ) ;
597
+ self . handles2
598
+ . spawn ( cmd. hash , |state| async move {
599
+ match state {
600
+ SpawnArg :: Active ( state) => {
601
+ let ctx = HashContext {
602
+ slot : state. state ,
603
+ ctx,
604
+ } ;
605
+ import_bao ( cmd, ctx) . await
606
+ }
607
+ _ => { }
608
+ }
609
+ } )
610
+ . await
611
+ . ok ( ) ;
612
+ // let ctx = self.hash_context(cmd.hash);
613
+ // self.spawn(import_bao(cmd, ctx));
589
614
}
590
615
Command :: Observe ( cmd) => {
591
616
trace ! ( "{cmd:?}" ) ;
592
- let ctx = self . hash_context ( cmd. hash ) ;
593
- self . spawn ( observe ( cmd, ctx) ) ;
617
+ let ctx = self . context . clone ( ) ;
618
+ self . handles2
619
+ . spawn ( cmd. hash , |state| async move {
620
+ match state {
621
+ SpawnArg :: Active ( state) => {
622
+ let ctx = HashContext {
623
+ slot : state. state ,
624
+ ctx,
625
+ } ;
626
+ observe ( cmd, ctx) . await
627
+ }
628
+ _ => { }
629
+ }
630
+ } )
631
+ . await
632
+ . ok ( ) ;
633
+ // let ctx = self.hash_context(cmd.hash);
634
+ // self.spawn(observe(cmd, ctx));
594
635
}
595
636
}
596
637
}
597
638
598
- /// Create a hash context for a given hash.
599
- fn hash_context ( & mut self , hash : Hash ) -> HashContext {
600
- HashContext {
601
- slot : self . handles . entry ( hash) . or_default ( ) . clone ( ) ,
602
- ctx : self . context . clone ( ) ,
603
- }
604
- }
605
-
606
639
async fn handle_fs_command ( & mut self , cmd : InternalCommand ) {
607
640
let span = cmd. parent_span ( ) ;
608
641
let _entered = span. enter ( ) ;
@@ -630,8 +663,22 @@ impl Actor {
630
663
format : cmd. format ,
631
664
} ,
632
665
) ;
633
- let ctx = self . hash_context ( cmd. hash ) ;
634
- self . spawn ( finish_import ( cmd, tt, ctx) ) ;
666
+ let ctx = self . context . clone ( ) ;
667
+ self . handles2
668
+ . spawn ( cmd. hash , |state| async move {
669
+ match state {
670
+ SpawnArg :: Active ( state) => {
671
+ let ctx = HashContext {
672
+ slot : state. state ,
673
+ ctx,
674
+ } ;
675
+ finish_import ( cmd, tt, ctx) . await
676
+ }
677
+ _ => { }
678
+ }
679
+ } )
680
+ . await
681
+ . ok ( ) ;
635
682
}
636
683
}
637
684
}
@@ -649,7 +696,7 @@ impl Actor {
649
696
Some ( cmd) = self . fs_cmd_rx. recv( ) => {
650
697
self . handle_fs_command( cmd) . await ;
651
698
}
652
- Some ( res) = self . tasks. join_next_with_id ( ) , if !self . tasks. is_empty( ) => {
699
+ Some ( res) = self . tasks. join_next ( ) , if !self . tasks. is_empty( ) => {
653
700
self . log_task_result( res) ;
654
701
}
655
702
}
@@ -700,8 +747,6 @@ impl Actor {
700
747
cmd_rx,
701
748
fs_cmd_rx : fs_commands_rx,
702
749
tasks : JoinSet :: new ( ) ,
703
- running : HashSet :: new ( ) ,
704
- handles : Default :: default ( ) ,
705
750
handles2 : EntityManager :: new ( slot_context, EntityManagerOptions :: default ( ) ) ,
706
751
temp_tags : Default :: default ( ) ,
707
752
_rt : rt,
@@ -1017,7 +1062,7 @@ async fn export_ranges_impl(
1017
1062
1018
1063
#[ instrument( skip_all, fields( hash = %cmd. hash_short( ) ) ) ]
1019
1064
async fn export_bao ( mut cmd : ExportBaoMsg , ctx : HashContext ) {
1020
- match ctx. get_maybe_create ( cmd. hash , false ) . await {
1065
+ match ctx. get ( cmd. hash ) . await {
1021
1066
Ok ( handle) => {
1022
1067
if let Err ( cause) = export_bao_impl ( cmd. inner , & mut cmd. tx , handle) . await {
1023
1068
cmd. tx
0 commit comments