rpcnet 0.1.0

RPC library based on QUIC+TLS encryption
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
# Zero-Downtime QUIC Connection Migration

This document describes the zero-downtime QUIC connection migration feature implemented in RpcNet, allowing live connections to be seamlessly transferred between backend workers without client reconnection.

## Overview

The migration system enables RpcNet applications to:

- **Seamlessly transfer active QUIC connections** between workers without dropping client connections
- **Preserve application state** during migration using encrypted snapshots
- **Maintain security** through cryptographic verification and token-based authentication
- **Handle failures gracefully** with automatic rollback and health monitoring
- **Scale horizontally** by redistributing connections across worker pools

## Architecture

### Core Components

```
┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Client        │    │   Director       │    │   Workers       │
│                 │    │                  │    │                 │
│ QUIC Connection │◄──►│ Connection Pool  │◄──►│ State Handlers  │
│ State Tracking  │    │ Migration Logic  │    │ Service Logic   │
└─────────────────┘    └──────────────────┘    └─────────────────┘
                       ┌──────────────────┐
                       │ Migration System │
                       │                  │
                       │ • State Capture  │
                       │ • Encryption     │ 
                       │ • Transfer       │
                       │ • Restoration    │
                       └──────────────────┘
```

### Migration Components

| Component | Purpose | Location |
|-----------|---------|----------|
| **Connection State Snapshot** | Captures connection and application state | `src/migration/models/connection_state_snapshot.rs` |
| **Migration Request/Confirmation** | Manages migration lifecycle | `src/migration/models/migration_*.rs` |
| **Encryption Service** | Secures state data with AES-256-GCM | `src/migration/state/encryption.rs` |
| **Serialization Service** | Handles state compression and chunking | `src/migration/state/serialization.rs` |
| **Token Manager** | Authentication and rate limiting | `src/migration/auth/token_manager.rs` |
| **State Machine** | Migration workflow orchestration | `src/migration/state_machine.rs` |
| **Session Manager** | Active connection tracking | `src/migration/session_manager.rs` |
| **Health Checker** | Migration status monitoring | `src/migration/health.rs` |

## Migration Workflow

### 1. State Capture

```rust
use rpcnet::migration::models::ConnectionStateSnapshot;

// Capture current connection state
let mut snapshot = ConnectionStateSnapshot::new(
    connection_id.clone(),
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
);

// Add stream buffers
snapshot = snapshot.add_stream_buffer(stream_id, buffer_data);

// Add application state
snapshot = snapshot.add_application_state("user_id".to_string(), user_id);
```

### 2. Encryption and Serialization

```rust
use rpcnet::migration::state::{
    encryption::{EncryptionService, EncryptionKey},
    serialization::{SerializationService, SerializationConfig, CompressionMethod},
};

// Initialize services
let encryption_service = EncryptionService::new();
let key = EncryptionKey::generate();
let serialization_service = SerializationService::new(SerializationConfig {
    compression: CompressionMethod::Gzip,
    chunk_size: 1024 * 1024, // 1MB chunks
});

// Serialize and encrypt
let serialized = serialization_service.serialize(&snapshot).await?;
let encrypted = encryption_service.encrypt(&serialized, &key)?;
```

### 3. Migration Request

```rust
use rpcnet::migration::models::MigrationRequest;
use rpcnet::migration::auth::token_manager::{MigrationTokenManager, TokenValidationConfig};

// Create migration request
let migration_request = MigrationRequest::new(
    "migration-001".to_string(),
    connection_id,
    source_worker_id,
    target_worker_id,
    encrypted_state,
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
);

// Generate secure token
let token_manager = MigrationTokenManager::new(TokenValidationConfig {
    max_requests_per_second: 100,
    token_expiry_seconds: 3600,
    secret_key: secret_key.clone(),
});

let token = token_manager.generate_token(&migration_request)?;
```

### 4. State Transfer and Restoration

```rust
// On target worker: validate and decrypt
let is_valid = token_manager.validate_token(&token, &migration_request)?;
assert!(is_valid);

let decrypted = encryption_service.decrypt(&encrypted_state, &key)?;
let restored_snapshot: ConnectionStateSnapshot = 
    serialization_service.deserialize(&decrypted).await?;

// Restore connection state
for (stream_id, buffer) in restored_snapshot.stream_buffers() {
    restore_stream_buffer(*stream_id, buffer.clone());
}

for (key, value) in restored_snapshot.application_state() {
    restore_application_state(key.clone(), value.clone());
}
```

### 5. Confirmation and Completion

```rust
use rpcnet::migration::models::{MigrationConfirmation, MigrationParty};

// Confirm successful migration
let confirmation = MigrationConfirmation::new(
    migration_request.migration_id().clone(),
    migration_request.connection_id().clone(),
    MigrationParty::System,
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
    true, // success
    Some("Migration completed successfully".to_string()),
);
```

## State Machine

The migration process follows a well-defined state machine:

```
    Idle
  Preparing ──► Failed
     │            ▲
     ▼            │
Transferring ─────┘
  Completed
```

### State Transitions

| From | To | Trigger | Description |
|------|----|---------| ------------|
| `Idle` | `Preparing` | Migration request | Begin migration preparation |
| `Preparing` | `Transferring` | State capture complete | Start state transfer |
| `Preparing` | `Failed` | Preparation error | Abort migration |
| `Transferring` | `Completed` | Successful confirmation | Migration complete |
| `Transferring` | `Failed` | Transfer error | Rollback migration |

## Security Features

### Encryption

- **Algorithm**: AES-256-GCM for authenticated encryption
- **Key Management**: Secure key generation and rotation
- **Integrity**: Built-in authentication prevents tampering

### Authentication

- **HMAC-SHA256**: Token-based request authentication
- **Rate Limiting**: Configurable request rate limits
- **Expiration**: Time-based token expiry

### Validation

- **State Integrity**: Cryptographic verification of state data
- **Request Validation**: Comprehensive input sanitization
- **Connection Verification**: Source and target worker validation

## Performance Characteristics

### Benchmarks

Based on the migration benchmarks (`benches/migration_benchmarks.rs`):

| Operation | 1KB State | 1MB State | 10MB State |
|-----------|-----------|-----------|------------|
| **Encryption** | ~10μs | ~1ms | ~10ms |
| **Serialization (Gzip)** | ~50μs | ~5ms | ~50ms |
| **Complete Workflow** | ~100μs | ~10ms | ~100ms |

### Memory Usage

- **Streaming**: Large states processed in configurable chunks
- **Compression**: Automatic compression reduces transfer size
- **Cleanup**: Automatic memory cleanup after migration

### Scalability

- **Concurrent Migrations**: Supports multiple simultaneous migrations
- **Rate Limiting**: Prevents resource exhaustion
- **Fault Tolerance**: Graceful handling of worker failures

## Configuration

### Encryption Configuration

```rust
// Configure encryption service
let encryption_service = EncryptionService::new();
let key = EncryptionKey::from_bytes(&[/* 32-byte key */])?;
```

### Serialization Configuration

```rust
let config = SerializationConfig {
    compression: CompressionMethod::Gzip, // None, Gzip, or Zstd
    chunk_size: 1024 * 1024, // 1MB chunks
};
```

### Token Manager Configuration

```rust
let config = TokenValidationConfig {
    max_requests_per_second: 100,
    token_expiry_seconds: 3600, // 1 hour
    secret_key: your_secret_key,
};
```

## Error Handling

### Migration Failures

The system handles various failure scenarios:

```rust
match migration_result {
    Ok(confirmation) => {
        // Migration successful
        log::info!("Migration completed: {}", confirmation.migration_id());
    }
    Err(MigrationError::InvalidToken) => {
        // Authentication failed
        log::error!("Migration failed: invalid token");
    }
    Err(MigrationError::StateCorruption) => {
        // State data corrupted
        log::error!("Migration failed: state corruption detected");
    }
    Err(MigrationError::TargetUnavailable) => {
        // Target worker unavailable
        log::error!("Migration failed: target worker unavailable");
    }
}
```

### Rollback Strategy

Failed migrations trigger automatic rollback:

1. **State Cleanup**: Remove partial state from target worker
2. **Connection Restoration**: Restore connection on source worker
3. **Health Check**: Verify connection health after rollback
4. **Notification**: Alert monitoring systems of failure

## Monitoring and Health Checks

### Health Checker

```rust
use rpcnet::migration::health::HealthChecker;

let health_checker = HealthChecker::new();
let health_status = health_checker.check_migration_health(&migration_request).await?;

if health_status.is_healthy() {
    println!("Migration system healthy");
} else {
    println!("Migration issues detected: {:?}", health_status.issues());
}
```

### Metrics

The migration system provides metrics for:

- **Migration Success Rate**: Percentage of successful migrations
- **Migration Latency**: Time to complete migrations
- **State Size Distribution**: Size of migrated states
- **Error Rates**: Types and frequency of migration errors

## Integration Guide

### Basic Integration

1. **Add Migration Support to Your Service**:

```rust
use rpcnet::migration::session_manager::SessionManager;

pub struct MyService {
    session_manager: SessionManager,
    // ... other fields
}

impl MyService {
    pub async fn handle_migration(&self, request: MigrationRequest) -> Result<(), MigrationError> {
        // Implement migration handling
    }
}
```

2. **Register Migration Handlers**:

```rust
// In your RPC service setup
service.register_migration_handler(|request| async move {
    // Handle incoming migration requests
});
```

