Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize bulk unlocking #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased](https://github.com/veeqo/activejob-uniqueness/compare/v0.3.2...HEAD)

### Changed
- [#82](https://github.com/veeqo/activejob-uniqueness/pull/82) Optimize bulk unlocking [@sharshenov](https://github.com/sharshenov)

## [0.3.2](https://github.com/veeqo/activejob-uniqueness/compare/v0.3.1...v0.3.2) - 2024-08-16

### Added
Expand Down
8 changes: 7 additions & 1 deletion lib/active_job/uniqueness/lock_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ def delete_lock(resource)
true
end

DELETE_LOCKS_SCAN_COUNT = 1000

# Unlocks multiple resources by key wildcard.
def delete_locks(wildcard)
@servers.each do |server|
synced_redis_connection(server) do |conn|
conn.scan('MATCH', wildcard).each { |key| conn.call('DEL', key) }
cursor = 0
while cursor != '0'
cursor, keys = conn.call('SCAN', cursor, 'MATCH', wildcard, 'COUNT', DELETE_LOCKS_SCAN_COUNT)
conn.call('DEL', *keys) unless keys.empty?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth switching to unlink to speed up removal a bit more.

end
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/active_job/uniqueness/unlock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,26 @@
end
end
end

describe 'bulk deletion' do
subject(:unlock!) { described_class.unlock! }

let(:expected_initial_number_of_locks) { 1_103 } # 1_100 + 2 + 1
let(:expected_number_of_delete_commands) { 2 } # 1103 / 1000 (ActiveJob::Uniqueness::LockManager::DELETE_LOCKS_SCAN_COUNT)

before { 1_100.times.each { |i| job_class.perform_later(3, i) } }

it 'removes locks efficiently' do
expect { unlock! }.to change { locks_count }.from(expected_initial_number_of_locks).to(0)
.and change { delete_commands_calls }.by(expected_number_of_delete_commands)
end

def delete_commands_calls
info = redis.call('INFO', 'commandstats')
del_stats = info.split("\n").find { |line| line.start_with?('cmdstat_del:') }
return 0 unless del_stats

del_stats.match(/cmdstat_del:calls=(\d+)/)[1].to_i
end
end
end
Loading