moonming commented on code in PR #14208:
URL: https://github.com/apache/apisix/pull/14208#discussion_r4613693963


##########
apisix/utils/batch-processor.lua:
##########
@@ -59,11 +87,15 @@ local function schedule_func_exec(self, delay, batch)
                                "while exiting: ", err2)
                 return
             end
+            delay = 0
         else
             core.log.error("failed to create process timer: ", err)
             return
         end
     end
+
+    batch.timer_deadline = now() + delay

Review Comment:
   **MEDIUM: use the same clock basis as the timers being tracked.**
   
   `now` is `ngx.now`, which reads wall time. NGINX uses a monotonic clock for 
timer deadlines on supported platforms. A forward wall-clock adjustment can 
therefore make a retry that is not yet due appear overdue here; a backward 
adjustment can postpone recovery. This also affects the buffer token deadlines 
and the sweep cutoff.
   
   Please use a compatible monotonic source for all four new deadline/cutoff 
assignments, for example:
   
   ```lua
   batch.timer_deadline = timer_now() + delay
   local timer = {deadline = timer_now() + self.inactive_timeout}
   timer.deadline = timer_now() -- zero-delay exit fallback
   local cutoff = timer_now() - TIMER_GRACE
   ```
   
   There is a compatibility detail to resolve: 
`require("resty.core.time").monotonic_time` exists in OpenResty 1.21.4.2, but 
not 1.21.4.1, while APISIX currently accepts >= 1.21.4. Please preserve the 
supported-runtime contract when choosing `timer_now`; an unconditional call to 
the newer helper, or a silent fallback to wall time, would leave a problem.
   
   Sources: [OpenResty clock 
implementations](https://github.com/openresty/lua-nginx-module/blob/v0.10.28/src/ngx_http_lua_time.c#L14-L41),
 [NGINX timer 
clock](https://github.com/nginx/nginx/blob/release-1.27.1/src/core/ngx_times.c#L83-L106),
 [resty-core bundled in 
1.21.4.1](https://github.com/openresty/lua-resty-core/blob/cf1aa0cba143ff3163f9d2f8d7c590e96a5432f0/lib/resty/core/time.lua#L157-L159).
 This finding is based on those implementations; I did not change the system 
clock to reproduce it.



##########
apisix/utils/batch-processor.lua:
##########
@@ -141,19 +187,61 @@ end
 
 
 function create_buffer_timer(self)
-    local hdl, err = timer_at(self.inactive_timeout, flush_buffer, self)
+    ensure_recovery_timer()
+    local timer = {deadline = now() + self.inactive_timeout}
+    local hdl, err = timer_at(self.inactive_timeout, flush_buffer, self, timer)
     if not hdl then
         if err == "process exiting" then
-            timer_at(0, flush_buffer, self)
-        else
+            hdl, err = timer_at(0, flush_buffer, self, timer)
+            timer.deadline = now()
+        end
+        if not hdl then
+            self.is_timer_running = false
             core.log.error("failed to create buffer timer: ", err)
             return
         end
     end
+    pending_buffers[self] = timer
     self.is_timer_running = true
 end
 
 
+function recover_timers(premature)
+    if premature then
+        return
+    end
+
+    local cutoff = now() - TIMER_GRACE
+    local dropped = {}
+    for batch, self in pairs(pending_batches) do
+        if batch.timer_deadline <= cutoff then
+            pending_batches[batch] = nil
+            batch.timer_deadline = nil
+            local count = #batch.entries
+            self.processed_entries = self.processed_entries + count

Review Comment:
   **MEDIUM: let already-due callbacks run before deciding they were dropped.**
   
   A deadline in the past does not establish that OpenResty discarded the 
callback. After a worker pause, the older recurring recovery tick can run 
before a later-due retry that is still queued. This block removes the 
registration and increments `processed_entries`; the real retry then returns at 
the ownership check without sending.
   
   I reproduced this with real timers and the default `retry_delay = 1`: the 
first send sleeps 0.2 s and returns `false`; after 0.3 s, the request blocks 
the worker in an `os.clock()` loop for 3 s, then calls `ngx.update_time()` and 
yields with `ngx.sleep(0.2)`. Current master reports `calls=2, sent=1, 
processed=1`; this head reports `calls=1, sent=0, processed=1` and logs a 
dropped entry. There is no `lua_max_running_timers` alert.
   
   One concrete repair direction is a recurring heartbeat that captures the 
cutoff, then schedules a non-yielding zero-delay sweep:
   
   ```lua
   local cutoff = timer_now() - TIMER_GRACE
   local ok, err = timer_at(0, recover_timers, cutoff)
   if not ok then
       core.log.error("failed to schedule batch recovery sweep: ", err)
   end
   ```
   
   Make the sweep accept `(premature, cutoff)` and do not recompute its cutoff 
when it finally runs. Due callbacks then get their dispatch opportunity and 
remove their registrations before reclamation; a rejected sweep can be retried 
by the next heartbeat. `timer_now` needs the compatible monotonic source 
discussed in the other comment. This direction passed the pause regression in a 
temporary combined tree.
   
   Please add the worker-pause regression, asserting one successful send and 
one accounting increment. If adopting a deferred sweep, narrow TEST 5's 
`ngx.timer.at` interception to the intended send callback and forward recovery 
scheduling to the real timer; its current catch-all would intercept the new 
sweep too.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to