Skip to content

v2.24.0

Choose a tag to compare

@bpacholek bpacholek released this 09 Oct 04:47
· 127 commits to main since this release

Minor release. The client's drain() now hands a JetStream pull consumer run the messages its pulls hold, waits for the handler while the acks go out, and then closes the connection; PullConsumerIterator::handle() then resolves with its count (#207). Before, the drain closed the connection under the run: those messages were lost on a consumer without acks and came again after ack_wait otherwise. disconnect() now discards what a run holds wherever the run is, as it discards the connection's own queue.

Upgrading is recommended if you shut pull consumer workers down with the client's drain(), for example from a SIGTERM handler. There are no API changes. What changes when the application closes a connection that a pull consumer run uses:

  • drain(): once its flush is done, it asks each run to hand every pull in flight to the handler, in order, while the connection is Draining, and waits for that, the handler included, within its budget (requestTimeoutMs). The run then ends and handle() resolves with its count instead of failing with Connection is not open. What the budget leaves is discarded and named in the drain's drain deadline exceeded report. While the client drains, a run issues no new pull.
  • Acks during that hand-over: the handler's ack(), nak(), term() and inProgress() go out. Requests do not: the connection refuses them while it is Draining, so ackSync(), a JetStream publish or a Key/Value write fails there with Connection is not open. A handler that may run during a drain should ack with ack().
  • A handler, onError or errorListener that awaits the client's drain() holds that drain for its whole budget, as a core subscription's handler does, since the drain waits for the run that waits for it. Stop or drain the iterator there and drain the client once handle() has resolved, or call drain() without awaiting it.
  • disconnect(), from the handler or another fiber, now also ends the hand-over of a pull the run retires, before the next message. The rest of the pull used to reach the handler after the close, every ack failing. With explicit acks the rest is now delivered again after ack_wait and processed once rather than twice; on a consumer without acks or with max_deliver: 1 it is lost, as in the hand-over before a failing run since 2.22.0. handle() still fails after a disconnect(), except where the client's drain() had already asked the run for its hand-over, or a finite run has no pull left to issue.

A worker that drains the iterator and awaits handle() before it closes the connection sees no change. symfony-nats-messenger fetches with fetchBatch(), not the pull consumer iterator, and works with this release unchanged.

Fixed

  • The client's drain() dropped what a pull consumer run's pulls held (#207). The engine hands a pull's messages to the handler only when the pull completes (its batch full, a status from the server, or its deadline), and the drain delivered only the connection's own queues before it closed the connection under the run. Measured on nats-server 2.12.15 with ack_wait 3 s, the stream holding m-1 and m-2, and drain() called 1 s into an infinite run of batch 3. With 2.23.0 the handler got nothing and handle() threw Connection is not open. With ack_policy: none both messages were gone, and with explicit acks both came again after ack_wait. With 2.24.0 the handler gets m-1 and m-2 while the connection is Draining, both acks reach the server (ack_floor 2, nothing pending), and handle() returns 2.

Changed

  • disconnect() discards what a pull consumer run holds also while the run hands over a pull it retires (#207). This matches what it does to the hand-over before a run fails and to the connection's own queue (nats.go Close() parity). Measured on the same server, with the handler awaiting disconnect() on m-1 of a batch-2 pull: with 2.23.0, m-2 reached the handler on the closed connection, its ack failing, and with explicit acks it was delivered again after ack_wait; with 2.24.0, m-2 is not handed over and is delivered again once.

New example

  • examples/pull-consumer-graceful-drain.php: a pull consumer worker shut down with drain() while its pull holds three messages. It prints OK pull-consumer-graceful-drain: drain() handed the 3 message(s) the pull held to the handler and waited for their acks (ack floor 3). Against 2.23.0 it fails with handle() failed after the drain, the handler having got 0 message(s): Connection is not open.

Quality gates

  • PHPStan level 8.
  • 3045 unit tests (with data sets) on PHP 8.2 to 8.5, 149 live integration tests and 48 Behat scenarios.
  • All 46 runnable examples executed against a live server, the new one included.
  • Statement coverage 98.84% from the unit suite (95% floor enforced in CI) and 98.90% combined (97% floor).
  • Infection covered MSI 92.9% over the 3786 mutants it ran (90% floor enforced in CI); it skipped 4410 of the 8196 it generated as too slow, so the score does not include them.
  • Every change has tests that fail on the old code. 29 new test cases, decided by the order of events: 26 fail on 2.23.0, and the other 3 are guards that pin what must not change. Three earlier guards that pinned the old close rule were inverted. On the changed lines, Infection generated 119 mutants: 23 killed, 2 timed out, 92 skipped as too slow (the drain tests wait out real budgets) and 2 escaped. One escaped mutant replaces a future the drain asks a run for only once; the other leaves the pulls open during the drain's hand-over, when nothing reaches the run's inbox any more. A first run had found two more in the drain report's count, now pinned by a test.
  • The change was reviewed for semantics and for interleavings by two independent reviewers, and their findings were applied.
  • symfony-nats-messenger's main branch against this release: PHPStan level max, 528 unit tests, 52 functional scenarios against live NATS and 5 examples pass.

The full record is in the CHANGELOG.