Skip to content

Commit c8172a2

Browse files
remove old actor keepalive code and bug fixes for ActorCache
Signed-off-by: Hero <caleb@calebgj.io>
1 parent 6087972 commit c8172a2

3 files changed

Lines changed: 43 additions & 137 deletions

File tree

odorobo-manager/src/actors/scheduler_actor/actor_keepalive.rs

Lines changed: 0 additions & 101 deletions
This file was deleted.

odorobo-manager/src/actors/scheduler_actor/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
pub mod actor_keepalive;
2-
31
use std::ops::ControlFlow;
42
use std::sync::Arc;
53
use std::time::Duration;
@@ -22,16 +20,10 @@ use tokio::task::JoinSet;
2220
use tracing::{info, warn, trace, debug};
2321
use ulid::Ulid;
2422

25-
use crate::actors::scheduler_actor::actor_keepalive::ActorAgentKeepalive;
26-
//use crate::actors::scheduler_actor::actor_keepalive::CachedAgentActor;
27-
use crate::actors::scheduler_actor::actor_keepalive::CachedVMActor;
28-
use crate::actors::scheduler_actor::actor_keepalive::keepalive_agents;
29-
30-
3123

3224
#[derive(RemoteActor)]
3325
pub struct SchedulerActor {
34-
pub agent_actor_cache: ActorCache<AgentActor, CachedAgentActor>
26+
pub agent_actor_cache: ActorCache<SchedulerActor, AgentActor, CachedAgentActor>
3527

3628
//pub vm_actor_cache: Arc<Mutex<AHashMap<ActorId, CachedVMActor>>>,
3729
//pub vm_keepalive_task: Option<tokio::task::JoinHandle<()>>,
@@ -140,7 +132,7 @@ impl Actor for SchedulerActor {
140132
info!("Actor started! Scheduler peer id: {peer_id}");
141133

142134
Ok(Self {
143-
agent_actor_cache: ActorCache::new(AgentActorCacheUpdater)?,
135+
agent_actor_cache: ActorCache::new(actor_ref, AgentActorCacheUpdater)?,
144136
})
145137
}
146138

@@ -156,8 +148,12 @@ impl Actor for SchedulerActor {
156148
let Some(actor_ref) = actor_ref.upgrade() else {
157149
return Ok(ControlFlow::Break(ActorStopReason::Killed));
158150
};
151+
152+
self.agent_actor_cache.info().await;
159153

160-
self.agent_actor_cache.on_link_died(id);
154+
self.agent_actor_cache.on_link_died(id).await;
155+
156+
self.agent_actor_cache.info().await;
161157

162158
Ok(ControlFlow::Continue(()))
163159
}

odorobo-shared/src/actor_cache/mod.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use tokio::{sync::{Mutex, MutexGuard}, task::JoinHandle};
77
use stable_eyre::{Report, Result, eyre::eyre};
88
use tracing::{info, trace};
99

10+
use std::fmt;
11+
1012
// future refactor TODO because I don't know how to do it now.
1113
// The best way to make this would be that you crate a struct with #[derive(ActorCache)]
1214
// and then you impl ActorCache with setting the ChildActor and Data as types similar to https://github.com/tqwewe/kameo/blob/1d498c0566b613b9afe6d54965c4b191c84432e0/src/actor.rs#L122
@@ -18,11 +20,11 @@ use tracing::{info, trace};
1820
// so unfortunately instead I have to use self inside of the ActorCacheUpdater trait to make it work.
1921
// Which I hate
2022
// this would also make it where we dont need two structs, one for data and one for the update function hooks.
21-
// I thnk it would also likely make a lot of the generic types simpler.
23+
// I thnk it would also likely make a lot of the generic types simpler since hopefully their trait bounds would only be in one place.
2224

2325

2426
#[async_trait]
25-
pub trait ActorCacheUpdater<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static>: Sync + Send + Copy + 'static {
27+
pub trait ActorCacheUpdater<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static + fmt::Debug>: Sync + Send + Copy + 'static {
2628
// todo: this could probably be better if it was an iterator, but I am lazy and don't want to right now.
2729
async fn get_actor_refs(&self) -> Result<Vec<RemoteActorRef<ChildActor>>>;
2830
async fn on_update(&self, actor_ref: &RemoteActorRef<ChildActor>, previous_value: Option<Data>) -> Result<Data, Report>;
@@ -40,7 +42,8 @@ pub trait ActorCacheUpdater<ChildActor: Actor + RemoteActor, Data: Clone + Send
4042
// I am leaving it this way because I just wanted to get things working. We may need to change the way the data is stored in the future.
4143
// My suggestions are either replacing it with a concurrent map, or using a RwLock. Either one might help, but I am not dealing with it now.
4244
#[derive(Debug)]
43-
pub struct ActorCache<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static> {
45+
pub struct ActorCache<ParentActor: Actor + RemoteActor, ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static + fmt::Debug> {
46+
parent_actor_ref: ActorRef<ParentActor>,
4447
data_cache: Arc<Mutex<AHashMap<ActorId, Data>>>,
4548
keepalive_tasks: Arc<Mutex<AHashMap<ActorId, JoinHandle<()>>>>,
4649
actor_finder: Option<JoinHandle<()>>,
@@ -50,40 +53,25 @@ pub struct ActorCache<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync
5053

5154
// todo: impl Drop to automatically kill all the keepalive_tasks and the actor_finder task.
5255

53-
/*
54-
/// todo: implement display
55-
async fn print_agent_caches(&self) {
56-
let keepalives = self.agent_actor_keepalive_tasks.lock().await;
57-
let cache = self.agent_actor_cache.lock().await;
58-
59-
info!("agent actor cache data");
60-
for keepalive in keepalives.iter() {
61-
info!("keepalive: {keepalive:?}");
62-
}
63-
for actor in cache.iter() {
64-
info!("actor: {actor:?}");
65-
}
66-
}
67-
68-
*/
69-
70-
impl<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static> ActorCache<ChildActor, Data> {
56+
impl<ParentActor: Actor + RemoteActor, ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static + fmt::Debug> ActorCache<ParentActor, ChildActor, Data> {
7157
pub fn new(
58+
parent_actor_ref: ActorRef<ParentActor>,
7259
updater: impl ActorCacheUpdater<ChildActor, Data>
7360
) -> Result<Self, Report> {
7461

7562
let data_cache = Arc::new(Mutex::new(AHashMap::new()));
7663
let keepalive_tasks = Arc::new(Mutex::new(AHashMap::new()));
7764

7865
let actor_cache = ActorCache {
66+
parent_actor_ref: parent_actor_ref.clone(),
7967
data_cache: data_cache,
8068
keepalive_tasks: keepalive_tasks,
8169
actor_finder: None,
8270

8371
child_actor_type: PhantomData
8472
};
8573

86-
actor_cache.start_actor_finder(updater);
74+
actor_cache.start_actor_finder(parent_actor_ref, updater);
8775

8876
Ok(actor_cache)
8977
}
@@ -112,39 +100,48 @@ impl<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static> Actor
112100
self.data_cache.lock().await
113101
}
114102

115-
// todo: this needs a refactor because holy crap the indention.
116103
fn start_actor_finder(
117104
&self,
105+
parent_actor_ref: ActorRef<ParentActor>,
118106
updater: impl ActorCacheUpdater<ChildActor, Data>
119107
) {
120108
let keepalive_tasks_clone = Arc::clone(&self.keepalive_tasks);
121109
let data_cache_clone = Arc::clone(&self.data_cache);
122110

123111
tokio::spawn(async move {
112+
let mut interval = tokio::time::interval(Duration::from_secs(1));
124113
loop {
114+
info!("running actor_finder");
125115
let _ = Self::actor_finder(
116+
parent_actor_ref.clone(),
126117
Arc::clone(&keepalive_tasks_clone),
127118
Arc::clone(&data_cache_clone),
128119
updater
129-
);
120+
).await;
121+
122+
interval.tick().await;
130123
}
131124
});
132125
}
133126

134127
async fn actor_finder(
128+
parent_actor_ref: ActorRef<ParentActor>,
135129
keepalive_tasks: Arc<Mutex<AHashMap<ActorId, JoinHandle<()>>>>,
136130
data_cache: Arc<Mutex<AHashMap<ActorId, Data>>>,
137131
updater: impl ActorCacheUpdater<ChildActor, Data>
138132
) -> Result<(), Report> {
139133
let actor_refs = updater.get_actor_refs().await?;
140134

135+
info!("actor_finder actor_refs: {actor_refs:?}");
136+
141137
for actor_ref in actor_refs {
142138
tracing::trace!("UpdateAgents: agent_actor={:?}", actor_ref);
143139

144140
let mut locked_agent_actors_keepalives = keepalive_tasks.lock().await;
145141

146142
if !locked_agent_actors_keepalives.contains_key(&actor_ref.id()) {
147-
actor_ref.link_remote(&actor_ref).await?;
143+
144+
parent_actor_ref.link_remote(&actor_ref).await?;
148145

149146
let actor_ref_clone = actor_ref.clone();
150147
let data_cache_clone = Arc::clone(&data_cache);
@@ -201,4 +198,18 @@ impl<ChildActor: Actor + RemoteActor, Data: Clone + Send + Sync + 'static> Actor
201198
interval.tick().await;
202199
}
203200
}
201+
202+
// todo: get cappy to tell me how you are supposed to do this properly
203+
pub async fn info(&self) {
204+
let keepalives = self.keepalive_tasks.lock().await;
205+
let cache = self.data_cache.lock().await;
206+
207+
info!("agent actor cache data");
208+
for keepalive in keepalives.iter() {
209+
info!("keepalive: {keepalive:?}");
210+
}
211+
for data in cache.iter() {
212+
info!("data: {data:?}");
213+
}
214+
}
204215
}

0 commit comments

Comments
 (0)