-
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
1.x: compensation for significant clock drifts in schedulePeriodically #3467
Merged
abersnaze
merged 1 commit into
ReactiveX:1.x
from
akarnokd:SchedulePeriodicallyClockShiftFix
Dec 15, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.functions.Action0; | ||
import rx.schedulers.Schedulers; | ||
|
||
public class SchedulerWorkerTest { | ||
|
||
static final class CustomDriftScheduler extends Scheduler { | ||
public volatile long drift; | ||
@Override | ||
public Worker createWorker() { | ||
final Worker w = Schedulers.computation().createWorker(); | ||
return new Worker() { | ||
|
||
@Override | ||
public void unsubscribe() { | ||
w.unsubscribe(); | ||
} | ||
|
||
@Override | ||
public boolean isUnsubscribed() { | ||
return w.isUnsubscribed(); | ||
} | ||
|
||
@Override | ||
public Subscription schedule(Action0 action) { | ||
return w.schedule(action); | ||
} | ||
|
||
@Override | ||
public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) { | ||
return w.schedule(action, delayTime, unit); | ||
} | ||
|
||
@Override | ||
public long now() { | ||
return super.now() + drift; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public long now() { | ||
return super.now() + drift; | ||
} | ||
} | ||
|
||
@Test | ||
public void testCurrentTimeDriftBackwards() throws Exception { | ||
CustomDriftScheduler s = new CustomDriftScheduler(); | ||
|
||
Scheduler.Worker w = s.createWorker(); | ||
|
||
try { | ||
final List<Long> times = new ArrayList<Long>(); | ||
|
||
Subscription d = w.schedulePeriodically(new Action0() { | ||
@Override | ||
public void call() { | ||
times.add(System.currentTimeMillis()); | ||
} | ||
}, 100, 100, TimeUnit.MILLISECONDS); | ||
|
||
Thread.sleep(150); | ||
|
||
s.drift = -1000 - TimeUnit.NANOSECONDS.toMillis(Scheduler.CLOCK_DRIFT_TOLERANCE_NANOS); | ||
|
||
Thread.sleep(400); | ||
|
||
d.unsubscribe(); | ||
|
||
Thread.sleep(150); | ||
|
||
System.out.println("Runs: " + times.size()); | ||
|
||
for (int i = 0; i < times.size() - 1 ; i++) { | ||
long diff = times.get(i + 1) - times.get(i); | ||
System.out.println("Diff #" + i + ": " + diff); | ||
assertTrue("" + i + ":" + diff, diff < 150 && diff > 50); | ||
} | ||
|
||
assertTrue("Too few invocations: " + times.size(), times.size() > 2); | ||
|
||
} finally { | ||
w.unsubscribe(); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testCurrentTimeDriftForwards() throws Exception { | ||
CustomDriftScheduler s = new CustomDriftScheduler(); | ||
|
||
Scheduler.Worker w = s.createWorker(); | ||
|
||
try { | ||
final List<Long> times = new ArrayList<Long>(); | ||
|
||
Subscription d = w.schedulePeriodically(new Action0() { | ||
@Override | ||
public void call() { | ||
times.add(System.currentTimeMillis()); | ||
} | ||
}, 100, 100, TimeUnit.MILLISECONDS); | ||
|
||
Thread.sleep(150); | ||
|
||
s.drift = 1000 + TimeUnit.NANOSECONDS.toMillis(Scheduler.CLOCK_DRIFT_TOLERANCE_NANOS); | ||
|
||
Thread.sleep(400); | ||
|
||
d.unsubscribe(); | ||
|
||
Thread.sleep(150); | ||
|
||
System.out.println("Runs: " + times.size()); | ||
|
||
assertTrue(times.size() > 2); | ||
|
||
for (int i = 0; i < times.size() - 1 ; i++) { | ||
long diff = times.get(i + 1) - times.get(i); | ||
System.out.println("Diff #" + i + ": " + diff); | ||
assertTrue("Diff out of range: " + diff, diff < 250 && diff > 50); | ||
} | ||
|
||
} finally { | ||
w.unsubscribe(); | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to put this into separate
static
block?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why, where is the other one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implicitly using an initializer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, ^.
BTW, @akarnokd don't you think that minutes is not the best unit of time measurement in programming? I'd prefer millis,
long
seems enough forCLOCK_DRIFT_TOLERANCE
, and anyway, you're converting it to nanos then solong
is 100% enough for millis.Also,
15
minutes seems too much for a default clock drift tolerance…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't inline it in case more parameters are asked for within this PR.
It is a question what is a reasonable tolerance for detecting drifts. One could argue it should be the function of the drift direction and the length of the period instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akarnokd Is it your preference to create a separate factor
rx.scheduler.drift-tolerance-backward
andrx.scheduler.drift-tolerance-forward
?I think the tolerance setting would be more easily generalized as a percentage of the period but it would be slightly more difficult to grok (if that is ever actually a concern for users). I'm okay with converting this to a percentage value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd leave it as it is now. I'm not sure about the percentage because what if the task itself is slow instead of a clock drift?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akarnokd May you please allow to set
CLOCK_DRIFT_TOLERANCE_NANOS
in seconds/milliseconds? I have a code which controls player position to play short pieces (few seconds) of media and pause them when the position is reached. Such as player may have various playback issues (fast/slow playback, delay in initialization) i useFlowable.interval
(50ms) to check its state/position periodically. However when system time synchronization occurs during playback flowable stops fire events until time become the same as it was before synchronizationHere how it looks in my log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please post a separate issue about this case.