allthingssecurity opened a new pull request, #30861: URL: https://github.com/apache/camel/pull/30861
# Description [CAMEL-27527](https://issues.apache.org/jira/browse/CAMEL-27527) follow-up of #30821. #30821 counts the producers between their read of the Disruptor reference and their publish, so that a reconfiguration can clear the reference and wait until no producer is still publishing on the old ring buffer. The count was one `AtomicInteger`, so every publish did two atomic writes on one cache line shared by all the producers. An earlier benchmark with fast consumers (`concurrentConsumers=4`) measured about 11% less producer throughput at 4 and 12 producer threads (no difference with one consumer). A later run on a busy machine did not reproduce that loss, but the cost of the shared counter shows clearly when the publish prologue is measured on its own (below). The fix: each thread counts in a cell of its own, chosen by its thread id and padded to 128 bytes. The increment and the decrement of a publish go to the same cell, and the reconfiguration waits until every cell reads 0. The protocol is unchanged: the producer increments, then reads the reference (and backs out if it was cleared); the reconfiguration clears the reference, then waits. Why the wait still cannot end while a producer that read the old reference publishes: - The increment (`AtomicLongArray.getAndIncrement`, a volatile getAndAdd) comes before the volatile read of the reference, and the clearing (a volatile write) before the volatile reads of the cells. If the producer read the old reference, the read of its cell sees its increment. This is the same Dekker argument as with the shared counter. - A thread decrements the cell it incremented, after the increment, so a cell is never below 0 and reads 0 only once each increment it holds has its decrement. The decrement comes after the publish, so the reconfiguration then sees that publish. - The cells are read one after the other, not at once. That cannot hide a producer that read the old reference: its read came before the clearing, so its increment did too, and every cell is read after the clearing. A producer that increments later sees the cleared reference and backs out, so it can only make the wait longer. Why not `LongAdder`: `Striped64` moves a thread to another cell when a CAS on its cell fails, and its first updates go to `base` until the cells exist. The increment and the decrement of one publish can then land in different cells, and `sum()` can see the decrement of a producer that saw the cleared reference without its increment: -1, which hides another producer still in flight. Its cells are also written with `weakCompareAndSetRelease` (JDK 21), which the memory model does not order before the later volatile read of the reference. Numbers (12-core Mac, JDK 21; the machine had a load of 30 to 40 from other jobs): | | none | shared (#30821) | striped | LongAdder | |---|---|---|---|---| | publish prologue, 4 threads, ns/op | 34 | 268 | 109 | 93 | The prologue: claim a sequence with a CAS on a shared cursor, read the reference, count around it (median of 3 runs). The 12-thread run is left out: with 12 threads on 12 busy cores it was noise (the striped counter even beat no counter). | disruptor bench, msgs/s, median of 4 interleaved runs | main | shared | striped | |---|---|---|---| | 1 consumer, 4 producers | 2.50M | 2.41M | 2.54M | | 1 consumer, 12 producers | 2.00M | 2.14M | 2.01M | | concurrentConsumers=4, 4 producers | 3.22M | 3.53M | 3.74M | | concurrentConsumers=4, 12 producers | 2.99M | 3.47M | 3.18M | With 4 consumers the runs varied by about 20% at that load, so these show no loss for the striped counter and do not reproduce the earlier 11% of the shared one either; the prologue numbers show where its cost is. Tests: no new test. The camel-disruptor tests pass (147), with `DisruptorReconfigureWithWaitingProducersTest` of #30821, and so do stress runs that add and remove consumers while producers publish (no message lost or duplicated). No upgrade guide entry: nothing changes for users. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27527](https://issues.apache.org/jira/browse/CAMEL-27527) (follow-up of #30821). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built and tested camel-disruptor with the formatter and import-sort plugins. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
