-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
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
66 changes: 66 additions & 0 deletions
66
src/main/java/org/opentripplanner/updater/spi/TimetableSnapshotFlushUpdater.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,66 @@ | ||
package org.opentripplanner.updater.spi; | ||
|
||
import java.time.Duration; | ||
import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource; | ||
import org.opentripplanner.updater.TimetableSnapshotSourceParameters; | ||
import org.opentripplanner.updater.trip.TimetableSnapshotSource; | ||
|
||
/** | ||
* Updater that flushes the snapshot buffer by committing pending changes. | ||
* The updater runs at the same frequency as the timetable snapshot throttle frequency. See | ||
* {@link org.opentripplanner.updater.TimetableSnapshotSourceParameters#withMaxSnapshotFrequency(Duration)} | ||
* It means that pending changes are committed at least once per throttling period. | ||
* This is useful for a system where real-time updates are sent in batches followed by silent periods. | ||
* In this case flushing the buffer periodically guarantees that a throttled update will be applied timely. | ||
* In a system that receives a steady flow of updates, this updater is not required but does not | ||
* hurt performance either. | ||
*/ | ||
public class TimetableSnapshotFlushUpdater extends PollingGraphUpdater { | ||
|
||
private final SiriTimetableSnapshotSource siriTimetableSnapshotSource; | ||
private final TimetableSnapshotSource gtfsTimetableSnapshotSource; | ||
protected WriteToGraphCallback saveResultOnGraph; | ||
|
||
public TimetableSnapshotFlushUpdater( | ||
TimetableSnapshotSourceParameters config, | ||
SiriTimetableSnapshotSource siriTimetableSnapshotSource, | ||
TimetableSnapshotSource gtfsTimetableSnapshotSource | ||
) { | ||
super(buildConfig(config)); | ||
this.siriTimetableSnapshotSource = siriTimetableSnapshotSource; | ||
this.gtfsTimetableSnapshotSource = gtfsTimetableSnapshotSource; | ||
} | ||
|
||
private static PollingGraphUpdaterParameters buildConfig( | ||
TimetableSnapshotSourceParameters config | ||
) { | ||
return new PollingGraphUpdaterParameters() { | ||
@Override | ||
public Duration frequency() { | ||
return config.maxSnapshotFrequency(); | ||
} | ||
|
||
@Override | ||
public String configRef() { | ||
return "TIMETABLE_SNAPSHOT_FLUSH_UPDATER"; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
protected void runPolling() { | ||
saveResultOnGraph.execute((graph, transitModel) -> { | ||
if (siriTimetableSnapshotSource != null) { | ||
siriTimetableSnapshotSource.flushBuffer(); | ||
} | ||
if (gtfsTimetableSnapshotSource != null) { | ||
gtfsTimetableSnapshotSource.flushBuffer(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void setup(WriteToGraphCallback writeToGraphCallback) { | ||
saveResultOnGraph = writeToGraphCallback; | ||
} | ||
} |
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