Stop GTFS-RT alerts refreshes from stalling arrivals and trip requests #481
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #481"
general
weight: 0.35
aaronbrethorst
Problem
Every request that reads service alerts can stall for seconds once per alerts refresh. That includes arrivals-and-departures, trip details, and vehicles. The stall grows with alert count and database latency.
GtfsRealtimeSource.handleAlertscallsServiceAlertsServiceImpl.createOrUpdateServiceAlertsandremoveServiceAlerts. Both aresynchronized, and both look up and save each alert in the database while holding the monitor.getServiceAlertIdsForKeyissynchronizedon the same monitor. Readers therefore wait for every one of those round trips.GtfsRealtimeAlertLibrarystampscreationTime/modifiedTimewith the current time on each conversion, so theexistingAlert.equals(serviceAlert)check never matches. This has been the case since 2011; it became expensive when alerts moved to the database in c76d8fac5 (2020).http-niothreadsBLOCKEDonServiceAlertsServiceImplwhilescheduledExecutorService-1held it, waiting in a Postgres socket read.King County Metro's feed has about 78 alerts. With 5 ms of database latency, each KCM refresh held the monitor for about 13 s out of every 30 s.
Fix
ServiceAlertsServiceImplReentrantLock. Readers take only a short index lock to copy alert IDs and look up records. No database call happens under the index lock, and the copy still prevents theConcurrentModificationExceptionthat 0f36d40da fixed.loadServiceAlertsreads rows before swapping the index. Readers no longer see an empty cache mid-reload.service-alerts-sync) and serves the current cache meanwhile. At most one check is queued at a time, and readers queue one at most every 250 ms, so busy traffic doesn't wake the sync thread on every request.getServiceAlertIdsForKey, which every indexed reader calls, so no reader can skip it (four didn't call it before).getServiceAlertForIdandgetAllServiceAlertscall it directly.getServiceAlertForIdandgetAllServiceAlerts, reads the cache under the index lock, so none of them can see a half-rebuilt cache during a reload. Before, the reader did the reload inline; in testing that held one request for 3.7 s.createOrUpdateServiceAlertsandremoveServiceAlertsreturn immediately for empty lists.GtfsRealtimeSourcewithoutTimestamps). The fields arerequired, hencebuildPartial(). After the first poll, only alerts that changed are saved.handleAlerts,handleAlertCollection) share onesaveAlertshelper.getServiceAlertForIdlookup.Saving to the database before updating the service's cache, so a failed save can't leave the cache serving unsaved content, is split out into #483. It changes every save path, including the admin console.
Behavior change
A reader-triggered sync is now eventual. A change written by another instance (or the admin console) shows up a moment after the first read that notices it, not on that read.
ServiceAlertsServiceImplTestwrites through its test transaction, which other threads can't see, so it injects a same-thread executor through the newsetSyncExecutor.Tests
ServiceAlertsServiceImplConcurrencyTest(alerts-persistence). Holds a fake database call open during a bulk update, a removal, and a stale-cache reload, and requires a reader to answer within 1 s each time. All three failed before the fix.GtfsRealtimeSourceAlertsTest. Polling an unchanged feed twice saves once, a changed alert is saved again, an unchanged alert the service lost is saved again, and a changed alert whose save failed is saved on the next poll. The unchanged and failed-save cases failed before their fixes.GtfsRealtimeSource+ServiceAlertsServiceImpl+ Hibernate/HSQLDB, a persister that fails once). After a failed save the database keeps v1 and the next poll writes v2; before the fix it stayed on v1. Unchanged polls don't write, and an alert deleted from the database is restored on the next poll.Version
Bumps every module from 2.7.1 to 2.7.2, along with the Dockerfile
OBA_VERSION, the SCM tag,CLAUDE.md, and the API-key CLI README.Measured
Setup:
onebusaway-api-webapp:2.7.1-latest, with this branch's two jars rebuilt fromv2.7.1.current-timeas a control.| | Stock 2.7.1 | This branch |
|---|---|---|
| KCM
handleAlertsper cycle | ~13,000 ms | ~40 ms (first cycle ~7,200 ms) || Arrivals p95, outside alerts windows | 7,500 ms | 65 ms |
| Arrivals p95, inside alerts windows | 11,788 ms | 88–369 ms |
| Arrivals overlapping the cold first cycle (all 78 KCM alerts written) | — | 27 requests, median 44 ms, max 177 ms |
|
current-timep95 (control) | 19–23 ms | 18–20 ms |On stock, even the "outside" requests stall, because CT and Intercity cycles also hold the monitor for 4–5 s. Alerts are still served after the change:
alerts-for-agency/1.pbreturned 41 KB.Re-check on the final commit
Same setup, but with the 2.7.2 build of this branch:
alerts-for-agency/1.pbreturns 41 KB.current-timep95 24 ms. No non-200 responses.max(modifiedtime)held steady across polls, so unchanged alerts aren't re-saved.current-timecontrol within about 10 ms. Both sat near 1 s, because something else makes a DB round trip on every request (#482).Review notes
These are judgment calls; I left them out of this PR:
GtfsRealtimeAlertLibrarystampsnow()intocreationTime/modifiedTimeon every conversion. Stamping 0 there would let the source use plainequals()and drop the stripping. I didn't make that change because it's outside this diff.cleanup()still runs its orphan DELETEs on every poll, even when nothing changed. It doesn't block readers anymore, but it is avoidable database load.getServiceAlertsForStopId, takes the index lock once per key. A reload landing between those acquisitions can mix two cache generations in one response.handleAlertsstops before pruning alerts that left the feed and beforecleanup(). While the database fails intermittently, removed alerts stay until a poll succeeds.<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
Bug Fixes
Alert lookups remain responsive while database updates, removals, or stale-cache refreshes are in progress. Stale data is refreshed in the background.
Repeated alerts from real-time feeds are not saved again when their content is unchanged. Changes to alert content or collection-feed timestamps are saved, and alerts missing from the service are saved again.
Failed alert saves can be retried, and duplicate alert IDs received in a single poll are processed only once.
* Empty bulk create or removal requests complete without unnecessary work.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->