pgbus 0.24.0 → 0.25.1

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +5 -3
  4. data/app/controllers/pgbus/deploy_tasks_controller.rb +11 -1
  5. data/app/helpers/pgbus/deploy_tasks_helper.rb +20 -0
  6. data/app/models/pgbus/deploy_task.rb +23 -1
  7. data/app/models/pgbus/deploy_task_run.rb +47 -5
  8. data/app/views/pgbus/deploy_tasks/_tasks_table.html.erb +3 -0
  9. data/app/views/pgbus/deploy_tasks/show.html.erb +25 -2
  10. data/config/i18n-tasks.yml +1 -0
  11. data/config/locales/da.yml +18 -0
  12. data/config/locales/de.yml +18 -0
  13. data/config/locales/en.yml +18 -0
  14. data/config/locales/es.yml +18 -0
  15. data/config/locales/fi.yml +18 -0
  16. data/config/locales/fr.yml +18 -0
  17. data/config/locales/it.yml +18 -0
  18. data/config/locales/ja.yml +18 -0
  19. data/config/locales/nb.yml +18 -0
  20. data/config/locales/nl.yml +18 -0
  21. data/config/locales/pt.yml +18 -0
  22. data/config/locales/sv.yml +18 -0
  23. data/lib/generators/pgbus/add_deploy_task_run_outcomes_generator.rb +48 -0
  24. data/lib/generators/pgbus/templates/add_deploy_task_run_outcomes.rb.erb +12 -0
  25. data/lib/generators/pgbus/templates/add_deploy_tasks_tables.rb.erb +5 -0
  26. data/lib/generators/pgbus/templates/deploy_tasks.yml.erb +6 -0
  27. data/lib/generators/pgbus/templates/migration.rb.erb +5 -0
  28. data/lib/pgbus/active_job/adapter.rb +13 -0
  29. data/lib/pgbus/active_job/deploy_task_run_id.rb +53 -0
  30. data/lib/pgbus/active_job/executor.rb +57 -0
  31. data/lib/pgbus/client.rb +2 -3
  32. data/lib/pgbus/configuration.rb +25 -11
  33. data/lib/pgbus/dedicated_connection.rb +91 -3
  34. data/lib/pgbus/deploy/dependencies.rb +115 -0
  35. data/lib/pgbus/deploy/runner.rb +103 -18
  36. data/lib/pgbus/deploy/schema.rb +65 -0
  37. data/lib/pgbus/deploy/task.rb +23 -5
  38. data/lib/pgbus/doctor.rb +24 -2
  39. data/lib/pgbus/engine.rb +1 -0
  40. data/lib/pgbus/generators/migration_detector.rb +13 -3
  41. data/lib/pgbus/mcp/tools/deploy_tasks_tool.rb +1 -1
  42. data/lib/pgbus/streams/presence.rb +104 -13
  43. data/lib/pgbus/version.rb +1 -1
  44. data/lib/pgbus/web/data_source/deploy_task_list.rb +39 -6
  45. data/lib/pgbus/web/deploy_task_state.rb +7 -5
  46. data/lib/pgbus/web/streamer/instance.rb +7 -4
  47. data/lib/pgbus/web/streamer/listener.rb +32 -17
  48. data/lib/pgbus/web/streamer/master_hub.rb +6 -1
  49. data/lib/pgbus/web/streamer/stream_event_dispatcher.rb +1 -1
  50. data/sig/pgbus/configuration.rbs +2 -0
  51. metadata +6 -1
@@ -28,8 +28,10 @@ module Pgbus
28
28
  # the pipe is touched from other threads
29
29
  #
30
30
  # Health check: once per health_check_ms the listener runs `SELECT 1` as
31
- # a TCP keepalive, on a deadline: an idle connection sees exactly one per
32
- # cycle, and a stream of wakes or notifications cannot postpone it. If
31
+ # a keepalive, on a deadline: an idle connection sees exactly one per
32
+ # cycle, and a stream of wakes or notifications cannot postpone it. The
33
+ # interval only bounds how long a silently dropped LISTEN goes unnoticed:
34
+ # no wait budget is derived from it (ack_timeout_ms is, issue #664). If
33
35
  # it raises, the listener rebuilds a FRESH connection via
34
36
  # connection_factory and re-LISTENs every channel in `@listening_to`.
35
37
  # This is the fix for design doc §11 #1 (silently dropped LISTEN
@@ -62,10 +64,14 @@ module Pgbus
62
64
 
63
65
  RECONNECT_BACKOFF_SECONDS = 0.5
64
66
 
65
- # Grace added to one health-check cycle when #stop joins the listener
66
- # thread. See #stop_join_timeout.
67
+ # Grace added to the ack budget when #stop joins the listener thread.
68
+ # See #stop_join_timeout.
67
69
  STOP_JOIN_GRACE_SECONDS = 5
68
70
 
