Skip to content

fix: rebuild stream router when services change#13318

Open
shreemaan-abhishek wants to merge 3 commits intoapache:masterfrom
shreemaan-abhishek:fix/stream-router-rebuild-on-service-change
Open

fix: rebuild stream router when services change#13318
shreemaan-abhishek wants to merge 3 commits intoapache:masterfrom
shreemaan-abhishek:fix/stream-router-rebuild-on-service-change

Conversation

@shreemaan-abhishek
Copy link
Copy Markdown
Contributor

Description

The stream router in apisix/stream/router/ip_port.lua only tracked stream_routes.conf_version to decide when to rebuild the internal router. As a result, updates to a service referenced by a stream route did not trigger a rebuild, so changes such as a service arriving after the route, status updates, or deletion were not consistently reflected. Additionally, when a rebuild produced zero TLS routes, the previously built tls_router was left in place and could keep dispatching against stale routes. This change tracks the services conf_version alongside the stream routes version and rebuilds the router when either changes, and resets tls_router to nil when the rebuild yields no TLS routes.

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Apr 29, 2026
Copy link
Copy Markdown
Member

@nic-6443 nic-6443 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add test cases

Cover the four scenarios the fix addresses end-to-end via stream
sockets: service status disable, re-enable, deletion, and recreation.
Each asserts that route matching reflects the new service state once
the rebuild path runs.

Ports the test file from api7-ee apache#1417 (the source PR for this
backport) which was omitted from the initial port.
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Apr 30, 2026
nic-6443
nic-6443 previously approved these changes Apr 30, 2026
membphis
membphis previously approved these changes Apr 30, 2026
AlinsRan
AlinsRan previously approved these changes Apr 30, 2026
The previous commit started rebuilding the stream router whenever
services.conf_version changes, but the rebuild produced the same
routing decisions because create_router() didn't actually consult
service status — service_fetch wasn't even imported in this file.

Add the missing service_id enforcement at the top of the iteration
loop:

  - If a stream_route references a service_id whose service can't be
    fetched (deleted, or not yet synced from etcd), log
    'failed to fetch service configuration by id: <id>' and skip
    the route.
  - If the service status is 0 (explicitly disabled), skip the route.

Combined with the conf_version trigger from the previous commit, a
service status flip / delete / late-sync now propagates into routing
decisions on the next match() call.

This is what the integration tests added in
t/stream-node/service-change-rebuild-router.t exercise; without this
patch, TESTs 2-4 fail because the disabled / deleted service is
ignored at routing time.

Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
@shreemaan-abhishek shreemaan-abhishek dismissed stale reviews from AlinsRan, membphis, and nic-6443 via 504bc1b April 30, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants