diff --git a/src/ext-test/java/org/opentripplanner/ext/realtimeresolver/RealtimeResolverTest.java b/src/ext-test/java/org/opentripplanner/ext/realtimeresolver/RealtimeResolverTest.java index af415b5e883..92b971f62ad 100644 --- a/src/ext-test/java/org/opentripplanner/ext/realtimeresolver/RealtimeResolverTest.java +++ b/src/ext-test/java/org/opentripplanner/ext/realtimeresolver/RealtimeResolverTest.java @@ -176,8 +176,9 @@ private static TransitService makeTransitService( transitModel.updateCalendarServiceData(true, calendarServiceData, DataImportIssueStore.NOOP); transitModel.index(); - var alertService = new TransitAlertServiceImpl(transitModel); return new DefaultTransitService(transitModel) { + final TransitAlertService alertService = new TransitAlertServiceImpl(this); + @Override public TransitAlertService getTransitAlertService() { return alertService; diff --git a/src/ext-test/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandlerTest.java b/src/ext-test/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandlerTest.java index 7c603ea7b2e..dcaac1ee5f3 100644 --- a/src/ext-test/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandlerTest.java +++ b/src/ext-test/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandlerTest.java @@ -83,11 +83,11 @@ public void setUp() throws Exception { transitAlertService.getAllAlerts().clear(); } if (alertsUpdateHandler == null) { - transitAlertService = new TransitAlertServiceImpl(transitModel); + transitAlertService = new TransitAlertServiceImpl(transitService); alertsUpdateHandler = new SiriAlertsUpdateHandler( FEED_ID, - transitModel, + transitService, transitAlertService, SiriFuzzyTripMatcher.of(transitService), Duration.ZERO diff --git a/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/stops/RealtimeStopsLayerTest.java b/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/stops/RealtimeStopsLayerTest.java index bb258beb76f..8208351dd3d 100644 --- a/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/stops/RealtimeStopsLayerTest.java +++ b/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/stops/RealtimeStopsLayerTest.java @@ -65,8 +65,9 @@ void realtimeStopLayer() { var transitModel = new TransitModel(new StopModel(), deduplicator); transitModel.initTimeZone(ZoneIds.HELSINKI); transitModel.index(); - var alertService = new TransitAlertServiceImpl(transitModel); var transitService = new DefaultTransitService(transitModel) { + final TransitAlertService alertService = new TransitAlertServiceImpl(this); + @Override public TransitAlertService getTransitAlertService() { return alertService; diff --git a/src/ext/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandler.java b/src/ext/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandler.java index 8a747e765da..bc6c3c3102d 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandler.java +++ b/src/ext/java/org/opentripplanner/ext/siri/SiriAlertsUpdateHandler.java @@ -20,8 +20,6 @@ import org.opentripplanner.routing.alertpatch.TransitAlertBuilder; import org.opentripplanner.routing.services.TransitAlertService; import org.opentripplanner.transit.model.framework.FeedScopedId; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; import org.opentripplanner.transit.service.TransitService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,7 +65,7 @@ public class SiriAlertsUpdateHandler { */ public SiriAlertsUpdateHandler( String feedId, - TransitModel transitModel, + TransitService transitService, TransitAlertService transitAlertService, SiriFuzzyTripMatcher siriFuzzyTripMatcher, Duration earlyStart @@ -76,7 +74,6 @@ public SiriAlertsUpdateHandler( this.transitAlertService = transitAlertService; this.earlyStart = earlyStart; - TransitService transitService = new DefaultTransitService(transitModel); this.affectsMapper = new AffectsMapper(feedId, siriFuzzyTripMatcher, transitService); } diff --git a/src/ext/java/org/opentripplanner/ext/siri/SiriTimetableSnapshotSource.java b/src/ext/java/org/opentripplanner/ext/siri/SiriTimetableSnapshotSource.java index 9e2dfaff76b..597bf4d3e9f 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/SiriTimetableSnapshotSource.java +++ b/src/ext/java/org/opentripplanner/ext/siri/SiriTimetableSnapshotSource.java @@ -133,7 +133,13 @@ public TimetableSnapshot getTimetableSnapshot() { return snapshotManager.getTimetableSnapshot(); } - private TimetableSnapshot getTimetableSnapshotBuffer() { + /** + * @return the current timetable snapshot buffer that contains pending changes (not yet published + * in a snapshot). + * This should be used in the context of an updater to build a TransitEditorService that sees all + * the changes applied so far by real-time updates. + */ + public TimetableSnapshot getTimetableSnapshotBuffer() { return snapshotManager.getTimetableSnapshotBuffer(); } diff --git a/src/ext/java/org/opentripplanner/ext/siri/updater/SiriETUpdater.java b/src/ext/java/org/opentripplanner/ext/siri/updater/SiriETUpdater.java index c811d3ee5d8..247e7787d73 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/updater/SiriETUpdater.java +++ b/src/ext/java/org/opentripplanner/ext/siri/updater/SiriETUpdater.java @@ -3,8 +3,7 @@ import java.util.List; import java.util.function.Consumer; import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.spi.PollingGraphUpdater; import org.opentripplanner.updater.spi.ResultLogger; import org.opentripplanner.updater.spi.UpdateResult; @@ -41,7 +40,7 @@ public class SiriETUpdater extends PollingGraphUpdater { public SiriETUpdater( SiriETUpdaterParameters config, - TransitModel transitModel, + TransitService transitService, SiriTimetableSnapshotSource timetableSnapshotSource ) { super(config); @@ -62,7 +61,7 @@ public SiriETUpdater( new EstimatedTimetableHandler( timetableSnapshotSource, config.fuzzyTripMatching(), - new DefaultTransitService(transitModel), + transitService, feedId ); diff --git a/src/ext/java/org/opentripplanner/ext/siri/updater/SiriSXUpdater.java b/src/ext/java/org/opentripplanner/ext/siri/updater/SiriSXUpdater.java index 5ededbb3bf0..58939a43117 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/updater/SiriSXUpdater.java +++ b/src/ext/java/org/opentripplanner/ext/siri/updater/SiriSXUpdater.java @@ -11,8 +11,7 @@ import org.opentripplanner.framework.retry.OtpRetryBuilder; import org.opentripplanner.routing.impl.TransitAlertServiceImpl; import org.opentripplanner.routing.services.TransitAlertService; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.alert.TransitAlertProvider; import org.opentripplanner.updater.spi.PollingGraphUpdater; import org.opentripplanner.updater.spi.WriteToGraphCallback; @@ -45,7 +44,7 @@ public class SiriSXUpdater extends PollingGraphUpdater implements TransitAlertPr private final SiriHttpLoader siriHttpLoader; private final OtpRetry retry; - public SiriSXUpdater(SiriSXUpdaterParameters config, TransitModel transitModel) { + public SiriSXUpdater(SiriSXUpdaterParameters config, TransitService transitService) { super(config); // TODO: add options to choose different patch services this.url = config.url(); @@ -58,13 +57,13 @@ public SiriSXUpdater(SiriSXUpdaterParameters config, TransitModel transitModel) //Keeping original requestorRef use as base for updated requestorRef to be used in retries this.originalRequestorRef = requestorRef; this.blockReadinessUntilInitialized = config.blockReadinessUntilInitialized(); - this.transitAlertService = new TransitAlertServiceImpl(transitModel); + this.transitAlertService = new TransitAlertServiceImpl(transitService); this.updateHandler = new SiriAlertsUpdateHandler( config.feedId(), - transitModel, + transitService, transitAlertService, - SiriFuzzyTripMatcher.of(new DefaultTransitService(transitModel)), + SiriFuzzyTripMatcher.of(transitService), config.earlyStart() ); siriHttpLoader = new SiriHttpLoader(url, config.timeout(), config.requestHeaders()); diff --git a/src/ext/java/org/opentripplanner/ext/siri/updater/azure/AbstractAzureSiriUpdater.java b/src/ext/java/org/opentripplanner/ext/siri/updater/azure/AbstractAzureSiriUpdater.java index a72c1797d38..f7c15bcc538 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/updater/azure/AbstractAzureSiriUpdater.java +++ b/src/ext/java/org/opentripplanner/ext/siri/updater/azure/AbstractAzureSiriUpdater.java @@ -26,8 +26,6 @@ import org.opentripplanner.ext.siri.SiriFuzzyTripMatcher; import org.opentripplanner.framework.application.ApplicationShutdownSupport; import org.opentripplanner.framework.io.OtpHttpClientFactory; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.spi.GraphUpdater; import org.opentripplanner.updater.spi.HttpHeaders; @@ -69,7 +67,10 @@ public abstract class AbstractAzureSiriUpdater implements GraphUpdater { */ protected final int timeout; - public AbstractAzureSiriUpdater(SiriAzureUpdaterParameters config, TransitModel transitModel) { + public AbstractAzureSiriUpdater( + SiriAzureUpdaterParameters config, + TransitService transitService + ) { this.configRef = config.configRef(); this.authenticationType = config.getAuthenticationType(); this.fullyQualifiedNamespace = config.getFullyQualifiedNamespace(); @@ -80,7 +81,6 @@ public AbstractAzureSiriUpdater(SiriAzureUpdaterParameters config, TransitModel this.feedId = config.feedId(); this.autoDeleteOnIdle = config.getAutoDeleteOnIdle(); this.prefetchCount = config.getPrefetchCount(); - TransitService transitService = new DefaultTransitService(transitModel); this.entityResolver = new EntityResolver(transitService, feedId); this.fuzzyTripMatcher = config.isFuzzyTripMatching() ? SiriFuzzyTripMatcher.of(transitService) : null; diff --git a/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureETUpdater.java b/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureETUpdater.java index 2b8c48dce11..6a95c0ae780 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureETUpdater.java +++ b/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureETUpdater.java @@ -16,7 +16,7 @@ import javax.xml.stream.XMLStreamException; import org.apache.hc.core5.net.URIBuilder; import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.spi.ResultLogger; import org.opentripplanner.updater.spi.UpdateResult; import org.opentripplanner.updater.trip.UpdateIncrementality; @@ -40,10 +40,10 @@ public class SiriAzureETUpdater extends AbstractAzureSiriUpdater { public SiriAzureETUpdater( SiriAzureETUpdaterParameters config, - TransitModel transitModel, + TransitService transitService, SiriTimetableSnapshotSource snapshotSource ) { - super(config, transitModel); + super(config, transitService); this.fromDateTime = config.getFromDateTime(); this.snapshotSource = snapshotSource; this.recordMetrics = TripUpdateMetrics.streaming(config); diff --git a/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureSXUpdater.java b/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureSXUpdater.java index b04149a44bf..e8181e0e9b9 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureSXUpdater.java +++ b/src/ext/java/org/opentripplanner/ext/siri/updater/azure/SiriAzureSXUpdater.java @@ -18,7 +18,7 @@ import org.opentripplanner.ext.siri.SiriAlertsUpdateHandler; import org.opentripplanner.routing.impl.TransitAlertServiceImpl; import org.opentripplanner.routing.services.TransitAlertService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.alert.TransitAlertProvider; import org.rutebanken.siri20.util.SiriXml; import org.slf4j.Logger; @@ -35,15 +35,15 @@ public class SiriAzureSXUpdater extends AbstractAzureSiriUpdater implements Tran private final LocalDate fromDateTime; private final LocalDate toDateTime; - public SiriAzureSXUpdater(SiriAzureSXUpdaterParameters config, TransitModel transitModel) { - super(config, transitModel); + public SiriAzureSXUpdater(SiriAzureSXUpdaterParameters config, TransitService transitService) { + super(config, transitService); this.fromDateTime = config.getFromDateTime(); this.toDateTime = config.getToDateTime(); - this.transitAlertService = new TransitAlertServiceImpl(transitModel); + this.transitAlertService = new TransitAlertServiceImpl(transitService); this.updateHandler = new SiriAlertsUpdateHandler( feedId, - transitModel, + transitService, transitAlertService, fuzzyTripMatcher(), Duration.ZERO diff --git a/src/ext/java/org/opentripplanner/ext/siri/updater/google/SiriETGooglePubsubUpdater.java b/src/ext/java/org/opentripplanner/ext/siri/updater/google/SiriETGooglePubsubUpdater.java index 58c01815230..87f52393d5b 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/updater/google/SiriETGooglePubsubUpdater.java +++ b/src/ext/java/org/opentripplanner/ext/siri/updater/google/SiriETGooglePubsubUpdater.java @@ -5,8 +5,7 @@ import org.opentripplanner.ext.siri.updater.AsyncEstimatedTimetableProcessor; import org.opentripplanner.ext.siri.updater.AsyncEstimatedTimetableSource; import org.opentripplanner.ext.siri.updater.EstimatedTimetableHandler; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.spi.GraphUpdater; import org.opentripplanner.updater.spi.UpdateResult; import org.opentripplanner.updater.spi.WriteToGraphCallback; @@ -27,7 +26,7 @@ public class SiriETGooglePubsubUpdater implements GraphUpdater { public SiriETGooglePubsubUpdater( SiriETGooglePubsubUpdaterParameters config, - TransitModel transitModel, + TransitService transitService, SiriTimetableSnapshotSource timetableSnapshotSource ) { configRef = config.configRef(); @@ -46,7 +45,7 @@ public SiriETGooglePubsubUpdater( new EstimatedTimetableHandler( timetableSnapshotSource, config.fuzzyTripMatching(), - new DefaultTransitService(transitModel), + transitService, config.feedId() ); diff --git a/src/main/java/org/opentripplanner/routing/impl/TransitAlertServiceImpl.java b/src/main/java/org/opentripplanner/routing/impl/TransitAlertServiceImpl.java index 0cc24c691e9..b4d75345de9 100644 --- a/src/main/java/org/opentripplanner/routing/impl/TransitAlertServiceImpl.java +++ b/src/main/java/org/opentripplanner/routing/impl/TransitAlertServiceImpl.java @@ -13,7 +13,7 @@ import org.opentripplanner.routing.services.TransitAlertService; import org.opentripplanner.transit.model.framework.FeedScopedId; import org.opentripplanner.transit.model.timetable.Direction; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; /** * This is the primary implementation of TransitAlertService, which actually retains its own set @@ -32,12 +32,12 @@ */ public class TransitAlertServiceImpl implements TransitAlertService { - private final TransitModel transitModel; + private final TransitService transitService; private Multimap alerts = HashMultimap.create(); - public TransitAlertServiceImpl(TransitModel transitModel) { - this.transitModel = transitModel; + public TransitAlertServiceImpl(TransitService transitService) { + this.transitService = transitService; } @Override @@ -85,8 +85,8 @@ public Collection getStopAlerts( } if (result.isEmpty()) { // Search for alerts on parent-stop - if (transitModel != null) { - var quay = transitModel.getStopModel().getRegularStop(stopId); + if (transitService != null) { + var quay = transitService.getRegularStop(stopId); if (quay != null) { // TODO - SIRI: Add alerts from parent- and multimodal-stops /* diff --git a/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java b/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java index 79e248a22cf..b8a71352e1f 100644 --- a/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java +++ b/src/main/java/org/opentripplanner/updater/alert/GtfsRealtimeAlertsUpdater.java @@ -7,8 +7,7 @@ import org.opentripplanner.framework.tostring.ToStringBuilder; import org.opentripplanner.routing.impl.TransitAlertServiceImpl; import org.opentripplanner.routing.services.TransitAlertService; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher; import org.opentripplanner.updater.spi.HttpHeaders; import org.opentripplanner.updater.spi.PollingGraphUpdater; @@ -33,15 +32,15 @@ public class GtfsRealtimeAlertsUpdater extends PollingGraphUpdater implements Tr public GtfsRealtimeAlertsUpdater( GtfsRealtimeAlertsUpdaterParameters config, - TransitModel transitModel + TransitService transitService ) { super(config); this.url = config.url(); this.headers = HttpHeaders.of().acceptProtobuf().add(config.headers()).build(); - TransitAlertService transitAlertService = new TransitAlertServiceImpl(transitModel); + TransitAlertService transitAlertService = new TransitAlertServiceImpl(transitService); var fuzzyTripMatcher = config.fuzzyTripMatching() - ? new GtfsRealtimeFuzzyTripMatcher(new DefaultTransitService(transitModel)) + ? new GtfsRealtimeFuzzyTripMatcher(transitService) : null; this.transitAlertService = transitAlertService; diff --git a/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java b/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java index 83e0bd0fe85..29529522ed3 100644 --- a/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java +++ b/src/main/java/org/opentripplanner/updater/configure/UpdaterConfigurator.java @@ -16,7 +16,9 @@ import org.opentripplanner.routing.graph.Graph; import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository; import org.opentripplanner.service.vehiclerental.VehicleRentalRepository; +import org.opentripplanner.transit.service.DefaultTransitService; import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.GraphUpdaterManager; import org.opentripplanner.updater.UpdatersParameters; import org.opentripplanner.updater.alert.GtfsRealtimeAlertsUpdater; @@ -156,32 +158,50 @@ private List createUpdatersFromConfig() { } } for (var configItem : updatersParameters.getGtfsRealtimeAlertsUpdaterParameters()) { - updaters.add(new GtfsRealtimeAlertsUpdater(configItem, transitModel)); + updaters.add(new GtfsRealtimeAlertsUpdater(configItem, provideGtfsTransitService())); } for (var configItem : updatersParameters.getPollingStoptimeUpdaterParameters()) { updaters.add( - new PollingTripUpdater(configItem, transitModel, provideGtfsTimetableSnapshot()) + new PollingTripUpdater( + configItem, + provideGtfsTransitService(), + provideGtfsTimetableSnapshot() + ) ); } for (var configItem : updatersParameters.getVehiclePositionsUpdaterParameters()) { updaters.add( - new PollingVehiclePositionUpdater(configItem, realtimeVehicleRepository, transitModel) + new PollingVehiclePositionUpdater( + configItem, + realtimeVehicleRepository, + provideGtfsTransitService() + ) ); } for (var configItem : updatersParameters.getSiriETUpdaterParameters()) { - updaters.add(new SiriETUpdater(configItem, transitModel, provideSiriTimetableSnapshot())); + updaters.add( + new SiriETUpdater(configItem, provideSiriTransitService(), provideSiriTimetableSnapshot()) + ); } for (var configItem : updatersParameters.getSiriETGooglePubsubUpdaterParameters()) { updaters.add( - new SiriETGooglePubsubUpdater(configItem, transitModel, provideSiriTimetableSnapshot()) + new SiriETGooglePubsubUpdater( + configItem, + provideSiriTransitService(), + provideSiriTimetableSnapshot() + ) ); } for (var configItem : updatersParameters.getSiriSXUpdaterParameters()) { - updaters.add(new SiriSXUpdater(configItem, transitModel)); + updaters.add(new SiriSXUpdater(configItem, provideSiriTransitService())); } for (var configItem : updatersParameters.getMqttGtfsRealtimeUpdaterParameters()) { updaters.add( - new MqttGtfsRealtimeUpdater(configItem, transitModel, provideGtfsTimetableSnapshot()) + new MqttGtfsRealtimeUpdater( + configItem, + provideGtfsTransitService(), + provideGtfsTimetableSnapshot() + ) ); } for (var configItem : updatersParameters.getVehicleParkingUpdaterParameters()) { @@ -214,11 +234,15 @@ private List createUpdatersFromConfig() { } for (var configItem : updatersParameters.getSiriAzureETUpdaterParameters()) { updaters.add( - new SiriAzureETUpdater(configItem, transitModel, provideSiriTimetableSnapshot()) + new SiriAzureETUpdater( + configItem, + provideSiriTransitService(), + provideSiriTimetableSnapshot() + ) ); } for (var configItem : updatersParameters.getSiriAzureSXUpdaterParameters()) { - updaters.add(new SiriAzureSXUpdater(configItem, transitModel)); + updaters.add(new SiriAzureSXUpdater(configItem, provideSiriTransitService())); } return updaters; @@ -236,6 +260,17 @@ private SiriTimetableSnapshotSource provideSiriTimetableSnapshot() { return siriTimetableSnapshotSource; } + /** + * Provide a TransitService aware of the latest (uncommitted) SIRI real-time updates. + * Should be injected only in SIRI updaters. + */ + private TransitService provideSiriTransitService() { + return new DefaultTransitService( + transitModel, + siriTimetableSnapshotSource.getTimetableSnapshotBuffer() + ); + } + private TimetableSnapshotSource provideGtfsTimetableSnapshot() { if (gtfsTimetableSnapshotSource == null) { this.gtfsTimetableSnapshotSource = @@ -244,6 +279,17 @@ private TimetableSnapshotSource provideGtfsTimetableSnapshot() { return gtfsTimetableSnapshotSource; } + /** + * Provide a TransitService aware of the latest (uncommitted) GTFS real-time updates. + * Should be injected only in GTFS-RT updaters. + */ + private TransitService provideGtfsTransitService() { + return new DefaultTransitService( + transitModel, + gtfsTimetableSnapshotSource.getTimetableSnapshotBuffer() + ); + } + /** * If SIRI or GTFS real-time updaters are in use, configure a periodic flush of the timetable * snapshot. diff --git a/src/main/java/org/opentripplanner/updater/trip/MqttGtfsRealtimeUpdater.java b/src/main/java/org/opentripplanner/updater/trip/MqttGtfsRealtimeUpdater.java index 3ba7af0305b..0e3996f16ce 100644 --- a/src/main/java/org/opentripplanner/updater/trip/MqttGtfsRealtimeUpdater.java +++ b/src/main/java/org/opentripplanner/updater/trip/MqttGtfsRealtimeUpdater.java @@ -17,8 +17,7 @@ import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.opentripplanner.framework.tostring.ToStringBuilder; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher; import org.opentripplanner.updater.spi.GraphUpdater; import org.opentripplanner.updater.spi.UpdateResult; @@ -66,7 +65,7 @@ public class MqttGtfsRealtimeUpdater implements GraphUpdater { public MqttGtfsRealtimeUpdater( MqttGtfsRealtimeUpdaterParameters parameters, - TransitModel transitModel, + TransitService transitService, TimetableSnapshotSource snapshotSource ) { this.configRef = parameters.configRef(); @@ -78,8 +77,7 @@ public MqttGtfsRealtimeUpdater( this.snapshotSource = snapshotSource; // Set properties of realtime data snapshot source if (parameters.getFuzzyTripMatching()) { - this.fuzzyTripMatcher = - new GtfsRealtimeFuzzyTripMatcher(new DefaultTransitService(transitModel)); + this.fuzzyTripMatcher = new GtfsRealtimeFuzzyTripMatcher(transitService); } this.recordMetrics = TripUpdateMetrics.streaming(parameters); LOG.info("Creating streaming GTFS-RT TripUpdate updater subscribing to MQTT broker at {}", url); diff --git a/src/main/java/org/opentripplanner/updater/trip/PollingTripUpdater.java b/src/main/java/org/opentripplanner/updater/trip/PollingTripUpdater.java index 8af27d9818c..3cce5f2092b 100644 --- a/src/main/java/org/opentripplanner/updater/trip/PollingTripUpdater.java +++ b/src/main/java/org/opentripplanner/updater/trip/PollingTripUpdater.java @@ -4,8 +4,7 @@ import java.util.List; import java.util.function.Consumer; import org.opentripplanner.framework.tostring.ToStringBuilder; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; +import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher; import org.opentripplanner.updater.spi.PollingGraphUpdater; import org.opentripplanner.updater.spi.UpdateResult; @@ -47,7 +46,7 @@ public class PollingTripUpdater extends PollingGraphUpdater { public PollingTripUpdater( PollingTripUpdaterParameters parameters, - TransitModel transitModel, + TransitService transitService, TimetableSnapshotSource snapshotSource ) { super(parameters); @@ -57,8 +56,7 @@ public PollingTripUpdater( this.backwardsDelayPropagationType = parameters.backwardsDelayPropagationType(); this.snapshotSource = snapshotSource; if (parameters.fuzzyTripMatching()) { - this.fuzzyTripMatcher = - new GtfsRealtimeFuzzyTripMatcher(new DefaultTransitService(transitModel)); + this.fuzzyTripMatcher = new GtfsRealtimeFuzzyTripMatcher(transitService); } this.recordMetrics = BatchTripUpdateMetrics.batch(parameters); diff --git a/src/main/java/org/opentripplanner/updater/trip/TimetableSnapshotSource.java b/src/main/java/org/opentripplanner/updater/trip/TimetableSnapshotSource.java index 56e3c380b67..fe04d413607 100644 --- a/src/main/java/org/opentripplanner/updater/trip/TimetableSnapshotSource.java +++ b/src/main/java/org/opentripplanner/updater/trip/TimetableSnapshotSource.java @@ -333,6 +333,16 @@ public TimetableSnapshot getTimetableSnapshot() { return snapshotManager.getTimetableSnapshot(); } + /** + * @return the current timetable snapshot buffer that contains pending changes (not yet published + * in a snapshot). + * This should be used in the context of an updater to build a TransitEditorService that sees all + * the changes applied so far by real-time updates. + */ + public TimetableSnapshot getTimetableSnapshotBuffer() { + return snapshotManager.getTimetableSnapshotBuffer(); + } + private static void logUpdateResult( String feedId, Map failuresByRelationship, diff --git a/src/main/java/org/opentripplanner/updater/vehicle_position/PollingVehiclePositionUpdater.java b/src/main/java/org/opentripplanner/updater/vehicle_position/PollingVehiclePositionUpdater.java index f1355ca0fa4..17e059e3d6d 100644 --- a/src/main/java/org/opentripplanner/updater/vehicle_position/PollingVehiclePositionUpdater.java +++ b/src/main/java/org/opentripplanner/updater/vehicle_position/PollingVehiclePositionUpdater.java @@ -1,14 +1,9 @@ package org.opentripplanner.updater.vehicle_position; import com.google.transit.realtime.GtfsRealtime.VehiclePosition; -import java.time.LocalDate; import java.util.List; import org.opentripplanner.framework.tostring.ToStringBuilder; import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository; -import org.opentripplanner.transit.model.network.TripPattern; -import org.opentripplanner.transit.model.timetable.Trip; -import org.opentripplanner.transit.service.DefaultTransitService; -import org.opentripplanner.transit.service.TransitModel; import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher; import org.opentripplanner.updater.spi.PollingGraphUpdater; @@ -40,14 +35,11 @@ public class PollingVehiclePositionUpdater extends PollingGraphUpdater { public PollingVehiclePositionUpdater( VehiclePositionsUpdaterParameters params, RealtimeVehicleRepository realtimeVehicleRepository, - TransitModel transitModel + TransitService transitService ) { super(params); this.vehiclePositionSource = new GtfsRealtimeHttpVehiclePositionSource(params.url(), params.headers()); - // TODO Inject TransitService, do not create it here. We currently do not - // support dagger injection in updaters, so this is ok for now. - TransitService transitService = new DefaultTransitService(transitModel); var fuzzyTripMatcher = params.fuzzyTripMatching() ? new GtfsRealtimeFuzzyTripMatcher(transitService) : null; @@ -56,7 +48,7 @@ public PollingVehiclePositionUpdater( params.feedId(), transitService::getTripForId, transitService::getPatternForTrip, - (trip, date) -> getPatternIncludingRealtime(transitModel, trip, date), + transitService::getPatternForTrip, realtimeVehicleRepository, transitService.getTimeZone(), fuzzyTripMatcher, @@ -95,14 +87,4 @@ public void runPolling() { public String toString() { return ToStringBuilder.of(this.getClass()).addObj("source", vehiclePositionSource).toString(); } - - private static TripPattern getPatternIncludingRealtime( - TransitModel transitModel, - Trip trip, - LocalDate sd - ) { - // a new instance of DefaultTransitService must be created to retrieve - // the current TimetableSnapshot - return (new DefaultTransitService(transitModel)).getPatternForTrip(trip, sd); - } } diff --git a/src/test/java/org/opentripplanner/GtfsTest.java b/src/test/java/org/opentripplanner/GtfsTest.java index 074e31147a3..2ca5e3c5734 100644 --- a/src/test/java/org/opentripplanner/GtfsTest.java +++ b/src/test/java/org/opentripplanner/GtfsTest.java @@ -39,6 +39,7 @@ import org.opentripplanner.transit.model.basic.TransitMode; import org.opentripplanner.transit.model.framework.Deduplicator; import org.opentripplanner.transit.model.framework.FeedScopedId; +import org.opentripplanner.transit.service.DefaultTransitService; import org.opentripplanner.transit.service.StopModel; import org.opentripplanner.transit.service.TransitModel; import org.opentripplanner.updater.TimetableSnapshotSourceParameters; @@ -214,7 +215,7 @@ protected void setUp() throws Exception { .withMaxSnapshotFrequency(Duration.ZERO), transitModel ); - alertPatchServiceImpl = new TransitAlertServiceImpl(transitModel); + alertPatchServiceImpl = new TransitAlertServiceImpl(new DefaultTransitService(transitModel)); alertsUpdateHandler.setTransitAlertService(alertPatchServiceImpl); alertsUpdateHandler.setFeedId(feedId.getId()); diff --git a/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java b/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java index 79590ca2775..64560d17a21 100644 --- a/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java +++ b/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java @@ -198,7 +198,7 @@ static void setup() { var busRoute = routes.stream().filter(r -> r.getMode().equals(BUS)).findFirst().get(); TransitEditorService transitService = new DefaultTransitService(transitModel) { - private final TransitAlertService alertService = new TransitAlertServiceImpl(transitModel); + private final TransitAlertService alertService = new TransitAlertServiceImpl(this); @Override public List getModesOfStopLocation(StopLocation stop) { diff --git a/src/test/java/org/opentripplanner/routing/algorithm/filterchain/filters/transit/TransitAlertFilterTest.java b/src/test/java/org/opentripplanner/routing/algorithm/filterchain/filters/transit/TransitAlertFilterTest.java index 379eb207155..cc3e46050d0 100644 --- a/src/test/java/org/opentripplanner/routing/algorithm/filterchain/filters/transit/TransitAlertFilterTest.java +++ b/src/test/java/org/opentripplanner/routing/algorithm/filterchain/filters/transit/TransitAlertFilterTest.java @@ -12,6 +12,7 @@ import org.opentripplanner.routing.alertpatch.TransitAlert; import org.opentripplanner.routing.impl.TransitAlertServiceImpl; import org.opentripplanner.transit.model.framework.FeedScopedId; +import org.opentripplanner.transit.service.DefaultTransitService; import org.opentripplanner.transit.service.TransitModel; class TransitAlertFilterTest implements PlanTestConstants { @@ -20,7 +21,9 @@ class TransitAlertFilterTest implements PlanTestConstants { @Test void testFilter() { - var transitAlertService = new TransitAlertServiceImpl(new TransitModel()); + var transitAlertService = new TransitAlertServiceImpl( + new DefaultTransitService(new TransitModel()) + ); transitAlertService.setAlerts( List.of( TransitAlert diff --git a/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java b/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java index 0474c328162..01128629bb0 100644 --- a/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java +++ b/src/test/java/org/opentripplanner/updater/alert/AlertsUpdateHandlerTest.java @@ -25,13 +25,16 @@ import org.opentripplanner.routing.alertpatch.TransitAlert; import org.opentripplanner.routing.impl.TransitAlertServiceImpl; import org.opentripplanner.routing.services.TransitAlertService; +import org.opentripplanner.transit.service.DefaultTransitService; import org.opentripplanner.transit.service.TransitModel; public class AlertsUpdateHandlerTest { private AlertsUpdateHandler handler; - private final TransitAlertService service = new TransitAlertServiceImpl(new TransitModel()); + private final TransitAlertService service = new TransitAlertServiceImpl( + new DefaultTransitService(new TransitModel()) + ); @BeforeEach public void setUp() {