71
+ # Default for ack_timeout_ms: the old 250 ms cycle plus its 1 s margin,
72
+ # now fixed instead of following the keepalive (issue #664).
73
+ DEFAULT_ACK_TIMEOUT_MS = 1_250
74
+
69
75
  attr_reader :listening_to
70
76
 
71
77
  # @param connection_factory [#call] builds a FRESH PG connection on each
@@ -84,14 +90,18 @@ module Pgbus
84
90
  # most once per `maintenance.interval` seconds with THIS connection, so a
85
91
  # maintenance query (e.g. pg_stat_activity headroom) reuses the existing
86
92
  # idle connection instead of opening its own. nil disables it.
93
+ # `ack_timeout_ms:` bounds how long ensure_listening waits for this
94
+ # thread's ack, and (plus a grace) how long #stop joins it
95
+ # (streams_listen_ack_timeout_ms; issue #664).
87
96
  def initialize(pg_connection:, dispatch_queue:, health_check_ms:,
88
- connection_factory:, dispatch_queue_limit: 0, maintenance: nil,
89
- logger: Pgbus.logger, clock: nil)
97
+ connection_factory:, ack_timeout_ms: DEFAULT_ACK_TIMEOUT_MS, dispatch_queue_limit: 0,
98
+ maintenance: nil, logger: Pgbus.logger, clock: nil)
90
99
  raise ArgumentError, "connection_factory is required" unless connection_factory.respond_to?(:call)
91
100
 
92
101
  @conn = pg_connection
93
102
  @dispatch_queue = dispatch_queue
94
103
  @health_check_ms = health_check_ms
104
+ @ack_timeout_ms = ack_timeout_ms
95
105
  @logger = logger
96
106
  @connection_factory = connection_factory
97
107
  @dispatch_queue_limit = dispatch_queue_limit
@@ -182,9 +192,12 @@ module Pgbus
182
192
  # a process-killing SEGV, not a rescuable PG::Error (issue #375).
183
193
  # The listener thread is the sole owner of @conn and closes it in
184
194
  # run_loop's ensure; clearing @running above is the whole stop signal,
185
- # and the wake makes the thread see it at once. The join budget still
186
- # covers a full health-check cycle: the thread may be inside a
187
- # keepalive or a reconnect rather than the select.
195
+ # and the wake makes the thread see it at once. The join budget
196
+ # (the ack budget plus a grace, 6.25 s by default) covers a thread
197
+ # inside a normal keepalive or reconnect attempt. A keepalive stuck
198
+ # on a dead socket can outlast it (libpq's TCP bound is ~11 s): #stop
199
+ # then logs "did not stop" and the thread exits on its own once
200
+ # libpq raises.
188
201
  wake
189
202
  joined = @thread.nil? || @thread.join(stop_join_timeout)
190
203
  # A thread past its budget stays referenced, so #start refuses to
@@ -208,9 +221,9 @@ module Pgbus
208
221
  # in the read_after result nor delivered as a WakeMessage.
209
222
  #
210
223
  # The wake pipe makes the typical wait a thread hop (issue #630). The
211
- # wait is bounded by one health-check cycle
212
- # (streams_listen_health_check_ms, default 250ms) plus a small margin,
213
- # so a stuck listener can't hang the dispatcher forever.
224
+ # wait is bounded by the ack budget (streams_listen_ack_timeout_ms,
225
+ # default 1.25 s; issue #664), so a stuck listener can't hang the
226
+ # dispatcher forever.
214
227
  def ensure_listening(queue_name)
215
228
  # A connect the dispatcher drains during shutdown (Instance#shutdown!
216
229
  # stops the listener first) must not wait out the ack budget.
@@ -411,18 +424,20 @@ module Pgbus
411
424
 
412
425
  # Caller wait budget for ensure_listening's ack. The wake pipe
413
426
  # makes the usual wait a thread hop; a thread busy in a keepalive
414
- # or a reconnect gets one health-check cycle plus a margin, so the
415
- # dispatcher fails loud rather than hanging if the thread is dead.
427
+ # or a reconnect gets ack_timeout_ms, so the dispatcher fails loud
428
+ # rather than hanging if the thread is dead. Not derived from the
429
+ # keepalive interval: raising that must not slow this failure path
430
+ # (issue #664).
416
431
  def ack_timeout
417
- (@health_check_ms / 1000.0) + 1.0
432
+ @ack_timeout_ms / 1000.0
418
433
  end
419
434
 
420
435
  # How long #stop waits for the listener thread to notice the cleared
421
436
  # @running and finish teardown. The wake pipe interrupts its select at
422
437
  # once, but it may be inside a keepalive or a reconnect attempt, so the
423
- # budget stays one health-check cycle plus grace.
438
+ # budget is the ack budget plus grace, independent of the keepalive.
424
439
  def stop_join_timeout
425
- (@health_check_ms / 1000.0) + STOP_JOIN_GRACE_SECONDS
440
+ ack_timeout + STOP_JOIN_GRACE_SECONDS
426
441
  end
