karafka-core 2.6.5 → 2.6.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 236e0814cbc0a24dc6758f75c71057e7744c0827f1c9cb4b5be5f305bceefc46
4
- data.tar.gz: b3d9b8d7b42e9af2aaa2609bd929dab513375aa86ebf49d198c94e11cb81427c
3
+ metadata.gz: 985a6489d2138041eac532dc648092e518e19ead6cd0d3cf7def938529f74d12
4
+ data.tar.gz: 141b16d8d305582b79ad6e2b70ae61f5d0878b5dc0a8267ccdee8936bfd0451e
5
5
  SHA512:
6
- metadata.gz: e6cabe34dc4bb8449ba87cec2d1dc7d120277fecc771ee7a90509f34454e28705a7ab3e883a68799ac128badd9e5f8ad3d5d6898e2e4d1565568dad3b98f0bb9
7
- data.tar.gz: 80072d69c0ae0eb59b933c83d530e3099bf16f16c596b34bdf16c70f0986938c599dbb65c13331c69e7881119cde8c200fc51713729e84a085c15dd6d989fdba
6
+ metadata.gz: 3e6dec63a69f7d0e8a91db0ad061f2c852bba964eacc88d3a7b47a9a14f7bb03b060a402efeb6979a1837301a67f079230dcdaf72f239aeffac5bd7f78b470df
7
+ data.tar.gz: cf7e821ea42e1930bb6323e4a8d358311b75293094d608aa07a104bced17af79c518d1f76ca0cf6eff63fed4f63514b25b1ece7e3b7507d64accecb2d36d26b9
data/.github/CODEOWNERS CHANGED
@@ -1,3 +1,6 @@
1
+ # Every change needs a review from a maintainer before it can merge.
2
+ * @mensfeld
3
+
1
4
  /.github @mensfeld
2
5
  /.github/workflows/ @mensfeld
3
- /.github/actions/ @mensfeld
6
+ /.github/actions/ @mensfeld
@@ -14,9 +14,52 @@ permissions:
14
14
  contents: read
15
15
 
16
16
  jobs:
