-
Notifications
You must be signed in to change notification settings - Fork 345
ThreadLocalScopManager and parallelStream #353
Comments
You can use custom thread pool (https://www.baeldung.com/java-8-parallel-streams-custom-threadpool) that will transfer context to worker threads. |
Hi, I agree that I can use a custom thread pool, but the code can become much more verbose and relying on that can be error-prone, since one can simply forget to change the thread pool.
|
I don't see how inheritable TL would help when using a thread pool, since the worker threads creation is not correlated with the caller thread. I agree custom executor leads to uglier code, but that's a defect in ForkJoinPool design, since it does not allow reconfiguring its default "common" pool. To my knowledge, overriding it via byte code manipulation is the only way. |
It actually didn't work.
Do you have any examples of doing it through byte code manipulation? Not gonna lie that I was reading about ByteBuddy to create an agent and do some "magic stuff", but running the jar without the javaagent com lead to bizarre errors, so I stepped back a little bit. |
I haven't seen an example anywhere, opened a ticket opentracing-contrib/java-specialagent#102 |
Nice, thanks. Gonna take a look at this specialagent, I didn't know about him. Thanks a lot.
To test I used just a POC (not with opentracing actually). I set a header on an
@ edit: you're right, executor service under stress don't work as I told above. |
Update: you're correct @yurishkuro , without stress to the VM the behavior indeed work, but when under load it behaves as it should be, it loses the reference to the point that the behavior is non-deterministic, leading to undesired/undefined behaviors. |
Hey guys, I was just testing some ways to implement tracing using a POC application I created.
One of the services (product service) is called by the order service, but the products services don't expose an endpoint that return all the products at once, so I have to call it several times.
I know it's not good, not the best approach, but it's just a simulation that can occur in the real world.
The API is not well formed, so the items is represented by a Map<String, Long>, which is the item UUID and the item quantity.
My code run something like this (Kotlin):
My problem: as the parallelStream runs on another Thread(Pool), and the default implementation of the ScopeManager is ThreadLocalScopeManager, this doesn't work, as it loses the reference to the original span.
Question is, why it's not implemented using InheritableThreadLocal?
Second question is: there's some easy approach to solve this problem?
Thanks in advance and sorry about the bad english.
The text was updated successfully, but these errors were encountered: