skip to content
szczecin 53.43N 14.55E ./start-project

symfony-nats-messenger 5.17: subject filters behind a leaf node

(php, symfony, nats, release-notes) 25 min read by Bartosz Pachołek

5.17.0 makes additional_subjects follow the hub's JetStream behind a NATS leaf node: setup() reads the stream the hub returns, not the leaf node's release, and no longer leaves a consumer without a filter. Run on NATS 2.9, 2.10 and 2.15, with 5.16.0 and 5.17.0 side by side.

symfony-nats-messenger 5.17.0 is out with one fix, the twin of the one in 5.16.0. That release made stream_metadata follow the hub's JetStream behind a NATS leaf node. additional_subjects had the same blind spot: setup() judged it by the release of the leaf node it was connected to, while the stream and the consumer live on the hub. In one direction that refused a setup that would have worked. In the other it let one through that left the transport's consumer reading every message in the stream, other transports' messages included. 5.17.0 decides by the stream the hub's JetStream sends back.

We ran every example on PHP 8.5.9 against the official NATS images 2.9.25, 2.10.29 and 2.15.3 in Docker, with Symfony Messenger 8.2.0 and php-nats-jetstream-client 2.42.1. We installed 5.16.0 and 5.17.0 from Packagist side by side with the same composer.lock, so only the transport differs. The outputs are pasted as they came out.

GitHub release page of symfony-nats-messenger v5.17.0, additional_subjects follows the hub's JetStream on a leaf node, with its summary

Why additional_subjects needs NATS 2.10

additional_subjects came in 5.15.0, from Adrian Brajković's pull request #100, and our symfony-nats-messenger 5.15 post walks through it. A message stamped with NatsSubjectStamp goes to a subject of its own, such as orders.updated.42, and the transport's one durable consumer reads those subjects next to the topic, in stream order. The consumer gets them through filter_subjects, a list of filters that NATS added in 2.10. A JetStream before 2.10 does not know the field: it ignores it and leaves the consumer with no filter at all, reading the whole stream. So the transport refuses the option on a JetStream before 2.10.

Up to 5.16.0 it told such a JetStream by the version in the INFO of the server the transport connected to. Without a leaf node, that is the server that holds the stream. With one, it is not. A leaf node brings a hub's subjects to a site or an edge service, and when it has no JetStream of its own, the transport reaches the hub's JetStream through jetstream_domain. The check read the leaf node, and two wrong answers followed:

  • A leaf node before 2.10 in front of a hub of 2.10 or later was refused, though the hub keeps a consumer's filter_subjects.
  • A leaf node of 2.10 or later in front of a hub before 2.10 was let through. The stream gained the patterns, the consumer create went out with filter_subjects, and the hub dropped them. The NATS client's own check of the reply then failed the setup, after both writes.

Issue #212 wrote both down on 3 December, right after the stream_metadata fix of 5.16.0 had merged. It left one question open: whether the second case leaves the consumer without any filter, as issue #112 had found on a plain 2.9 server. The fix's own measurements answer it, and so do our runs below: it does.

GitHub issue 212: on a leaf node under jetstream_domain the additional_subjects version check reads the leaf node's release, not the hub's, with the two cases

The fix: read the stream, not the server

For stream_metadata, 5.16.0 asked the hub's JetStream for its API level (our symfony-nats-messenger 5.16 post has that story). That cannot work here. The level starts in NATS 2.11, so 2.9 and 2.10 both report none, and 2.10 is exactly the line filter_subjects draws.

5.17.0 reads a different signal, one that setup() already has in hand. From NATS 2.10.0 on, the configuration of a stream in every JetStream reply carries a compression setting, written even when it is "none". A 2.9 JetStream has no such setting. So setup() looks at the stream it gets back:

  1. After a create, at the create's reply. With additional_subjects and no compression, the setup stops there: the stream is written, its consumer is not.
  2. For a stream that exists, at the stream as it reads it before the update, never at the update's reply. By then the update would have added the patterns to the stream of an old JetStream. The setup stops before writing anything.
  3. Without additional_subjects, the same answer decides which filters an existing consumer gets. A JetStream before 2.10 gets the topic alone, as its consumers hold one filter at most. Any other keeps the filters it has, with the topic added.

