-
Notifications
You must be signed in to change notification settings - Fork 9
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
[WIP] Features safe time #156
Draft
zilm13
wants to merge
11
commits into
develop
Choose a base branch
from
features/safe-time
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
65f7fb6
util: time providers added
zilm13 cf26375
time: add mappers
zilm13 9222fc5
time: add strategies
zilm13 c24ba46
NetworkTime: recheck network time if it has changed dramatically
zilm13 6b43624
time providers: added logging
zilm13 69c061e
Update util/time/src/main/java/org/ethereum/beacon/time/provider/Syst…
zilm13 a3124ad
use Thread api for thread interruption instead of Exception
zilm13 c62ba23
use Thread api for thread interruption instead of Exception in Networ…
zilm13 423c586
use Thread api for thread interruption instead of Exception in Networ…
zilm13 60cb8c5
Merge branch 'develop' into features/safe-time
zilm13 32bc55a
util/time: fix ObjectTimeMapperTest
zilm13 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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,5 +0,0 @@ | ||
dependencies { | ||
implementation 'io.projectreactor:reactor-core' | ||
implementation 'com.google.guava:guava' | ||
implementation 'commons-beanutils:commons-beanutils' | ||
} | ||
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,5 @@ | ||
dependencies { | ||
implementation 'io.projectreactor:reactor-core' | ||
implementation 'com.google.guava:guava' | ||
implementation 'commons-beanutils:commons-beanutils' | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,12 @@ | ||
dependencies { | ||
implementation project(':util:core') | ||
implementation project(':core') | ||
|
||
implementation 'io.projectreactor:reactor-core' | ||
implementation 'commons-net:commons-net' | ||
implementation 'org.apache.logging.log4j:log4j-core' | ||
|
||
testImplementation project(':consensus') | ||
testImplementation project(':ssz') | ||
testImplementation 'junit:junit' | ||
} |
60 changes: 60 additions & 0 deletions
60
util/time/src/main/java/org/ethereum/beacon/time/NetworkFirstStrategy.java
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,60 @@ | ||
package org.ethereum.beacon.time; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.ethereum.beacon.core.types.Time; | ||
import org.ethereum.beacon.schedulers.Scheduler; | ||
import org.ethereum.beacon.stream.SimpleProcessor; | ||
import org.ethereum.beacon.time.provider.NetworkTime; | ||
import org.ethereum.beacon.time.provider.StatisticsTime; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Strategy which prioritize {@link NetworkTime} but if the delta between {@link NetworkTime} and | ||
* {@link StatisticsTime} goes above allowedDelta, it uses {@link StatisticsTime} as time provider. | ||
*/ | ||
public class NetworkFirstStrategy implements TimeStrategy { | ||
private final SimpleProcessor<Time> timeProcessor; | ||
private final AtomicLong latestNetwork = new AtomicLong(-1); | ||
private final AtomicLong latestStatistics = new AtomicLong(-1); | ||
private static final Logger logger = LogManager.getLogger("time"); | ||
|
||
public NetworkFirstStrategy( | ||
Scheduler scheduler, | ||
NetworkTime networkTime, | ||
StatisticsTime statisticsTime, | ||
int allowedDelta) { | ||
this.timeProcessor = new SimpleProcessor<Time>(scheduler, "NetworkFirstStrategy"); | ||
Flux.from(networkTime.getTimeStream()) | ||
.subscribe( | ||
t -> { | ||
this.latestNetwork.set(t.getValue()); | ||
if (latestStatistics.get() == -1) { | ||
return; | ||
} | ||
if (Math.abs(latestNetwork.get() - latestStatistics.get()) <= allowedDelta) { | ||
timeProcessor.onNext(t); | ||
} | ||
}); | ||
Flux.from(statisticsTime.getTimeStream()) | ||
.subscribe( | ||
t -> { | ||
this.latestStatistics.set(t.getValue()); | ||
if (latestNetwork.get() == -1) { | ||
return; | ||
} | ||
if (Math.abs(latestNetwork.get() - latestStatistics.get()) > allowedDelta) { | ||
logger.trace(() -> String.format("Using time from statistics time provider: %s", t.getValue())); | ||
timeProcessor.onNext(t); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Publisher<Time> getTimeStream() { | ||
return timeProcessor; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
util/time/src/main/java/org/ethereum/beacon/time/StatisticsStrategy.java
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,18 @@ | ||
package org.ethereum.beacon.time; | ||
|
||
import org.ethereum.beacon.core.types.Time; | ||
import org.ethereum.beacon.time.provider.StatisticsTime; | ||
import org.reactivestreams.Publisher; | ||
|
||
public class StatisticsStrategy implements TimeStrategy { | ||
private final StatisticsTime statisticsTime; | ||
|
||
public StatisticsStrategy(StatisticsTime statisticsTime) { | ||
this.statisticsTime = statisticsTime; | ||
} | ||
|
||
@Override | ||
public Publisher<Time> getTimeStream() { | ||
return statisticsTime.getTimeStream(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
util/time/src/main/java/org/ethereum/beacon/time/TimeStrategy.java
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,12 @@ | ||
package org.ethereum.beacon.time; | ||
|
||
import org.ethereum.beacon.core.types.Time; | ||
import org.reactivestreams.Publisher; | ||
|
||
/** | ||
* High level time supplier, uses several {@link org.ethereum.beacon.time.provider.TimeProvider} to | ||
* produce result time | ||
*/ | ||
public interface TimeStrategy { | ||
Publisher<Time> getTimeStream(); | ||
} |
30 changes: 30 additions & 0 deletions
30
util/time/src/main/java/org/ethereum/beacon/time/mapper/ObjectTimeMapper.java
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,30 @@ | ||
package org.ethereum.beacon.time.mapper; | ||
|
||
import org.ethereum.beacon.core.types.Time; | ||
import org.ethereum.beacon.schedulers.Scheduler; | ||
import org.ethereum.beacon.stream.SimpleProcessor; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.function.Function; | ||
|
||
public class ObjectTimeMapper<T> implements TimeMapper { | ||
private final Function<T, Time> timeFunc; | ||
private final SimpleProcessor<Time> timeProcessor; | ||
|
||
public ObjectTimeMapper( | ||
Scheduler scheduler, Publisher<T> objectStream, Function<T, Time> timeFunc) { | ||
this.timeProcessor = new SimpleProcessor<>(scheduler, "TimeMapper"); | ||
this.timeFunc = timeFunc; | ||
Flux.from(objectStream).map(this::mapObjectFunc).subscribe(timeProcessor::onNext); | ||
} | ||
|
||
Time mapObjectFunc(T obj) { | ||
return timeFunc.apply(obj); | ||
} | ||
|
||
@Override | ||
public Publisher<Time> getTimeStream() { | ||
return timeProcessor; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
util/time/src/main/java/org/ethereum/beacon/time/mapper/TimeMapper.java
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,9 @@ | ||
package org.ethereum.beacon.time.mapper; | ||
|
||
import org.ethereum.beacon.core.types.Time; | ||
import org.reactivestreams.Publisher; | ||
|
||
/** Maps stream of objects with some kind of time info to time stream */ | ||
public interface TimeMapper { | ||
Publisher<Time> getTimeStream(); | ||
} |
Oops, something went wrong.
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.
May we have a component with two inbound streams and a single processor that handles them both?