dactor 0.3.0

An abstract framework for distributed actors in Rust
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use crate::errors::ClusterError;
use crate::node::NodeId;

/// Events emitted by the cluster membership system.
///
/// Subscribe via [`ClusterEvents::subscribe`] to react to topology changes
/// such as scaling, failover, or planned maintenance.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum ClusterEvent {
    /// A new node has joined the cluster and is ready to receive messages.
    NodeJoined(NodeId),
    /// A node has left the cluster (gracefully or due to failure).
    NodeLeft(NodeId),
    /// A node attempted to join but was rejected during the version handshake.
    ///
    /// This event is emitted when a connecting node fails the compatibility
    /// check (different wire protocol version or adapter) or when the
    /// handshake transport call itself fails. The rejected node does **not**
    /// appear in the cluster's node list.
    NodeRejected {
        /// The node that was rejected.
        node_id: NodeId,
        /// Why the node was rejected.
        reason: NodeRejectionReason,
        /// Human-readable detail message.
        detail: String,
    },
}

/// Reason a node was rejected during cluster join.
///
/// This is the **cluster-level** rejection reason, used in
/// [`ClusterEvent::NodeRejected`]. It is distinct from
/// [`RejectionReason`](crate::system_actors::RejectionReason), which is
/// the handshake-level (wire protocol) reason. Use the [`From`]
/// implementation to convert handshake rejections into cluster events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum NodeRejectionReason {
    /// The remote node's wire protocol MAJOR version differs (Category 1).
    IncompatibleProtocol,
    /// The remote node uses a different actor framework adapter.
    IncompatibleAdapter,
    /// The transport-level handshake call failed (timeout, network error,
    /// or the remote node did not respond).
    ConnectionFailed,
}

impl std::fmt::Display for NodeRejectionReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NodeRejectionReason::IncompatibleProtocol => {
                write!(f, "incompatible wire protocol")
            }
            NodeRejectionReason::IncompatibleAdapter => {
                write!(f, "incompatible adapter")
            }
            NodeRejectionReason::ConnectionFailed => {
                write!(f, "connection failed")
            }
        }
    }
}

impl From<crate::system_actors::RejectionReason> for NodeRejectionReason {
    fn from(reason: crate::system_actors::RejectionReason) -> Self {
        match reason {
            crate::system_actors::RejectionReason::IncompatibleProtocol => {
                NodeRejectionReason::IncompatibleProtocol
            }
            crate::system_actors::RejectionReason::IncompatibleAdapter => {
                NodeRejectionReason::IncompatibleAdapter
            }
        }
    }
}

/// Opaque handle returned by [`ClusterEvents::subscribe`], used to cancel
/// a subscription via [`ClusterEvents::unsubscribe`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SubscriptionId(pub(crate) u64);

impl SubscriptionId {
    /// Create a new `SubscriptionId` from a raw integer.
    ///
    /// Intended for use by adapter crates implementing `ClusterEvents`.
    pub fn from_raw(id: u64) -> Self {
        Self(id)
    }
}

/// Subscription to cluster membership events.
pub trait ClusterEvents: Send + Sync + 'static {
    /// Subscribe to cluster membership changes. The callback is invoked for
    /// each [`ClusterEvent`] (`NodeJoined`, `NodeLeft`, `NodeRejected`).
    /// Returns a [`SubscriptionId`] that can be used to cancel the
    /// subscription.
    fn subscribe(
        &self,
        on_event: Box<dyn Fn(ClusterEvent) + Send + Sync>,
    ) -> Result<SubscriptionId, ClusterError>;

    /// Remove a previously registered subscription. Idempotent.
    fn unsubscribe(&self, id: SubscriptionId) -> Result<(), ClusterError>;
}

// ---------------------------------------------------------------------------
// ClusterEventEmitter
// ---------------------------------------------------------------------------

/// In-process event emitter for cluster membership changes.
///
/// Manages subscriber callbacks and dispatches [`ClusterEvent`]s to all
/// active subscribers. Used by adapter runtimes to notify actors of
/// topology changes.
pub struct ClusterEventEmitter {
    next_id: u64,
    subscribers: std::collections::HashMap<SubscriptionId, Box<dyn Fn(ClusterEvent) + Send + Sync>>,
}

impl ClusterEventEmitter {
    /// Create a new emitter with no subscribers.
    pub fn new() -> Self {
        Self {
            next_id: 1,
            subscribers: std::collections::HashMap::new(),
        }
    }

    /// Subscribe to cluster events. Returns a subscription ID.
    pub fn subscribe(
        &mut self,
        on_event: Box<dyn Fn(ClusterEvent) + Send + Sync>,
    ) -> SubscriptionId {
        let id = SubscriptionId(self.next_id);
        self.next_id += 1;
        self.subscribers.insert(id, on_event);
        id
    }

    /// Remove a subscription. Idempotent.
    pub fn unsubscribe(&mut self, id: SubscriptionId) {
        self.subscribers.remove(&id);
    }

    /// Emit an event to all subscribers.
    pub fn emit(&self, event: ClusterEvent) {
        for callback in self.subscribers.values() {
            callback(event.clone());
        }
    }

    /// Number of active subscribers.
    pub fn subscriber_count(&self) -> usize {
        self.subscribers.len()
    }
}

impl Default for ClusterEventEmitter {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// AdapterCluster trait (R4: Connection management)
// ---------------------------------------------------------------------------

/// Connection management for adapter runtimes.
///
/// Adapters implement this trait to wire cluster discovery, node connections,
/// and health monitoring into their runtime. The dactor framework calls
/// these methods during startup and when topology changes are detected.
#[async_trait::async_trait]
pub trait AdapterCluster: Send + Sync + 'static {
    /// Connect to a remote node. Called when discovery finds a new peer
    /// or when reconnecting after failure.
    async fn connect(&self, node: &NodeId) -> Result<(), ClusterError>;

    /// Disconnect from a remote node. Called on graceful shutdown or when
    /// removing a node from the cluster.
    async fn disconnect(&self, node: &NodeId) -> Result<(), ClusterError>;

    /// Reconnect to a node (disconnect + connect). Used for recovery after
    /// transient failures.
    async fn reconnect(&self, node: &NodeId) -> Result<(), ClusterError> {
        self.disconnect(node).await?;
        self.connect(node).await
    }

    /// Check if a node is currently reachable.
    async fn is_reachable(&self, node: &NodeId) -> bool;

    /// Get the list of currently connected nodes.
    async fn connected_nodes(&self) -> Vec<NodeId>;
}

// ---------------------------------------------------------------------------
// HealthChecker trait (C5: Health delegation)
// ---------------------------------------------------------------------------

/// Result of a node health check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HealthStatus {
    /// Node is healthy and responsive.
    Healthy,
    /// Node is unhealthy or unresponsive.
    Unhealthy {
        /// Description of the health issue.
        reason: String,
    },
    /// Health check timed out.
    Timeout,
}

/// Trait for checking the health of a remote node.
///
/// Adapters delegate health checks to their underlying provider's
/// mechanism (e.g., gRPC health check, TCP ping, heartbeat).
#[async_trait::async_trait]
pub trait HealthChecker: Send + Sync + 'static {
    /// Check the health of a remote node.
    async fn check(&self, node: &NodeId) -> HealthStatus;
}

/// Called when a node becomes unreachable. Adapters implement this to
/// handle the failure (e.g., mark actors as stopped, notify watchers).
#[async_trait::async_trait]
pub trait UnreachableHandler: Send + Sync + 'static {
    /// Called when a node is determined to be unreachable.
    async fn on_node_unreachable(&self, node: &NodeId);
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::sync::Arc;

    #[test]
    fn cluster_event_emitter_subscribe_and_emit() {
        let mut emitter = ClusterEventEmitter::new();
        let count = Arc::new(AtomicU64::new(0));
        let count_clone = Arc::clone(&count);

        let _id = emitter.subscribe(Box::new(move |_event| {
            count_clone.fetch_add(1, Ordering::SeqCst);
        }));

        assert_eq!(emitter.subscriber_count(), 1);

        emitter.emit(ClusterEvent::NodeJoined(NodeId("n1".into())));
        emitter.emit(ClusterEvent::NodeLeft(NodeId("n1".into())));

        assert_eq!(count.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn cluster_event_emitter_unsubscribe() {
        let mut emitter = ClusterEventEmitter::new();
        let count = Arc::new(AtomicU64::new(0));
        let count_clone = Arc::clone(&count);

        let id = emitter.subscribe(Box::new(move |_event| {
            count_clone.fetch_add(1, Ordering::SeqCst);
        }));

        emitter.emit(ClusterEvent::NodeJoined(NodeId("n1".into())));
        assert_eq!(count.load(Ordering::SeqCst), 1);

        emitter.unsubscribe(id);
        assert_eq!(emitter.subscriber_count(), 0);

        emitter.emit(ClusterEvent::NodeJoined(NodeId("n2".into())));
        assert_eq!(count.load(Ordering::SeqCst), 1); // no change
    }

    #[test]
    fn cluster_event_emitter_multiple_subscribers() {
        let mut emitter = ClusterEventEmitter::new();
        let count1 = Arc::new(AtomicU64::new(0));
        let count2 = Arc::new(AtomicU64::new(0));
        let c1 = Arc::clone(&count1);
        let c2 = Arc::clone(&count2);

        emitter.subscribe(Box::new(move |_| {
            c1.fetch_add(1, Ordering::SeqCst);
        }));
        emitter.subscribe(Box::new(move |_| {
            c2.fetch_add(10, Ordering::SeqCst);
        }));

        emitter.emit(ClusterEvent::NodeJoined(NodeId("n1".into())));

        assert_eq!(count1.load(Ordering::SeqCst), 1);
        assert_eq!(count2.load(Ordering::SeqCst), 10);
    }

    #[test]
    fn cluster_event_emitter_captures_event_data() {
        let mut emitter = ClusterEventEmitter::new();
        let captured = Arc::new(std::sync::Mutex::new(Vec::new()));
        let captured_clone = Arc::clone(&captured);

        emitter.subscribe(Box::new(move |event| {
            captured_clone.lock().unwrap().push(event);
        }));

        emitter.emit(ClusterEvent::NodeJoined(NodeId("alpha".into())));
        emitter.emit(ClusterEvent::NodeLeft(NodeId("beta".into())));

        let events = captured.lock().unwrap();
        assert_eq!(events.len(), 2);
        assert_eq!(events[0], ClusterEvent::NodeJoined(NodeId("alpha".into())));
        assert_eq!(events[1], ClusterEvent::NodeLeft(NodeId("beta".into())));
    }

    #[test]
    fn health_status_variants() {
        let healthy = HealthStatus::Healthy;
        assert_eq!(healthy, HealthStatus::Healthy);

        let unhealthy = HealthStatus::Unhealthy {
            reason: "connection refused".into(),
        };
        assert!(matches!(unhealthy, HealthStatus::Unhealthy { .. }));

        let timeout = HealthStatus::Timeout;
        assert_eq!(timeout, HealthStatus::Timeout);
    }

    #[test]
    fn subscription_id_from_raw() {
        let id = SubscriptionId::from_raw(42);
        assert_eq!(id, SubscriptionId(42));
    }

    // -- NodeRejected / NodeRejectionReason tests --

    #[test]
    fn node_rejected_event_construction() {
        let event = ClusterEvent::NodeRejected {
            node_id: NodeId("bad-node".into()),
            reason: NodeRejectionReason::IncompatibleProtocol,
            detail: "wire 1.0 vs 0.2".into(),
        };
        match &event {
            ClusterEvent::NodeRejected {
                node_id,
                reason,
                detail,
            } => {
                assert_eq!(node_id, &NodeId("bad-node".into()));
                assert_eq!(*reason, NodeRejectionReason::IncompatibleProtocol);
                assert!(detail.contains("1.0"));
            }
            _ => panic!("expected NodeRejected"),
        }
    }

    #[test]
    fn node_rejected_emitted_to_subscribers() {
        let mut emitter = ClusterEventEmitter::new();
        let captured = Arc::new(std::sync::Mutex::new(Vec::new()));
        let captured_clone = Arc::clone(&captured);

        emitter.subscribe(Box::new(move |event| {
            captured_clone.lock().unwrap().push(event);
        }));

        emitter.emit(ClusterEvent::NodeRejected {
            node_id: NodeId("rejected-node".into()),
            reason: NodeRejectionReason::IncompatibleAdapter,
            detail: "kameo vs ractor".into(),
        });

        let events = captured.lock().unwrap();
        assert_eq!(events.len(), 1);
        assert!(matches!(
            &events[0],
            ClusterEvent::NodeRejected {
                reason: NodeRejectionReason::IncompatibleAdapter,
                ..
            }
        ));
    }

    #[test]
    fn node_rejection_reason_from_handshake_rejection() {
        use crate::system_actors::RejectionReason;

        let protocol: NodeRejectionReason = RejectionReason::IncompatibleProtocol.into();
        assert_eq!(protocol, NodeRejectionReason::IncompatibleProtocol);

        let adapter: NodeRejectionReason = RejectionReason::IncompatibleAdapter.into();
        assert_eq!(adapter, NodeRejectionReason::IncompatibleAdapter);
    }

    #[test]
    fn node_rejection_reason_display() {
        assert_eq!(
            NodeRejectionReason::IncompatibleProtocol.to_string(),
            "incompatible wire protocol"
        );
        assert_eq!(
            NodeRejectionReason::IncompatibleAdapter.to_string(),
            "incompatible adapter"
        );
        assert_eq!(
            NodeRejectionReason::ConnectionFailed.to_string(),
            "connection failed"
        );
    }

    #[test]
    fn node_rejection_reason_connection_failed() {
        let event = ClusterEvent::NodeRejected {
            node_id: NodeId("unreachable".into()),
            reason: NodeRejectionReason::ConnectionFailed,
            detail: "transport error: connection refused".into(),
        };
        assert!(matches!(
            event,
            ClusterEvent::NodeRejected {
                reason: NodeRejectionReason::ConnectionFailed,
                ..
            }
        ));
    }

    #[test]
    fn node_rejected_equality() {
        let a = ClusterEvent::NodeRejected {
            node_id: NodeId("n1".into()),
            reason: NodeRejectionReason::IncompatibleProtocol,
            detail: "test".into(),
        };
        let b = ClusterEvent::NodeRejected {
            node_id: NodeId("n1".into()),
            reason: NodeRejectionReason::IncompatibleProtocol,
            detail: "test".into(),
        };
        assert_eq!(a, b);
    }

}