๐จ CRITICAL: Migration Hangs Due to Circular Dependency Resolution in BookLoader
๐จ CRITICAL BLOCKING ISSUE
The MongoDB โ PostgreSQL migration system is experiencing critical hanging issues during the book loading phase due to circular dependency resolution failures. This prevents completion of the full migration process.
๐ Impact Assessment
- Current Status: Only 3 books successfully migrated out of 6,565 expected
- Migration Completion: 0% (hanging prevents completion)
- Affected Component: BookLoader dependency resolution system
- Business Impact: Complete migration pipeline blockage
๐ Root Cause Analysis
Primary Issue: Infinite Loop in Dependency Resolution
Location: pkg/migration/dependencies/dependency_resolver.go:163-212
The ExpandTitles() method contains a hardcoded iteration limit that gets exceeded during dependency graph traversal:
// Current problematic code
maxIterations := 100 // Much smaller limit for performance
iterations := 0
for len(queue) > 0 && iterations < maxIterations {
iterations++
// ... processing logic
if limit > 0 && len(needed) >= limit {
break
}
}
if iterations >= maxIterations {
logger.LogWarn(fmt.Sprintf("ExpandTitles hit max iterations limit (%d) - possible circular dependency", iterations), "MIGRATION")
}
Observable Symptoms
- Migration hangs with warning:
"ExpandTitles hit max iterations limit" - Books loading stops after processing only 3 books
- System appears frozen during Phase 1 book processing
- No graceful error handling or recovery
๐ ๏ธ DEBUGGING METHODOLOGY โ ๏ธ
CRITICAL: Always use the standardized debugging approach:
โ REQUIRED Commands
# ALWAYS use this command for debugging
make migrate-strict
# For incremental testing
make migrate-strict ARGS="--limit 1" # Single book test
make migrate-strict ARGS="--limit 100" # Moderate test
make migrate-strict ARGS="--limit 0" # Full migration (currently fails)
โ REQUIRED Investigation Tools
- MCP MongoDB Tools: Investigate Sefaria database through MCP tool interface
- MCP PostgreSQL Tools: Validate target database state
- Log Analysis: Always inspect most recent logs first
- Database Verification: Check for hanging processes and data integrity
โ NEVER Use
- Direct script execution
- Other migration commands besides
make migrate-strict - Manual database operations outside MCP tools
๐ Migration Status Summary
โ Issues Fixed
- --limit Flag Handling: Fixed
--limit 0(unlimited) flag recognition - Log References: Corrected "index collection" โ "indexmap collection"
- Metrics Tracking: Fixed "books" โ "books_loaded" metric collection
- Code Cleanup: Removed deprecated schema synchronization functions
๐จ Critical Outstanding Issues
1. Circular Dependency Hanging (BLOCKING)
- Symptom: Migration hangs during Phase 1 with "ExpandTitles hit max iterations limit"
- Root Cause: Circular references in book dependency graph cause infinite recursion
- Impact: Cannot complete migration with any moderate limit (>= 100 books)
- Solution Needed: Implement proper cycle detection in dependency resolver
2. Missing Cycle Detection Algorithm
- Current State: Queue-based traversal without visited node tracking
- Problem: No detection mechanism for dependency cycles
- Required Fix: Add
visited := make(map[string]bool)pattern with cycle detection
๐ก Immediate Technical Solutions
1. Implement Cycle Detection
Add proper cycle detection to ExpandTitles() method:
// Required fix pattern
func (dr *DependencyResolver) ExpandTitles(ctx context.Context, titles []string, limit int) ([]string, error) {
visited := make(map[string]bool)
processing := make(map[string]bool) // Track currently processing nodes
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
// Cycle detection
if processing[current] {
logger.LogWarn(fmt.Sprintf("Circular dependency detected for: %s", current), "MIGRATION")
continue // Skip circular reference
}
if visited[current] {
continue // Already processed
}
processing[current] = true
// ... process dependencies
processing[current] = false
visited[current] = true
}
}
2. Add Context Timeout
Implement timeout for dependency resolution:
func (dr *DependencyResolver) ExpandTitles(ctx context.Context, titles []string, limit int) ([]string, error) {
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
// Process with timeout context
}
3. Graceful Error Recovery
Replace hanging behavior with controlled failure:
- Log circular dependency chains
- Continue processing non-circular dependencies
- Provide actionable error messages
๐ Testing Requirements
Validation Steps
- Unit Test:
make migrate-strict ARGS="--limit 1"(should complete quickly) - Integration Test:
make migrate-strict ARGS="--limit 100"(should not hang) - Full Migration:
make migrate-strict ARGS="--limit 0"(ultimate goal)
Success Criteria
- [ ] No "ExpandTitles hit max iterations limit" warnings
- [ ] All 6,565 books successfully processed
- [ ] Migration completes within reasonable time bounds (<2 hours)
- [ ] Proper handling of circular dependencies with logging
๐ง Additional Context
LLM Schema Generation Status: โ AVAILABLE
Correction to debugging summary: LLM schema generation system is fully functional
- Location:
llm_api/src/schema_generator_production.py - Capabilities: Can generate schemas for all 6,000+ books
- Integration: Ready for production use with OpenAI API
Architecture Notes
- Migration Type: MongoDB (source) โ PostgreSQL (target)
- Processing: Parallel worker pools with circuit breaker patterns
- Configuration: Extensive Makefile with 100+ specialized targets
- Monitoring: Real-time progress tracking and dashboard capabilities
๐ฏ Next Steps
- Immediate: Implement cycle detection in
ExpandTitles()method - Validation: Test with
make migrate-strictusing incremental limits - Monitoring: Use MCP tools to validate database states during testing
- Documentation: Update migration guides with proper debugging workflow
โ ๏ธ Important Notes
- Schema Issues Are Bugs: Not cosmetic issues - investigate through MCP MongoDB tool
- Incremental Debugging: Always start with
make migrate-strictand investigate failures step-by-step - Database Inspection: Use MCP tools to examine both MongoDB and PostgreSQL states
- No Silent Errors: Address root causes, don't skip or silence errors
---
Priority: ๐ด CRITICAL - BLOCKING PRODUCTION MIGRATION
Assignment: Immediate attention required for dependency resolution expert
Timeline: Fix required before full migration deployment
This issue has 3 comments on GitHub. Read the full discussion on GitHub โ