mm1_node/runtime/system/
protocol_system.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use mm1_common::errors::error_of::ErrorOf;
use mm1_core::context::Call;
use mm1_core::envelope::{Envelope, EnvelopeInfo};
use mm1_proto_system::{
    Exit, InitAck, Kill, Link, SpawnErrorKind, SpawnRequest, SpawnResponse, TrapExit, Unlink,
    Unwatch, Watch, WatchRef,
};
use tokio::sync::oneshot;
use tracing::trace;

use crate::runtime::context::ActorContext;
use crate::runtime::sys_call::SysCall;
use crate::runtime::sys_msg::{ExitReason, SysLink, SysMsg};
use crate::runtime::system::Local;
use crate::runtime::{config, container};

impl Call<Local, SpawnRequest<Local>> for ActorContext {
    type Outcome = SpawnResponse;

    async fn call(&mut self, _to: Local, message: SpawnRequest<Local>) -> Self::Outcome {
        let SpawnRequest {
            runnable,
            ack_to,
            link_to,
        } = message;

        let actor_key = self
            .actor_key
            .child(runnable.func_name(), Default::default());

        let execute_on = self.rt_api.choose_executor(&actor_key);

        trace!("starting [ack-to: {:?}; link-to: {:?}]", ack_to, link_to);

        let subnet_lease = self
            .rt_api
            .request_address(config::stubs::ACTOR_NETMASK)
            .await
            .map_err(|e| ErrorOf::new(SpawnErrorKind::ResourceConstraint, e.to_string()))?;

        trace!("subnet-lease: {}", subnet_lease.net_address());

        let rt_api = self.rt_api.clone();
        let container = container::Container::create(
            container::ContainerArgs {
                ack_to,
                link_to,
                actor_key,
                inbox_size: config::stubs::INBOX_SIZE,
                subnet_lease,
                rt_api,
            },
            runnable,
        )
        .map_err(|e| ErrorOf::new(SpawnErrorKind::InternalError, e.to_string()))?;
        let actor_address = container.actor_address();

        trace!("actor-address: {}", actor_address);

        // TODO: maybe keep it somewhere too?
        let _join_handle = execute_on.spawn(container.run());

        Ok(actor_address)
    }
}

impl Call<Local, Kill> for ActorContext {
    type Outcome = bool;

    async fn call(&mut self, _to: Local, message: Kill) -> Self::Outcome {
        let Kill { peer: address } = message;

        self.rt_api.sys_send(address, SysMsg::Kill).is_ok()
    }
}

impl Call<Local, InitAck> for ActorContext {
    type Outcome = ();

    async fn call(&mut self, _to: Local, message: InitAck) -> Self::Outcome {
        let Some(ack_to_address) = self.ack_to.take() else {
            return;
        };
        let envelope = Envelope::new(EnvelopeInfo::new(ack_to_address), message);
        let _ = self
            .rt_api
            .send(ack_to_address, true, envelope.into_erased());
    }
}

impl Call<Local, TrapExit> for ActorContext {
    type Outcome = ();

    async fn call(&mut self, _to: Local, message: TrapExit) -> Self::Outcome {
        let TrapExit { enable } = message;

        self.call.invoke(SysCall::TrapExit(enable)).await;
    }
}

impl Call<Local, Link> for ActorContext {
    type Outcome = ();

    async fn call(&mut self, _to: Local, message: Link) -> Self::Outcome {
        let Link { peer } = message;

        self.call
            .invoke(SysCall::Link {
                sender:   self.actor_address,
                receiver: peer,
            })
            .await;
    }
}

impl Call<Local, Unlink> for ActorContext {
    type Outcome = ();

    async fn call(&mut self, _to: Local, message: Unlink) -> Self::Outcome {
        let Unlink { peer } = message;

        self.call
            .invoke(SysCall::Unlink {
                sender:   self.actor_address,
                receiver: peer,
            })
            .await;
    }
}

impl Call<Local, Exit> for ActorContext {
    type Outcome = bool;

    async fn call(&mut self, _to: Local, msg: Exit) -> Self::Outcome {
        let Exit { peer } = msg;
        self.rt_api
            .sys_send(
                peer,
                SysMsg::Link(SysLink::Exit {
                    sender:   self.actor_address,
                    receiver: peer,
                    reason:   ExitReason::Terminate,
                }),
            )
            .is_ok()
    }
}

impl Call<Local, Watch> for ActorContext {
    type Outcome = WatchRef;

    async fn call(&mut self, _to: Local, msg: Watch) -> Self::Outcome {
        let Watch { peer } = msg;
        let (reply_tx, reply_rx) = oneshot::channel();
        self.call
            .invoke(SysCall::Watch {
                sender: self.actor_address,
                receiver: peer,
                reply_tx,
            })
            .await;
        reply_rx.await.expect("sys-call remained unanswered")
    }
}

impl Call<Local, Unwatch> for ActorContext {
    type Outcome = ();

    async fn call(&mut self, _to: Local, msg: Unwatch) -> Self::Outcome {
        let Unwatch { watch_ref } = msg;
        self.call
            .invoke(SysCall::Unwatch {
                sender: self.actor_address,
                watch_ref,
            })
            .await;
    }
}