17
+ # Decides whether the change touches anything but docs. Docs-only pull requests skip the jobs
18
+ # below; CI Success still runs and reports success, so the required check does not stay pending
19
+ changes:
20
+ timeout-minutes: 5
21
+ runs-on: ubuntu-latest
22
+ outputs:
23
+ code: ${{ steps.filter.outputs.code }}
24
+ steps:
25
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
26
+ with:
27
+ fetch-depth: 2
28
+
29
+ - name: Detect code changes
30
+ id: filter
31
+ env:
32
+ EVENT_NAME: ${{ github.event_name }}
33
+ run: |
34
+ # Only pull requests can skip; scheduled and any other runs always test everything
35
+ if [ "$EVENT_NAME" != "pull_request" ]; then
36
+ echo "code=true" >> "$GITHUB_OUTPUT"
37
+ exit 0
38
+ fi
39
+
40
+ # HEAD is the PR merge commit, so HEAD^1..HEAD is what the PR changes in master
41
+ changed="$(git diff --name-only --no-renames HEAD^1 HEAD)"
42
+
43
+ # Anything not on the docs list counts as code (an empty diff too)
44
+ code=false
45
+ while IFS= read -r file; do
46
+ case "$file" in
47
+ lib/*|spec/*|test/*|ext/*|bin/*) code=true ;;
48
+ *.md|LICENSE*|MIT-LICENSE|.github/ISSUE_TEMPLATE/*|.github/FUNDING.yml) ;;
49
+ *.png|*.jpg|*.jpeg|*.svg|*.gif) ;;
50
+ *) code=true ;;
51
+ esac
52
+ done <<< "$changed"
53
+
54
+ echo "Changed files:"
55
+ echo "$changed"
56
+ echo "code=$code" >> "$GITHUB_OUTPUT"
57
+
17
58
  tests:
18
59
  timeout-minutes: 15
19
60
  runs-on: ubuntu-latest
61
+ needs: changes
62
+ if: needs.changes.outputs.code == 'true'
20
63
  strategy:
21
64
  fail-fast: false
22
65
  matrix:
@@ -24,6 +67,7 @@ jobs:
24
67
  - '4.0'
25
68
  - '3.4'
26
69
  - '3.3'
70
+ - 'jruby-10.0'
27
71
  include:
28
72
  - ruby: '4.0'
29
73
  coverage: 'true'
@@ -36,7 +80,7 @@ jobs:
36
80
  run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"
37
81
 
38
82
  - name: Set up Ruby
39
- uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
83
+ uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
40
84
  with:
41
85
  ruby-version: ${{matrix.ruby}}
42
86
  bundler: 'latest'
@@ -61,6 +105,8 @@ jobs:
61
105
  rubocop:
62
106
  timeout-minutes: 5
63
107
  runs-on: ubuntu-latest
108
+ needs: changes
109
+ if: needs.changes.outputs.code == 'true'
64
110
  env:
65
111
  BUNDLE_GEMFILE: Gemfile.lint
66
112
  steps:
@@ -68,9 +114,9 @@ jobs:
68
114
  with:
69
115
  fetch-depth: 0
70
116
  - name: Set up Ruby
71
- uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
117
+ uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
72
118
  with:
73
- ruby-version: '4.0.7'
119
+ ruby-version: '4.0.9'
74
120
  bundler-cache: true
75
121
  - name: Run rubocop
76
122
  run: bundle exec rubocop
@@ -78,6 +124,8 @@ jobs:
78
124
  yard-lint:
79
125
  timeout-minutes: 5
80
126
  runs-on: ubuntu-latest
127
+ needs: changes
128
+ if: needs.changes.outputs.code == 'true'
81
129
  env:
82
130
  BUNDLE_GEMFILE: Gemfile.lint
83
131
  steps:
@@ -85,9 +133,9 @@ jobs:
85
133
  with:
86
134
  fetch-depth: 0
87
135
  - name: Set up Ruby
88
- uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
136
+ uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
89
137
  with:
90
- ruby-version: '4.0.7'
138
+ ruby-version: '4.0.9'
91
139
  bundler-cache: true
92
140
  - name: Run yard-lint
93
141
  run: bundle exec yard-lint lib/
@@ -95,6 +143,8 @@ jobs:
95
143
  lostconf:
96
144
  timeout-minutes: 5
97
145
  runs-on: ubuntu-latest
146
+ needs: changes
147
+ if: needs.changes.outputs.code == 'true'
98
148
  steps:
99
149
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
100
150
  with:
@@ -114,15 +164,18 @@ jobs:
114
164
  runs-on: ubuntu-latest
115
165
  if: always()
116
166
  needs:
167
+ - changes
117
168
  - rubocop
118
169
  - tests
119
170
  - yard-lint
120
171
  - lostconf
121
172
  steps:
173
+ # Skipped jobs are fine only when the change is docs-only
122
174
  - name: Check all jobs passed
123
175
  if: |
176
+ needs.changes.result != 'success' ||
124
177
  contains(needs.*.result, 'failure') ||
125
178
  contains(needs.*.result, 'cancelled') ||
126
- contains(needs.*.result, 'skipped')
179
+ (needs.changes.outputs.code != 'false' && contains(needs.*.result, 'skipped'))
127
180
  run: exit 1
128
181
  - run: echo "All CI checks passed!"
@@ -24,7 +24,7 @@ jobs:
24
24
  fetch-depth: 0
25
25
 
26
26
  - name: Set up Ruby
27
- uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
27
+ uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
28
28
  with:
29
29
  bundler-cache: false
30
30
 
@@ -10,7 +10,14 @@ jobs:
10
10
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
11
11
  - name: Check SHA pins
12
12
  run: |
13
- if grep -E -r "uses: .*/.*@(v[0-9]+|main|master)($|[[:space:]]|$)" --include="*.yml" --include="*.yaml" .github/workflows/ | grep -v "#"; then
13
+ # Every remote action must be pinned to a full 40-character commit SHA. Local actions
14
+ # (./) and docker:// images are not checked.
15
+ unpinned=$(grep -E -rn --include="*.yml" --include="*.yaml" \
16
+ "^[[:space:]]*(-[[:space:]]+)?uses:[[:space:]]*['\"]?[^.'\"[:space:]]" .github/workflows/ \
17
+ | grep -Ev "uses:[[:space:]]*['\"]?docker://" \
18
+ | grep -Ev "@[0-9a-f]{40}['\"]?([[:space:]]|$)" || true)
19
+ if [ -n "$unpinned" ]; then
20
+ echo "$unpinned"
14
21
  echo "::error::Actions should use SHA pins, not tags or branch names"
15
22
  exit 1
16
23
  fi
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 4.0.7
1
+ 4.0.9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Karafka Core Changelog
2
2
 
3
+ ## 2.6.7 (2026-11-10)
4
+ - [Maintenance] Declare `rake` in the `Gemfile`. It came only through `karafka-rdkafka`, which will stop depending on it for precompiled platform gems.
5
+ - [Enhancement] Make `Taggable::Tags#add` with the value a tag already has and `#delete` of a missing tag about 6x faster on MRI and JRuby.
6
+ - [Fix] Make `Contractable::Result#errors` keep the first error when one path has more than one, instead of the last.
7
+ - [Fix] Make `Contractable::Contract#call` return a successful result for a contract with no rules of its own, instead of raising `NoMethodError`.
8
+ - [Fix] Make `Contractable::Contract.rules` return an empty Array for a contract with no rules, instead of `nil`.
9
+
10
+ ## 2.6.6 (2026-10-25)
11
+ - [Maintenance] Drop unused `byebug` from the test bundle.
12
+ - [Maintenance] Close the producer in `bindings_test.rb` so the suite exits at once; on JRuby suite exit time dropped from ~315 s to 13–15 s.
13
+ - [Fix] Make `Taggable::Tags` safe to read while another thread adds or removes tags. On JRuby a concurrent read could raise `NullPointerException`.
14
+ - [Fix] Make `Taggable#tags` return one `Tags` per object when several threads call it first. On JRuby each thread could get its own `Tags`, and tags were lost.
15
+ - [Fix] Make `Notifications#register_event` safe while other threads call `#instrument`. On JRuby a registered event could look unregistered or be dispatched without its method.
16
+ - [Fix] Make the `Monitor` event name cache thread-safe for a namespace or Symbol event ids, so on JRuby concurrent `#instrument` calls no longer corrupt it.
17
+
3
18
  ## 2.6.5 (2026-10-19)
