Skip to content

Latest commit

 

History

History
328 lines (239 loc) · 9.38 KB

File metadata and controls

328 lines (239 loc) · 9.38 KB

Event Replay Implementation Summary

Overview

This implementation provides a complete disaster recovery solution for the Nova Launch platform by enabling replay of historical contract events from Stellar Horizon to rebuild read models (projections) after data loss.

What Was Implemented

1. Core Service: EventReplayService

File: backend/src/services/eventReplayService.ts

A production-ready service that:

  • Fetches events from Stellar Horizon with configurable ledger ranges
  • Implements automatic retry with exponential backoff for network failures
  • Routes events to appropriate parsers (Token, Governance, Stream, Vault)
  • Persists cursor state for resumable recovery
  • Supports dry-run mode for validation without persistence
  • Collects and reports errors without stopping replay
  • Provides clear and rebuild for complete recovery scenarios

Key methods:

  • replay(options) - Replay events from configurable starting point
  • clearAndRebuild(options) - Destructive: clear all projections and rebuild
  • fetchEventsWithRetry() - Network-resilient event fetching
  • processEvent() - Route events to appropriate parsers

2. Admin Routes

File: backend/src/routes/admin/eventReplay.ts

Two HTTP endpoints for disaster recovery:

POST /admin/event-replay

Replay events from Stellar to rebuild projections.

Query parameters:

  • startLedger - Starting ledger (optional, uses stored cursor if not provided)
  • endLedger - Ending ledger (optional, no limit if not provided)
  • batchSize - Events per request (default: 100, max: 200)
  • dryRun - Validate without persisting (default: false)
  • maxRetries - Network retry attempts (default: 5)

Response:

{
  "eventsProcessed": 1500,
  "eventsSkipped": 2,
  "startLedger": 50000000,
  "endLedger": 50001500,
  "finalCursor": "50001500-1",
  "errors": [{"ledger": 50000500, "error": "..."}],
  "duration": 45000
}

POST /admin/event-replay/clear-and-rebuild

Clear all projections and rebuild from scratch (requires ?confirm=yes).

3. Comprehensive Integration Tests

File: backend/src/__tests__/eventReplayService.integration.test.ts

Test coverage includes:

  • Event Processing: Correct ordering, idempotency, boundary handling
  • Cursor Management: Loading from store, persistence, resumability
  • Error Handling: Network failures, retries, non-retryable errors
  • Dry-Run Mode: Validation without persistence
  • Performance: Large batch processing (1000+ events)
  • Edge Cases: Empty streams, duplicate events, out-of-order delivery

All tests use mocked external services (Horizon API, Prisma) for isolation.

4. Documentation

File: docs/EVENT_REPLAY_RECOVERY.md

Complete operational guide covering:

  • Architecture: Event flow, idempotency guarantees
  • Usage: Basic replay, targeted ranges, dry-run validation
  • Recovery Procedures: Database corruption, complete data loss, partial sync failure
  • Monitoring: Logs, metrics, health checks
  • Performance: Batch size tuning, retry configuration
  • Troubleshooting: Common issues and solutions
  • Best Practices: Backups, testing, documentation

Key Design Decisions

1. Idempotency

All event parsers are idempotent:

  • Duplicate events yield identical state
  • Out-of-order events are handled gracefully
  • Terminal states are stable under replay
  • Counters are recalculated from events, not incremented

2. Network Resilience

Automatic retry with exponential backoff:

  • Retryable errors (5xx, timeouts) trigger retry
  • Non-retryable errors (4xx) fail immediately
  • Configurable retry count and delay
  • Graceful degradation on persistent failures

3. Cursor Persistence

Tracks progress for resumable recovery:

  • Cursor stored in IntegrationState table
  • Loaded on service restart
  • Updated after each successful batch
  • Supports recovery from any point

4. Dry-Run Mode

Validate recovery without persistence:

  • Checks event structure and contract ID
  • Routes to parsers for validation
  • No database writes
  • Useful for pre-flight checks

5. Error Collection

Continues processing despite errors:

  • Collects errors with ledger numbers
  • Reports all errors in response
  • Allows partial recovery
  • Enables targeted re-runs

Integration Points

Event Parsers

The service integrates with existing parsers:

  • TokenEventParser.parseEvent() - Token lifecycle events
  • GovernanceEventParser.parseEvent() - Governance events
  • StreamEventParser.parseEvent() - Stream events
  • Vault event parsers - Vault lifecycle events

Database

