You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "nic-6443 (via GitHub)" <gi...@apache.org> on 2025/09/09 11:47:59 UTC

[PR] fix(standalone): support stream_route in admin api mode [apisix]

nic-6443 opened a new pull request, #12604:
URL: https://github.com/apache/apisix/pull/12604

   ### Description
   
   <!-- Please include a summary of the change and which issue is fixed. -->
   <!-- Please also include relevant motivation and context. -->
   
   #### Which issue(s) this PR fixes:
   <!--
   *Automatically closes linked issue when PR is merged.
   Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
   -->
   Fixes https://github.com/apache/apisix/issues/12556
   
   ### 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](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2335290695


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()

Review Comment:
   ref from https://github.com/apache/apisix/issues/12556
   > To address this issue and further ensure the reliability of the event notification mechanism, I believe we should introduce a background timer loop. This would allow the configuration refresh mechanism to passively take effect even if event broadcasting fails.
   
   Since we check `METADATA_LAST_MODIFIED` and `last_sync_time` first, this timer is lightweight. I think we can run it in the HTTP subsystem too, ensuring config sync even if event broadcasting fails (not only for stream subsystem).



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2336287389


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   I thought maybe we could just store `METADATA_LAST_MODIFIED` in this local variable.
   Then use the rule whether the shdict stored config is higher than the local timestamp to check if the config should be sync again.
   We only need to track last modified and no longer need to record a new timestamp additionally or use the difference between last sync and now to check if it should be changed.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2335251386


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()

Review Comment:
   This timer is only applicable in stream mode. Should we check if stream is enabled?



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2336329176


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   Event-triggered updates should also update the last sync time on each worker, so the last sync should be a file-level variable rather than in init_worker.
   Each timer loop only needs to check whether the last modification stored in shdict is consistent with the modification time of the current worker to determine whether the timer needs to update again.
   
   Therefore, we can assume that in most normal cases event broadcasting will take effect, and the last sync time on the worker will always be overwritten by the update triggered by the event and consistent with the shdict, so the timer loop will not actually work.
   This timer only works when event broadcasting is abnormal or when broadcasting to stream from http side. In this case, since the last sync time is not overwritten by the event triggered update, the timer loop will actually update the configuration. In most cases, it is just a fallback.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2343921386


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   > if config[METADATA_LAST_MODIFIED] > last_sync_time then
   
   This introduces two distinct time coordinate systems and compares them. Although both are epoch timestamps, their starting points may be unreliable. For example, NTP might occasionally cause system time to roll back.
   Each instance may have multiple workers, any of which could restart at any time. I believe this creates the potential for time inconsistencies across workers, which we can entirely avoid.
   
   What I mean is that we should ideally use a single time coordinate system and avoid numerical comparisons whenever possible. That is, if we all use LAST_MODIFIED, I can simply perform equality checks. Timestamps should only ever be written by a single worker; other workers should not generate another timestamp for comparison. This is likely a more robust design.
   
   ---
   
   I don't understand how `last_sync_time` affects the timer. Shouldn't we be able to simply register a fixed timer using `ngx.timer.every`? 
   
   Regarding the overhead of `shdict:get`, this is primarily memory-related, with the overhead occurring in the locking portion. If you believe these timer-driven `get` operations might also incur unexpected overhead due to lock waits, I think we could introduce some random delays before `shdict:get`. This would spread out the `get` calls as much as possible, striving to avoid the lock being held at that exact moment.
   
   Regarding the overhead of JSON decode, if this is a concern, we can change the encoding method for shdict to use text encoding like `<LAST_MODIFIED>|<DIGEST>|<JSON>`. This way, data extraction can be done using PCRE, which can be JIT optimized.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2336329176


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   Event-triggered updates should also update the last sync time on each worker, so the last sync should be a file-level variable rather than in init_worker.
   Each timer loop only needs to check whether the last modification stored in shdict is consistent with the modification time of the current worker to determine whether the timer needs to update again.
   Since the worker is single-threaded, we don't need to worry about races. If an update is already triggered by an event, the timer loop will never intervene and repeated updates will not occur. Since UDS communication is almost always faster than our fixed-interval timer, most updates will be triggered and executed by events.
   Only in rare cases, such as when a new update occurs immediately after the UDS event broadcast and the worker has not had time to receive it, will the timer detect the difference between the last sync time and shdict and perform an update. This is expected to be rare, and even if it happens, our design is sufficient to handle it correctly.
   
   Therefore, we can assume that in most normal cases event broadcasting will take effect, and the last sync time on the worker will always be overwritten by the update triggered by the event and consistent with the shdict, so the timer loop will not actually work.
   This timer only works when event broadcasting is abnormal or when broadcasting to stream from http side. In this case, since the last sync time is not overwritten by the event triggered update, the timer loop will actually update the configuration. In most cases, it is just a fallback.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2343921386


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   > if config[METADATA_LAST_MODIFIED] > last_sync_time then
   
   This introduces two distinct time coordinate systems and compares them. Although both are epoch timestamps, their starting points may be unreliable. For instance, NTP might occasionally cause system time to roll back.
   Each instance may have multiple workers, any of which could restart at any time. I believe this creates the potential for time inconsistencies across workers, which we can entirely avoid.
   
   What I mean is that we should ideally use a single time coordinate system and avoid numerical comparisons whenever possible. That is, if we all use LAST_MODIFIED, I can simply perform equality checks. Timestamps should only ever be written by a single worker; other workers should not generate another timestamp for comparison. This is likely a more robust design.
   
   ---
   
   I don't understand how `last_sync_time` affects the timer. Shouldn't we be able to simply register a fixed timer using `ngx.timer.every`? 
   
   Regarding the overhead of `shdict:get`, this is primarily memory-related, with the overhead occurring in the locking portion. If you believe these timer-driven `get` operations might also incur unexpected overhead due to lock waits, I think we could introduce some random delays before `shdict:get`. This would spread out the `get` calls as much as possible, striving to avoid the lock being held at that exact moment.
   
   Regarding the overhead of JSON decode, if this is a concern, we can change the encoding method for shdict to use text encoding like `<LAST_MODIFIED>|<DIGEST>|<JSON>`. This way, data extraction can be done using PCRE, which can be JIT optimized.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2339439485


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   > I thought maybe we could just store METADATA_LAST_MODIFIED in this local variable.
   Then use the rule whether the shdict stored config is higher than the local timestamp to check if the config should be sync again.
   
   However, if we remove `last_sync_time`, we will not be able to control this timer to run once per second. This means that apisix will loop through the execution of the two functions `shareddict:get` and `json.decode`. I think this will consume more computing resources than comparing two local variables.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2333254818


