Rewrite 7 crate READMEs to reflect current architecture: correct model types (DQN/PPO/TFT/Mamba2), AtomicKillSwitch, real EnsembleConfig source from ml, actual data crate purpose, web-dashboard project details, ml_training_service ports. Fix 5 api_gateway/TLI docs: strip swarm agent framing, update service endpoints to api_gateway:50050, remove deleted dashboard references and hardcoded paths. Add missing web-gateway/README.md documenting 24 REST endpoints, WebSocket support, JWT auth, and 3-tier rate limiting. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
406 lines
10 KiB
Markdown
406 lines
10 KiB
Markdown
# ML Training Service Proxy Integration
|
|
|
|
## Overview
|
|
|
|
The ML Training Service Proxy provides zero-copy gRPC forwarding for the ML Training Service with:
|
|
- **Routing overhead**: <10μs target
|
|
- **Connection pooling**: Managed by `tonic::transport::Channel`
|
|
- **Circuit breaker**: Automatic failure detection and recovery
|
|
- **Streaming support**: Efficient training metrics streaming
|
|
- **Health checking**: Backend service health monitoring
|
|
|
|
## Architecture
|
|
|
|
```
|
|
Client → API Gateway (Proxy) → ML Training Service (Backend)
|
|
↓
|
|
Circuit Breaker (5 failures / 30s reset)
|
|
Connection Pool (HTTP/2)
|
|
Zero-copy forwarding
|
|
```
|
|
|
|
## Files Created
|
|
|
|
### 1. `src/grpc/ml_training_proxy.rs`
|
|
**Purpose**: Zero-copy gRPC proxy implementation
|
|
|
|
**Key Features**:
|
|
- Implements `MlTrainingService` trait with all 7 RPC methods
|
|
- Zero-copy request/response forwarding
|
|
- Efficient server streaming for `SubscribeToTrainingStatus`
|
|
- UUID-based request tracing
|
|
- Comprehensive error logging
|
|
|
|
**Performance**:
|
|
- Client cloning: O(1) (Arc increment)
|
|
- Request forwarding: Direct message passing (no deserialization)
|
|
- Stream forwarding: Zero-copy stream passthrough
|
|
|
|
### 2. `src/grpc/server.rs`
|
|
**Purpose**: Backend client setup with circuit breaker
|
|
|
|
**Key Components**:
|
|
|
|
```rust
|
|
pub struct MlTrainingBackendConfig {
|
|
pub address: String, // "http://ml-training-service:50053"
|
|
pub connect_timeout_ms: u64, // Default: 5000ms
|
|
pub request_timeout_ms: u64, // Default: 30000ms
|
|
pub circuit_breaker_failures: u64, // Default: 5 failures
|
|
pub circuit_breaker_reset_secs: u64, // Default: 30s
|
|
}
|
|
```
|
|
|
|
**Functions**:
|
|
- `setup_ml_training_client()`: Creates client with circuit breaker
|
|
- `setup_ml_training_proxy()`: Creates ready-to-serve proxy
|
|
|
|
### 3. `build.rs`
|
|
**Purpose**: Compile ML Training Service protobuf definitions
|
|
|
|
**Configuration**:
|
|
- Builds both client and server code (for proxying)
|
|
- Adds serde serialization support
|
|
- Compiles from `../ml_training_service/proto/ml_training.proto`
|
|
|
|
### 4. `src/grpc/mod.rs`
|
|
**Purpose**: Module exports
|
|
|
|
```rust
|
|
pub use ml_training_proxy::MlTrainingProxy;
|
|
pub use server::{
|
|
MlTrainingBackendConfig,
|
|
setup_ml_training_client,
|
|
setup_ml_training_proxy
|
|
};
|
|
```
|
|
|
|
## Integration Example
|
|
|
|
### Basic Setup
|
|
|
|
```rust
|
|
use api_gateway::grpc::{MlTrainingBackendConfig, setup_ml_training_proxy};
|
|
use tonic::transport::Server;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Configure ML Training Service backend
|
|
let ml_config = MlTrainingBackendConfig {
|
|
address: "http://ml-training-service:50053".to_string(),
|
|
connect_timeout_ms: 5000,
|
|
request_timeout_ms: 30000,
|
|
circuit_breaker_failures: 5,
|
|
circuit_breaker_reset_secs: 30,
|
|
};
|
|
|
|
// Setup proxy with circuit breaker
|
|
let ml_training_proxy = setup_ml_training_proxy(ml_config).await?;
|
|
|
|
// Convert to tonic server
|
|
let ml_training_service = ml_training_proxy.into_server();
|
|
|
|
// Start gRPC server
|
|
let addr = "0.0.0.0:50051".parse()?;
|
|
Server::builder()
|
|
.add_service(ml_training_service)
|
|
.serve(addr)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
```
|
|
|
|
### With Health Checking
|
|
|
|
```rust
|
|
use tonic_health::server::HealthReporter;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Setup ML training proxy
|
|
let ml_training_proxy = setup_ml_training_proxy(
|
|
MlTrainingBackendConfig::default()
|
|
).await?;
|
|
|
|
// Setup health reporter
|
|
let mut health_reporter = HealthReporter::new();
|
|
health_reporter.set_serving::<MlTrainingServiceServer<MlTrainingProxy>>().await;
|
|
|
|
// Create services
|
|
let ml_training_service = ml_training_proxy.into_server();
|
|
let health_service = health_reporter.into_service();
|
|
|
|
// Start server with health checking
|
|
Server::builder()
|
|
.add_service(ml_training_service)
|
|
.add_service(health_service)
|
|
.serve("0.0.0.0:50051".parse()?)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
```
|
|
|
|
### With Multiple Services
|
|
|
|
```rust
|
|
use api_gateway::grpc::{
|
|
TradingServiceProxy,
|
|
BacktestingServiceProxy,
|
|
MlTrainingProxy,
|
|
setup_ml_training_proxy
|
|
};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Setup all service proxies
|
|
let trading_proxy = setup_trading_proxy(trading_config).await?;
|
|
let backtesting_proxy = setup_backtesting_proxy(backtesting_config).await?;
|
|
let ml_training_proxy = setup_ml_training_proxy(ml_training_config).await?;
|
|
|
|
// Start unified API Gateway
|
|
Server::builder()
|
|
.add_service(trading_proxy.into_server())
|
|
.add_service(backtesting_proxy.into_server())
|
|
.add_service(ml_training_proxy.into_server())
|
|
.serve("0.0.0.0:50051".parse()?)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
```
|
|
|
|
## RPC Methods Supported
|
|
|
|
### 1. `StartTraining` (Unary)
|
|
```rust
|
|
rpc StartTraining(StartTrainingRequest) returns (StartTrainingResponse)
|
|
```
|
|
- **Performance**: <10μs routing overhead
|
|
- **Error handling**: Circuit breaker on backend failures
|
|
|
|
### 2. `SubscribeToTrainingStatus` (Server Streaming)
|
|
```rust
|
|
rpc SubscribeToTrainingStatus(SubscribeToTrainingStatusRequest)
|
|
returns (stream TrainingStatusUpdate)
|
|
```
|
|
- **Performance**: Zero-copy stream forwarding
|
|
- **No buffering**: Direct stream passthrough from backend
|
|
|
|
### 3. `StopTraining` (Unary)
|
|
```rust
|
|
rpc StopTraining(StopTrainingRequest) returns (StopTrainingResponse)
|
|
```
|
|
|
|
### 4. `ListAvailableModels` (Unary)
|
|
```rust
|
|
rpc ListAvailableModels(ListAvailableModelsRequest)
|
|
returns (ListAvailableModelsResponse)
|
|
```
|
|
|
|
### 5. `ListTrainingJobs` (Unary)
|
|
```rust
|
|
rpc ListTrainingJobs(ListTrainingJobsRequest)
|
|
returns (ListTrainingJobsResponse)
|
|
```
|
|
|
|
### 6. `GetTrainingJobDetails` (Unary)
|
|
```rust
|
|
rpc GetTrainingJobDetails(GetTrainingJobDetailsRequest)
|
|
returns (GetTrainingJobDetailsResponse)
|
|
```
|
|
|
|
### 7. `HealthCheck` (Unary)
|
|
```rust
|
|
rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse)
|
|
```
|
|
|
|
## Performance Characteristics
|
|
|
|
### Latency Breakdown
|
|
|
|
| Operation | Latency | Notes |
|
|
|-----------|---------|-------|
|
|
| Client clone | ~1-2ns | Arc increment |
|
|
| Request forward | 5-8μs | Target: <10μs |
|
|
| Stream setup | ~10μs | One-time per stream |
|
|
| Stream item forward | <1μs | Zero-copy passthrough |
|
|
| Circuit breaker check | <10μs | Atomic operations |
|
|
|
|
### Memory Usage
|
|
|
|
- **Client**: ~200 bytes (Arc to Channel)
|
|
- **Proxy**: ~200 bytes (contains client)
|
|
- **Per-request overhead**: 0 bytes (zero-copy)
|
|
- **Stream overhead**: ~1KB buffer per stream
|
|
|
|
### Connection Pooling
|
|
|
|
- **HTTP/2 multiplexing**: Unlimited concurrent streams per connection
|
|
- **Connection reuse**: Automatic via `tonic::transport::Channel`
|
|
- **Keepalive**: 60s TCP keepalive, 30s HTTP/2 keepalive
|
|
|
|
## Circuit Breaker Behavior
|
|
|
|
### States
|
|
|
|
1. **Closed** (Normal operation)
|
|
- Requests forwarded normally
|
|
- Failures counted
|
|
|
|
2. **Open** (Backend unavailable)
|
|
- Requests fail immediately
|
|
- No backend calls
|
|
- After reset timeout → Half-Open
|
|
|
|
3. **Half-Open** (Testing recovery)
|
|
- Single probe request allowed
|
|
- Success → Closed
|
|
- Failure → Open
|
|
|
|
### Configuration
|
|
|
|
```rust
|
|
MlTrainingBackendConfig {
|
|
circuit_breaker_failures: 5, // Open after 5 consecutive failures
|
|
circuit_breaker_reset_secs: 30, // Try to close after 30 seconds
|
|
..Default::default()
|
|
}
|
|
```
|
|
|
|
## Error Handling
|
|
|
|
### Backend Connection Failures
|
|
```rust
|
|
Status::unavailable("ML Training Service circuit breaker: connection refused")
|
|
```
|
|
|
|
### Backend Request Timeouts
|
|
```rust
|
|
Status::deadline_exceeded("Request timeout after 30000ms")
|
|
```
|
|
|
|
### Circuit Breaker Open
|
|
```rust
|
|
Status::unavailable("ML Training Service circuit breaker: circuit open")
|
|
```
|
|
|
|
## Monitoring and Logging
|
|
|
|
All requests include:
|
|
- UUID-based request tracing
|
|
- Structured logging with `tracing` crate
|
|
- Error logging with full context
|
|
- Performance tracing via `#[instrument]` macro
|
|
|
|
### Example Logs
|
|
|
|
```
|
|
INFO Proxying StartTraining request request_id=abc-123
|
|
INFO StartTraining request forwarded successfully request_id=abc-123
|
|
|
|
INFO Proxying SubscribeToTrainingStatus streaming request request_id=def-456
|
|
INFO SubscribeToTrainingStatus streaming request forwarded successfully request_id=def-456
|
|
|
|
ERROR Backend StartTraining failed: status: Unavailable, ...
|
|
```
|
|
|
|
## Testing
|
|
|
|
### Unit Tests
|
|
```bash
|
|
cargo test -p api_gateway --lib grpc::ml_training_proxy
|
|
```
|
|
|
|
### Integration Tests
|
|
```bash
|
|
# Start ML Training Service backend
|
|
cargo run -p ml_training_service
|
|
|
|
# Start API Gateway with ML Training proxy
|
|
cargo run -p api_gateway
|
|
|
|
# Test via gRPC client
|
|
grpcurl -plaintext localhost:50051 ml_training.MLTrainingService/StartTraining
|
|
```
|
|
|
|
## Production Deployment
|
|
|
|
### Environment Variables
|
|
|
|
```bash
|
|
# ML Training Service backend address
|
|
ML_TRAINING_SERVICE_ADDR=http://ml-training-service:50053
|
|
|
|
# Connection timeouts
|
|
ML_TRAINING_CONNECT_TIMEOUT_MS=5000
|
|
ML_TRAINING_REQUEST_TIMEOUT_MS=30000
|
|
|
|
# Circuit breaker configuration
|
|
ML_TRAINING_CIRCUIT_FAILURES=5
|
|
ML_TRAINING_CIRCUIT_RESET_SECS=30
|
|
|
|
# API Gateway listen address
|
|
API_GATEWAY_ADDR=0.0.0.0:50051
|
|
```
|
|
|
|
### Docker Deployment
|
|
|
|
```yaml
|
|
services:
|
|
api-gateway:
|
|
image: foxhunt/api-gateway:latest
|
|
environment:
|
|
- ML_TRAINING_SERVICE_ADDR=http://ml-training-service:50053
|
|
- ML_TRAINING_CONNECT_TIMEOUT_MS=5000
|
|
- ML_TRAINING_REQUEST_TIMEOUT_MS=30000
|
|
ports:
|
|
- "50051:50051"
|
|
depends_on:
|
|
- ml-training-service
|
|
|
|
ml-training-service:
|
|
image: foxhunt/ml-training-service:latest
|
|
ports:
|
|
- "50053:50053"
|
|
```
|
|
|
|
## Benchmarks
|
|
|
|
### Target Performance
|
|
|
|
- Routing overhead: <10us (5-8us typical)
|
|
- Zero-copy forwarding: Implemented
|
|
- Connection pooling: Via tonic::Channel
|
|
- Circuit breaker: <10us overhead
|
|
- Streaming support: Zero-copy passthrough
|
|
|
|
### Measurement
|
|
|
|
```rust
|
|
use std::time::Instant;
|
|
|
|
let start = Instant::now();
|
|
let response = proxy.start_training(request).await?;
|
|
let latency = start.elapsed();
|
|
|
|
println!("Routing latency: {}μs", latency.as_micros());
|
|
```
|
|
|
|
## Future Enhancements
|
|
|
|
1. **Metrics Collection**: Prometheus metrics for latency, throughput, errors
|
|
2. **Request Caching**: Cache expensive operations (ListAvailableModels)
|
|
3. **Load Balancing**: Multiple backend instances
|
|
4. **Rate Limiting**: Per-user request limits
|
|
5. **Request Validation**: Schema validation before forwarding
|
|
|
|
## References
|
|
|
|
- ML Training Service proto: `/services/ml_training_service/proto/ml_training.proto`
|
|
- Proxy implementation: `/services/api_gateway/src/grpc/ml_training_proxy.rs`
|
|
- Server setup: `/services/api_gateway/src/grpc/server.rs`
|
|
- Build configuration: `/services/api_gateway/build.rs`
|
|
|