Uses Prisma for all database operations:

  • Reads/writes projections (Token, Proposal, Stream, Campaign, etc.)
  • Manages cursor state via IntegrationState
  • Supports transactions for consistency

Stellar Integration

Fetches events from Stellar Horizon:

  • Uses existing STELLAR_HORIZON_URL configuration
  • Filters by FACTORY_CONTRACT_ID
  • Respects Horizon rate limits
  • Handles network failures gracefully

Testing Strategy

Unit Tests

Comprehensive test suite with mocked dependencies:

  • Event processing logic
  • Cursor management
  • Error handling
  • Retry behavior
  • Dry-run validation

Integration Tests

Tests with real database (in CI):

  • Full replay workflow
  • Projection consistency
  • Cursor persistence
  • Error recovery

Manual Testing

Recommended procedures:

  1. Dry-run validation: ?dryRun=true
  2. Targeted replay: ?startLedger=X&endLedger=Y
  3. Full replay: No parameters
  4. Verify projections after recovery

Performance Characteristics

Batch Processing

  • Small batches (10-50): More API calls, slower overall
  • Medium batches (100): Balanced, recommended
  • Large batches (200): Fewer API calls, higher memory

Network Retry

  • Low retries (1-2): Fast failure, may miss transient errors
  • Medium retries (5): Balanced, recommended
  • High retries (10+): Tolerates poor connectivity, slower

Ledger Range

  • Full replay: Slowest, most thorough
  • Targeted range: Faster, requires knowing affected ledgers
  • Dry-run first: Validate before persisting

Deployment Checklist

  • Service implementation complete
  • Admin routes implemented
  • Integration tests written
  • Documentation complete
  • Error handling comprehensive
  • Network resilience implemented
  • Cursor persistence working
  • Dry-run mode functional
  • TypeScript compilation successful
  • Run full test suite
  • Deploy to staging
  • Test recovery procedures
  • Deploy to production

Usage Examples

Basic Replay

curl -X POST http://localhost:3001/admin/event-replay \
  -H "x-admin-key: $JWT_SECRET"

Replay from Specific Ledger

curl -X POST "http://localhost:3001/admin/event-replay?startLedger=50000000" \
  -H "x-admin-key: $JWT_SECRET"

Dry-Run Validation

curl -X POST "http://localhost:3001/admin/event-replay?dryRun=true" \
  -H "x-admin-key: $JWT_SECRET"

Clear and Rebuild

curl -X POST "http://localhost:3001/admin/event-replay/clear-and-rebuild?confirm=yes" \
  -H "x-admin-key: $JWT_SECRET"

Configuration

Environment Variables

# Stellar network
STELLAR_HORIZON_URL=https://horizon-testnet.stellar.org
FACTORY_CONTRACT_ID=CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4

# Recovery starting point
STELLAR_CURSOR_ORIGIN=0-0

# Admin authentication
JWT_SECRET=your-secret-key

Query Parameters

Parameter Type Default Description
startLedger number stored cursor Starting ledger
endLedger number unlimited Ending ledger
batchSize number 100 Events per request (1-200)
dryRun boolean false Validate without persisting
maxRetries number 5 Network retry attempts

Monitoring

Logs

Key log messages:

  • [EventReplay] Starting replay from ledger X
  • [EventReplay] Fetch failed (attempt N/M), retrying in Xms
  • [EventReplay] Error processing event at ledger X: ...
  • [EventReplay] Cursor persisted: X-Y
  • [EventReplay] Completed: N processed, M skipped in Xms

Metrics

Monitor these during recovery:

  • events_replayed_total - Total events processed
  • events_replay_errors_total - Events that failed
  • event_replay_duration_ms - Time to complete
  • projection_lag_ms - Lag between latest event and projection

Health Checks

After recovery, verify:

  1. Projection consistency
  2. Event cursor at latest
  3. Data integrity spot-checks

Related Documentation

Future Enhancements

Potential improvements for future iterations:

  1. Parallel Processing: Process multiple ledger ranges in parallel
  2. Streaming API: WebSocket for real-time replay progress
  3. Selective Replay: Replay only specific event types
  4. Validation Framework: Automated consistency checks
  5. Metrics Export: Prometheus metrics for monitoring
  6. Replay Scheduling: Scheduled recovery jobs
  7. Backup Integration: Automatic recovery from backups

Support

For issues or questions:

  1. Check EVENT_REPLAY_RECOVERY.md troubleshooting section
  2. Review logs for error details
  3. Run dry-run to validate configuration
  4. Contact platform team for assistance