Skip to content

Fix block-level cache contamination#465

Open
KTibow wants to merge 1 commit into
OneBusAway:mainfrom
KTibow:fix-trip-scoped-arrival-realtime
Open

Fix block-level cache contamination#465
KTibow wants to merge 1 commit into
OneBusAway:mainfrom
KTibow:fix-trip-scoped-arrival-realtime

Conversation

@KTibow

@KTibow KTibow commented Jul 2, 2026

Copy link
Copy Markdown

As reported in #216 (this PR fixes #216) and suffered by me and other transit apps, if any trip on a block had GTFS‑RT data, the cache returned a non‑empty location list, and predicted=true leaked to every trip on that block even if they had no RT data.

The fix adds a realtimeTripId field to BlockLocation - it records which trip the GTFS‑RT data actually belongs to. Then in applyRealTimeToStopTimeInstance, it checks isBlockLocationApplicableToInstance:

// Before: if locations was non-empty, ALL trips got predicted=true
if (locations.isEmpty()) { ... fall back to scheduled ... }

// After: only trips that actually match the RT data get predicted=true
if (!hasApplicableLocation) { ... fall back to scheduled ... }

The check says: a BlockLocation is "applicable" to a trip if either:

  • It has a timepoint prediction for this specific trip, or
  • Its realtimeTripId matches this trip

Otherwise the trip falls through to the scheduled‑only path, which produces predicted=false. No more schedule data wearing the predicted=true badge just because a different trip on the same block has real‑time data.

Disclosure: Generated by GPT-5.5.

Summary by CodeRabbit

  • Bug Fixes
    • Improved real-time arrival/departure handling so predictions are applied only to the correct trip and do not leak to later trips in the same block.
    • Prevented duplicate trip instances in multi-vehicle scenarios, keeping arrivals and departures aligned with the intended vehicle.
    • Real-time trip matching is now more precise when evaluating whether a vehicle location should affect a stop prediction.

@CLAassistant

CLAassistant commented Jul 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds a realtimeTripId field to BlockLocation, populated from VehicleLocationRecord data, and introduces applicability filtering in ArrivalAndDepartureServiceImpl so only realtime locations matching a StopTimeInstance's trip are applied, preventing prediction propagation into unrelated trips. Tests are updated and added accordingly.

Changes

Realtime trip applicability filtering

Layer / File(s) Summary
BlockLocation realtimeTripId field and accessors
.../services/realtime/BlockLocation.java, .../impl/realtime/AbstractBlockLocationServiceImpl.java
Adds a realtimeTripId field with getter/setter to BlockLocation, populated from VehicleLocationRecord.getTripId() during cache-based location construction.
Applicability check in ArrivalAndDepartureServiceImpl
.../impl/ArrivalAndDepartureServiceImpl.java
Adds isBlockLocationApplicableToInstance and hasTimepointPredictionForTrip helpers, tracks a hasApplicableLocation flag while filtering realtime locations, and changes the fallback trigger from locations.isEmpty() to !hasApplicableLocation.
Test coverage for applicability filtering
.../impl/ArrivalAndDepartureServiceImplTest.java
Updates propagation assertions to prevent prediction leakage into subsequent trips, adds a new test verifying no duplicate trip instances occur for a two-vehicle block, and adds supporting helper methods (getArrivalsAndDeparturesForTwoVehicleBlock, addVehicleLocationRecord, countArrivalsAndDeparturesForTrip).

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AbstractBlockLocationServiceImpl
  participant BlockLocation
  participant ArrivalAndDepartureServiceImpl
  participant StopTimeInstance

  AbstractBlockLocationServiceImpl->>BlockLocation: setRealtimeTripId(record.getTripId())
  ArrivalAndDepartureServiceImpl->>BlockLocation: getRealtimeTripId()
  ArrivalAndDepartureServiceImpl->>ArrivalAndDepartureServiceImpl: isBlockLocationApplicableToInstance(location, instance)
  ArrivalAndDepartureServiceImpl->>StopTimeInstance: compare trip id / check timepoint predictions
  alt location applicable
    ArrivalAndDepartureServiceImpl->>ArrivalAndDepartureServiceImpl: apply realtime prediction to instance
  else no applicable location
    ArrivalAndDepartureServiceImpl->>ArrivalAndDepartureServiceImpl: fall back to schedule-based instance
  end
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main fix: preventing cached block-level realtime data from leaking across trips.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@KTibow

KTibow commented Jul 2, 2026

Copy link
Copy Markdown
Author

This CLA is wrong - it's currently displaying the default SAP SE corporate template.

@KTibow

KTibow commented Jul 2, 2026

Copy link
Copy Markdown
Author

cc @aaronbrethorst - seems you added CONTRIBUTING.md referencing the CLA, but the CLA is wrong ("to SAP"):

Copyright License. You hereby grant, and agree to grant, to SAP a non-exclusive, perpetual, irrevocable, worldwide, fully-paid, royalty-free, transferable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, and distribute your Contributions and such derivative works, with the right to sublicense the foregoing rights through multiple tiers of sublicensees.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delay is appearing on all trip of vehicle and trip repeating multiple times

2 participants