This is an automated email from the ASF dual-hosted git repository.

shreemaan-abhishek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 1dbf696ce fix: rebuild stream router when services change (#13318)
1dbf696ce is described below

commit 1dbf696ce9a0d06c9a67468daa02f698c3a28493
Author: Shreemaan Abhishek <[email protected]>
AuthorDate: Thu May 7 10:49:49 2026 +0800

    fix: rebuild stream router when services change (#13318)
    
    * fix: rebuild stream router when services change
    
    * test: add integration tests for stream router rebuild on service change
    
    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 #1417 (the source PR for this
    backport) which was omitted from the initial port.
    
    * fix: skip stream routes whose service is missing or disabled
    
    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 <[email protected]>
    
    * refactor(stream-router): drop unreachable service lookup in rebuild
    
    Apisix admin guarantees stream_routes' referenced services exist
    (referenced services cannot be deleted), and the services schema does
    not expose a status field, so there is no actionable outcome from a
    per-route service fetch. Rebuilding on services.conf_version is enough.
    
    Refocus the integration test on what apisix exposes: update the bound
    service and verify routing continues to match through the
    conf_version-driven rebuild.
    
    ---------
    
    Signed-off-by: Abhishek Choudhary <[email protected]>
---
 apisix/stream/router/ip_port.lua              |  12 ++-
 t/stream-node/service-change-rebuild-router.t | 142 ++++++++++++++++++++++++++
 2 files changed, 153 insertions(+), 1 deletion(-)

diff --git a/apisix/stream/router/ip_port.lua b/apisix/stream/router/ip_port.lua
index 4d502cab0..3b89b59a2 100644
--- a/apisix/stream/router/ip_port.lua
+++ b/apisix/stream/router/ip_port.lua
@@ -19,6 +19,7 @@ local core_ip  = require("apisix.core.ip")
 local config_util = require("apisix.core.config_util")
 local stream_plugin_checker = require("apisix.plugin").stream_plugin_checker
 local router_new = require("apisix.utils.router").new
+local service_mod = require("apisix.http.service")
 local apisix_ssl = require("apisix.ssl")
 local xrpc = require("apisix.stream.xrpc")
 local error     = error
@@ -27,6 +28,7 @@ local ipairs = ipairs
 
 local user_routes
 local router_ver
+local service_ver
 local tls_router
 local other_routes = {}
 local _M = {version = 0.1}
@@ -132,6 +134,8 @@ do
             end
 
             tls_router = router
+        else
+            tls_router = nil
         end
 
         return nil
@@ -143,13 +147,19 @@ do
     local match_opts = {}
 
     function _M.match(api_ctx)
-        if router_ver ~= user_routes.conf_version then
+        -- Rebuild the router when stream_routes change OR when services 
change,
+        -- so updates to a referenced service (status, deletion, late sync from
+        -- etcd) are reflected in routing decisions for stream routes.
+        local _, cur_svc_ver = service_mod.services()
+        if router_ver ~= user_routes.conf_version
+           or service_ver ~= cur_svc_ver then
             local err = create_router(user_routes.values)
             if err then
                 return false, "failed to create router: " .. err
             end
 
             router_ver = user_routes.conf_version
+            service_ver = cur_svc_ver
         end
 
         local sni = apisix_ssl.server_name()
diff --git a/t/stream-node/service-change-rebuild-router.t 
b/t/stream-node/service-change-rebuild-router.t
new file mode 100644
index 000000000..90ccbccc5
--- /dev/null
+++ b/t/stream-node/service-change-rebuild-router.t
@@ -0,0 +1,142 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+log_level('info');
+no_root_location();
+no_shuffle();
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: setup a service and a stream_route bound to it
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/99',
+                ngx.HTTP_PUT,
+                [[{
+                    "upstream": {
+                        "nodes": {"127.0.0.1:1995": 1},
+                        "type": "roundrobin"
+                    }
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+
+            code, body = t('/apisix/admin/stream_routes/99',
+                ngx.HTTP_PUT,
+                [[{
+                    "remote_addr": "127.0.0.1",
+                    "service_id": 99
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 2: stream connection routes through the service upstream
+--- stream_request
+mmm
+--- stream_response
+hello world
+
+
+
+=== TEST 3: update the service (no change to the stream_route itself)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/99',
+                ngx.HTTP_PUT,
+                [[{
+                    "name": "svc99",
+                    "desc": "updated",
+                    "upstream": {
+                        "nodes": {"127.0.0.1:1995": 1},
+                        "type": "roundrobin"
+                    }
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 4: stream routing still works after the service version changes
+--- stream_request
+mmm
+--- stream_response
+hello world
+
+
+
+=== TEST 5: delete the stream_route, then the service
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code = t('/apisix/admin/stream_routes/99', ngx.HTTP_DELETE)
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            code = t('/apisix/admin/services/99', ngx.HTTP_DELETE)
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.say("ok")
+        }
+    }
+--- request
+GET /t
+--- response_body
+ok
+
+
+
+=== TEST 6: stream connection no longer matches any route
+--- stream_enable
+--- stream_response
+--- error_log
+not hit any route

Reply via email to