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 in allow_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 outer allow_cross_slot_commands block, 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

Screenshots or screen recordings

Not applicable, there are no UI changes.

How to set up and validate locally

  1. In a Rails console, call Gitlab::Redis::ClusterUtil.batch_get(%w[a b c], redis) inside Gitlab::Redis::Cache.with { |redis| ... } without wrapping it in allow_cross_slot_commands. It no longer raises CrossSlotError.

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.

Edited by Priyanshu Jha

Merge request reports

Loading
Loading