celers-core 0.1.0

Core traits and types for CeleRS distributed task queue
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# CeleRS Core Architecture

> Comprehensive architecture documentation for the CeleRS Core crate

## Table of Contents

1. [Overview]#overview
2. [Design Principles]#design-principles
3. [Module Organization]#module-organization
4. [Core Abstractions]#core-abstractions
5. [Data Flow]#data-flow
6. [State Machine]#state-machine
7. [Dependency Graph]#dependency-graph
8. [Error Handling Strategy]#error-handling-strategy
9. [Extension Points]#extension-points
10. [Integration with Other Crates]#integration-with-other-crates
11. [Performance Considerations]#performance-considerations

---

## Overview

CeleRS Core is the foundational crate in the CeleRS distributed task queue system. It provides:

- **Core traits** for extensibility (`Task`, `Broker`, `EventEmitter`, `ExceptionHandler`)
- **Type system** for task representation (`TaskMetadata`, `SerializedTask`, `TaskState`)
- **Infrastructure** for task management (retry strategies, routing, rate limiting, time limits)
- **Zero runtime dependencies** on specific broker implementations

### Design Goals

1. **Separation of Concerns**: Core abstractions separate from implementations
2. **Type Safety**: Leverage Rust's type system for compile-time guarantees
3. **Flexibility**: Support multiple backends, retry strategies, and execution patterns
4. **Performance**: Minimal overhead with inline optimizations
5. **Observability**: Built-in event system for monitoring and debugging

---

## Design Principles

### 1. Trait-Based Extensibility

All core abstractions are defined as traits, allowing users to:
- Implement custom brokers for any message queue backend
- Create custom exception handlers for domain-specific error handling
- Build custom event emitters for monitoring and logging
- Define custom rate limiters for specific use cases

```rust
#[async_trait]
pub trait Broker: Send + Sync {
    async fn publish(&self, task: SerializedTask) -> Result<()>;
    async fn consume(&self) -> Result<Option<BrokerMessage>>;
    // ...
}
```

### 2. Builder Pattern for Ergonomics

All complex types use builder patterns for construction:

```rust
let task = SerializedTask::new("my_task".to_string(), payload)
    .with_priority(5)
    .with_max_retries(3)
    .with_timeout(60)
    .with_dependency(parent_id);
```

### 3. Explicit State Management

Tasks use an explicit state machine with clear transitions:

```
Pending → Reserved → Running → Succeeded
                              ↘ Failed → Retrying → Reserved
```

### 4. Composable Handlers

Exception handlers and event emitters can be composed:

```rust
let chain = ExceptionHandlerChain::new()
    .add_handler(Box::new(LoggingExceptionHandler::new()))
    .add_handler(Box::new(PolicyExceptionHandler::new(policy)));
```

### 5. Zero-Copy Where Possible

Task payloads use `Vec<u8>` for flexibility, with documented trade-offs for zero-copy alternatives.

---

## Module Organization

```
celers-core/
├── broker.rs          # Broker trait and BrokerMessage wrapper
├── control.rs         # Control plane types (stats, commands, responses)
├── dag.rs             # Task dependency DAG (Directed Acyclic Graph)
├── error.rs           # Unified error type (CelersError)
├── event.rs           # Event system for observability
├── exception.rs       # Structured exception handling
├── executor.rs        # Task registry for task type mapping
├── rate_limit.rs      # Rate limiting (token bucket, sliding window)
├── result.rs          # Task result storage abstraction
├── retry.rs           # Retry strategies and policies
├── revocation.rs      # Task revocation and cancellation
├── router.rs          # Pattern-based task routing
├── state.rs           # Task state machine and history
├── task.rs            # Task types (Task, TaskMetadata, SerializedTask)
├── time_limit.rs      # Soft/hard time limits for tasks
└── lib.rs             # Public API and documentation
```

### Module Dependencies

```
task.rs
  ├→ state.rs (TaskState)
  ├→ error.rs (CelersError)
  └→ dag.rs (TaskId for dependencies)

broker.rs
  └→ task.rs (SerializedTask)

exception.rs
  └→ error.rs (for error conversion)

retry.rs
  └→ error.rs (for retry decision)

executor.rs
  ├→ task.rs (Task trait)
  └→ error.rs

event.rs
  ├→ task.rs (TaskMetadata)
  └→ state.rs (TaskState)

control.rs
  ├→ task.rs (TaskId, TaskMetadata)
  └→ time_limit.rs (TimeLimitStatus)
```

---

## Core Abstractions

### 1. Task Trait

The fundamental abstraction for executable units of work:

```rust
#[async_trait]
pub trait Task: Send + Sync {
    async fn execute(&self, payload: &[u8]) -> Result<Vec<u8>>;
    fn name(&self) -> &str;
}
```

