proxy icon indicating copy to clipboard operation
proxy copied to clipboard

Re-implement RotatingScope with thread local storage

Open wangjian-pg opened this issue 1 month ago • 9 comments

What this PR does / why we need it:

The current implementation of metric rotation relies on timing to ensure that the scope pointer used by the worker thread is valid, which is probably not a good practice or design as far as I know.

After digging into the envoy's implementation of ThreadLocalStoreImpl(which faces similar issues of how to propagate scoped metrics to worker threads and expire them when the scope is released), I found that storing the active scope reference in thread local storage and replacing it with the new one when rotation occurs might be a better design. With this approach, we eliminate the use of raw pointers, making rotation behavior more predictable and still lock-free.

I re-implemented the rotation as described and tested the code in my environment, it works as expected. Please take a look. Looking forward to any feedback.

wangjian-pg avatar May 23 '24 13:05 wangjian-pg