merlimat opened a new pull request, #25669:
URL: https://github.com/apache/pulsar/pull/25669

   ### Motivation
   
   The existing `Backoff` class always subtracts up to 10% jitter from the next 
delay, never adds, and skips jitter on the first invocation entirely (the 
result is clamped back up to `initialDelay`). When many clients restart 
together, the first reconnect attempt fires at the exact configured initial 
delay — defeating the point of jitter. The jitter percentage was also 
hard-coded.
   
   ### Modifications
   
   - `Backoff` takes a configurable `jitterPercent` (default 10).
   - Jitter is applied symmetrically as `±jitterPercent/2`: the underlying 
delay is multiplied by a uniform random factor in `[1 − jitterPercent/200, 1 + 
jitterPercent/200)`.
   - Jitter is applied on every call, including the first. The previous 
`Math.max(initial, …)` clamp is removed; the returned value may now be slightly 
below `initialDelay` or slightly above `maxBackoff`.
   - `BackoffPolicy` (v5 client API) gains a `jitterPercent` field with a 
`withJitter(double)` helper. Existing factory methods seed the default jitter 
so callers are unaffected.
   
   The new `BackoffPolicy.jitterPercent` is currently API-shape only: the 
connection-backoff path is still TODO'd in `PulsarClientBuilderV5`, and the 
redelivery-backoff path goes through `MultiplierRedeliveryBackoff` which has no 
jitter support. Wiring is left for a follow-up.
   
   ### Verifying this change
   
   This change added tests and can be verified as follows:
   
   - New unit tests in `BackoffTest`:
     - `jitterIsAppliedSymmetricallyOnFirstCall` — across 500 resets verifies 
values both above and below the base, all within `[base · 0.9, base · 1.1]` for 
`jitterPercent=20`.
     - `jitterPercentZeroDisablesJitter` — verifies the disable path returns 
the exact base.
     - `negativeJitterIsRejected` — validation.
   - Existing tests updated to set `jitterPercent(0)` for deterministic 
exponential-progression and mandatory-stop checks.
   
   ### Does this pull request potentially affect one of the following parts:
   
   - [x] The public API
   
   `org.apache.pulsar.common.util.Backoff` and 
`org.apache.pulsar.client.api.v5.config.BackoffPolicy` gain new optional 
fields/builder methods. Existing callers are unaffected (defaults preserve the 
10% magnitude, though the direction now spans `±5%` instead of `−10%`).


-- 
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]

Reply via email to