**Purpose**: Define a common interface for all task types
**Responsibility**: Execute business logic given a payload
**Extension Point**: Implement for custom task types

### 2. Broker Trait

Message queue backend abstraction:

```rust
#[async_trait]
pub trait Broker: Send + Sync {
    async fn publish(&self, task: SerializedTask) -> Result<()>;
    async fn consume(&self) -> Result<Option<BrokerMessage>>;
    async fn acknowledge(&self, message: BrokerMessage) -> Result<()>;
    async fn reject(&self, message: BrokerMessage, requeue: bool) -> Result<()>;
}
```

**Purpose**: Decouple task processing from message queue implementation
**Responsibility**: Handle message queue operations
**Extension Point**: Implement for any message queue (Redis, RabbitMQ, SQS, SQL, etc.)

### 3. TaskMetadata

Rich metadata for task lifecycle management:

```rust
pub struct TaskMetadata {
    pub id: TaskId,
    pub name: String,
    pub state: TaskState,
    pub priority: u8,
    pub max_retries: u32,
    pub created_at: DateTime<Utc>,
    pub timeout: Option<u64>,
    pub group_id: Option<String>,
    pub chord_id: Option<String>,
    pub dependencies: HashSet<TaskId>,
    // ...
}
```

**Purpose**: Track task lifecycle and configuration
**Features**:
- Unique ID generation (UUID v4)
- State tracking with history
- Priority-based scheduling (0-255)
- Retry configuration
- Timeout management
- Task dependencies (DAG support)
- Workflow primitives (groups, chords)

### 4. SerializedTask

Wire format for task transmission:

```rust
pub struct SerializedTask {
    pub metadata: TaskMetadata,
    pub payload: Vec<u8>,
}
```

**Purpose**: Represent tasks in serialized form for transmission
**Design Decision**: Use `Vec<u8>` for payload to support any serialization format
**Trade-offs**:
- ✅ Flexibility: Works with JSON, MessagePack, Protobuf, etc.
- ✅ Simplicity: No lifetime parameters
- ⚠️ Copy overhead: One allocation per task (acceptable for most use cases)

### 5. BrokerMessage

Wrapper for broker-specific metadata:

```rust
pub struct BrokerMessage {
    pub task: SerializedTask,
    pub receipt_handle: Option<String>,
}
```

**Purpose**: Attach broker-specific information (e.g., SQS receipt handle)
**Design**: Thin wrapper with delegation methods for ergonomics

---

## Data Flow

### Task Submission Flow

```
User Code
    ├─→ Create Task
    │   ├─→ TaskMetadata::new()
    │   ├─→ Builder methods (.with_priority(), .with_timeout())
    │   └─→ SerializedTask::new()
    ├─→ Route Task (optional)
    │   └─→ Router::route() → queue name
    └─→ Publish to Broker
        └─→ Broker::publish(task)
            └─→ Message Queue Backend
```

### Task Execution Flow

```
Message Queue Backend
    └─→ Broker::consume()
        ├─→ BrokerMessage (with receipt handle)
        ├─→ Check Revocation
        │   └─→ RevocationManager::is_revoked()
        ├─→ Check Time Limits
        │   └─→ TimeLimitTracker::check()
        ├─→ Check Rate Limits
        │   └─→ RateLimiter::try_acquire()
        ├─→ Execute Task
        │   ├─→ TaskRegistry::get()
        │   ├─→ Task::execute(payload)
        │   └─→ Handle Exceptions
        │       └─→ ExceptionHandler::handle()
        ├─→ Update State
        │   ├─→ TaskState::transition()
        │   └─→ StateHistory::record()
        └─→ Acknowledge/Reject
            └─→ Broker::acknowledge() or Broker::reject()
```

### Error Flow

```
Task Execution Error
    ├─→ Wrap in TaskException
    │   ├─→ Capture traceback
    │   ├─→ Classify category (Retryable, Fatal, etc.)
    │   └─→ Add metadata
    ├─→ ExceptionHandler::handle()
    │   ├─→ Apply ExceptionPolicy
    │   │   ├─→ Match patterns (retry_on, fail_on, ignore_on)
    │   │   └─→ Return ExceptionAction
    │   │
    │   └─→ ExceptionHandler::on_exception()
    │       └─→ Log, emit metrics, etc.
    └─→ Apply Action
        ├─→ Retry: RetryStrategy::calculate_delay()
        ├─→ Fail: Send to DLQ
        ├─→ Ignore: Mark as succeeded
        └─→ Reject: Remove from queue
```

---

## State Machine

### Task State Transitions