3. **Implement State Capture**:

```rust
impl MyService {
    fn capture_connection_state(&self, connection_id: &str) -> ConnectionStateSnapshot {
        let mut snapshot = ConnectionStateSnapshot::new(
            connection_id.to_string(),
            SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
        );
        
        // Capture your service-specific state
        // snapshot = snapshot.add_application_state(key, value);
        
        snapshot
    }
}
```

### Advanced Integration

For advanced use cases, you can:

- **Custom State Serialization**: Implement custom serialization for complex state
- **Migration Policies**: Define when and how migrations should occur
- **Load Balancing Integration**: Use migration for dynamic load balancing
- **Monitoring Integration**: Connect to your monitoring infrastructure

## Example: Connection Swap Demo

The `examples/connection_swap/` directory contains a complete working example:

```bash
# Terminal 1: Start director
CONNECTION_SWAP_DIRECTOR_ADDR=127.0.0.1:61000 \
RUST_LOG=info \
cargo run --manifest-path examples/connection_swap/Cargo.toml --bin director

# Terminal 2: Start worker A
CONNECTION_SWAP_DIRECTOR_TARGET=127.0.0.1:61000 \
CONNECTION_SWAP_WORKER_ADDR=127.0.0.1:62001 \
CONNECTION_SWAP_WORKER_LABEL=worker-a \
RUST_LOG=info \
cargo run --manifest-path examples/connection_swap/Cargo.toml --bin worker

# Terminal 3: Start worker B  
CONNECTION_SWAP_DIRECTOR_TARGET=127.0.0.1:61000 \
CONNECTION_SWAP_WORKER_ADDR=127.0.0.1:62002 \
CONNECTION_SWAP_WORKER_LABEL=worker-b \
RUST_LOG=info \
cargo run --manifest-path examples/connection_swap/Cargo.toml --bin worker

# Terminal 4: Start client
CONNECTION_SWAP_DIRECTOR_TARGET=127.0.0.1:61000 \
RUST_LOG=info \
cargo run --manifest-path examples/connection_swap/Cargo.toml --bin client
```

This example demonstrates:
- Live connection transfer between workers
- Seamless client experience (no reconnection)
- Failure simulation and recovery
- State preservation across migrations

## Testing

### Unit Tests

Run individual component tests:

```bash
cargo test migration --lib
```

### Integration Tests

Run full workflow tests:

```bash
cargo test migration_integration_tests --test migration_integration_tests
```

### Performance Benchmarks

Run migration benchmarks:

```bash
cargo bench migration_benchmarks
```

## Best Practices

### Security

1. **Key Management**: Use proper key rotation and secure storage
2. **Network Security**: Use TLS for all migration communications
3. **Validation**: Always validate migration tokens and state integrity
4. **Monitoring**: Monitor for suspicious migration patterns

### Performance

1. **State Size**: Keep migrated state as small as possible
2. **Compression**: Use appropriate compression for your data
3. **Chunking**: Configure chunk sizes based on your network
4. **Concurrency**: Limit concurrent migrations to prevent resource exhaustion

### Reliability

1. **Health Checks**: Implement comprehensive health monitoring
2. **Timeouts**: Set appropriate timeouts for migration operations
3. **Rollback**: Always have a rollback strategy for failed migrations
4. **Testing**: Thoroughly test migration scenarios

## Troubleshooting

### Common Issues

**Migration Fails with "Invalid Token"**:
- Check token expiry configuration
- Verify secret key consistency across workers
- Check system clock synchronization

**State Corruption Detected**:
- Verify encryption key consistency
- Check network reliability during transfer
- Validate serialization/deserialization logic

**Target Worker Unavailable**:
- Check worker health and connectivity
- Verify load balancer configuration
- Check resource availability on target

**Performance Issues**:
- Monitor state size and compression ratios
- Check network bandwidth and latency
- Verify chunk size configuration

### Debug Mode

Enable detailed logging:

```bash
RUST_LOG=debug cargo run your_application
```

### Health Monitoring

Use the health checker for debugging:

```rust
let health_status = health_checker.check_migration_health(&request).await?;
println!("Health details: {:#?}", health_status);
```

## Future Enhancements

Planned improvements include:

- **Cross-Region Migration**: Support for geographic migration
- **Partial State Migration**: Migrate only specific state components
- **Migration Scheduling**: Advanced scheduling and orchestration
- **Custom Compression**: Pluggable compression algorithms
- **Migration Analytics**: Enhanced metrics and analysis

## Contributing

To contribute to the migration system:

1. Read the architecture documentation
2. Run the test suite: `cargo test migration`
3. Run benchmarks: `cargo bench migration`
4. Follow the security guidelines
5. Update documentation for any API changes

For questions or issues, please see the main project README or file an issue in the repository.