4
19
  - **[EOL]** Drop Ruby 3.2 support due to EOL; require Ruby 3.3+.
5
20
  - [Change] Use the offending value (event id, setting name, class or constant name) as the message of errors raised by `Notifications`, `Configurable::Node`, `Configurable::Importer` and the spec locators.
@@ -12,8 +27,8 @@
12
27
 
13
28
  ## 2.6.2 (2026-06-29)
14
29
  - [Enhancement] Document that a setting default is shared across all config instances, so a mutable default such as `[]` must not be changed in place.
15
- - [Fix] Require `pathname` explicitly, so `Karafka::Core.gem_root` no longer raises `NameError` when nothing else loaded it.
16
30
  - [Enhancement] Document that a `virtual` rule must return a freshly built `Array` of `[path, message]` error pairs on each call; a memoized, shared or frozen array is unsupported.
31
+ - [Fix] Require `pathname` explicitly, so `Karafka::Core.gem_root` no longer raises `NameError` when nothing else loaded it.
17
32
  - [Fix] `Configurable::Node#register` now raises the documented "already registered" `ArgumentError` for a name used by an unread lazy-with-constructor setting, which it previously overwrote silently.
18
33
  - [Fix] Stop a rescued error inside `Contractable::Contract.nested` from prefixing its path onto every rule defined afterwards.
19
34
  - [Fix] Report a missing path instead of raising `NoMethodError` when validating a non-Hash root with a 1-key or 2-key rule.
@@ -32,17 +47,17 @@
32
47
 
33
48
  ## 2.6.1 (2026-06-15)
34
49
  - [Enhancement] Speed up `Contract#call` by ~1.25–1.4x, the per-message validation path in WaterDrop producers.
35
- - [Fix] Report a missing path instead of raising `NoMethodError` when a 3+ key rule path meets a non-Hash intermediate value.
36
- - [Change] Reject reserved setting names in `Configurable::Node#setting` and `#register` with an `ArgumentError`, since they could corrupt the node state.
37
50
  - [Enhancement] Speed up `Monitor#instrument` dispatch (~1.2x) on the common no-subscribers path.
38
51
  - [Enhancement] Speed up `Configurable::Node` settings reads (~1.4x flat, ~1.6x nested) on hot paths.
39
52
  - [Enhancement] Reduce object shape variations of config nodes by giving each settings layout its own anonymous `Node` subclass.
40
- - [Fix] Symbolize setting names at definition time and on config store writes, so `String` setting names work end to end and cannot corrupt node internal state when they match reserved internal names.
53
+ - [Change] Reject reserved setting names in `Configurable::Node#setting` and `#register` with an `ArgumentError`, since they could corrupt the node state.
41
54
  - [Change] Config nodes are instances of anonymous `Node` subclasses: `is_a?(Karafka::Core::Configurable::Node)` still holds, but `instance_of?(Node)` is `false` and `node.class.name` is `nil`.
42
55
  - [Change] Assigning a setting on a frozen config node now raises `FrozenError` (previously the write silently mutated internal storage despite the freeze).
56
+ - [Fix] Report a missing path instead of raising `NoMethodError` when a 3+ key rule path meets a non-Hash intermediate value.
57
+ - [Fix] Symbolize setting names at definition time and on config store writes, so `String` setting names work end to end and cannot corrupt node internal state when they match reserved internal names.
43
58
 
44
59
  ## 2.6.0 (2026-06-10)
45
- - [Enhancement] Add `Node#register` to allow runtime key-value registration on compiled nodes without going through the static `setting` DSL. Useful for dynamic registries (e.g. named clusters) where setting names are not known at class-load time.
60
+ - [Enhancement] Add `Node#register` to register keys at runtime on compiled nodes, for dynamic registries (e.g. named clusters) whose setting names are not known at class-load time.
46
61
  - [Enhancement] Enable all opt-in Ruby warning categories via `Warning.categories`, so new ones (e.g. `strict_unused_block` in Ruby 3.4+) are picked up automatically.
47
62
 
48
63
  ## 2.5.13 (2026-04-08)
@@ -54,24 +69,24 @@
54
69
  ## 2.5.11 (2026-04-02)
55
70
  - [Enhancement] Specialize `Contract#dig` for common 1-key and 2-key paths to avoid iterator overhead, yielding ~1.5x faster single-key lookups and ~1.45x faster two-key nested lookups.
56
71
  - [Enhancement] Replace `Node#build_accessors` `@local_defs` Array with Hash for O(1) membership checks instead of O(n) `Array#include?`, yielding up to ~5x faster accessor lookups at 50 settings.
57
- - [Enhancement] Use frozen `EMPTY_ARRAY` constant for `Contract#call` and `#validate!` default `scope` parameter to avoid allocating a new Array on every invocation, yielding ~1.36x faster call dispatch and saving 1 Array allocation per call.
58
- - [Enhancement] Pre-resolve `@events_methods_map` method name before the listener notification loop in `Notifications#instrument` to avoid repeated Hash lookup per listener, yielding ~1.12x faster event dispatch with multiple listeners.
72
+ - [Enhancement] Stop allocating an Array on every `Contract#call` and `#validate!` call (~1.36x faster dispatch).
73
+ - [Enhancement] Speed up `Notifications#instrument` with multiple listeners (~1.12x faster event dispatch).
59
74
  - [Enhancement] Cache a frozen success `Result` singleton via `Result.success` to eliminate 1 object allocation per successful `Contract#call` on the happy path.
60
- - [Enhancement] Skip nestings block re-evaluation in `Node#deep_dup` to avoid recreating children that are immediately overwritten, yielding ~14x faster deep_dup and reducing allocations from ~620 to ~66 objects for large configs.
61
- - [Enhancement] Cache `CallbacksManager#call` values snapshot and invalidate on `add`/`delete` to avoid allocating a new Array on every invocation while preserving thread-safety snapshot semantics, saving 1 Array allocation per call.
75
+ - [Enhancement] Speed up `Node#deep_dup` ~14x and cut its allocations from ~620 to ~66 objects for large configs.
76
+ - [Enhancement] Stop allocating an Array on every `CallbacksManager#call` call.
62
77
  - [Enhancement] Build the `Event` payload hash lazily on `#payload` access, saving one Hash allocation per `Notifications#instrument` call with `#[]` access, for ~1.7x faster event dispatch.
63
- - [Enhancement] Replace `StatisticsDecorator#diff` pending-writes buffer with `keys.each` direct-write iteration, eliminating the buffer and write-back loop for ~13% faster decoration at scale (10 brokers, 20 topics, 2000 partitions).
78
+ - [Enhancement] Speed up `StatisticsDecorator#diff` by ~13% at scale (10 brokers, 20 topics, 2000 partitions).
64
79
  - [Enhancement] Reorder `StatisticsDecorator#diff` type checks to test `Numeric` before `Hash`, matching the ~80% numeric value distribution in librdkafka statistics.
65
80
  - [Enhancement] Support an `only_keys` option in `StatisticsDecorator` to decorate only the listed numeric keys. With `excluded_keys`, this cuts decoration cost from ~80ms to ~8.5ms per call on large clusters.
66
- - [Enhancement] Cache `Tags#to_a` values array and invalidate on `add`/`delete`/`clear` to avoid allocating a new Array and running `uniq` on every call, yielding ~7x faster reads at 5 tags and ~28x faster at 20 tags.
81
+ - [Enhancement] Cache `Tags#to_a` (~7x faster reads at 5 tags, ~28x at 20 tags).
67
82
 
68
83
  ## 2.5.10 (2026-03-02)
69
84
  - [Enhancement] Introduce `MinitestLocator` helper for minitest/spec subject class auto-discovery from test file paths.
70
85
 
71
86
  ## 2.5.9 (2026-03-02)
72
- - [Enhancement] Optimize `StatisticsDecorator` to eliminate per-hash Array allocations by using `each_pair` with a per-call pending-writes buffer instead of `current.keys.each`, reducing allocations from tens of thousands to one per call at scale.
87
+ - [Enhancement] Reduce `StatisticsDecorator` allocations from tens of thousands to one per call at scale.
73
88
  - [Enhancement] Inline `StatisticsDecorator#append` and `#suffix_keys_for` into `#diff` to reduce method call overhead by ~96% (from ~915k to ~39k calls at 6400 partitions).
74
- - [Enhancement] Support `excluded_keys` option in `StatisticsDecorator` to skip decoration of unused librdkafka statistics subtrees (e.g. broker toppars, window stats), yielding ~28% additional speedup at scale.
89
+ - [Enhancement] Support an `excluded_keys` option in `StatisticsDecorator` to skip unused librdkafka statistics subtrees (e.g. broker toppars, window stats), ~28% faster at scale.
75
90
 
76
91
  ## 2.5.8 (2025-11-23)
77
92
  - [Enhancement] Memoize `StatisticsDecorator` suffix keys to reduce string allocations (#268).
data/Gemfile CHANGED
@@ -4,8 +4,9 @@ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
+ gem "rake"
8
+
7
9
  group :test do
8
- gem "byebug"
9
10
  gem "minitest"
10
11
  gem "simplecov"
11
12
  gem "warning"
data/Gemfile.lock CHANGED
@@ -1,15 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- karafka-core (2.6.5)
4
+ karafka-core (2.6.7)
5
5
  karafka-rdkafka (>= 0.20.0)
6
6
  logger (>= 1.6.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- byebug (13.0.0)
12
- reline (>= 0.6.0)
13
11
  drb (2.2.3)
14
12
  ffi (1.17.4)
15
13
  ffi (1.17.4-aarch64-linux-gnu)
@@ -22,39 +20,38 @@ GEM
22
20
  ffi (1.17.4-x86_64-darwin)
23
21
  ffi (1.17.4-x86_64-linux-gnu)
24
22
  ffi (1.17.4-x86_64-linux-musl)
25
- io-console (0.9.4)
26
- json (3.0.2)
27
- karafka-rdkafka (0.30.2)
23
+ json (3.1.0)
24
+ karafka-rdkafka (0.31.0)
28
25
  ffi (~> 1.17.1)
29
26
  json (> 2.0)
30
27
  logger
31
28
  mini_portile2 (~> 2.6)
32
29
  rake (> 12)
33
- karafka-rdkafka (0.30.2-aarch64-linux-gnu)
30
+ karafka-rdkafka (0.31.0-aarch64-linux-gnu)
34
31
  ffi (~> 1.17.1)
35
32
  json (> 2.0)
36
33
  logger
37
34
  mini_portile2 (~> 2.6)
38
35
  rake (> 12)
39
- karafka-rdkafka (0.30.2-aarch64-linux-musl)
36
+ karafka-rdkafka (0.31.0-aarch64-linux-musl)
40
37
  ffi (~> 1.17.1)
41
38
  json (> 2.0)
42
39
  logger
43
40
  mini_portile2 (~> 2.6)
44
41
  rake (> 12)
45
- karafka-rdkafka (0.30.2-arm64-darwin)
42
+ karafka-rdkafka (0.31.0-arm64-darwin)
46
43
  ffi (~> 1.17.1)
47
44
  json (> 2.0)
48
45
  logger
49
46
  mini_portile2 (~> 2.6)
50
47
  rake (> 12)
51
- karafka-rdkafka (0.30.2-x86_64-linux-gnu)
48
+ karafka-rdkafka (0.31.0-x86_64-linux-gnu)
52
49
  ffi (~> 1.17.1)
53
50
  json (> 2.0)
54
51
  logger
55
52
  mini_portile2 (~> 2.6)
56
53
  rake (> 12)
57
- karafka-rdkafka (0.30.2-x86_64-linux-musl)
54
+ karafka-rdkafka (0.31.0-x86_64-linux-musl)
58
55
  ffi (~> 1.17.1)
59
56
  json (> 2.0)
60
57
  logger
@@ -67,8 +64,6 @@ GEM
67
64
  prism (~> 1.5)
68
65
  prism (1.9.0)
69
66
  rake (13.4.2)
70
- reline (0.7.0)
71
- io-console (~> 0.5)
72
67
  simplecov (1.3.2)
73
68
  warning (1.6.0)
74
69
 
@@ -86,9 +81,9 @@ PLATFORMS
86
81
  x86_64-linux-musl
87
82
 
88
83
  DEPENDENCIES
89
- byebug
90
84
  karafka-core!
91
85
  minitest
86
+ rake
92
87
  simplecov
93
88
  warning
94
89
 
data/README.md CHANGED
@@ -17,6 +17,6 @@ It includes:
17
17
 
18
18
  First, thank you for considering contributing to the Karafka ecosystem! It's people like you that make the open source community such a great community!
19
19
 
20
- Each pull request must pass all the RSpec specs, integration tests and meet our quality requirements.
20
+ Each pull request must pass all the tests and meet our quality requirements.
21
21
 
22
22
  Fork it, update and wait for the Github Actions results.
@@ -29,6 +29,15 @@ module Karafka
29
29
  # @return [Array<Rule>] all the validation rules defined for a given contract
30
30
  attr_reader :rules
31
31
 
32
+ # Gives each contract its own rules accumulator, so {.rules} is an Array also for a
33
+ # contract that defines no rules and `#call` needs no fallback
34
+ #
35
+ # @param subclass [Class] new contract class
36
+ def inherited(subclass)
37
+ super
38
+ subclass.init_accu
39
+ end
40
+
32
41
  # Allows for definition of a scope/namespace for nested validations
33
42
  #
34
43
  # @param path [Symbol] path in the hash for nesting
@@ -54,14 +63,25 @@ module Karafka
54
63
  # error if missing)
55
64
  #
56
65
  # @param keys [Array<Symbol>] single or full path
57
- # @param block [Proc] validation rule
66
+ # @param block [Proc] validation rule, called with `(value, data, errors, contract)`.
67
+ # It returns `true` when the value is valid. A String is used as the error message.
68
+ # `false` or `nil` reports the `:format` error key. Any other value (e.g. a Symbol) is
69
+ # used as the error key to look up in the error messages.
70
+ #
71
+ # @note Only `true` means valid. Any other truthy value is an error key, so a block
72
+ # ending with e.g. `value =~ regexp` (which returns `0` on a match) reports an error,
73
+ # and building its message raises `KeyError` when no such key is defined.
58
74
  def required(*keys, &block)
59
75
  init_accu
60
76
  @rules << Rule.new(@nested + keys, :required, block).freeze
61
77
  end
62
78
 
79
+ # Defines a rule for an optional field. A missing key is not an error, but a present
80
+ # key is validated, also when its value is `nil`.
81
+ #
63
82
  # @param keys [Array<Symbol>] single or full path
64
- # @param block [Proc] validation rule
83
+ # @param block [Proc] validation rule, with the same arguments and return values as in
84
+ # {.required}
65
85
  def optional(*keys, &block)
66
86
  init_accu
67
87
  @rules << Rule.new(@nested + keys, :optional, block).freeze
@@ -80,12 +100,16 @@ module Karafka
80
100
  # supported -- in-place scoping would accumulate the prefix across validations (or
81
101
  # raise `FrozenError`). Build the result in the block (e.g. `[[%i[id], :invalid]]`)
82
102
  # rather than returning a constant.
103
+ #
104
+ # @note A virtual rule ignores an enclosing `nested`: its block receives the root data
105
+ # (also when it is not a Hash) and its error paths start at the root. Define virtual
106
+ # rules outside of `nested` and use full paths in the returned errors.
83
107
  def virtual(&block)
84
108
  init_accu
85
109
  @rules << Rule.new([], :virtual, block).freeze
86
110
  end
87
111
 
88
- private
112
+ protected
89
113
 
90
114
  # Initializes nestings and rules building accumulator
91
115
  def init_accu
@@ -94,6 +118,8 @@ module Karafka
94
118
  end
95
119
  end
96
120
 
121
+ init_accu
122
+
97
123
  # Runs the validation
98
124
  #
99
125
  # The per-rule handling is inlined instead of dispatching to per-type methods because
@@ -126,7 +152,7 @@ module Karafka
126
152
 
127
153
  # Apply the scope prefix in place on the rule's returned pairs and collect them
128
154
  # directly. Per the `virtual` contract the rule hands back a freshly built Array
129
- # each call (see `DSL#virtual`), so mutating it here is safe and avoids allocating a
155
+ # each call (see `Contract.virtual`), so mutating it here is safe and avoids allocating a
130
156
  # new pair per error.
131
157
  result.each do |sub_result|
132
158
  sub_result[0] = scope + sub_result[0]
@@ -5,6 +5,9 @@ module Karafka
5
5
  module Contractable
6
6
  # Representation of a validation result with resolved error messages
7
7
  class Result
8
+ # @return [Hash{Symbol => String}] error message per path. When a path has more than one
9
+ # error, the first one reported is kept: rules run in definition order and a virtual
10
+ # rule's errors keep the order it returns them in.
8
11
  attr_reader :errors
9
12
 
10
13
  # Empty hash that we use when no errors to save on allocations
@@ -37,6 +40,8 @@ module Karafka
37
40
  errors.each do |error|
38
41
  scope = error.first.map(&:to_s).join(".").to_sym
39
42
 
43
+ next if hashed.key?(scope)
44
+
40
45
  # This will allow for usage of custom messages instead of yaml keys if needed
41
46
  hashed[scope] = if error.last.is_a?(String)
42
47
  error.last
@@ -21,6 +21,7 @@ module Karafka
21
21
  @notifications_bus = notifications_bus
22
22
  @namespace = namespace
23
23
  @mapped_events = {}
24
+ @mutex = Mutex.new
24
25
  end
25
26
 
26
27
  # Passes the instrumentation block (if any) into the notifications bus
@@ -35,7 +36,7 @@ module Karafka
35
36
  full_event_name = if @namespace.nil? && event_id.is_a?(String)
36
37
  event_id
37
38
  else
38
- @mapped_events[event_id] ||= [event_id, @namespace].compact.join(".")
39
+ @mapped_events[event_id] || map_event(event_id)
39
40
  end
40
41
 
41
42
  @notifications_bus.instrument(full_event_name, payload, &)
@@ -65,6 +66,24 @@ module Karafka
65
66
  def listeners
66
67
  @notifications_bus.listeners
67
68
  end
69
+
70
+ private
71
+
72
+ # Builds and caches the full event name for an event id
73
+ #
74
+ # @param event_id [String, Symbol] event id
75
+ # @return [String] full event name
76
+ def map_event(event_id)
77
+ @mutex.synchronize do
78
+ # Copy-on-write: #instrument reads the Hash without the lock and on JRuby a Hash that
79
+ # is written by many threads at once can get corrupted, so we replace it instead.
80
+ @mapped_events[event_id] || begin
81
+ full_event_name = [event_id, @namespace].compact.join(".")
82
+ @mapped_events = @mapped_events.merge(event_id => full_event_name).freeze
83
+ full_event_name
84
+ end
85
+ end
86
+ end
68
87
  end
69
88
  end
70
89
  end
@@ -38,8 +38,13 @@ module Karafka
38
38
  # @param event_id [String] event id
39
39
  def register_event(event_id)
40
40
  @mutex.synchronize do
41
- @listeners[event_id] = []
42
- @events_methods_map[event_id] = :"on_#{event_id.to_s.tr(".", "_")}"
41
+ # Copy-on-write: #instrument reads both Hashes without the lock, and on JRuby a Hash
42
+ # that grows while another thread reads it can return nil for a registered key. The
43
+ # methods map goes first, so an event visible in @listeners always has its method.
44
+ @events_methods_map = @events_methods_map.merge(
45
+ event_id => :"on_#{event_id.to_s.tr(".", "_")}"
46
+ )
47
+ @listeners = @listeners.merge(event_id => [])
43
48
  end
44
49
  end
45
50
 
@@ -48,15 +53,16 @@ module Karafka
48
53
  # @param event_id [String] the key of the event to clear listeners for.
49
54
  def clear(event_id = nil)
50
55
  @mutex.synchronize do
51
- # Copy-on-write: replace the per-event arrays rather than mutating them in place, so a
52
- # dispatch iterating a previously captured array is unaffected (see #notify_listeners).
56
+ # Copy-on-write: replace the Hash and the per-event arrays rather than mutating them in
57
+ # place, so lock-free reads in #instrument and a dispatch iterating a previously
58
+ # captured array are unaffected (see #register_event and #notify_listeners).
53
59
  unless event_id
54
- @listeners.transform_values! { [] }
60
+ @listeners = @listeners.transform_values { [] }
55
61
  return
56
62
  end
57
63
 
58
64
  if @listeners.key?(event_id)
59
- @listeners[event_id] = []
65
+ @listeners = @listeners.merge(event_id => [])
60
66
  return
61
67
  end
62
68
 
@@ -85,17 +91,21 @@ module Karafka
85
91
 
86
92
  raise EventNotRegistered, event_id unless @listeners.key?(event_id)
87
93
 
88
- # Copy-on-write: append by replacing the array, never mutating the one a concurrent
89
- # dispatch may be iterating (see #notify_listeners).
90
- @listeners[event_id] += [block]
94
+ # Copy-on-write: append by replacing the Hash and the array, never mutating the ones
95
+ # a concurrent #instrument or dispatch may be reading (see #notify_listeners).
96
+ @listeners = @listeners.merge(event_id => @listeners[event_id] + [block])
91
97
  else
92
98
  listener = event_id_or_listener
93
99
 
94
- @listeners.each_key do |reg_event_id|
100
+ listeners = @listeners.dup
101
+
102
+ @listeners.each do |reg_event_id, event_listeners|
95
103
  next unless listener.respond_to?(@events_methods_map[reg_event_id])
96
104
 
97
- @listeners[reg_event_id] += [listener]
105
+ listeners[reg_event_id] = event_listeners + [listener]
98
106
  end
107
+
108
+ @listeners = listeners
99
109
  end
100
110
  end
101
111
  end
@@ -109,9 +119,10 @@ module Karafka
109
119
  # unsubscribe(my_listener)
110
120
  def unsubscribe(listener_or_block)
111
121
  @mutex.synchronize do
112
- # Copy-on-write: rebuild each array without the listener instead of deleting in place,
113
- # so a dispatch iterating a previously captured array still sees the full set.
114
- @listeners.transform_values! { |event_listeners| event_listeners - [listener_or_block] }
122
+ # Copy-on-write: rebuild the Hash and each array without the listener instead of
123
+ # deleting in place, so a dispatch iterating a previously captured array still sees
124
+ # the full set.
125
+ @listeners = @listeners.transform_values { |event_listeners| event_listeners - [listener_or_block] }
115
126
  end
116
127
  end
117
128
 
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Core
5
+ module Taggable
6
+ class Tags
7
+ # Tags by name and their unique values, published together so a reader never sees one
8
+ # without the other
9
+ Snapshot = Struct.new(:tags, :values)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -9,34 +9,43 @@ module Karafka
9
9
  class Tags
10
10
  # Creates new tags accumulator
11
11
  def initialize
12
- @accu = {}
13
- @values_cache = nil
12
+ @mutex = Mutex.new
13
+ @snapshot = Snapshot.new({}.freeze, []).freeze
14
14
  end
15
15
 
16
16
  # Adds a tag with a given name to tags
17
17
  # @param name [Symbol] name we want to use for a given tag
18
18
  # @param tag [#to_s] any object that can be converted into a string via `#to_s`
19
19
  def add(name, tag)
20
- @accu[name] = tag.to_s
21
- @values_cache = nil
20
+ value = tag.to_s
21
+ snapshot = @snapshot
22
+
23
+ # The snapshot is frozen and replaced in one assignment, so one read of it is consistent
24
+ # on any engine without the lock
25
+ return snapshot if snapshot.tags[name] == value
26
+
27
+ @mutex.synchronize { publish(@snapshot.tags.merge(name => value)) }
22
28
  end
23
29
 
24
30
  # Removes all the tags
25
31
  def clear
26
- @accu.clear
27
- @values_cache = nil
32
+ @mutex.synchronize { publish({}) }
28
33
  end
29
34
 
30
35
  # Removes a tag with a given name
31
36
  # @param name [Symbol] name of the tag
32
37
  def delete(name)
33
- @accu.delete(name)
34
- @values_cache = nil
38
+ snapshot = @snapshot
39
+
40
+ # Safe without the lock for the same reason as in #add
41
+ return snapshot unless snapshot.tags.key?(name)
42
+
43
+ @mutex.synchronize { publish(@snapshot.tags.except(name)) }
35
44
  end
36
45
 
37
46
  # @return [Array<String>] all unique tags registered
38
47
  def to_a
39
- @values_cache ||= @accu.values.uniq
48
+ @snapshot.values
40
49
  end
41
50
 
42
51
  # @param _args [Object] anything that the standard `to_json` accepts
@@ -50,6 +59,17 @@ module Karafka
50
59
  def as_json(*_args)
51
60
  to_a
52
61
  end
62
+
63
+ private
64
+
65
+ # Replaces the snapshot with one built from the given tags. Readers do not lock, so the tags
66
+ # are never changed in place: each write publishes a new snapshot in one assignment.
67
+ #
68
+ # @param tags [Hash{Symbol => String}] all the tags after the change
69
+ # @return [Snapshot] the published snapshot
70
+ def publish(tags)
71
+ @snapshot = Snapshot.new(tags.freeze, tags.values.uniq).freeze
72
+ end
53
73
  end
54
74
  end
55
75
  end
@@ -8,9 +8,31 @@ module Karafka
8
8
  #
9
9
  # Tags will be converted to strings when they are added
10
10
  module Taggable
11
+ # One lock for the first `#tags` call of every object. That call is rare, and a lock per
12
+ # object would need a safe way to create it as well. Kept off the constants, because
13
+ # constants of this module are visible in every class that includes it.
14
+ @tags_lock = Mutex.new
15
+
16
+ class << self
17
+ # @return [Mutex] lock guarding the lazy creation of tags
18
+ attr_reader :tags_lock
19
+ end
20
+
11
21
  # @return [::Karafka::Core::Taggable::Tags] tags object
12
22
  def tags
13
- @tags ||= Taggable::Tags.new
23
+ @tags || taggable_build_tags
24
+ end
25
+
26
+ private
27
+
28
+ # Do not inline this into `#tags`. A `#synchronize` block there makes every warm `#tags` call
29
+ # about 2.5x slower on JRuby, even when `@tags` is already set. A hand-written `lock` /
30
+ # `ensure unlock` keeps it fast but can leave the lock held when an async exception
31
+ # (`Thread#raise`, `Timeout`) lands between `lock` and `begin`.
32
+ #
33
+ # @return [::Karafka::Core::Taggable::Tags] tags object
34
+ def taggable_build_tags
35
+ Taggable.tags_lock.synchronize { @tags ||= Taggable::Tags.new }
14
36
  end
15
37
  end
16
38
  end
@@ -3,6 +3,6 @@
3
3
  module Karafka
4
4
  module Core
5
5
  # Current Karafka::Core version. We follow the versioning schema of given Karafka version
6
- VERSION = "2.6.5"
6
+ VERSION = "2.6.7"
7
7
  end
8
8
  end
data/lib/karafka-core.rb CHANGED
@@ -24,6 +24,7 @@ require "karafka/core/instrumentation"
24
24
  require "karafka/core/instrumentation/callbacks_manager"
25
25
  require "karafka/core/taggable"
26
26
  require "karafka/core/taggable/tags"
27
+ require "karafka/core/taggable/tags/snapshot"
27
28
  require "karafka/core/patches/rdkafka/bindings"
28
29
 
29
30
  # Karafka framework main namespace
data/package-lock.json CHANGED
@@ -286,9 +286,9 @@
286
286
  }
287
287
  },
288
288
  "node_modules/smol-toml": {
289
- "version": "1.9.0",
290
- "resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.9.0.tgz",
291
- "integrity": "sha512-hpd+HLON7HdZXqYchMM/+LaTTbdK0AU3NngIJ4KVyWbY9bfQqdL9cD+4yf6dUoU2Ap4VsU0JkQi6FxAI1B2mXQ==",
289
+ "version": "1.9.1",
290
+ "resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.9.1.tgz",
291
+ "integrity": "sha512-j7WyPdf0CJoutMzMoPLF/bVXHtKZIT9Rb4eGeEbWw8X2t5oirjPgpL+OpaENZrILYaXqYwnVH+qgUwVbl+pUJQ==",
292
292
  "dev": true,
293
293
  "license": "BSD-3-Clause",
294
294
  "engines": {
data/renovate.json CHANGED
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "bundler": {
3
- "managerFilePatterns": ["/(^|/)Gemfile\\.lint$/"]
3
+ "managerFilePatterns": [
4
+ "/(^|/)Gemfile\\.lint$/"
5
+ ]
4
6
  },
5
7
  "$schema": "https://docs.renovatebot.com/renovate-schema.json",
6
8
  "extends": [
@@ -42,6 +44,13 @@
42
44
  "ruby/setup-ruby"
43
45
  ],
44
46
  "minimumReleaseAge": "5 days"
47
+ },
48
+ {
49
+ "description": "Major updates stay manual",
50
+ "matchUpdateTypes": [
51
+ "major"
52
+ ],
53
+ "automerge": false
45
54
  }
46
55
  ],
47
56
  "labels": [
@@ -53,5 +62,13 @@
53
62
  "schedule": [
54
63
  "before 4am on the first day of the month"
55
64
  ]
65
+ },
66
+ "automerge": true,
67
+ "platformAutomerge": true,
68
+ "osvVulnerabilityAlerts": true,
69
+ "vulnerabilityAlerts": {
70
+ "labels": [
71
+ "security"
72
+ ]
56
73
  }
57
74
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karafka-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.5
4
+ version: 2.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Mensfeld
@@ -93,6 +93,7 @@ files:
93
93
  - lib/karafka/core/patches/rdkafka/bindings.rb
94
94
  - lib/karafka/core/taggable.rb
95
95
  - lib/karafka/core/taggable/tags.rb
96
+ - lib/karafka/core/taggable/tags/snapshot.rb
96
97
  - lib/karafka/core/version.rb
97
98
  - log/.gitkeep
98
99
  - package-lock.json