Skip to content

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:

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