No request is added and no permission is needed: the reply was already there. A reply without a configuration, which none of the measured releases sends, is let through, and the client's reply check stays the guard. The message keeps its first words, The 'additional_subjects' option requires NATS Server >= 2.10, so a log alert that matches them still matches, and the rest now names the stream and says what was written.

Proof of concept: three hubs and their leaf nodes

Every example below runs on one Compose project: a hub with JetStream under the domain hub for each of three releases, a leaf node with no JetStream in front of each, and a 2.9.25 server on its own. The pairs are picked to cross the 2.10 line in both directions:

yaml
# Three hubs with JetStream under the domain "hub", a leaf node with no JetStream in
# front of each, and one server alone. Host ports:
#   hub29      14881  nats-server 2.9.25, before filter_subjects
#   leaf215    14882  nats-server 2.15.3, leaf node of hub29
#   hub210     14883  nats-server 2.10.29, the first release with filter_subjects
#   leaf29x210 14884  nats-server 2.9.25, leaf node of hub210
#   hub215     14885  nats-server 2.15.3
#   leaf29x215 14886  nats-server 2.9.25, leaf node of hub215
#   solo29     14887  nats-server 2.9.25 with JetStream of its own, no leaf node
x-leaf: &leaf
  volumes: ["./leaf.conf:/etc/nats/leaf.conf:ro"]
x-hub: &hub
  volumes: ["./hub.conf:/etc/nats/hub.conf:ro"]
services:
  hub29:
    <<: *hub
    image: nats:2.9.25-alpine
    command: ["-c", "/etc/nats/hub.conf", "--server_name", "hub29"]
    ports: ["14881:4222"]
  leaf215:
    <<: *leaf
    image: nats:2.15.3-alpine
    command: ["-c", "/etc/nats/leaf.conf", "--server_name", "leaf215"]
    environment: { HUB: "nats-leaf://hub29:7422" }
    ports: ["14882:4222"]
    depends_on: [hub29]
  hub210:
    <<: *hub
    image: nats:2.10.29-alpine
    command: ["-c", "/etc/nats/hub.conf", "--server_name", "hub210"]
    ports: ["14883:4222"]
  leaf29x210:
    <<: *leaf
    image: nats:2.9.25-alpine
    command: ["-c", "/etc/nats/leaf.conf", "--server_name", "leaf29x210"]
    environment: { HUB: "nats-leaf://hub210:7422" }
    ports: ["14884:4222"]
    depends_on: [hub210]
  hub215:
    <<: *hub
    image: nats:2.15.3-alpine
    command: ["-c", "/etc/nats/hub.conf", "--server_name", "hub215"]
    ports: ["14885:4222"]
  leaf29x215:
    <<: *leaf
    image: nats:2.9.25-alpine
    command: ["-c", "/etc/nats/leaf.conf", "--server_name", "leaf29x215"]
    environment: { HUB: "nats-leaf://hub215:7422" }
    ports: ["14886:4222"]
    depends_on: [hub215]
  solo29:
    image: nats:2.9.25-alpine
    command: ["-js", "--server_name", "solo29"]
    ports: ["14887:4222"]

The hubs and the leaf nodes share two small configurations. A leaf node reads its hub's address from the HUB variable that Compose sets:

text
# hub.conf: JetStream under the domain "hub", and a port for leaf nodes.
port: 4222

jetstream {
  domain: hub
  store_dir: "/data"
}

leafnodes {
  port: 7422
}
text
# leaf.conf: a leaf node of the hub in $HUB, with no JetStream of its own.
port: 4222

leafnodes {
  remotes: [
    { url: $HUB }
  ]
}

The project needs only the transport:

json
{
    "require": {
        "idct/symfony-nats-messenger": "^5.17"
    }
}

lib.php holds the map of which server fronts which, and the helpers. api() sends one raw JetStream API request, transport() builds a transport on the stream orders through a leaf node with jetstream_domain=hub, or on solo29 without it, and subjects() and consumer() read the stream and a consumer straight from the hub:

php
<?php

// What the examples share. api() sends one raw JetStream API request, as any other
// tool does; transport() builds a transport on the stream "orders" through one of the
// servers; subjects() and consumer() read the stream and a consumer straight from the
// JetStream that holds them.

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Composer\InstalledVersions;
use IDCT\NatsMessenger\NatsTransport;
use IDCT\NatsMessenger\Options\NatsTransportConfigurationBuilder;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;