427
442
 
428
443
  # The channel joins @listening_to BEFORE the exec: if the LISTEN
@@ -158,13 +158,18 @@ module Pgbus
158
158
 
159
159
  def default_listener_factory
160
160
  lambda do |dispatch_queue:|
161
- build_connection = -> { Pgbus::DedicatedConnection.connect(@config.streams_connection_options) }
161
+ # TCP keepalives and the ack budget as on a per-worker listener:
162
+ # in :master scope this is the connection that LISTENs (issue #664).
163
+ build_connection = lambda do
164
+ Pgbus::DedicatedConnection.connect_with_keepalives(@config.streams_connection_options)
165
+ end
162
166
  conn = build_connection.call
163
167
  Pgbus::Process::PrimaryValidator.validate_primary!(conn)
164
168
  Listener.new(
165
169
  pg_connection: conn,
166
170
  dispatch_queue: dispatch_queue,
167
171
  health_check_ms: @config.streams_listen_health_check_ms,
172
+ ack_timeout_ms: @config.streams_listen_ack_timeout_ms,
168
173
  connection_factory: build_connection,
169
174
  dispatch_queue_limit: @config.streams_dispatch_queue_limit,
170
175
  logger: @logger
@@ -398,7 +398,7 @@ module Pgbus
398
398
  #
399
399
  # The fast path holds while the listener reconnects, on purpose.
400
400
  # Waiting on a reconnecting listener would cost every connect the
401
- # full ack budget (health_check_ms + 1 s) for as long as the
401
+ # full ack budget (streams_listen_ack_timeout_ms) for as long as the
402
402
  # database stays unreachable: the storm this rule removes. A
403
403
  # connect in that window has the same exposure as every connection
404
404
  # already registered: a durable stream's next wake after the
@@ -239,6 +239,8 @@ module Pgbus
239
239
  def streams_idle_timeout=: (untyped) -> untyped
240
240
  def streams_listen_health_check_ms: () -> Integer
241
241
  def streams_listen_health_check_ms=: (untyped) -> untyped
242
+ def streams_listen_ack_timeout_ms: () -> Integer
243
+ def streams_listen_ack_timeout_ms=: (untyped) -> untyped
242
244
  def streams_listen_scope: () -> Symbol
243
245
  def streams_listen_scope=: (untyped scope) -> untyped
244
246
  def streams_max_connections: () -> Integer
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.0
4
+ version: 0.25.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
@@ -285,6 +285,7 @@ files:
285
285
  - lib/active_job/queue_adapters/pgbus_adapter.rb
286
286
  - lib/generators/pgbus/add_batch_callback_jobs_generator.rb
287
287
  - lib/generators/pgbus/add_batch_executions_generator.rb
288
+ - lib/generators/pgbus/add_deploy_task_run_outcomes_generator.rb
288
289
  - lib/generators/pgbus/add_deploy_tasks_generator.rb
289
290
  - lib/generators/pgbus/add_failed_events_index_generator.rb
290
291
  - lib/generators/pgbus/add_failed_events_source_queue_generator.rb
@@ -308,6 +309,7 @@ files:
308
309
  - lib/generators/pgbus/migration_path.rb
309
310
  - lib/generators/pgbus/templates/add_batch_callback_jobs.rb.erb
310
311
  - lib/generators/pgbus/templates/add_batch_executions.rb.erb
312
+ - lib/generators/pgbus/templates/add_deploy_task_run_outcomes.rb.erb
311
313
  - lib/generators/pgbus/templates/add_deploy_tasks_tables.rb.erb
312
314
  - lib/generators/pgbus/templates/add_failed_events_source_queue.rb.erb
313
315
  - lib/generators/pgbus/templates/add_failed_events_unique_index.rb.erb
@@ -344,6 +346,7 @@ files:
344
346
  - lib/pgbus/active_job/batch_id.rb
345
347
  - lib/pgbus/active_job/current_attributes.rb
346
348
  - lib/pgbus/active_job/delivery.rb
349
+ - lib/pgbus/active_job/deploy_task_run_id.rb
347
350
  - lib/pgbus/active_job/executor.rb
348
351
  - lib/pgbus/active_job/quiet_logs.rb
349
352
  - lib/pgbus/autovacuum_tuning.rb
@@ -380,9 +383,11 @@ files:
380
383
  - lib/pgbus/dead_letter_header.rb
381
384
  - lib/pgbus/dedicated_connection.rb
382
385
  - lib/pgbus/dedup_cache.rb
386
+ - lib/pgbus/deploy/dependencies.rb
383
387
  - lib/pgbus/deploy/revision.rb
384
388
  - lib/pgbus/deploy/rollover.rb
385
389
  - lib/pgbus/deploy/runner.rb
390
+ - lib/pgbus/deploy/schema.rb
386
391
  - lib/pgbus/deploy/task.rb
387
392
  - lib/pgbus/doctor.rb
388
393
  - lib/pgbus/engine.rb