-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rx Schedulers #19
Comments
Interesting quote from that page |
How would an operator know how many messages it is going to have? Only the origin Observable could know that - a given operator along the chain won't know so how does this get accomplished? |
I guess this would mean that things like "toObservable(1, 2, 3, 4)" are a known thing and can be done immediately without a thread, but merging 4 unknown sequences can't be known. Obviously when Timers are used a Thread of some kind is needed (java.util.Timer or another implementation like this: https://github.com/Netflix/Hystrix/blob/master/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java). For the majority of cases though where the "cost" of the observable sequence is unknown I don't know that I like automatically spawning them off on threads. It has worked well for the Netflix API to leave that choice to the origin of the observable (to be synchronous or asynchronous, one thread or many, etc). The introduction of Schedulers makes perfect sense when an app is dealing with data structures, their own synchronous IO operations or CPU bound computations, but it becomes awkward when consuming from a 3rd party API exposing Observables who can and will make their own choice about being synchronous or asynchronous. In fact, that's been a huge part of the draw to Rx is that the API doesn't need to change when the backend implementations moves between synchronous and asynchronous for whatever reason it may choose to do so. If the Observable is already asynchronous it would be inefficient to spin up another thread that in turn blocks on an async call. Other than documentation on the API calls that return Observables is there a better way to handle that scenario? I can imagine a scenario where some apps (such as the Netflix API) may want to disable any use of Schedulers so the origin retains control since Rx has allowed us to decouple the writing of business logic from the decisions of concurrency. Before flaming me ... I DO like schedulers, it's very powerful and we will definitely get them added, I just have some questions about balancing that power (and inevitable inefficiencies of making poor scheduler choices) with the elegant simplicity of Rx Observables without them where concurrency is not a thought - everything is just asynchronous. I'm interested in all of your perspectives so please chime in. |
The docs say "least amount of concurrency" which I interpreted to mean that if the amount of work is unknown that it would it default to immediate. We could still manipulate the defaults and probably ignore Schedulers passed in through a strategy. |
Yes, I think a strategy pattern will be needed to accomplish the Netflix API use case. |
Some thoughts while working on the design of this:
|
Sections 6.9 through 6.12 of the Rx Design Guidelines PDF (http://go.microsoft.com/fwlink/?LinkID=205219) should be read by anyone involved in Schedulers design and implementation. |
There's also this video explaining the motivation behind introducing schedulers in Rx. |
Good video ... thanks for the link. |
Open questions: 1) Scheduler TimeWe're not using the Scheduler.now value anywhere, should we be? or is that only for the Virtual scheduler used for testing? 2) Use of SubscribeOn vs Scheduler.schedulerI'm trying to understand how operator overloads should use Scheduler. Here is a potential implementation: #227 and another #226 I have not yet found C# source code or documentation that clarifies this. I have also had feedback (that I agree with) that it this is clearer: merge(o1, o2).subscribeOn(scheduler) than this merge(o1, o2, scheduler) So is there anything different between this? 3) Multiple Schedulers in SequenceI'm trying to understand how a sequence should work when multiple Can someone with an Rx.Net environment setup implement a test similar to this from Java and tell me the output? @Test
public void testMixedSchedulers() throws InterruptedException {
final String mainThreadName = Thread.currentThread().getName();
Observable<String> o = Observable.<String> create(new Func1<Observer<String>, Subscription>() {
@Override
public Subscription call(Observer<String> observer) {
System.out.println("Origin observable is running on: " + Thread.currentThread().getName());
assertFalse(Thread.currentThread().getName().equals(mainThreadName));
assertTrue("Actually: " + Thread.currentThread().getName(), Thread.currentThread().getName().startsWith("RxIOThreadPool"));
observer.onNext("one");
observer.onNext("two");
observer.onNext("three");
observer.onCompleted();
return Subscriptions.empty();
}
}).subscribeOn(Schedulers.threadPoolForIO()); // subscribe to the source on the IO thread pool
// now merge on the CPU threadpool
o = Observable.<String> merge(o, Observable.<String> from("four", "five"))
.subscribeOn(Schedulers.threadPoolForComputation())
.map(new Func1<String, String>() {
@Override
public String call(String v) {
// opportunity to see what thread the merge is running on
System.out.println("Merge is running on: " + Thread.currentThread().getName());
return v;
}
});
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<RuntimeException> onError = new AtomicReference<RuntimeException>();
// subscribe on a new thread
o.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
System.out.println("==> received onCompleted");
latch.countDown();
}
@Override
public void onError(Exception e) {
System.out.println("==> received onError: " + e.getMessage());
onError.set((RuntimeException) e);
latch.countDown();
}
@Override
public void onNext(String v) {
System.out.println("==> Final subscribe is running on: " + Thread.currentThread().getName());
System.out.println("==> onNext: " + v);
}
}, Schedulers.newThread());
// wait for the above to finish or blow up if it's blocked
latch.await(5, TimeUnit.SECONDS);
} Of course Rx.Net doesn't have the IO and CPU thread pools ... those are just helper methods to Executors which would be 2 separate threadpools for different work types so you'll need to adjust that. |
Concerning 1), I guess it will come in handy when implementing #90 (or clock-like observables in general), at least if I understand this correctly. I'm currently figuring out how working with the schedulers feels by playing around with an implementation for #74, which also requires a "clock", though it doesn't seem to require the current time). |
I received the following feedback that will require a breaking change to the Scheduler interface:
Interface IScheduler
{
Schedule<TState>(TState s, Func<IScheduler, TState, IDisposable> a)
Schedule<TState>(TState s, DateTimeOffset d, Func<IScheduler, TState, IDisposable> a).
Schedule<TState>(TState s, TimeSpan t, Func<IScheduler, TState, IDisposable> a)
}
void Main()
{
var repeat = Observable.Create<int>(observer =>
{
while(true) observer.OnNext(42);
return () => {};
});
//var dispose = repeat.Subscribe(Console.WriteLine);
var dispose = ObservableEx.ToObservable(NewThreadScheduler.Default)
.Select(_ => 42)
.Subscribe(x => Console.WriteLine(x));
Console.ReadLine();
dispose.Dispose();
Console.WriteLine("Bye");
}
static class ObservableEx
{
public static IObservable<Unit> ToObservable(this IScheduler scheduler)
{
return Observable.Create<Unit>(observer =>
{
return scheduler.ScheduleAsync(async (_scheduler, token) =>
{
while(!token.IsCancellationRequested)
{
observer.OnNext(Unit.Default);
await _scheduler.Sleep(TimeSpan.FromSeconds(2));
}
});
});
}
} |
Here is another use case: var scheduler = TaskPoolScheduler.Default;
var xs = Observable.Generate
( 0
, i=>true
, i=>i+1
, i=>i
, i=>TimeSpan.FromSeconds(1)
, scheduler
);
var ys = Observable.Create<int>(observer =>
{
return scheduler.ScheduleAsync(async (_scheduler, cancel) =>
{
await _scheduler.Yield();
for(var i = 0; !cancel.IsCancellationRequested; i++)
{
observer.OnNext(i);
await _scheduler.Sleep(TimeSpan.FromSeconds(1));
}
});
});
//var dispose = ys.Timestamp().Subscribe(x => Console.WriteLine(x.ToString()));
var dispose = ys.Timestamp().DumpLive().Subscribe();
Console.ReadLine();
dispose.Dispose();
Console.WriteLine("disposed");
Console.ReadLine(); |
Note that I'm unavailable to work on this until the 15th. Anyone else who wants to jump in and determine the changes needed based on this feedback please do. |
Here is some simple code I was playing with to prove out the use of Anyone find faults in this? import rx.*
import rx.concurrency.Schedulers
/*
* ******** PRODUCER CODE ********
* This is the "Observable API"
*/
Observable<Video> getVideos() {
return Observable.create({
observer ->
Thread.sleep(200); // simulate network traffic
// 10 videos are fetched in a batch and emitted
observer.onNext(new Video(1));
observer.onNext(new Video(2));
observer.onNext(new Video(3));
observer.onNext(new Video(4));
observer.onNext(new Video(5));
observer.onNext(new Video(6));
observer.onNext(new Video(7));
observer.onNext(new Video(8));
observer.onNext(new Video(9));
observer.onNext(new Video(10));
observer.onCompleted();
})
}
class Video {
final int id;
public Video(int id) {
this.id = id;
}
Observable<Rating> getRating() {
return Observable.create({
observer ->
Thread.sleep(200); // simulate network traffic
observer.onNext(new Rating(id));
observer.onCompleted();
}).subscribeOn(Schedulers.threadPoolForIO())
}
Observable<Bookmark> getBookmark() {
return Observable.create({
observer ->
Thread.sleep(200); // simulate network traffic
observer.onNext(new Bookmark(id));
observer.onCompleted();
}).subscribeOn(Schedulers.newThread())
}
}
class Rating {
final String value;
public Rating(int id) {
this.value = "ratingFor_" + id;
}
}
class Bookmark {
final String value;
public Bookmark(int id) {
this.value = "bookmarkFor_" + id;
}
}
/*
* ******** CONSUMER CODE ********
* This is a client consuming the "Observable API"
*/
long start = System.currentTimeMillis();
getVideos().mapMany({
Video video ->
// fetch and transform bookmark
Observable ob = video.getBookmark().map({b ->
return "transformed-" + b.value;
})
// fetch ratings and zip together with bookmark
return Observable.zip(ob, video.getRating(), {b, r -> return [b.value, r.value]})
.map({ tuple ->
// combine all metadata for a single Video
return ["id" : video.id, "bookmark" : tuple[0], "rating": tuple[1]]
})
}).forEach({
videoMap ->
System.out.println("Video: " + videoMap["id"] + " bookmark: " + videoMap["bookmark"] + " rating: " + videoMap["rating"] + " Thread: " + Thread.currentThread());
})
long end = System.currentTimeMillis();
System.out.println("time: " + (end-start)) Output is:
|
@benjchristensen I'm left wondering if or how question 3) from your comment (#19 (comment)) was addressed or whether this is still an open question? In our app we haven't quite figured out yet which layer should be responsible for scheduling an observable. If we schedule on the service layer--which would make sense when trying to make client code oblivious as to whether code runs concurrently or not--then what does that mean for reusability of observables? Would, say, service A be able to take an observable from service B which has already been scheduled by B, transform and re-schedule it? With the pre-0.8 Schedulers, this is not possible, since subscribeOn/observeOn will wrap as many times as you call these methods. |
I think the JavaDocs haven't been updated yet: http://netflix.github.io/RxJava/javadoc/rx/Scheduler.html Is there any documentation / examples around what the state parameter is used for? Looking at the existing schedulers, I only ever see it being passed through, so I wonder what this accomplishes? |
Here's an example (by @mairbek) using state: #229 (comment) |
I forgot to upload the new Javadocs ... will do so once I'm at my laptop. Sorry about that. |
@mttkay I found wifi ... uploaded javadocs for 0.8.0. Also, the example from @mairbek was incorporated into unit tests here: https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/test/java/rx/concurrency/TestSchedulers.java#L255 |
I believe we're pretty comfortable with the |
Implementation of Rx Schedulers (http://msdn.microsoft.com/en-us/library/hh242963(v=vs.103).aspx) ... probably to go into the rx.concurrency package (https://github.com/Netflix/RxJava/tree/master/rxjava-core/src/main/java/rx/concurrency).
The text was updated successfully, but these errors were encountered: