rustkernel-ecosystem 0.4.0

Web framework integrations for RustKernels: Axum REST, Tower middleware, gRPC, Actix actors
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
//! Actix Actor Integration
//!
//! Provides GPU-persistent actors for the Actix framework.
//!
//! # Example
//!
//! ```rust,ignore
//! use rustkernel_ecosystem::actix::{KernelActor, KernelActorConfig};
//! use actix::prelude::*;
//!
//! let actor = KernelActor::new(registry, config);
//! let addr = actor.start();
//!
//! // Send kernel request
//! let result = addr.send(ExecuteKernel {
//!     kernel_id: "graph/pagerank".to_string(),
//!     input: serde_json::json!({ "edges": [...] }),
//! }).await?;
//! ```

use crate::{EcosystemError, RequestMetadata, ResponseMetadata};
use actix::prelude::*;
use rustkernel_core::registry::KernelRegistry;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Actor configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelActorConfig {
    /// Actor name
    pub name: String,
    /// Mailbox capacity
    pub mailbox_capacity: usize,
    /// Default timeout for requests
    pub default_timeout: Duration,
    /// Enable message batching
    pub batching_enabled: bool,
    /// Batch size
    pub batch_size: usize,
    /// Batch timeout
    pub batch_timeout: Duration,
}

impl Default for KernelActorConfig {
    fn default() -> Self {
        Self {
            name: "kernel-actor".to_string(),
            mailbox_capacity: 1000,
            default_timeout: Duration::from_secs(30),
            batching_enabled: false,
            batch_size: 100,
            batch_timeout: Duration::from_millis(10),
        }
    }
}

/// GPU-persistent kernel actor
pub struct KernelActor {
    registry: Arc<KernelRegistry>,
    config: KernelActorConfig,
    start_time: Instant,
    messages_processed: u64,
}

impl KernelActor {
    /// Create a new kernel actor
    pub fn new(registry: Arc<KernelRegistry>, config: KernelActorConfig) -> Self {
        Self {
            registry,
            config,
            start_time: Instant::now(),
            messages_processed: 0,
        }
    }

    /// Create with default configuration
    pub fn with_registry(registry: Arc<KernelRegistry>) -> Self {
        Self::new(registry, KernelActorConfig::default())
    }

    /// Get actor statistics
    pub fn stats(&self) -> ActorStats {
        ActorStats {
            uptime_secs: self.start_time.elapsed().as_secs(),
            messages_processed: self.messages_processed,
            kernels_available: self.registry.stats().total,
        }
    }
}

impl Actor for KernelActor {
    type Context = Context<Self>;

    fn started(&mut self, _ctx: &mut Self::Context) {
        tracing::info!("KernelActor started: {}", self.config.name);
    }

    fn stopped(&mut self, _ctx: &mut Self::Context) {
        tracing::info!(
            "KernelActor stopped: {} (processed {} messages)",
            self.config.name,
            self.messages_processed
        );
    }
}

/// Execute kernel message
#[derive(Debug, Clone, Serialize, Deserialize, Message)]
#[rtype(result = "Result<ExecuteResult, ActorError>")]
pub struct ExecuteKernel {
    /// Kernel ID
    pub kernel_id: String,
    /// Input data
    pub input: serde_json::Value,
    /// Request metadata
    #[serde(default)]
    pub metadata: RequestMetadata,
}

/// Execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecuteResult {
    /// Request ID
    pub request_id: String,
    /// Kernel ID
    pub kernel_id: String,
    /// Output data
    pub output: serde_json::Value,
    /// Execution metadata
    pub metadata: ResponseMetadata,
}

impl Handler<ExecuteKernel> for KernelActor {
    type Result = Result<ExecuteResult, ActorError>;

    fn handle(&mut self, msg: ExecuteKernel, _ctx: &mut Context<Self>) -> Self::Result {
        let start = Instant::now();
        self.messages_processed += 1;

        let request_id = uuid::Uuid::new_v4().to_string();
        let timeout = Duration::from_millis(
            msg.metadata
                .timeout_ms
                .unwrap_or(self.config.default_timeout.as_millis() as u64),
        );

        // Try batch kernel execution
        if let Some(entry) = self.registry.get_batch(&msg.kernel_id) {
            let kernel = entry.create();

            let input_bytes = serde_json::to_vec(&msg.input)
                .map_err(|e| ActorError::InvalidInput(format!("Invalid input: {}", e)))?;

            // Execute synchronously by blocking on the async operation.
            // Actix actor handlers are synchronous; bridge to async via block_in_place.
            let result = tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(async {
                    tokio::time::timeout(timeout, kernel.execute_dyn(&input_bytes)).await
                })
            });

            match result {
                Ok(Ok(output_bytes)) => {
                    let output: serde_json::Value =
                        serde_json::from_slice(&output_bytes).map_err(|e| {
                            ActorError::ExecutionFailed(format!("Output deserialization: {}", e))
                        })?;

                    let duration_us = start.elapsed().as_micros() as u64;

                    Ok(ExecuteResult {
                        request_id,
                        kernel_id: msg.kernel_id,
                        output,
                        metadata: ResponseMetadata {
                            duration_us,
                            backend: entry.metadata.mode.as_str().to_uppercase(),
                            gpu_memory_bytes: None,
                            trace_id: msg.metadata.trace_id,
                        },
                    })
                }
                Ok(Err(e)) => Err(ActorError::ExecutionFailed(e.to_string())),
                Err(_) => Err(ActorError::Timeout),
            }
        } else if self.registry.get(&msg.kernel_id).is_some() {
            Err(ActorError::InvalidInput(format!(
                "Kernel '{}' is a Ring kernel and cannot be executed via actor message. \
                 Use the Ring protocol for persistent kernel dispatch.",
                msg.kernel_id
            )))
        } else {
            Err(ActorError::KernelNotFound(msg.kernel_id))
        }
    }
}

/// Get kernel info message
#[derive(Debug, Clone, Message)]
#[rtype(result = "Result<KernelInfo, ActorError>")]
pub struct GetKernelInfo {
    /// Kernel ID
    pub kernel_id: String,
}

/// Kernel info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelInfo {
    /// Kernel ID
    pub id: String,
    /// Domain
    pub domain: String,
    /// Mode
    pub mode: String,
    /// Description
    pub description: String,
}

impl Handler<GetKernelInfo> for KernelActor {
    type Result = Result<KernelInfo, ActorError>;

    fn handle(&mut self, msg: GetKernelInfo, _ctx: &mut Context<Self>) -> Self::Result {
        let kernel_meta = self
            .registry
            .get(&msg.kernel_id)
            .ok_or_else(|| ActorError::KernelNotFound(msg.kernel_id.clone()))?;

        Ok(KernelInfo {
            id: kernel_meta.id.clone(),
            domain: format!("{:?}", kernel_meta.domain),
            mode: format!("{:?}", kernel_meta.mode),
            description: kernel_meta.description.clone(),
        })
    }
}

/// List kernels message
#[derive(Debug, Clone, Default, Message)]
#[rtype(result = "Result<Vec<KernelInfo>, ActorError>")]
pub struct ListKernels {
    /// Filter by domain
    pub domain: Option<String>,
}

impl Handler<ListKernels> for KernelActor {
    type Result = Result<Vec<KernelInfo>, ActorError>;

    fn handle(&mut self, msg: ListKernels, _ctx: &mut Context<Self>) -> Self::Result {
        let kernels: Vec<KernelInfo> = self
            .registry
            .all_kernel_ids()
            .iter()
            .filter_map(|id| self.registry.get(id))
            .filter(|k| {
                if let Some(ref domain) = msg.domain {
                    format!("{:?}", k.domain).to_lowercase() == domain.to_lowercase()
                } else {
                    true
                }
            })
            .map(|k| KernelInfo {
                id: k.id.clone(),
                domain: format!("{:?}", k.domain),
                mode: format!("{:?}", k.mode),
                description: k.description.clone(),
            })
            .collect();

        Ok(kernels)
    }
}

/// Get stats message
#[derive(Debug, Clone, Message)]
#[rtype(result = "Result<ActorStats, ActorError>")]
pub struct GetStats;

impl Handler<GetStats> for KernelActor {
    type Result = Result<ActorStats, ActorError>;

    fn handle(&mut self, _msg: GetStats, _ctx: &mut Context<Self>) -> Self::Result {
        Ok(self.stats())
    }
}

/// Actor statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActorStats {
    /// Uptime in seconds
    pub uptime_secs: u64,
    /// Messages processed
    pub messages_processed: u64,
    /// Available kernels
    pub kernels_available: usize,
}

/// Actor errors
#[derive(Debug, thiserror::Error)]
pub enum ActorError {
    /// Kernel not found
    #[error("Kernel not found: {0}")]
    KernelNotFound(String),

    /// Execution failed
    #[error("Execution failed: {0}")]
    ExecutionFailed(String),

    /// Invalid input
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Timeout
    #[error("Request timed out")]
    Timeout,

    /// Mailbox full
    #[error("Mailbox full")]
    MailboxFull,

    /// Actor stopped
    #[error("Actor stopped")]
    ActorStopped,
}

impl From<EcosystemError> for ActorError {
    fn from(err: EcosystemError) -> Self {
        match err {
            EcosystemError::KernelNotFound(id) => ActorError::KernelNotFound(id),
            EcosystemError::ExecutionFailed(msg) => ActorError::ExecutionFailed(msg),
            EcosystemError::InvalidRequest(msg) => ActorError::InvalidInput(msg),
            _ => ActorError::ExecutionFailed(err.to_string()),
        }
    }
}

/// Supervisor for managing kernel actors
pub struct KernelActorSupervisor {
    registry: Arc<KernelRegistry>,
    actors: Vec<Addr<KernelActor>>,
}

impl KernelActorSupervisor {
    /// Create a new supervisor
    pub fn new(registry: Arc<KernelRegistry>) -> Self {
        Self {
            registry,
            actors: Vec::new(),
        }
    }

    /// Spawn a new actor
    pub fn spawn(&mut self, config: KernelActorConfig) -> Addr<KernelActor> {
        let actor = KernelActor::new(self.registry.clone(), config);
        let addr = actor.start();
        self.actors.push(addr.clone());
        addr
    }

    /// Get all actor addresses
    pub fn actors(&self) -> &[Addr<KernelActor>] {
        &self.actors
    }

    /// Get number of actors
    pub fn actor_count(&self) -> usize {
        self.actors.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_actor_config() {
        let config = KernelActorConfig::default();
        assert_eq!(config.mailbox_capacity, 1000);
        assert!(!config.batching_enabled);
    }

    #[actix_rt::test]
    async fn test_kernel_actor() {
        let registry = Arc::new(KernelRegistry::new());
        let actor = KernelActor::with_registry(registry);
        let addr = actor.start();

        // Try to execute nonexistent kernel
        let result = addr
            .send(ExecuteKernel {
                kernel_id: "nonexistent".to_string(),
                input: serde_json::json!({}),
                metadata: RequestMetadata::default(),
            })
            .await
            .unwrap();

        assert!(matches!(result, Err(ActorError::KernelNotFound(_))));
    }

    #[actix_rt::test]
    async fn test_get_stats() {
        let registry = Arc::new(KernelRegistry::new());
        let actor = KernelActor::with_registry(registry);
        let addr = actor.start();

        let stats = addr.send(GetStats).await.unwrap().unwrap();
        assert_eq!(stats.messages_processed, 0);
    }

    #[test]
    fn test_supervisor() {
        let registry = Arc::new(KernelRegistry::new());
        let supervisor = KernelActorSupervisor::new(registry);

        assert_eq!(supervisor.actor_count(), 0);
    }
}