pgbus 0.23.1 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +1 -1
- data/app/assets/javascripts/pgbus/stream_source_element.js +55 -11
- data/app/assets/javascripts/pgbus/stream_source_element.min.js +4 -4
- data/app/assets/javascripts/pgbus/stream_source_element.min.js.map +1 -1
- data/app/frontend/pgbus/application.min.js.map +1 -1
- data/app/frontend/pgbus/modules/charts.min.js.map +1 -1
- data/app/frontend/pgbus/vendor/turbo.min.js.map +1 -1
- data/app/models/pgbus/batch_entry.rb +70 -29
- data/app/models/pgbus/batch_execution.rb +8 -6
- data/app/models/pgbus/blocked_execution.rb +17 -15
- data/app/models/pgbus/job_stat.rb +16 -13
- data/app/models/pgbus/semaphore.rb +16 -14
- data/app/models/pgbus/uniqueness_key.rb +58 -45
- data/lib/pgbus/active_job/adapter.rb +7 -5
- data/lib/pgbus/batch.rb +76 -37
- data/lib/pgbus/client.rb +19 -0
- data/lib/pgbus/concurrency/blocked_execution.rb +28 -24
- data/lib/pgbus/concurrency/semaphore.rb +17 -9
- data/lib/pgbus/event_bus/registry.rb +77 -0
- data/lib/pgbus/failed_event_recorder.rb +34 -25
- data/lib/pgbus/version.rb +1 -1
- data/sig/pgbus/client.rbs +2 -0
- metadata +1 -1
data/lib/pgbus/client.rb
CHANGED
|
@@ -1098,6 +1098,25 @@ module Pgbus
|
|
|
1098
1098
|
end
|
|
1099
1099
|
end
|
|
1100
1100
|
|
|
1101
|
+
# Removes the +pattern+ binding from the logical +queue_name+ (issue #657).
|
|
1102
|
+
# True when a binding was removed, false when there was none. Never creates
|
|
1103
|
+
# the queue.
|
|
1104
|
+
def unbind_topic(pattern, queue_name)
|
|
1105
|
+
full_name = config.queue_name(queue_name)
|
|
1106
|
+
with_stale_connection_retry do
|
|
1107
|
+
synchronized { @pgmq.unbind_topic(pattern, full_name) }
|
|
1108
|
+
end
|
|
1109
|
+
end
|
|
1110
|
+
|
|
1111
|
+
# The patterns bound to the logical +queue_name+ in pgmq.topic_bindings
|
|
1112
|
+
# (issue #657).
|
|
1113
|
+
def topic_patterns(queue_name)
|
|
1114
|
+
full_name = config.queue_name(queue_name)
|
|
1115
|
+
with_stale_connection_retry do
|
|
1116
|
+
synchronized { @pgmq.list_topic_bindings(queue_name: full_name).map { |binding| binding[:pattern] } }
|
|
1117
|
+
end
|
|
1118
|
+
end
|
|
1119
|
+
|
|
1101
1120
|
def publish_to_topic(routing_key, payload, headers: nil, delay: 0)
|
|
1102
1121
|
with_stale_connection_retry do
|
|
1103
1122
|
synchronized do
|
|
@@ -58,28 +58,30 @@ module Pgbus
|
|
|
58
58
|
def promote_next(concurrency_key, client:, delay: 0)
|
|
59
59
|
released = nil
|
|
60
60
|
msg_id = nil
|
|
61
|
-
Pgbus::BlockedExecution.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
61
|
+
Pgbus::BlockedExecution.connection_pool.with_connection do
|
|
62
|
+
Pgbus::BlockedExecution.transaction(requires_new: true) do
|
|
63
|
+
released = release_next(concurrency_key)
|
|
64
|
+
raise ActiveRecord::Rollback unless released
|
|
65
|
+
|
|
66
|
+
# Resolve the delay first: a parked scheduled job goes back into
|
|
67
|
+
# PGMQ invisible, and nothing renews its lease until a worker
|
|
68
|
+
# picks it up, so the wait has to be part of the lease.
|
|
69
|
+
actual_delay = resolve_delay(released[:payload], delay)
|
|
70
|
+
raise ActiveRecord::Rollback unless slot_taken?(concurrency_key, released[:payload], actual_delay)
|
|
71
|
+
|
|
72
|
+
# Carry the enqueuer's priority through: under priority routing it
|
|
73
|
+
# picks the _pN sub-queue, not just the release order (issue #423).
|
|
74
|
+
headers = released[:payload].is_a?(Hash) ? released[:payload].delete(HEADERS_KEY) : nil
|
|
75
|
+
msg_id = client.send_message(released[:queue_name], released[:payload],
|
|
76
|
+
delay: actual_delay, priority: released[:priority],
|
|
77
|
+
**(headers ? { headers: headers } : {}))
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
next false unless released && msg_id
|
|
81
|
+
|
|
82
|
+
backfill(released, msg_id, client)
|
|
83
|
+
true
|
|
77
84
|
end
|
|
78
|
-
|
|
79
|
-
return false unless released && msg_id
|
|
80
|
-
|
|
81
|
-
backfill(released, msg_id, client)
|
|
82
|
-
true
|
|
83
85
|
rescue StandardError => e
|
|
84
86
|
Pgbus.logger.warn { "[Pgbus] Promote blocked execution failed for #{concurrency_key}: #{e.message}" }
|
|
85
87
|
false
|
|
@@ -112,9 +114,11 @@ module Pgbus
|
|
|
112
114
|
# un-taking the slot while the message is already live, so the job
|
|
113
115
|
# runs a second time.
|
|
114
116
|
def backfill(released, msg_id, client)
|
|
115
|
-
Pgbus::BlockedExecution.
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
Pgbus::BlockedExecution.connection_pool.with_connection do
|
|
118
|
+
Pgbus::BlockedExecution.transaction(requires_new: true) do
|
|
119
|
+
Batch.backfill_execution(released[:payload], msg_id,
|
|
120
|
+
client.target_queue(released[:queue_name], released[:priority]))
|
|
121
|
+
end
|
|
118
122
|
end
|
|
119
123
|
rescue StandardError => e
|
|
120
124
|
Pgbus.logger.warn { "[Pgbus] Batch execution backfill failed after promote: #{e.message}" }
|
|
@@ -19,17 +19,23 @@ module Pgbus
|
|
|
19
19
|
|
|
20
20
|
# Release one slot in the semaphore. Called after a job completes.
|
|
21
21
|
def release(key)
|
|
22
|
-
Pgbus::Semaphore.
|
|
22
|
+
Pgbus::Semaphore.connection_pool.with_connection do
|
|
23
|
+
Pgbus::Semaphore.where(key: key).update_all("value = GREATEST(value - 1, 0)")
|
|
24
|
+
end
|
|
23
25
|
end
|
|
24
26
|
|
|
25
27
|
# A job holding a slot is done: give the slot back and hand it to the
|
|
26
28
|
# next parked job for this key, in one transaction. The decrement locks
|
|
27
29
|
# the semaphore row first, so an enqueue that is parking a job right
|
|
28
30
|
# now commits before the promote looks for parked rows.
|
|
31
|
+
# The lease is the caller's when it holds one, else taken for the
|
|
32
|
+
# signal and returned at its end (issue #649).
|
|
29
33
|
def signal(key, client:)
|
|
30
|
-
Pgbus::Semaphore.
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
Pgbus::Semaphore.connection_pool.with_connection do
|
|
35
|
+
Pgbus::Semaphore.transaction do
|
|
36
|
+
release(key)
|
|
37
|
+
BlockedExecution.promote_next(key, client: client)
|
|
38
|
+
end
|
|
33
39
|
end
|
|
34
40
|
end
|
|
35
41
|
|
|
@@ -54,11 +60,13 @@ module Pgbus
|
|
|
54
60
|
# Returns an array of hashes with expired keys.
|
|
55
61
|
# Uses DELETE ... RETURNING for atomicity (no race between pluck and delete).
|
|
56
62
|
def expire_stale
|
|
57
|
-
result = Pgbus::Semaphore.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
result = Pgbus::Semaphore.connection_pool.with_connection do |conn|
|
|
64
|
+
conn.exec_query(
|
|
65
|
+
"DELETE FROM pgbus_semaphores WHERE expires_at < $1 RETURNING key",
|
|
66
|
+
"Pgbus Semaphore Expire",
|
|
67
|
+
[Time.current]
|
|
68
|
+
)
|
|
69
|
+
end
|
|
62
70
|
result.rows.map { |row| { "key" => row[0] } }
|
|
63
71
|
end
|
|
64
72
|
|
|
@@ -62,6 +62,40 @@ module Pgbus
|
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
# Makes each subscriber queue's pgmq.topic_bindings equal to the patterns
|
|
66
|
+
# registered for it (issue #657). A subscriber queue is named after its
|
|
67
|
+
# handler, so narrowing a subscription ("#" → "order.created") binds the
|
|
68
|
+
# new pattern on the same queue and leaves the old row, which keeps
|
|
69
|
+
# copying events the consumer archives as unrouted.
|
|
70
|
+
#
|
|
71
|
+
# For every registered queue (or only +queue_names+, logical names, not
|
|
72
|
+
# the prefixed ones #event_queue_names returns): bind the patterns its
|
|
73
|
+
# subscribers declare first, so the queue is never left without a
|
|
74
|
+
# binding, then unbind every other pattern bound to it. Run it with every
|
|
75
|
+
# subscriber registered: a pattern only an unloaded subscriber declares
|
|
76
|
+
# counts as stale. +queue_names: []+ prunes nothing; a name no registered
|
|
77
|
+
# subscriber uses raises ArgumentError before anything changes (a typo, a
|
|
78
|
+
# prefixed name or a process without the handlers loaded would otherwise
|
|
79
|
+
# strip a live queue). A removed handler's queue is cleaned up with
|
|
80
|
+
# Pgbus.client.topic_patterns and Pgbus.client.unbind_topic.
|
|
81
|
+
#
|
|
82
|
+
# Returns { queue_name => [removed patterns] } for the queues it changed;
|
|
83
|
+
# a second run returns {}.
|
|
84
|
+
#
|
|
85
|
+
# Deliberately not part of setup_all!: during a rolling deploy a process
|
|
86
|
+
# still on the old release owns the old pattern, so pruning is an explicit
|
|
87
|
+
# step to run once every process runs the new subscriptions.
|
|
88
|
+
def prune_stale_bindings!(queue_names: nil)
|
|
89
|
+
declared = declared_bindings
|
|
90
|
+
targets = queue_names.nil? ? declared.keys : physical_targets(queue_names, declared)
|
|
91
|
+
|
|
92
|
+
targets.each_with_object({}) do |physical, removed|
|
|
93
|
+
queue_name, patterns = declared.fetch(physical)
|
|
94
|
+
stale = prune_queue_bindings(queue_name, patterns)
|
|
95
|
+
removed[queue_name] = stale unless stale.empty?
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
65
99
|
# Subscribers a message read from +queue_name+ must be dispatched to
|
|
66
100
|
# (issue #469). Every subscriber gets its own queue, so a topic with N
|
|
67
101
|
# matching subscribers produces N queue copies of each event; selecting by
|
|
@@ -133,6 +167,49 @@ module Pgbus
|
|
|
133
167
|
|
|
134
168
|
private
|
|
135
169
|
|
|
170
|
+
# { physical queue name => [logical name, declared patterns] }. Keyed by
|
|
171
|
+
# the physical name so a Symbol and a String, or two spellings
|
|
172
|
+
# QueueNameValidator folds to one table ("Audit-Q", "audit_q"), count as
|
|
173
|
+
# one queue; the logical name is the first registered spelling.
|
|
174
|
+
def declared_bindings
|
|
175
|
+
subscribers = @mutex.synchronize { @subscribers.dup }
|
|
176
|
+
subscribers.each_with_object({}) do |subscriber, declared|
|
|
177
|
+
physical = Pgbus.configuration.queue_name(subscriber.queue_name)
|
|
178
|
+
entry = declared[physical] ||= [subscriber.queue_name.to_s, []]
|
|
179
|
+
entry[1] |= [subscriber.pattern]
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# The physical names of +queue_names+; raises before any change when one
|
|
184
|
+
# names no registered subscriber queue.
|
|
185
|
+
def physical_targets(queue_names, declared)
|
|
186
|
+
targets = queue_names.to_h { |name| [name, Pgbus.configuration.queue_name(name)] }
|
|
187
|
+
unknown = targets.reject { |_name, physical| declared.key?(physical) }.keys
|
|
188
|
+
unless unknown.empty?
|
|
189
|
+
raise ArgumentError, "No event subscriber is registered on queue(s) #{unknown.join(", ")}; " \
|
|
190
|
+
"prune_stale_bindings! takes logical subscriber queue names"
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
targets.values.uniq
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Binds +patterns+ to +queue_name+, unbinds every other pattern bound to
|
|
197
|
+
# it, and returns the patterns this call removed (one another process
|
|
198
|
+
# removed first is not reported).
|
|
199
|
+
def prune_queue_bindings(queue_name, patterns)
|
|
200
|
+
client = Pgbus.client
|
|
201
|
+
patterns.each { |pattern| client.bind_topic(pattern, queue_name) }
|
|
202
|
+
|
|
203
|
+
stale = client.topic_patterns(queue_name) - patterns
|
|
204
|
+
removed = stale.select { |pattern| client.unbind_topic(pattern, queue_name) }
|
|
205
|
+
unless removed.empty?
|
|
206
|
+
Pgbus.logger.info do
|
|
207
|
+
"[Pgbus] Unbound stale topic patterns #{removed.inspect} from event queue #{queue_name}"
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
removed
|
|
211
|
+
end
|
|
212
|
+
|
|
136
213
|
# True when running inside a rake schema/asset task, where setup_all!(safe:)
|
|
137
214
|
# should skip rather than open a connection. Delegates to the shared,
|
|
138
215
|
# public detector (issue #409) so there is a single implementation.
|
|
@@ -32,11 +32,12 @@ module Pgbus
|
|
|
32
32
|
retry_count
|
|
33
33
|
]
|
|
34
34
|
legacy = !source_queue_column?
|
|
35
|
-
if legacy
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
sql, binds = if legacy
|
|
36
|
+
[LEGACY_RECORD_SQL, [queue_name, msg_id.to_i, *details]]
|
|
37
|
+
else
|
|
38
|
+
[RECORD_SQL, [queue_name, source_queue, msg_id.to_i, *details]]
|
|
39
|
+
end
|
|
40
|
+
with_connection { |conn| conn.exec_query(sql, "FailedEvent Record", binds) }
|
|
40
41
|
rescue StandardError => e
|
|
41
42
|
# A process started before the migration would keep sending the old
|
|
42
43
|
# ON CONFLICT target, whose index the migration drops: probe again.
|
|
@@ -46,7 +47,9 @@ module Pgbus
|
|
|
46
47
|
|
|
47
48
|
def exists?(queue_name:, msg_id:, source_queue:)
|
|
48
49
|
sql, binds = message_scope(queue_name, msg_id, source_queue)
|
|
49
|
-
result =
|
|
50
|
+
result = with_connection do |conn|
|
|
51
|
+
conn.select_value("SELECT 1 FROM pgbus_failed_events WHERE #{sql} LIMIT 1", "FailedEvent Exists", binds)
|
|
52
|
+
end
|
|
50
53
|
!result.nil?
|
|
51
54
|
rescue StandardError => e
|
|
52
55
|
Pgbus.logger.debug { "[Pgbus] FailedEvent exists? check failed: #{e.class}: #{e.message}" }
|
|
@@ -61,12 +64,14 @@ module Pgbus
|
|
|
61
64
|
def last_error(queue_name:, msg_id:, source_queue:)
|
|
62
65
|
sql, binds = message_scope(queue_name, msg_id, source_queue)
|
|
63
66
|
order = binds.size > 2 ? " ORDER BY source_queue IS NULL, failed_at DESC" : ""
|
|
64
|
-
row =
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
row = with_connection do |conn|
|
|
68
|
+
conn.select_one(
|
|
69
|
+
"SELECT error_class, error_message, backtrace, retry_count, failed_at " \
|
|
70
|
+
"FROM pgbus_failed_events WHERE #{sql}#{order} LIMIT 1",
|
|
71
|
+
"FailedEvent Last Error",
|
|
72
|
+
binds
|
|
73
|
+
)
|
|
74
|
+
end
|
|
70
75
|
return unless row
|
|
71
76
|
|
|
72
77
|
{
|
|
@@ -89,11 +94,13 @@ module Pgbus
|
|
|
89
94
|
# case, while a row may carry the executor's configured spelling
|
|
90
95
|
# ("Critical") or a physical name from before #633.
|
|
91
96
|
def clear_queue!(queue_names)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
with_connection do |conn|
|
|
98
|
+
conn.exec_delete(
|
|
99
|
+
"DELETE FROM pgbus_failed_events WHERE lower(queue_name) = ANY($1::text[])",
|
|
100
|
+
"FailedEvent Clear Queue",
|
|
101
|
+
["{#{queue_names.map(&:downcase).join(",")}}"]
|
|
102
|
+
)
|
|
103
|
+
end
|
|
97
104
|
rescue StandardError => e
|
|
98
105
|
Pgbus.logger.error do
|
|
99
106
|
"[Pgbus] Failed to clear failed events for dropped queue(s) #{queue_names.join(", ")}: " \
|
|
@@ -105,7 +112,7 @@ module Pgbus
|
|
|
105
112
|
# existed: a sibling sub-queue's row with the same msg_id stays.
|
|
106
113
|
def clear!(queue_name:, msg_id:, source_queue:)
|
|
107
114
|
sql, binds = message_scope(queue_name, msg_id, source_queue)
|
|
108
|
-
|
|
115
|
+
with_connection { |conn| conn.exec_delete("DELETE FROM pgbus_failed_events WHERE #{sql}", "FailedEvent Clear", binds) }
|
|
109
116
|
rescue StandardError => e
|
|
110
117
|
# ERROR-level: a failed clear leaves a stale row in the dashboard
|
|
111
118
|
# AFTER the job actually succeeded — confusing and load-bearing
|
|
@@ -168,7 +175,7 @@ module Pgbus
|
|
|
168
175
|
end
|
|
169
176
|
|
|
170
177
|
def detect_source_queue_column(warn:)
|
|
171
|
-
supported =
|
|
178
|
+
supported = with_connection { |conn| conn.column_exists?(:pgbus_failed_events, :source_queue) }
|
|
172
179
|
if warn && !supported
|
|
173
180
|
Pgbus.logger.warn do
|
|
174
181
|
"[Pgbus] pgbus_failed_events is missing the source_queue column; under priority routing two " \
|
|
@@ -186,12 +193,14 @@ module Pgbus
|
|
|
186
193
|
value.to_s
|
|
187
194
|
end
|
|
188
195
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
196
|
+
# A lease the caller already holds is reused and kept; otherwise one is
|
|
197
|
+
# taken for the block and returned at its end (issue #649), so the
|
|
198
|
+
# executor's bookkeeping leaves no connection pinned to the job's
|
|
199
|
+
# thread or fiber. BusRecord's pool is the pgbus database's under
|
|
200
|
+
# connects_to and the primary's otherwise; asking first whether it had
|
|
201
|
+
# connected sent a cold process's first failure to the primary.
|
|
202
|
+
def with_connection(&)
|
|
203
|
+
BusRecord.connection_pool.with_connection(&)
|
|
195
204
|
end
|
|
196
205
|
end
|
|
197
206
|
|
data/lib/pgbus/version.rb
CHANGED
data/sig/pgbus/client.rbs
CHANGED
|
@@ -71,7 +71,9 @@ module Pgbus
|
|
|
71
71
|
def streams_swap_stats: () -> untyped
|
|
72
72
|
def synchronizing?: () -> bool
|
|
73
73
|
def target_queue: (untyped queue_name, ?untyped priority) -> untyped
|
|
74
|
+
def topic_patterns: (untyped queue_name) -> Array[String]
|
|
74
75
|
def transaction: () ?{ (*untyped) -> untyped } -> untyped
|
|
76
|
+
def unbind_topic: (untyped pattern, untyped queue_name) -> bool
|
|
75
77
|
def uniqueness_keys_present: (untyped lock_keys) -> untyped
|
|
76
78
|
def update_notify_insert: (untyped queue_name, throttle_interval_ms: untyped) -> untyped
|
|
77
79
|
def verify_connection!: () -> untyped
|