Repository navigation
Add ShapeService and move TripPattern geometries out of the transit model - #8149
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## dev-2.x #8149 +/- ##
=============================================
+ Coverage 76.14% 76.16% +0.01%
- Complexity 24836 24896 +60
=============================================
Files 2658 2671 +13
Lines 93078 93270 +192
Branches 9288 9293 +5
=============================================
+ Hits 70878 71042 +164
- Misses 19058 19086 +28
Partials 3142 3142 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| "No geometry created for trip pattern " + tripPattern.getId() | ||
| ); | ||
| } | ||
| int expectedHops = Math.max(tripPattern.numberOfStops() - 1, 0); |
There was a problem hiding this comment.
Can the number of stops be negative?
There was a problem hiding this comment.
While working on the code there were references to the possibility of tripPatterns without stops, which is weird. This is an old check from TripPatternBuilder
| } | ||
| int expectedHops = Math.max(tripPattern.numberOfStops() - 1, 0); | ||
| if (tripPatternGeometry.size() != expectedHops) { | ||
| throw new IllegalStateException( |
There was a problem hiding this comment.
I don't remember what was the exception handling in the data builder, but does the build crash when you throw this?
There was a problem hiding this comment.
All of these IllegalStateExceptions are caused by states that should never be reachable. I think we should discuss how the contract between shapes and tripPatterns is handled and if/where to put these checks.
If this happens during the data build, the build should crash (I haven't verified). However, what if a similar thing happens in a realtime updater?
| */ | ||
| private transient boolean frozen = false; | ||
|
|
||
| public ShapeDataImport() {} |
There was a problem hiding this comment.
I don't know if we have these empty public constructors defined or not. This looked a bit odd so we probably don't usually do it.
There was a problem hiding this comment.
TimetableDataImport does the same thing
| } | ||
| return new CompactLineStringSequence(packed, cumulativeDistanceMeters); | ||
| return new CompactLineStringSequence( | ||
| geometries.toArray(new CompactLineString[0]), |
There was a problem hiding this comment.
I asked AI why the CompactLineString size isn't allocated properly here and it claimed that this 0 approach is as fast or faster. It's some standard java idiom that I just wasn't aware of but I'm not sure if everyone else is or is it worth a comment here why it's done like this.
There was a problem hiding this comment.
I would just leave it as is without comment
|
|
||
| private static final ScheduledShapeData EMPTY = new ScheduledShapeData(Map.of()); | ||
|
|
||
| private final Map<FeedScopedId, CompactLineStringSequence> geometriesByTripPattern; |
There was a problem hiding this comment.
This doesn't yet support different shapes for a pattern, but I guess we can do that change as a follow up.
There was a problem hiding this comment.
yes, this PR is aiming for not making functional changes, per-trip shapes will come in later phases
|
could you fix the conflicts? |
|
there are some conflicts again. |
…into add-shape-module
| var originalTripPatternGeometry = repository.getTripPatternGeometry(original.getId()); | ||
| if (originalTripPatternGeometry == null) { | ||
| throw new IllegalStateException( | ||
| "No geometry found for the original trip pattern " + original.getId() | ||
| ); | ||
| } |
There was a problem hiding this comment.
Should we clear the shapes for a FULL_DATASET update? In that case, can the fact that the trip pattern cache does not clear trip patterns cause problems? For example, a trip pattern from a realtime update is based on a cleared trip pattern.
There was a problem hiding this comment.
One option would be to not clear them at all. In dev-2.x the trip pattern cache is never cleared, which means that a realtime generated geometry stored on a trip pattern is also stored forever. However, for example SIRI extra journeys skip the cache and are garbage collected once cleared on dev-2.x. If the geometries are not cleared in the ShapeRepository, this would be a behaviour change for SIRI extra journeys since their geometries would now be stored forever.
vpaturet
left a comment
There was a problem hiding this comment.
Some general comments:
-
Have you considered TripPatternGeometry instead of Shape?
-
The existing invariant "A TripPattern has always a geometry" should be preserved.
I would suggest keeping the checks on the "impossible cases" with IllegalStateException to prove the invariant.
In the current state of the PR there is no code path that leads to a TripPattern without geometry, but you could secure this with:
- inverting the order of insertion (shape first, then trip pattern) so that a failure in saving the shape prevents the creation of the pattern (you may end up with an orphan entry in the shape repository, but this is a minor issue)
- ensuring that there is always a fallback to a straight-line geometry.
- The growth of the repository is unbounded, this may be problematic for SIRI-Lite which uses the FULL_DATASET mode. I would suggest adding logic to track the removal of patterns and propagate it to the shape repository.
Ideally this would be implemented using events. Could be worth a follow-up.
Thanks, I think 1. and 2. are pretty straightforward, but I think we need to discuss how to handle the removal of patterns. Right now I can think of 3 options:
|
As long as a shape is always provided, order does not matter.
|
…into add-shape-module
…into add-shape-module
…nstead of throwing exception.
bd15a0e
| public CompactLineStringSequence getTripPatternShape(TripPattern tripPattern) { | ||
| var tripPatternShape = shapes.getTripPatternShape(tripPattern.getId()); | ||
| if (tripPatternShape == null) { | ||
| LOG.warn("No shape found for trip pattern {}. Using straight lines.", tripPattern.getId()); | ||
| return TripShapeFactory.straightLines(tripPattern.getStopPattern(), DeduplicatorService.NOOP); | ||
| } | ||
| return tripPatternShape; | ||
| } |
There was a problem hiding this comment.
It would feel better if the shape service didn't interact with patterns but just with feedscopedids. It does feel tempting for DRYness reasons to put the fallback code here though. So before you change anything, it would be nice to hear someone else's opinion on this.
There was a problem hiding this comment.
Discussed in the dev meeting: This is not ideal but this is still the best compromise. In this case using feed ids would make the logic more complicated and redundant.
…into add-shape-module
…into add-shape-module
012e167
|
Some last minute merge conflicts |
Summary
Adds a shape module (
service/shape) that extractsTripPatterngeometries out ofTripPatterninto a dedicated repository/service, and resolves geometries for real-time patterns viaRealTimeShapeResolver.CompactLineString*were moved into the new module. The newPackedLineStringCodecstays in thestreetmodule.This is the first phase of adding real-time shape capability to OTP by extracting a shape module. This PR aims to not change existing functionality.
Functional changes
Mostly a refactoring, but there are some behavior changes:
nullin the GTFS(
geometry,patternGeometry) and Transmodel (pointsOnLink) APIs.RealTimeShapeResolver)instead of when the
TripPatternis built, using the same algorithm.--build --serveshapes are empty, same as the timetable data.Review focus
RealTimeShapeResolver: derives a real-time pattern's geometry from the original pattern's. Itruns last in
TripUpdateApplier.apply, so no shape is written for an update that failed. If theresolver itself throws, the timetable update is already applied and the exception is caught by
the caller, so the pattern is committed without a geometry.
DefaultShapeRepositoryonly adds real-time geometries, andevery commit copies the whole map. SIRI creates a new pattern id for each added/extra-call
update, so the map grows with uptime. Previously the geometry was garbage collected together
with the pattern.
RealTimeShapeResolver:60IllegalStateException: hop count must equalmax(stops - 1, 0).Previously
TripPatternBuilder.buildHopGeometriesenforced this when the pattern was built;geometry creation is now separate from the pattern, so the resolver checks it (real-time only).
RealTimeShapeResolver:83IllegalStateException: the replaced original pattern must have ageometry. New: the geometry used to be on the original
TripPatternitself.DefaultShapeService:27IllegalStateException: every committed pattern must have a geometry.New: a
TripPatternused to always carry its geometry.ShapeDataImport:63IllegalStateException: nosetTripPatternGeometryaftertoScheduledShapeData(). New: mirrorsTimetableDataImport.CompactLineStringSequence:47/55/61IllegalArgumentException: cumulative distances must havegeometries + 1entries, start at 0 and be non-decreasing. Moved unchanged fromCompactLineStringSequence.ScheduledTransitLegReference.getLeg: shape lookup moved here.TripPattern.Issue
Ref #7939
Unit tests
ScheduledShapeDataTest,ShapeDataImportTest,DefaultShapeRepositoryTest,DefaultShapeServiceTest,ShapeRepositoryLifecycleTestTripGeometryFactoryTest,CompactLineStringTest,CompactLineStringSequenceTest,PackedLineStringCodecTestRealTimeShapeResolverTest,TripUpdateApplierTestScheduledTransitLegReferenceTest,GraphSerializationTestDocumentation
service/shape/package.mddescribing the design, data flow and a glossary.