Skip to content

Commit 33c9fad

Browse files
authored
More performant strand unblocker (#36)
* More performant strand unblocker * Drop support for EOL rails 7.0
1 parent 30dcb14 commit 33c9fad

7 files changed

Lines changed: 16 additions & 265 deletions

File tree

.github/workflows/continuous_integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
fail-fast: false
2222
matrix:
2323
ruby: ['3.1', '3.2', '3.3', '3.4']
24-
lockfile: ['activerecord-7.0', 'activerecord-7.1', 'Gemfile.lock']
24+
lockfile: ['activerecord-7.1', 'Gemfile.lock']
2525

2626
services:
2727
postgres:

Gemfile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ gem "rubocop-rspec", "~> 3.0", require: false
2525
gem "simplecov", "~> 0.21", require: false
2626
gem "zeitwerk", "~> 2.6", "< 2.7", require: false
2727

28-
lockfile "activerecord-7.0" do
29-
gem "activerecord", "~> 7.0.0"
30-
gem "base64", "~> 0.1", require: RUBY_VERSION >= "3.4.0"
31-
gem "bigdecimal", "~> 3.1", require: RUBY_VERSION >= "3.4.0"
32-
gem "drb", "~> 2.1", require: RUBY_VERSION >= "3.4.0"
33-
gem "mutex_m", "~> 0.1", require: RUBY_VERSION >= "3.4.0"
34-
gem "railties", "~> 7.0.0"
35-
end
36-
3728
lockfile "activerecord-7.1" do
3829
gem "activerecord", "~> 7.1.0"
3930
gem "activerecord-pg-extensions", "~> 0.5"

Gemfile.activerecord-7.0.lock

Lines changed: 0 additions & 243 deletions
This file was deleted.

Gemfile.activerecord-7.1.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
switchman-inst-jobs (4.1.0)
4+
switchman-inst-jobs (4.2.0)
55
inst-jobs (>= 2.4.9, < 4.0)
66
parallel (>= 1.19)
77
railties (>= 7.0, < 8.0)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
switchman-inst-jobs (4.1.0)
4+
switchman-inst-jobs (4.2.0)
55
inst-jobs (>= 2.4.9, < 4.0)
66
parallel (>= 1.19)
77
railties (>= 7.0, < 8.0)

lib/switchman_inst_jobs/jobs_migrator.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,16 @@ def unblock_strands(target_shard, batch_size: 10_000)
230230
blocked_shard_ids = blocked_shards.pluck(:id)
231231
query = lambda { |column, scope|
232232
::Delayed::Job
233-
.where(id: ::Delayed::Job.select("DISTINCT ON (#{column}) id")
234-
.where(scope)
233+
.where(id: ::Delayed::Job
234+
.with(all_col: ::Delayed::Job.and(scope).distinct.select(column))
235+
.with(blocked_col: ::Delayed::Job.from("all_col").select(column).where.not(
236+
::Delayed::Job.select(1)
237+
.where(next_in_strand: true).or(::Delayed::Job.where(source: "JobsMigrator::StrandBlocker"))
238+
.where("#{column} = all_col.#{column}").arel.exists
239+
))
240+
.select("DISTINCT ON (#{column}) id")
235241
.where.not(shard_id: blocked_shard_ids)
236-
.where(
237-
::Delayed::Job.select(1).from("#{::Delayed::Job.quoted_table_name} dj2")
238-
.where("dj2.next_in_strand = true OR dj2.source = 'JobsMigrator::StrandBlocker'")
239-
.where("dj2.#{column} = delayed_jobs.#{column}").arel.exists.not
240-
)
242+
.where({ column => ::Delayed::Job.from("blocked_col").select(column) })
241243
.order(column, :strand_order_override, :id)).limit(batch_size)
242244
}
243245

@@ -249,12 +251,13 @@ def unblock_strands(target_shard, batch_size: 10_000)
249251
# batches
250252

251253
loop do
252-
break if query.call(:strand, "strand IS NOT NULL").update_all(next_in_strand: true).zero?
254+
break if query.call(:strand, ::Delayed::Job.where.not(strand: nil)).update_all(next_in_strand: true).zero?
253255
end
254256

255257
loop do
256258
break if query.call(:singleton,
257-
"strand IS NULL AND singleton IS NOT NULL").update_all(next_in_strand: true).zero?
259+
::Delayed::Job.where(strand: nil)
260+
.where.not(singleton: nil)).update_all(next_in_strand: true).zero?
258261
end
259262
end
260263
end

lib/switchman_inst_jobs/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SwitchmanInstJobs
4-
VERSION = "4.1.0"
4+
VERSION = "4.2.0"
55
end

0 commit comments

Comments
 (0)