Fix max_batch_size error during BBM batch optimization
Summary
When Gitlab::Database::BackgroundMigration::BatchedMigration#max_batch_size
is below 1000, an error will happen trying to optimize the batch size
MIN_BATCH_SIZE = 1_000
MAX_BATCH_SIZE = 2_000_000
max_batch_size = 250
max_batch = max_batch_size || MAX_BATCH_SIZE
=> 250
1.clamp(MIN_BATCH_SIZE, max_batch)
ArgumentError: min argument must be smaller than max argument
from (pry):12:in `clamp'
Background migration examples:
- https://gitlab.com/gitlab-org/gitlab/blob/dd314a479e51cf6814cb0c58a63c71690230559c/db/post_migrate/20230112141236_schedule_vulnerabilities_feedback_migration2.rb#L9-9
- https://gitlab.com/gitlab-org/gitlab/blob/dd314a479e51cf6814cb0c58a63c71690230559c/db/post_migrate/20220920180451_schedule_vulnerabilities_feedback_migration.rb#L9-9
- https://gitlab.com/gitlab-org/gitlab/blob/dd314a479e51cf6814cb0c58a63c71690230559c/db/post_migrate/20230203122602_schedule_vulnerabilities_feedback_migration3.rb#L9-9
What is the current bug behavior?
It raises an error if max_batch_size
is lower than 1000
Possible fixes
if multiplier = batch_size_multiplier
max_batch = migration.max_batch_size || MAX_BATCH_SIZE
min_batch = [MIN_BATCH_SIZE, max_batch].min
migration.batch_size = (migration.batch_size * multiplier).to_i.clamp(MIN_BATCH_SIZE, max_batch)
migration.save!
end
Edited by Leonardo da Rosa