```
                ┌───────────┐
                │  Pending  │ (Initial state)
                └─────┬─────┘
                ┌───────────┐
                │ Reserved  │ (Claimed by worker)
                └─────┬─────┘
                ┌───────────┐
           ┌───→│  Running  │ (Actively executing)
           │    └─────┬─────┘
           │          │
           │    ┌─────┴─────┐
           │    │           │
           │    ▼           ▼
           │ ┌──────────┐ ┌──────────┐
           │ │Succeeded │ │  Failed  │ (Terminal states)
           │ └──────────┘ └─────┬────┘
           │                    │
           │                    ▼
           │              ┌───────────┐
           └──────────────┤ Retrying  │ (If retries remain)
                          └───────────┘
```

### State Properties

| State      | Active | Terminal | Can Retry | Description                    |
|------------|--------|----------|-----------|--------------------------------|
| Pending    ||| N/A       | Waiting to be claimed          |
| Reserved   ||| N/A       | Claimed by worker, not started |
| Running    ||| N/A       | Currently executing            |
| Retrying   |||| Waiting before retry           |
| Succeeded  |||| Completed successfully         |
| Failed     |||| Failed (may have retries left) |

### State History

Tasks maintain a complete history of state transitions:

```rust
pub struct StateHistory {
    transitions: Vec<StateTransition>,
}

pub struct StateTransition {
    from: Option<TaskState>,
    to: TaskState,
    timestamp: DateTime<Utc>,
}
```

**Benefits**:
- Audit trail for debugging
- Performance analysis (time in each state)
- Retry analysis
- SLA monitoring

---

## Dependency Graph

### Task DAG (Directed Acyclic Graph)

Tasks can have dependencies, forming a DAG:

```rust
pub struct TaskDag {
    nodes: HashMap<TaskId, DagNode>,
}

pub struct DagNode {
    task_id: TaskId,
    task_name: String,
    dependencies: HashSet<TaskId>,
    dependents: HashSet<TaskId>,
}
```

### Operations

1. **Add Node**: `dag.add_node(id, name)`
2. **Add Dependency**: `dag.add_dependency(child, parent)`
3. **Validate**: `dag.validate()` (detect cycles)
4. **Topological Sort**: `dag.topological_sort()` (execution order)
5. **Query**:
   - `dag.roots()` - Tasks with no dependencies
   - `dag.leaves()` - Tasks with no dependents
   - `dag.dependencies(id)` - Direct dependencies
   - `dag.dependents(id)` - Direct dependents

### Example Workflow

```rust
// Data pipeline: Load → Transform → Save
let mut dag = TaskDag::new();

let load_id = Uuid::new_v4();
let transform_id = Uuid::new_v4();
let save_id = Uuid::new_v4();

dag.add_node(load_id, "load_data");
dag.add_node(transform_id, "transform_data");
dag.add_node(save_id, "save_results");

dag.add_dependency(transform_id, load_id)?;
dag.add_dependency(save_id, transform_id)?;

// Get execution order
let order = dag.topological_sort()?;
// [load_id, transform_id, save_id]
```

---

## Error Handling Strategy

### Unified Error Type

```rust
pub enum CelersError {
    Serialization(String),
    Deserialization(String),
    BrokerError(String),
    TaskExecution(String),
    NotFound(String),
    Timeout(String),
    InvalidState(String),
    Configuration(String),
    Network(String),
}
```

**Design**: Use `thiserror` for Display implementations
**Strategy**: Convert all errors to `CelersError` at API boundaries

### Exception Handling Layers

1. **TaskException**: Structured exception with traceback
   - Captures file/line/function information
   - Categorizes exceptions (Retryable, Fatal, etc.)
   - Preserves cause chain

2. **ExceptionPolicy**: Declarative error handling
   - Pattern matching on exception types
   - Actions: Retry, Fail, Ignore, Reject

3. **ExceptionHandler**: Programmatic error handling
   - Custom logic for exception handling
   - Composable via ExceptionHandlerChain

### Retry Decision Flow

```
Exception Raised
    ├─→ Convert to TaskException
    ├─→ ExceptionPolicy::should_retry(exception)
    │   ├─→ Check fail_on patterns → No retry
    │   ├─→ Check reject_on patterns → No retry, no DLQ
    │   ├─→ Check ignore_on patterns → Mark success
    │   └─→ Check retry_on patterns → Retry
    ├─→ Check retry count vs max_retries
    └─→ RetryStrategy::calculate_delay(attempt)
        └─→ Schedule retry or send to DLQ
```

---

## Extension Points

### 1. Custom Brokers

Implement the `Broker` trait for any message queue:

```rust
pub struct MyBroker {
    // Your broker state
}

#[async_trait]
impl Broker for MyBroker {
    async fn publish(&self, task: SerializedTask) -> Result<()> {
        // Your implementation
    }
    // ... other methods
}
```

**Examples in workspace**:
- `celers-broker-sql`: PostgreSQL/SQLite backend
- `celers-broker-sqs`: AWS SQS backend

### 2. Custom Exception Handlers

Implement the `ExceptionHandler` trait:

