Allow cross slot commands in ClusterUtil.batch
What does this MR do and why?
Gitlab::Redis::ClusterUtil.batch (used by batch_get, batch_del and batch_unlink) pipelines commands on keys that can hash to different slots. Callers had to wrap each call in Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands themselves.
This MR moves allow_cross_slot_commands into ClusterUtil.batch, so all ClusterUtil batch methods allow cross slot commands by default, as suggested in the issue. The change is limited to ClusterUtil and its spec:
lib/gitlab/redis/cluster_util.rb: wrap the batching inallow_cross_slot_commands. The validator tracks nesting, so existing callers that still wrap their calls keep working.spec/lib/gitlab/redis/cluster_util_spec.rb: the batch examples no longer use an outerallow_cross_slot_commandsblock, and new examples check that cross slot commands are allowed while batching and that the state is restored afterwards, including when the block raises.
Callers (Gitlab::AvatarCache, Gitlab::SetCache, Gitlab::RepositoryHashCache, Gitlab::ReactiveCacheSetCache, Gitlab::SidekiqStatus) are left unchanged on purpose: their non-cluster branches (redis.unlink(*keys), redis.mget(*keys)) still need the wrapper. The Rails.cache.*_multi callers mentioned in the issue can be cleaned up in a follow-up.
References
- Related to #474372
- Original discussion: !159542 (comment 2014931413)
- https://docs.gitlab.com/ee/development/redis.html#multi-key-commands
Screenshots or screen recordings
Not applicable, there are no UI changes.
How to set up and validate locally
- In a Rails console, call
Gitlab::Redis::ClusterUtil.batch_get(%w[a b c], redis)insideGitlab::Redis::Cache.with { |redis| ... }without wrapping it inallow_cross_slot_commands. It no longer raisesCrossSlotError.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.