/** Where the transport connects => where its JetStream is (see compose.yaml). */
const PAIRS = [
    'leaf215' => 'hub29',
    'leaf29x210' => 'hub210',
    'leaf29x215' => 'hub215',
    'solo29' => 'solo29',
];

const PORTS = [
    'hub29' => 14881, 'leaf215' => 14882, 'hub210' => 14883, 'leaf29x210' => 14884,
    'hub215' => 14885, 'leaf29x215' => 14886, 'solo29' => 14887,
];

/** The message of every example: a line of text. */
final class OrderEvent
{
    public function __construct(public readonly string $text)
    {
    }
}

function client(string $server): mixed
{
    $port = PORTS[$server];
    $client = (new NatsTransportConfigurationBuilder())
        ->build("nats-jetstream://127.0.0.1:$port/x/x")->client;
    $client->connect()->await();

    return $client;
}

/** The name and release the server gives when a client connects to it. */
function server(string $server): string
{
    $client = client($server);
    try {
        $info = $client->serverInfo();

        return "$info->serverName $info->version";
    } finally {
        $client->disconnect()->await();
    }
}

/** One JetStream API request ($JS.API. or another prefix), its reply decoded. */
function api(string $server, string $subject, ?array $body = null,
    string $prefix = '$JS.API.'): array
{
    $client = client($server);
    try {
        $payload = $body === null ? '' : json_encode($body, JSON_THROW_ON_ERROR);
        $reply = $client->request($prefix . $subject, $payload)->await();

        return json_decode($reply->payload, true, 512, JSON_THROW_ON_ERROR);
    } finally {
        $client->disconnect()->await();
    }
}

function versions(): void
{
    printf(
        "symfony-nats-messenger %s, php-nats-jetstream-client %s\n",
        InstalledVersions::getPrettyVersion('idct/symfony-nats-messenger'),
        InstalledVersions::getPrettyVersion('idct/php-nats-jetstream-client'),
    );
}

/** A transport on the stream "orders" through $via; on a leaf node, jetstream_domain. */
function transport(string $via, string $topic, string $consumer,
    array $options = []): NatsTransport
{
    $port = PORTS[$via];
    $dsn = "nats-jetstream://127.0.0.1:$port/orders/$topic?consumer=$consumer";
    if (PAIRS[$via] !== $via) {
        // A leaf node: the stream and the consumer live on its hub.
        $dsn .= '&jetstream_domain=hub';
    }

    return new NatsTransport($dsn, $options + ['max_batch_timeout' => 0.2],
        new PhpSerializer());
}

/** Runs setup(): "done", or the exception it threw. */
function setup(NatsTransport $transport): string
{
    try {
        $transport->setup();

        return 'done';
    } catch (Throwable $e) {
        return (new ReflectionClass($e))->getShortName() . ': ' . $e->getMessage();
    }
}

/** The stream's subjects, as the JetStream that holds it has them. */
function subjects(string $holder, string $stream): string
{
    $info = api($holder, "STREAM.INFO.$stream");
    if (isset($info['error'])) {
        return "no stream $stream";
    }

    return '[' . implode(', ', $info['config']['subjects']) . ']';
}

/** The consumer's filters, in whichever field it holds them, and its num_pending. */
function consumer(string $holder, string $stream, string $name): string
{
    $info = api($holder, "CONSUMER.INFO.$stream.$name");
    if (isset($info['error'])) {
        return "no consumer $name";
    }
    $config = $info['config'];
    $filters = match (true) {
        isset($config['filter_subjects']) => 'filter_subjects ['
            . implode(', ', $config['filter_subjects']) . ']',
        isset($config['filter_subject']) => "filter_subject {$config['filter_subject']}",
        default => 'no filter',
    };

    return sprintf('%s, num_pending %d', $filters, $info['num_pending']);
}

/**
 * Pulls for $seconds as a worker does, acknowledging what arrives.
 *
 * @return list<string> the text of each message received
 */
function receive(NatsTransport $transport, float $seconds): array
{
    $received = [];
    $until = microtime(true) + $seconds;
    while (microtime(true) < $until) {
        foreach ($transport->get() as $envelope) {
            $transport->ack($envelope);
            $received[] = $envelope->getMessage()->text;
        }
    }

    return $received;
}

/** Prints a line, wrapped under 90 characters, the indent of the next lines included. */
function out(string $line, string $indent = '    '): void
{
    echo wordwrap(rtrim($line), 88 - strlen($indent), "\n" . $indent, true), "\n";
}

Start it:

bash
docker compose up --detach
composer install

What each JetStream returns

compression.php creates a plain stream through each leaf node, as any tool would, and looks for the setting 5.17.0 reads:

php
<?php

// What 5.17.0 reads: the config of the stream a JetStream returns. A plain stream is
// created through each leaf node under the domain (on solo29, without it), and the
// create's reply is checked for a compression setting. Run: php compression.php

declare(strict_types=1);

require __DIR__ . '/lib.php';

foreach (PAIRS as $via => $holder) {
    $prefix = $via === $holder ? '$JS.API.' : '$JS.hub.API.';
    api($holder, 'STREAM.DELETE.probe');
    $reply = api($via, 'STREAM.CREATE.probe',
        ['name' => 'probe', 'subjects' => ['probe.>']], $prefix);
    $config = $reply['config'];
    $seen = array_key_exists('compression', $config)
        ? "compression \"{$config['compression']}\"" : "no 'compression' setting";
    echo server($via), " sends {$prefix}STREAM.CREATE.probe\n";
    out('  the JetStream of ' . server($holder) . " returns a config with $seen");
    api($holder, 'STREAM.DELETE.probe');
}

php compression.php:

text
leaf215 2.15.3 sends $JS.hub.API.STREAM.CREATE.probe
  the JetStream of hub29 2.9.25 returns a config with no 'compression' setting
leaf29x210 2.9.25 sends $JS.hub.API.STREAM.CREATE.probe
  the JetStream of hub210 2.10.29 returns a config with compression "none"
leaf29x215 2.9.25 sends $JS.hub.API.STREAM.CREATE.probe
  the JetStream of hub215 2.15.3 returns a config with compression "none"
solo29 2.9.25 sends $JS.API.STREAM.CREATE.probe
  the JetStream of solo29 2.9.25 returns a config with no 'compression' setting

The request leaves the leaf node under the domain and the hub answers it, so the leaf node's release does not matter. The 2.10.29 hub behind a 2.9.25 leaf node answers with compression "none", which tells it apart from a 2.9 hub where the API level could not. That is the whole rule.

A new stream, 5.16.0 and 5.17.0 side by side

create.php deletes the stream orders on the server that holds it, then runs setup() of a transport with the topic orders.placed, the consumer worker and additional_subjects: [orders.updated.>] through each pair. Then it reads the stream and the consumer from the hub:

php
<?php

// A new stream: setup() of a transport with additional_subjects through each pair, after
// the stream is deleted on the server that holds it. Then what that JetStream holds.
// Run: php create.php

declare(strict_types=1);

require __DIR__ . '/lib.php';

versions();
foreach (PAIRS as $via => $holder) {
    echo 'through ', server($via), ', the stream on ', server($holder), "\n";
    api($holder, 'STREAM.DELETE.orders');
    $worker = transport($via, 'orders.placed', 'worker',
        ['additional_subjects' => ['orders.updated.>']]);
    out('  setup, additional_subjects: [orders.updated.>]: ' . setup($worker));
    $worker->close();
    out("    stream on $holder: " . subjects($holder, 'orders'), '      ');
    out('    worker: ' . consumer($holder, 'orders', 'worker'), '      ');
}

php create.php on 5.16.0:

text
symfony-nats-messenger v5.16.0, php-nats-jetstream-client v2.42.1
through leaf215 2.15.3, the stream on hub29 2.9.25
  setup, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.15.3. Remove additional_subjects or upgrade NATS.
    stream on hub29: [orders.placed, orders.updated.>]
    worker: no filter, num_pending 0
through leaf29x210 2.9.25, the stream on hub210 2.10.29
  setup, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.9.25. Remove additional_subjects or upgrade NATS.
    stream on hub210: no stream orders
    worker: no consumer worker
through leaf29x215 2.9.25, the stream on hub215 2.15.3
  setup, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.9.25. Remove additional_subjects or upgrade NATS.
    stream on hub215: no stream orders
    worker: no consumer worker
through solo29 2.9.25, the stream on solo29 2.9.25
  setup, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.9.25. Remove additional_subjects or upgrade NATS.
    stream on solo29: no stream orders
    worker: no consumer worker

And on 5.17.0:

text
symfony-nats-messenger v5.17.0, php-nats-jetstream-client v2.42.1
through leaf215 2.15.3, the stream on hub29 2.9.25
  setup, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the stream 'orders'
    that the create returned has no 'compression' setting, which every JetStream of 2.10
    or later reports: the JetStream that holds it (on a leaf node under
    jetstream_domain, the hub's) is older than 2.10 and would leave the consumer without
    a filter. The stream was written, its consumer was not. Remove additional_subjects
    or upgrade NATS.
    stream on hub29: [orders.placed, orders.updated.>]
    worker: no consumer worker
through leaf29x210 2.9.25, the stream on hub210 2.10.29
  setup, additional_subjects: [orders.updated.>]: done
    stream on hub210: [orders.placed, orders.updated.>]
    worker: filter_subjects [orders.placed, orders.updated.>], num_pending 0
through leaf29x215 2.9.25, the stream on hub215 2.15.3
  setup, additional_subjects: [orders.updated.>]: done
    stream on hub215: [orders.placed, orders.updated.>]
    worker: filter_subjects [orders.placed, orders.updated.>], num_pending 0
through solo29 2.9.25, the stream on solo29 2.9.25
  setup, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the stream 'orders'
    that the create returned has no 'compression' setting, which every JetStream of 2.10
    or later reports: the JetStream that holds it (on a leaf node under
    jetstream_domain, the hub's) is older than 2.10 and would leave the consumer without
    a filter. The stream was written, its consumer was not. Remove additional_subjects
    or upgrade NATS.
    stream on solo29: [orders.placed, orders.updated.>]
    worker: no consumer worker

What to look at:

  • The 2.15.3 leaf node in front of the 2.9.25 hub. 5.16.0 passed its own check, wrote the stream with the patterns and created worker with no filter, then failed on the client's check. Its message blames the connected server, which reports 2.15.3, a release that has the feature. 5.17.0 sees no compression in the create's reply and stops before the consumer. The stream is there with the patterns, and there is no worker.
  • 2.9.25 leaf nodes in front of the 2.10.29 and 2.15.3 hubs were refused by 5.16.0, and 5.17.0 sets them up with filter_subjects [orders.placed, orders.updated.>] on the hub. This is the case the fix opens up, and the 2.10 hub is the one the API level could not have proven.
  • solo29, a 2.9.25 server with no leaf node, is refused by both. 5.16.0 refused before writing anything. 5.17.0 learns the answer from the create, so the stream now exists, with the patterns and without a consumer, as the release notes say.

A stream two transports share

The second case of issue #212 hurts most when the stream is not yours alone. In existing.php, worker reads orders.placed and billing reads orders.paid on the same stream. Both are set up without the option, then worker is set up again with it. One message goes to each topic, and worker pulls for a second, acknowledging what arrives:

php
<?php

// A stream two transports share: worker reads orders.placed, billing reads orders.paid.
// Both are set up without additional_subjects, then worker again with it. One message
// is sent to each topic, and worker pulls for a second. Run: php existing.php

declare(strict_types=1);

require __DIR__ . '/lib.php';

use Symfony\Component\Messenger\Envelope;

versions();
$updates = ['additional_subjects' => ['orders.updated.>']];
foreach (PAIRS as $via => $holder) {
    echo 'through ', server($via), ', the stream on ', server($holder), "\n";
    api($holder, 'STREAM.DELETE.orders');
    $worker = transport($via, 'orders.placed', 'worker');
    $billing = transport($via, 'orders.paid', 'billing');
    out('  setup worker, then billing: ' . setup($worker) . ', ' . setup($billing));
    $again = transport($via, 'orders.placed', 'worker', $updates);
    out('  setup worker, additional_subjects: [orders.updated.>]: ' . setup($again));
    out('    stream: ' . subjects($holder, 'orders'), '      ');
    $worker->send(new Envelope(new OrderEvent('order 7 placed')));
    $billing->send(new Envelope(new OrderEvent('order 7 paid')));
    out('    worker: ' . consumer($holder, 'orders', 'worker'), '      ');
    out('    worker receives: ' . implode(', ', receive($again, 1.0)), '      ');
    foreach ([$worker, $billing, $again] as $transport) {
        $transport->close();
    }
}

php existing.php on 5.16.0:

text
symfony-nats-messenger v5.16.0, php-nats-jetstream-client v2.42.1
through leaf215 2.15.3, the stream on hub29 2.9.25
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.15.3. Remove additional_subjects or upgrade NATS.
    stream: [orders.placed, orders.paid, orders.updated.>]
    worker: no filter, num_pending 2
    worker receives: order 7 placed, order 7 paid
through leaf29x210 2.9.25, the stream on hub210 2.10.29
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.9.25. Remove additional_subjects or upgrade NATS.
    stream: [orders.placed, orders.paid]
    worker: filter_subject orders.placed, num_pending 1
    worker receives: order 7 placed
through leaf29x215 2.9.25, the stream on hub215 2.15.3
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.9.25. Remove additional_subjects or upgrade NATS.
    stream: [orders.placed, orders.paid]
    worker: filter_subject orders.placed, num_pending 1
    worker receives: order 7 placed
through solo29 2.9.25, the stream on solo29 2.9.25
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.9.25. Remove additional_subjects or upgrade NATS.
    stream: [orders.placed, orders.paid]
    worker: filter_subject orders.placed, num_pending 1
    worker receives: order 7 placed

On 5.17.0:

text
symfony-nats-messenger v5.17.0, php-nats-jetstream-client v2.42.1
through leaf215 2.15.3, the stream on hub29 2.9.25
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the stream 'orders'
    as it exists has no 'compression' setting, which every JetStream of 2.10 or later
    reports: the JetStream that holds it (on a leaf node under jetstream_domain, the
    hub's) is older than 2.10 and would leave the consumer without a filter. Nothing was
    written. Remove additional_subjects or upgrade NATS.
    stream: [orders.placed, orders.paid]
    worker: filter_subject orders.placed, num_pending 1
    worker receives: order 7 placed
through leaf29x210 2.9.25, the stream on hub210 2.10.29
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: done
    stream: [orders.placed, orders.paid, orders.updated.>]
    worker: filter_subjects [orders.placed, orders.updated.>], num_pending 1
    worker receives: order 7 placed
through leaf29x215 2.9.25, the stream on hub215 2.15.3
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: done
    stream: [orders.placed, orders.paid, orders.updated.>]
    worker: filter_subjects [orders.placed, orders.updated.>], num_pending 1
    worker receives: order 7 placed
through solo29 2.9.25, the stream on solo29 2.9.25
  setup worker, then billing: done, done
  setup worker, additional_subjects: [orders.updated.>]: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the stream 'orders'
    as it exists has no 'compression' setting, which every JetStream of 2.10 or later
    reports: the JetStream that holds it (on a leaf node under jetstream_domain, the
    hub's) is older than 2.10 and would leave the consumer without a filter. Nothing was
    written. Remove additional_subjects or upgrade NATS.
    stream: [orders.placed, orders.paid]
    worker: filter_subject orders.placed, num_pending 1
    worker receives: order 7 placed

On 5.16.0, behind the 2.15.3 leaf node, the failed setup rewrote worker with no filter, and worker received order 7 paid, the message meant for billing, and acknowledged it. In a real deploy that is a worker receiving, handling and acknowledging messages meant for another transport, after a setup that reported an error but left the change in place. 5.17.0 judges the stream as it exists, before the update, and writes nothing: the stream keeps its subjects, and worker keeps filter_subject orders.placed and its own message. The 2.9.25 leaf nodes in front of newer hubs, refused by 5.16.0, now set up with both filters, and worker receives only its own message.

A renamed topic, without the option

The same answer now also decides what happens to a transport that does not use additional_subjects at all. Since 5.15.0, setup() keeps the filters of a durable consumer that exists and adds the topic beside them, so a topic renamed in the DSN does not drop the old one while messages may still wait there. A JetStream before 2.10 is the exception: its consumer holds one filter, so it gets the topic alone. renamed.php sets up worker on orders.old, then on orders.placed, with the stream and the consumer name kept:

php
<?php

// The topic renamed in the DSN, the stream and the consumer name kept, and no
// additional_subjects: setup() on orders.old, then on orders.placed.
// Run: php renamed.php

declare(strict_types=1);

require __DIR__ . '/lib.php';

versions();
foreach (PAIRS as $via => $holder) {
    echo 'through ', server($via), ', the stream on ', server($holder), "\n";
    api($holder, 'STREAM.DELETE.orders');
    foreach (['orders.old', 'orders.placed'] as $topic) {
        $worker = transport($via, $topic, 'worker');
        out("  setup on $topic: " . setup($worker));
        $worker->close();
        out('    worker: ' . consumer($holder, 'orders', 'worker'), '      ');
    }
}

php renamed.php on 5.16.0:

text
symfony-nats-messenger v5.16.0, php-nats-jetstream-client v2.42.1
through leaf215 2.15.3, the stream on hub29 2.9.25
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: RuntimeException: NATS Server feature 'filter_subjects'
    requires version >= 2.10, but the connected server reports 2.15.3.
    worker: no filter, num_pending 0
through leaf29x210 2.9.25, the stream on hub210 2.10.29
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subject orders.placed, num_pending 0
through leaf29x215 2.9.25, the stream on hub215 2.15.3
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subject orders.placed, num_pending 0
through solo29 2.9.25, the stream on solo29 2.9.25
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subject orders.placed, num_pending 0

On 5.17.0:

text
symfony-nats-messenger v5.17.0, php-nats-jetstream-client v2.42.1
through leaf215 2.15.3, the stream on hub29 2.9.25
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subject orders.placed, num_pending 0
through leaf29x210 2.9.25, the stream on hub210 2.10.29
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subjects [orders.placed, orders.old], num_pending 0
through leaf29x215 2.9.25, the stream on hub215 2.15.3
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subjects [orders.placed, orders.old], num_pending 0
through solo29 2.9.25, the stream on solo29 2.9.25
  setup on orders.old: done
    worker: filter_subject orders.old, num_pending 0
  setup on orders.placed: done
    worker: filter_subject orders.placed, num_pending 0

Both wrong answers show up again on 5.16.0. Behind the 2.15.3 leaf node, the rename sent two filters to a 2.9.25 hub, failed and left worker with no filter. Behind the 2.9.25 leaf nodes, the newer hubs got the topic alone, so orders.old was dropped from worker, and anything still stored there would no longer be delivered. On 5.17.0 the 2.9.25 hub gets the topic alone and the newer hubs keep [orders.placed, orders.old], as on any server of 2.10 or later. solo29 gets the topic alone on both releases.

After the upgrade: a consumer 5.16.0 left without a filter

5.17.0 stops the damage, but does it undo it? repair.php runs one step per process on the leaf215 and hub29 pair, and repair.sh runs the steps in the install of each release, as a deploy would:

php
<?php

// A consumer that 5.16.0 left without a filter, and what 5.17.0 does with it. worker and
// billing share the stream "orders" on hub29 (2.9.25), through leaf215 (2.15.3).
// Run: php repair.php fresh|option|plain
//   fresh   deletes the stream, sets up worker and billing, sends one message to each
//   option  sets up worker with additional_subjects: [orders.updated.>]
//   plain   sets up worker without it

declare(strict_types=1);

require __DIR__ . '/lib.php';

use Composer\InstalledVersions;
use Symfony\Component\Messenger\Envelope;

$step = $argv[1] ?? 'fresh';
$version = InstalledVersions::getPrettyVersion('idct/symfony-nats-messenger');
if ($step === 'fresh') {
    api('hub29', 'STREAM.DELETE.orders');
    $worker = transport('leaf215', 'orders.placed', 'worker');
    $billing = transport('leaf215', 'orders.paid', 'billing');
    $result = setup($worker) . ', ' . setup($billing);
    $worker->send(new Envelope(new OrderEvent('order 7 placed')));
    $billing->send(new Envelope(new OrderEvent('order 7 paid')));
    $worker->close();
    $billing->close();
    out("$version, worker and billing set up, a message sent to each: $result");
} else {
    $options = $step === 'option' ? ['additional_subjects' => ['orders.updated.>']] : [];
    $worker = transport('leaf215', 'orders.placed', 'worker', $options);
    $result = setup($worker);
    $worker->close();
    $with = $step === 'option' ? 'with additional_subjects' : 'without it';
    out("$version, setup worker $with: $result");
}
out('  worker: ' . consumer('hub29', 'orders', 'worker'));
bash
#!/bin/sh
# 5.16.0 runs setup() with additional_subjects on leaf215 in front of hub29, then the
# transport is upgraded to 5.17.0. One process per line, each in the install of its
# release: v5.16.0 and v5.17.0 side by side.
R=$(cd "$(dirname "$0")/.." && pwd)
(cd $R/v5.16.0 && php repair.php fresh)
(cd $R/v5.16.0 && php repair.php option)  # the option added before the upgrade
(cd $R/v5.17.0 && php repair.php option)  # the upgrade, the option still set
(cd $R/v5.17.0 && php repair.php plain)   # the option removed, as the message says

sh repair.sh:

text
v5.16.0, worker and billing set up, a message sent to each: done, done
  worker: filter_subject orders.placed, num_pending 1
v5.16.0, setup worker with additional_subjects: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the connected server
    reports 2.15.3. Remove additional_subjects or upgrade NATS.
  worker: no filter, num_pending 2
v5.17.0, setup worker with additional_subjects: RuntimeException: The
    'additional_subjects' option requires NATS Server >= 2.10, but the stream 'orders'
    as it exists has no 'compression' setting, which every JetStream of 2.10 or later
    reports: the JetStream that holds it (on a leaf node under jetstream_domain, the
    hub's) is older than 2.10 and would leave the consumer without a filter. Nothing was
    written. Remove additional_subjects or upgrade NATS.
  worker: no filter, num_pending 2
v5.17.0, setup worker without it: done
  worker: filter_subject orders.placed, num_pending 1

Upgrading alone does not repair the consumer. With the option still set, 5.17.0 refuses before writing anything, which also means it leaves the unfiltered worker as it found it, still holding billing's message in its num_pending. Removing the option, as the message says, and running the setup again puts filter_subject orders.placed back: 5.17.0 knows the hub is older than 2.10 and writes the topic alone. 5.16.0's write also left orders.updated.> on the stream, as the existing.php output shows, and setup() removes no subject it did not derive from its own configuration. Take it off with nats stream edit once nothing publishes there.

Also in 5.17.0

  • A test for a fallback message (#211). testSetupDescribesAStreamMetadataRefusalItDidNotMake pins the whole text the transport builds for a metadata refusal it did not make itself, one that no supported client release raises yet. A (string) cast in the stream_metadata check is gone: implode() turns an integer name into the same string, so no test could tell it was there.
  • docs/TESTS.md and STRUCTURE.md after #208 (#209). The list of surviving mutants drops the one #208 removed, leaving 29, 23 of them equivalent, and STRUCTURE.md says that filterSubjectsOf() still reads a consumer's filters through TypeCoercion.

Upgrading

bash
composer update idct/symfony-nats-messenger

The client floor stays at php-nats-jetstream-client 2.39.1, and no request or permission is added. Then, from the README's upgrade section and our runs:

  • No leaf node, or a leaf node and a hub that both run 2.10 or later: nothing changes.
  • A leaf node before 2.10 in front of a hub of 2.10 or later: additional_subjects now sets up. Without the option, an existing consumer is no longer narrowed to the topic: it keeps its filters, as on any server of 2.10 or later.
  • A leaf node of 2.10 or later in front of a hub before 2.10: with the option, setup() is now refused before it touches the consumer, and with auto_setup so is the first send or get of each process. If that pair ran the option on 5.16.0, check the consumer with nats consumer info: a setup without the option on 5.17.0 puts the topic back as its filter, as the run above shows. Without the option, a renamed topic now gets the topic alone instead of failing.
  • A plain server before 2.10: still refused, but a new stream is now created first, with the patterns and without a consumer. A stream that exists is not changed.

How it is tested

The README counts 1986 unit tests at about 99.8% coverage and a mutation score of 98.6%. The fix comes with tests such as testSetupRefusesAdditionalSubjectsWhenTheCreatedStreamShowsAJetStreamBeforeNats210, testSetupRefusesAdditionalSubjectsBeforeItUpdatesTheExistingStreamOfAJetStreamBeforeNats210, testSetupGoesOnWithAdditionalSubjectsWhenTheStreamShowsNats210ThoughTheConnectedServerIsOlder and testSetupSendsTheTopicAloneToAJetStreamBeforeNats210. Pull request #215 also lists ten wrong ways to write the fix, such as checking the update's reply instead of the read, or keeping the old version check beside the new rule, each applied to a scratch copy, and the tests each one turns red.

The README is plain about the gaps: the test stack has no NATS 2.9, so the pairs with a 2.9 hub or leaf node were measured by hand in the triage of #212, and the oldest server in CI is 2.10, so the plain-2.9 branch of the repository's per-message-subjects.php example is not reached there. The Compose project above covers those pairs, in a form anyone can rerun.

Get it