Skip to content

A standard for continuation-local variables? #2885

@eirslett

Description

@eirslett

https://groups.google.com/forum/#!topic/rxjava/3iW-U6VZ_8c

I'm cross-posting this here, since I'm not quite sure where it belongs. One could think of it as a feature request. (Just close the issue if it shouldn't be on GitHub) The topic didn't get that much discussion on the mailing list...

Hey!

I've been working a lot with Twitter's Zipkin project, which makes heavy use of the "Local" construct: https://github.com/twitter/util/blob/master/util-core/src/main/scala/com/twitter/util/Local.scala
It's basically an improved ThreadLocal, but a key feature of a Local variable is that its state is passed through to other threads whenever you make Futures, and compose these Futures. That makes it easy to do debug tracing in reactive systems. (Especially Zipkin tracing!)

Scala has DynamicVariable, and I think it works in a similar way with Scala Futures, but I'm not 100 % sure.

On of the problems with RxJava, is that ThreadLocal state is not passed on between Observables, at least not intentionally. So you have to add a custom execution hook that is Zipkin-specific, and transfer the state between Observables.
Would it be a good idea to factor out a common construct for this type of situation, a ContinuationLocal maybe (or just modify Twitter's "Local", as it's already implemented, but without the Scala dependency and dependency on Twitter's libraries) - so that one could reliably expect its state to be passed on across threads, as long as it's within the same context?

Even better, it would be great to have a construct in the core Java library that is expected to be passed between ExecutorServices - it's a real pain when working with legacy code, and Trace debugging doesn't work because we lose the state inside e.g. a ForkJoinPool. One has to decorate every ExecutorService in the application with a custom wrapper, but that's very intrusive! (and requires a lot of rewriting, also it's not always possible.)

I really think the concept should be shared across different implementations of reactive patterns, so you get interop between libraries whenever they're running within the same JVM.
(E.g. If I use a bridge from Finagle's thrift to Hystrix/RxJava, I would love to have the context transferred, without having to write custom code.)
Should this even be defined as a "mini standard" a.la. Reactive Streams? What are your thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions