Extract Gitlab::Database::Batch::Optimizer
Ref: #561698 (closed)
What does this MR do and why?
This MR refactors the batch size optimization logic for batched background migrations into a pure service class pattern.
Previously, the optimization logic was tightly coupled within a class. Now it's split into:
-
Gitlab::Database::Batch::Optimizer
- a pure service class that calculates optimized batch sizes without side effects -
BatchedMigration#optimize!
- orchestrates the optimization and handles persistence
This separation allows:
- Better testability with pure functions
- Clearer separation of concerns (calculation vs persistence)
- Flexibility in how optimization is applied
The optimizer adjusts batch sizes based on time efficiency to keep migrations running at optimal speed (90-95% efficiency), automatically increasing batch size when migrations are too fast and decreasing when they're too slow.
migration = BatchedMigration.find(id)
migration.optimize! # Calculates and updates batch size if needed
Edited by Max Orefice