Hello!
On Sat, Nov 3, 2012 at 6:15 AM, Brian Akins wrote:
>
>
> I don't know how the internal nginx event loop works, so this may not work. If the lock is just an atomic integer in shared memory, could the event loop in the locking process just do a compare and swap on the lock and run the callback when it succeeds? In a different project, we did this using an event loops "idle" handler (it was libev) and/or a timer.
>
> So the locking process on a lock call would:
> - do a compare_and_swap on the integer in shared memory
> - if this succeeds, just run the callback (or schedule it to run immediately)
> - if not, schedule a "lock checker" to be ran either during the loops "idle" handler or on a timer, or maybe even every loop iteration.
> - the lock checker simply does a compare_and_swap and calls the callback if it succeeds .
>
Thank you for sharing this method but I'm afraid the main problem with
this approach is that for all the requests accessing the shm
dictionary the latency can be unnecessarily long (and mostly
non-predictable), the upper bound of which is depending on the timer
interval or even the server load if the "idle" handler is used.
I think this is essentially time-slicing the spinlock cycles to serve
other requests that are not accessing the shm store. And that's also
why we decide not to implement automatic time-slicing for the "light
threads" (i.e., ngx.thread) in ngx_lua because the latency over event
cycles or timers are unmanageable. The drop in latency and throughput
may be okay for certain apps but not for others.
>
> This approach uses a little more cpu depending on lock contention, but is better than the current implementation, IMO.
I won't say this is generally *better*. I would say this is a
workaround for long-time lock holders which should not have existed in
the first place ;) The standard Memcached wire protocol, for example,
does not bother exposing a key/value iteration API that could possible
hold the lock for long. These cache store was not specifically
designed for such scenarios.
Still I suggest creating a new shm-based store that uses a different
model for this (possibly completely nonblocking) instead of going on
the wrong track for too long :)
> Like I said, I don't know the internals of the nginx event loop, so this may not be feasible in nginx.
>
The event model of Nginx does not look very different than other
popular event-related C libraries, though maybe not decently
encapsulated for the sake of performance.
Best regards,
-agentzh