tatara-engine 0.2.245

Drivers, nix eval, cluster logic, P2P, and scheduling engine for tatara
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use anyhow::Result;
use std::sync::Arc;

use crate::domain::store_adapter::ClusterStoreAdapter;
use tatara_core::domain::allocation::Allocation;
use tatara_core::domain::job::{Constraint, Job, JobStatus, JobType, Resources};
use tatara_core::domain::node::{Node, NodeStatus};

/// Scheduling strategy for task placement.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SchedulingStrategy {
    /// Bin-pack: prefer nodes with the least remaining capacity (tightest fit).
    BinPack,
    /// Spread: prefer nodes with the most remaining capacity (even distribution).
    Spread,
}

impl Default for SchedulingStrategy {
    fn default() -> Self {
        Self::BinPack
    }
}

/// Evaluates pending jobs and creates allocation plans.
pub struct Evaluator {
    store: Arc<ClusterStoreAdapter>,
    strategy: SchedulingStrategy,
}

impl Evaluator {
    pub fn new(store: Arc<ClusterStoreAdapter>) -> Self {
        Self {
            store,
            strategy: SchedulingStrategy::default(),
        }
    }

    pub fn with_strategy(mut self, strategy: SchedulingStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Process all pending jobs and create allocations.
    pub async fn evaluate(&self) -> Result<Vec<Allocation>> {
        let jobs = self.store.list_jobs().await;
        let nodes = self.store.list_nodes().await;
        let mut new_allocations = Vec::new();

        for job in &jobs {
            if job.status != JobStatus::Pending {
                continue;
            }

            let allocs = self.evaluate_job(job, &nodes).await?;
            for alloc in allocs {
                self.store.put_allocation(alloc.clone()).await?;
                new_allocations.push(alloc);
            }

            if !new_allocations.is_empty() {
                self.store
                    .update_job_status(&job.id, JobStatus::Running)
                    .await?;
            }
        }

        Ok(new_allocations)
    }

    async fn evaluate_job(&self, job: &Job, nodes: &[Node]) -> Result<Vec<Allocation>> {
        let mut allocations = Vec::new();

        // Filter to ready, eligible nodes
        let ready_nodes: Vec<&Node> = nodes
            .iter()
            .filter(|n| n.status == NodeStatus::Ready)
            .filter(|n| n.eligible)
            .collect();

        if ready_nodes.is_empty() {
            tracing::warn!(job_id = %job.id, "No ready/eligible nodes available for scheduling");
            return Ok(allocations);
        }

        for group in &job.groups {
            let count = match job.job_type {
                JobType::System => ready_nodes.len() as u32,
                _ => group.count,
            };

            // Track available resources as we allocate (for this eval cycle)
            let mut node_available: Vec<(&Node, Resources)> = ready_nodes
                .iter()
                .map(|n| (*n, n.available_resources.clone()))
                .collect();

            for i in 0..count {
                let node = match job.job_type {
                    JobType::System => ready_nodes[i as usize],
                    _ => {
                        match pick_node(
                            &node_available,
                            &group.resources,
                            &job.constraints,
                            self.strategy,
                        ) {
                            Some((node, idx)) => {
                                // Deduct resources from tracking
                                node_available[idx].1.cpu_mhz = node_available[idx]
                                    .1
                                    .cpu_mhz
                                    .saturating_sub(group.resources.cpu_mhz);
                                node_available[idx].1.memory_mb = node_available[idx]
                                    .1
                                    .memory_mb
                                    .saturating_sub(group.resources.memory_mb);
                                node
                            }
                            None => {
                                tracing::warn!(
                                    job_id = %job.id,
                                    group = %group.name,
                                    instance = i,
                                    "No node with sufficient resources for allocation"
                                );
                                continue;
                            }
                        }
                    }
                };

                let task_names: Vec<String> = group.tasks.iter().map(|t| t.name.clone()).collect();

                let alloc = Allocation::new(
                    job.id.clone(),
                    group.name.clone(),
                    node.id.clone(),
                    task_names,
                );

                allocations.push(alloc);
            }
        }

        Ok(allocations)
    }
}

/// Pick the best node for a task group based on resource requirements,
/// constraints, and scheduling strategy.
///
/// Returns the node and its index in the candidates list, or None if
/// no node satisfies the requirements.
fn pick_node<'a>(
    candidates: &[(&'a Node, Resources)],
    required: &Resources,
    constraints: &[Constraint],
    strategy: SchedulingStrategy,
) -> Option<(&'a Node, usize)> {
    let mut best: Option<(usize, f64)> = None;

    for (idx, (node, available)) in candidates.iter().enumerate() {
        // Resource filtering: node must have enough resources
        if !resources_sufficient(available, required) {
            continue;
        }

        // Constraint evaluation: all constraints must match
        if !constraints_match(node, constraints) {
            continue;
        }

        // Score the node based on strategy
        let score = match strategy {
            SchedulingStrategy::BinPack => {
                // Lower remaining = better score (tighter packing)
                // We want to MINIMIZE remaining capacity, so higher score = less remaining
                let remaining_cpu = available.cpu_mhz.saturating_sub(required.cpu_mhz);
                let remaining_mem = available.memory_mb.saturating_sub(required.memory_mb);
                // Invert: smaller remaining gets higher score
                let max_cpu = node.total_resources.cpu_mhz.max(1) as f64;
                let max_mem = node.total_resources.memory_mb.max(1) as f64;
                let cpu_utilization = 1.0 - (remaining_cpu as f64 / max_cpu);
                let mem_utilization = 1.0 - (remaining_mem as f64 / max_mem);
                (cpu_utilization + mem_utilization) / 2.0
            }
            SchedulingStrategy::Spread => {
                // Higher remaining = better score (more spread out)
                let remaining_cpu = available.cpu_mhz.saturating_sub(required.cpu_mhz);
                let remaining_mem = available.memory_mb.saturating_sub(required.memory_mb);
                let max_cpu = node.total_resources.cpu_mhz.max(1) as f64;
                let max_mem = node.total_resources.memory_mb.max(1) as f64;
                let cpu_headroom = remaining_cpu as f64 / max_cpu;
                let mem_headroom = remaining_mem as f64 / max_mem;
                (cpu_headroom + mem_headroom) / 2.0
            }
        };

        match best {
            None => best = Some((idx, score)),
            Some((_, best_score)) if score > best_score => best = Some((idx, score)),
            _ => {}
        }
    }

    best.map(|(idx, _)| (candidates[idx].0, idx))
}

/// Check if available resources meet the requirements.
fn resources_sufficient(available: &Resources, required: &Resources) -> bool {
    // If no resources are requested (both 0), any node qualifies
    if required.cpu_mhz == 0 && required.memory_mb == 0 {
        return true;
    }

    (required.cpu_mhz == 0 || available.cpu_mhz >= required.cpu_mhz)
        && (required.memory_mb == 0 || available.memory_mb >= required.memory_mb)
}

/// Evaluate all constraints against a node's attributes.
fn constraints_match(node: &Node, constraints: &[Constraint]) -> bool {
    constraints.iter().all(|c| constraint_matches(node, c))
}

/// Evaluate a single constraint against a node.
fn constraint_matches(node: &Node, constraint: &Constraint) -> bool {
    let attr_value = match constraint.attribute.as_str() {
        // Built-in attributes
        "os" | "${attr.os}" => Some(node.attributes.get("os").map(|s| s.as_str()).unwrap_or("")),
        "arch" | "${attr.arch}" => Some(
            node.attributes
                .get("arch")
                .map(|s| s.as_str())
                .unwrap_or(""),
        ),
        "hostname" | "${attr.hostname}" => Some(
            node.attributes
                .get("hostname")
                .map(|s| s.as_str())
                .unwrap_or(""),
        ),
        // Generic attribute lookup
        attr => {
            let key = attr
                .strip_prefix("${attr.")
                .and_then(|s| s.strip_suffix('}'))
                .unwrap_or(attr);
            node.attributes.get(key).map(|s| s.as_str())
        }
    };

    let Some(attr_value) = attr_value else {
        // Attribute not found on node — constraint fails unless operator is "!="
        return constraint.operator == "!=";
    };

    match constraint.operator.as_str() {
        "=" | "==" => attr_value == constraint.value,
        "!=" => attr_value != constraint.value,
        ">" => attr_value
            .parse::<f64>()
            .ok()
            .zip(constraint.value.parse::<f64>().ok())
            .map(|(a, b)| a > b)
            .unwrap_or(false),
        "<" => attr_value
            .parse::<f64>()
            .ok()
            .zip(constraint.value.parse::<f64>().ok())
            .map(|(a, b)| a < b)
            .unwrap_or(false),
        ">=" => attr_value
            .parse::<f64>()
            .ok()
            .zip(constraint.value.parse::<f64>().ok())
            .map(|(a, b)| a >= b)
            .unwrap_or(false),
        "<=" => attr_value
            .parse::<f64>()
            .ok()
            .zip(constraint.value.parse::<f64>().ok())
            .map(|(a, b)| a <= b)
            .unwrap_or(false),
        "regexp" | "~" => regex_match(attr_value, &constraint.value),
        "set_contains" => attr_value.split(',').any(|v| v.trim() == constraint.value),
        _ => {
            tracing::warn!(
                operator = %constraint.operator,
                "Unknown constraint operator, defaulting to equality"
            );
            attr_value == constraint.value
        }
    }
}

/// Simple regex match using basic patterns (no full regex crate dependency).
fn regex_match(value: &str, pattern: &str) -> bool {
    // Simple glob-like matching: * matches anything
    if pattern == "*" {
        return true;
    }
    if let Some(suffix) = pattern.strip_prefix('*') {
        return value.ends_with(suffix);
    }
    if let Some(prefix) = pattern.strip_suffix('*') {
        return value.starts_with(prefix);
    }
    value == pattern
}

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

    fn make_node(id: &str, cpu: u64, mem: u64, attrs: &[(&str, &str)]) -> Node {
        let mut attributes = HashMap::new();
        for (k, v) in attrs {
            attributes.insert(k.to_string(), v.to_string());
        }
        Node {
            id: id.to_string(),
            address: "127.0.0.1:4647".to_string(),
            status: NodeStatus::Ready,
            eligible: true,
            total_resources: Resources {
                cpu_mhz: cpu,
                memory_mb: mem,
            },
            available_resources: Resources {
                cpu_mhz: cpu,
                memory_mb: mem,
            },
            attributes,
            drivers: vec![],
            last_heartbeat: chrono::Utc::now(),
            allocations: Vec::new(),
        }
    }

    #[test]
    fn test_resources_sufficient() {
        let available = Resources {
            cpu_mhz: 2000,
            memory_mb: 1024,
        };
        let required = Resources {
            cpu_mhz: 1000,
            memory_mb: 512,
        };
        assert!(resources_sufficient(&available, &required));

        let too_much = Resources {
            cpu_mhz: 3000,
            memory_mb: 512,
        };
        assert!(!resources_sufficient(&available, &too_much));
    }

    #[test]
    fn test_resources_zero_means_any() {
        let available = Resources {
            cpu_mhz: 100,
            memory_mb: 64,
        };
        let zero = Resources {
            cpu_mhz: 0,
            memory_mb: 0,
        };
        assert!(resources_sufficient(&available, &zero));
    }

    #[test]
    fn test_constraint_equality() {
        let node = make_node("n1", 2000, 1024, &[("os", "linux"), ("arch", "x86_64")]);
        let c = Constraint {
            attribute: "os".to_string(),
            operator: "=".to_string(),
            value: "linux".to_string(),
        };
        assert!(constraint_matches(&node, &c));

        let c2 = Constraint {
            attribute: "os".to_string(),
            operator: "=".to_string(),
            value: "macos".to_string(),
        };
        assert!(!constraint_matches(&node, &c2));
    }

    #[test]
    fn test_constraint_not_equal() {
        let node = make_node("n1", 2000, 1024, &[("os", "linux")]);
        let c = Constraint {
            attribute: "os".to_string(),
            operator: "!=".to_string(),
            value: "windows".to_string(),
        };
        assert!(constraint_matches(&node, &c));
    }

    #[test]
    fn test_constraint_missing_attribute() {
        let node = make_node("n1", 2000, 1024, &[("os", "linux")]);
        let c = Constraint {
            attribute: "gpu".to_string(),
            operator: "=".to_string(),
            value: "true".to_string(),
        };
        assert!(!constraint_matches(&node, &c));
    }

    #[test]
    fn test_bin_pack_picks_tightest_fit() {
        let n1 = make_node("big", 4000, 2048, &[]);
        let n2 = make_node("small", 2000, 1024, &[]);

        let candidates = vec![
            (&n1, n1.available_resources.clone()),
            (&n2, n2.available_resources.clone()),
        ];
        let required = Resources {
            cpu_mhz: 1000,
            memory_mb: 512,
        };

        let result = pick_node(&candidates, &required, &[], SchedulingStrategy::BinPack);
        assert!(result.is_some());
        // Bin-pack should prefer the smaller node (tighter fit)
        assert_eq!(result.unwrap().0.id, "small");
    }

    #[test]
    fn test_spread_picks_most_headroom() {
        let n1 = make_node("big", 4000, 2048, &[]);
        let n2 = make_node("small", 2000, 1024, &[]);

        let candidates = vec![
            (&n1, n1.available_resources.clone()),
            (&n2, n2.available_resources.clone()),
        ];
        let required = Resources {
            cpu_mhz: 1000,
            memory_mb: 512,
        };

        let result = pick_node(&candidates, &required, &[], SchedulingStrategy::Spread);
        assert!(result.is_some());
        // Spread should prefer the bigger node (more remaining capacity)
        assert_eq!(result.unwrap().0.id, "big");
    }

    #[test]
    fn test_no_node_with_sufficient_resources() {
        let n1 = make_node("tiny", 500, 256, &[]);

        let candidates = vec![(&n1, n1.available_resources.clone())];
        let required = Resources {
            cpu_mhz: 1000,
            memory_mb: 512,
        };

        let result = pick_node(&candidates, &required, &[], SchedulingStrategy::BinPack);
        assert!(result.is_none());
    }

    #[test]
    fn test_constraints_filter_nodes() {
        let linux = make_node("linux-box", 4000, 2048, &[("os", "linux")]);
        let mac = make_node("mac-box", 4000, 2048, &[("os", "macos")]);

        let candidates = vec![
            (&linux, linux.available_resources.clone()),
            (&mac, mac.available_resources.clone()),
        ];
        let required = Resources {
            cpu_mhz: 1000,
            memory_mb: 512,
        };
        let constraints = vec![Constraint {
            attribute: "os".to_string(),
            operator: "=".to_string(),
            value: "linux".to_string(),
        }];

        let result = pick_node(
            &candidates,
            &required,
            &constraints,
            SchedulingStrategy::BinPack,
        );
        assert!(result.is_some());
        assert_eq!(result.unwrap().0.id, "linux-box");
    }

    #[test]
    fn test_ineligible_node_excluded() {
        let mut node = make_node("n1", 4000, 2048, &[]);
        node.eligible = false;

        // The evaluator filters ineligible nodes before calling pick_node,
        // but verify that the field exists and is respected.
        assert!(!node.eligible);
    }
}