##########
t/admin/standalone.t:
##########
@@ -91,7 +94,7 @@ X-Digest: t2
 
 === TEST 3: get config
 --- config
-    location /t {
+    location /config {

Review Comment:
   I modified the URL of this location because when `stream_enable` is used, test-nginx will occupy the `/t` URL to initiate stream requests.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2347787968


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()
+    local function sync_config()
+        local now = ngx_time()
+        if now - last_sync_time < 1 then
+            return
+        end
+
+        local config, err = get_config()
+        if not config then
+            if err ~= NOT_FOUND_ERR then
+                core.log.error("failed to get config: ", err)
+            end
+        else
+            if config[METADATA_LAST_MODIFIED] > last_sync_time then
+                update_config(config)
+            end
+        end
+        last_sync_time = now

Review Comment:
   got it, `ngx.timer.every` is a good idea, applied your suggestion, please review again



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(standalone): support stream route in admin api mode [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #12604:
URL: https://github.com/apache/apisix/pull/12604#discussion_r2335290695


##########
apisix/admin/standalone.lua:
##########
@@ -420,6 +430,29 @@ function _M.init_worker()
     end
     events:register(update_config, EVENT_UPDATE, EVENT_UPDATE)
 
+    -- due to the event module can not broadcast events between http and stream subsystems,
+    -- we need to poll the shared dict to keep the config in sync
+    local last_sync_time = ngx_time()

Review Comment:
   ref from https://github.com/apache/apisix/issues/12556
   > To address this issue and further ensure the reliability of the event notification mechanism, I believe we should introduce a background timer loop. This would allow the configuration refresh mechanism to passively take effect even if event broadcasting fails.
   
   Since we check `METADATA_LAST_MODIFIED` and `last_sync_time` first, this timer is lightweight. I think we can run it in the HTTP subsystem too, ensuring config sync even if event broadcasting fails.



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org