From c4c01f88942a1ad23cc58e26b5c64f39d58f046c Mon Sep 17 00:00:00 2001 From: Oliver Peate Date: Wed, 2 Dec 2020 16:50:00 +0000 Subject: [PATCH] Warn on boot if maxmemory policy is not 'noeviction' (#4752) --- lib/sidekiq/cli.rb | 5 +++++ test/test_cli.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index 936215efe..5b8361353 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -62,6 +62,11 @@ def run(boot_app: true) ver = Sidekiq.redis_info["redis_version"] raise "You are connecting to Redis v#{ver}, Sidekiq requires Redis v4.0.0 or greater" if ver < "4" + maxmemory_policy = Sidekiq.redis_info["maxmemory_policy"] + if maxmemory_policy != "noeviction" + logger.warn "'noeviction' maxmemory policy is recommended (current policy: '#{maxmemory_policy}'). See: https://github.com/mperham/sidekiq/wiki/Using-Redis#memory" + end + # Since the user can pass us a connection pool explicitly in the initializer, we # need to verify the size is large enough or else Sidekiq's performance is dramatically slowed. cursize = Sidekiq.redis_pool.size diff --git a/test/test_cli.rb b/test/test_cli.rb index 832ab4fe0..716ae32fb 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -481,6 +481,32 @@ def logdev assert_includes @logdev.string, "Booted Rails #{::Rails.version} application in production environment" end end + + describe 'checking maxmemory policy' do + it 'warns if the policy is not noeviction' do + redis_info = { "maxmemory_policy" => "allkeys-lru", "redis_version" => "6" } + + Sidekiq.stub(:redis_info, redis_info) do + subject.stub(:launch, nil) do + subject.run + end + end + + assert_includes @logdev.string, "'noeviction' maxmemory policy is recommended (current policy: 'allkeys-lru')" + end + + it 'silent if the policy is noeviction' do + redis_info = { "maxmemory_policy" => "noeviction", "redis_version" => "6" } + + Sidekiq.stub(:redis_info, redis_info) do + subject.stub(:launch, nil) do + subject.run + end + end + + refute_includes @logdev.string, "'noeviction' maxmemory policy is recommended" + end + end end describe 'signal handling' do