Skip to content

Commit ce2e6b9

Browse files
committed
Update examples and benchmarks for downstream runtime
1 parent 2c33a09 commit ce2e6b9

9 files changed

Lines changed: 61 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## 0.2.0 Unreleased
44

55
- Optional downstream processor delegation to `cdc-concurrent` and `cdc-parallel`.
6+
- Rails example now demonstrates `cdc-concurrent` downstream processing.
7+
- Benchmark can measure direct downstream delegation overhead.
68

79
## 0.1.2
810

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ bundle exec rake benchmark:enqueue
134134
```
135135

136136
Set `CDC_SOLID_QUEUE_BENCH_EVENTS` to control the event count.
137+
Set `CDC_SOLID_QUEUE_BENCH_MODE=downstream_direct` to measure direct downstream
138+
processor delegation overhead without Solid Queue enqueue translation.
137139

138140
Example local result on Ruby 3.4.9:
139141

‎benchmark/enqueue_benchmark.rb‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'benchmark'
44
require 'cdc/solid_queue'
5+
require 'cdc/core'
56

67
# Fake Active Job class used by the enqueue benchmark.
78
class BenchmarkJob
@@ -16,7 +17,14 @@ def perform_later(_payload)
1617
end
1718
end
1819

20+
BenchmarkProcessor = Class.new(CDC::Core::Processor) do
21+
def process(event)
22+
event
23+
end
24+
end
25+
1926
events = Integer(ENV.fetch('CDC_SOLID_QUEUE_BENCH_EVENTS', '10000'))
27+
mode = ENV.fetch('CDC_SOLID_QUEUE_BENCH_MODE', 'enqueue')
2028

2129
config = CDC::SolidQueue::Configuration.new
2230
config.processor_job = BenchmarkJob
@@ -33,9 +41,26 @@ def perform_later(_payload)
3341
}
3442

3543
elapsed = Benchmark.realtime do
36-
events.times { enqueuer.enqueue(event) }
44+
case mode
45+
when 'enqueue'
46+
events.times { enqueuer.enqueue(event) }
47+
when 'downstream_direct'
48+
config.downstream_processor = BenchmarkProcessor.new
49+
config.downstream_runtime = :direct
50+
processor = CDC::SolidQueue::DownstreamProcessor.new(config)
51+
change_event = CDC::SolidQueue::EventSerializer.load_event(event)
52+
events.times { processor.process(change_event) }
53+
else
54+
raise ArgumentError, "unknown benchmark mode: #{mode}"
55+
end
3756
end
3857

3958
rate = events / elapsed
4059

41-
puts format('events=%<events>d elapsed=%<elapsed>.4fs rate=%<rate>.2f events/s', events:, elapsed:, rate:)
60+
puts format(
61+
'mode=%<mode>s events=%<events>d elapsed=%<elapsed>.4fs rate=%<rate>.2f events/s',
62+
mode:,
63+
events:,
64+
elapsed:,
65+
rate:
66+
)

‎examples/rails_app/.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ DATABASE_URL=postgres://postgres:postgres@127.0.0.1:55432/cdc_solid_queue_exampl
22
CDC_SLOT=cdc_solid_queue_example
33
CDC_PUBLICATION=cdc_publication
44
CDC_AUTO_CREATE_SLOT=true
5+
CDC_CONCURRENT_CONCURRENCY=100
6+
CDC_CONCURRENT_TIMEOUT=5.0

‎examples/rails_app/Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
source 'https://rubygems.org'
44

5+
gem 'cdc-concurrent', path: '../../../cdc-concurrent'
56
gem 'cdc-solid-queue', path: '../..'
67
gem 'rails', '~> 7.1'
78
gem 'solid_queue', '~> 1.0'

‎examples/rails_app/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ the gem repository does not need to vendor a full app skeleton.
1313
- `config/database.yml` points Rails at the local PostgreSQL container.
1414
- `config/initializers/cdc_solid_queue.rb` configures ingestion.
1515
- `app/jobs/application_job.rb` provides the Rails job base class.
16-
- `app/jobs/user_changed_job.rb` consumes `CDC::Core::ChangeEvent` objects.
16+
- `app/jobs/user_changed_job.rb` hands events to the configured downstream runtime.
17+
- `app/processors/webhook_processor.rb` is a `cdc-concurrent` processor.
1718
- `compose.yml` starts PostgreSQL with logical replication enabled.
1819
- `db/init/01_cdc_example.sql` creates the example table and publication.
1920

@@ -40,8 +41,8 @@ bin/rails cdc_solid_queue:start
4041
```
4142

4243
The task streams pgoutput payloads, normalizes them to `CDC::Core::ChangeEvent`,
43-
enqueues the configured job through Solid Queue, and advances the checkpoint
44-
after enqueue succeeds.
44+
enqueues the configured job through Solid Queue, executes `WebhookProcessor`
45+
through `cdc-concurrent`, and advances the checkpoint after enqueue succeeds.
4546

4647
Generate a change event from another terminal:
4748

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Example job that consumes normalized CDC events.
3+
# Example job that hands normalized CDC events to configured downstream runtimes.
44
class UserChangedJob < ApplicationJob
55
include CDC::SolidQueue::ProcessorJob
6-
7-
def process(event)
8-
Rails.logger.info(
9-
"cdc event #{event.operation} #{event.qualified_table_name} primary_key=#{event.primary_key.inspect}"
10-
)
11-
end
126
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'cdc/core'
4+
require 'cdc/concurrent'
5+
6+
# Example I/O-oriented CDC processor executed through cdc-concurrent.
7+
class WebhookProcessor < CDC::Core::Processor
8+
concurrent_safe!
9+
10+
def process(event)
11+
Rails.logger.info(
12+
"downstream cdc event #{event.operation} #{event.qualified_table_name} primary_key=#{event.primary_key.inspect}"
13+
)
14+
CDC::Core::ProcessorResult.success(event)
15+
end
16+
end

‎examples/rails_app/config/initializers/cdc_solid_queue.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
config.preserve_order = true
77
config.ordering_key = :primary_key
88
config.checkpoint = CDC::SolidQueue::Checkpoint.new
9+
config.downstream_processor = WebhookProcessor.new
10+
config.downstream_runtime = :concurrent
11+
config.downstream_options = {
12+
concurrency: Integer(ENV.fetch('CDC_CONCURRENT_CONCURRENCY', '100')),
13+
timeout: Float(ENV.fetch('CDC_CONCURRENT_TIMEOUT', '5.0'))
14+
}
915
config.postgresql = {
1016
database_url: ENV.fetch('DATABASE_URL'),
1117
slot: ENV.fetch('CDC_SLOT', 'cdc_solid_queue'),

0 commit comments

Comments
 (0)