```rust
pub struct MyHandler;

#[async_trait]
impl ExceptionHandler for MyHandler {
    async fn handle(&self, exception: &TaskException) -> ExceptionAction {
        // Your decision logic
    }
}
```

### 3. Custom Event Emitters

Implement the `EventEmitter` trait:

```rust
pub struct MyEmitter;

#[async_trait]
impl EventEmitter for MyEmitter {
    async fn emit_task_event(&self, event: &TaskEvent) {
        // Send to your monitoring system
    }
}
```

### 4. Custom Rate Limiters

Implement the `RateLimiter` trait:

```rust
pub struct MyRateLimiter;

#[async_trait]
impl RateLimiter for MyRateLimiter {
    async fn try_acquire(&self, task_name: &str) -> Result<bool> {
        // Your rate limiting logic
    }
}
```

### 5. Custom Retry Strategies

Use `RetryStrategy::Custom`:

```rust
let strategy = RetryStrategy::Custom(vec![100, 500, 1000, 5000]);
```

---

## Integration with Other Crates

### CeleRS Ecosystem

```
┌─────────────────────────────────────────────────────────┐
│                     User Application                     │
└───────────┬──────────────────────────────┬──────────────┘
            │                              │
            ▼                              ▼
    ┌───────────────┐            ┌─────────────────┐
    │ celers-macros │            │     celers      │
    │  (proc macro) │            │  (high-level)   │
    └───────┬───────┘            └────────┬────────┘
            │                             │
            └──────────┬──────────────────┘
              ┌─────────────────┐
              │   celers-core   │ ← This crate
              │   (traits +     │
              │    types)       │
              └────────┬────────┘
         ┌─────────────┼─────────────┐
         │             │             │
         ▼             ▼             ▼
┌─────────────┐ ┌──────────┐ ┌──────────────┐
│celers-broker│ │ celers-  │ │  celers-     │
│    -sql     │ │  broker  │ │   metrics    │
│             │ │   -sqs   │ │              │
└─────────────┘ └──────────┘ └──────────────┘
```

### Integration Points

1. **celers-macros**: Uses `Task` trait for code generation
2. **celers**: Uses all core types for high-level API
3. **celers-broker-***: Implements `Broker` trait
4. **celers-metrics**: Uses `EventEmitter` for metrics collection

---

## Performance Considerations

### 1. Inline Optimization

Critical path functions are marked `#[inline]`:

```rust
#[inline]
pub fn has_priority(&self) -> bool {
    self.priority.is_some()
}
```

**Affected functions** (30+ total):
- All builder methods
- State check methods (`is_terminal()`, `is_active()`)
- Delegation methods in `SerializedTask` and `BrokerMessage`

### 2. Zero Allocations for Checks

State checks and metadata queries don't allocate:

```rust
task.is_high_priority()  // No allocation
task.has_timeout()       // No allocation
state.is_terminal()      // No allocation
```

### 3. Cloning Strategy

- `TaskMetadata`: Implements `Clone` for cheap copies
- `SerializedTask`: Clone copies both metadata and payload (acceptable overhead)
- Consider `Arc<SerializedTask>` for scenarios with many consumers

### 4. Serialization Format

- Use `Vec<u8>` for payload flexibility
- Support any serialization format (JSON, MessagePack, Protobuf)
- Trade-off: One allocation per task (optimized for flexibility over zero-copy)

### 5. Benchmarking

Comprehensive benchmarks in `benches/task_bench.rs`:

```bash
cargo bench
```

**Benchmarks include**:
- Task metadata creation
- Task metadata cloning
- Task serialization (various payload sizes)
- Validation operations

### 6. Memory Layout

```rust
// TaskMetadata: ~200 bytes
// SerializedTask: ~200 bytes + payload size
// BrokerMessage: ~200 bytes + payload size + receipt_handle

sizeof(TaskMetadata) ≈ 200 bytes
sizeof(SerializedTask) ≈ 200 bytes + len(payload)
```

**Optimization tips**:
- Use `Arc<SerializedTask>` for broadcast scenarios
- Use `Bytes` crate for zero-copy payload sharing (if needed)
- Limit payload size (use external storage for large data)

---

## Conclusion

CeleRS Core provides a robust, flexible, and performant foundation for distributed task processing. Its trait-based design allows for extensive customization while maintaining type safety and clear abstractions.

**Key Strengths**:
- ✅ Clean separation of concerns
- ✅ Comprehensive error handling
- ✅ Flexible retry strategies
- ✅ Built-in observability
- ✅ DAG-based dependency management
- ✅ Production-ready state machine

**Next Steps**:
- Explore broker implementations (`celers-broker-sql`, `celers-broker-sqs`)
- Review high-level API in `celers` crate
- Check out procedural macros in `celers-macros`
- Contribute custom handlers or strategies