Skip to content

Cleanup CO₂ emissions #6558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Apr 10, 2025
Merged

Conversation

t2gran
Copy link
Member

@t2gran t2gran commented Mar 19, 2025

Summary

This PR refactor the GTFS GraphBuilder and clean up the code. If contains a few bug fixes, the most important is
to make the emission sandbox module work with other data sources than just the file system. At Entur we uses Google Storage.

Some of the main changes:

  • Support for data-sources like Google cloud storage
  • Use dependency injection to inject Emission itinerary decorator into the itinerary filter-chain, the main OTP code does not need to know about any of the emission types like EmissionService or the DecorateWithEmission.
  • Renamed types to follow naming conventions
  • Make the logic for resolving feedId public available, so the emission graph builder module can reuse it.
  • Refactor the Graph build parameters so we can enforce "not null" for source. This is done by splitting the GTFSFeedParameters in two: GtfsFeedParameters and GtfsDefaultParameters. I used inheritance here, but delegation might have beed equally good. The nice thing about inheritance is that the parameters align with the structure of the build-config.

Issue

🟥 No issue for this, this is mostly cleanup code and a few bug fixes

Closes #45

Unit tests

✅ Some new tests are added, while emission tests no focus on business - test-coverage should be higher.

Documentation

✅ Relevant JavaDoc is updated

Changelog

🟥 Not relevant.

Bumping the serialization version id

✅ Needed, the emission repository is changed.

t2gran added 24 commits March 14, 2025 15:54
not in GraphBuild only there should be a "OTP Global" scope
name-space or FeedInfo.
- I used inheritance to do this, delegation would probably be at least as good.
- We can now do proper validation of the required source parameter.
- This add support for using all data-sources, not just the file system
@t2gran t2gran added Technical Debt bump serialization id Add this label if you want the serialization id automatically bumped after merging the PR Skip Changelog labels Mar 19, 2025
@t2gran t2gran added this to the 2.8 (next release) milestone Mar 19, 2025
@t2gran t2gran requested a review from a team as a code owner March 19, 2025 15:16
@t2gran
Copy link
Member Author

t2gran commented Mar 19, 2025

I see that there is a problem with the data-fetchers for Grams - I will investigate.

Copy link
Member

@optionsome optionsome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this but the emissions building failed with the following stack trace:

16:46:00.173 ERROR [main]  (OTPMain.java:61) An uncaught error occurred inside OTP: Cannot invoke "java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry)" because "this.zipFile" is null
java.lang.NullPointerException: Cannot invoke "java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry)" because "this.zipFile" is null
	at org.opentripplanner.datastore.file.ZipFileDataSource.entryStream(ZipFileDataSource.java:75)
	at org.opentripplanner.datastore.file.ZipFileEntryDataSource.asInputStream(ZipFileEntryDataSource.java:56)
	at org.opentripplanner.gtfs.graphbuilder.GtfsBundle$1.getResource(GtfsBundle.java:63)
	at org.opentripplanner.gtfs.graphbuilder.GtfsFeedIdResolver.fromGtfsFeed(GtfsFeedIdResolver.java:39)
	at org.opentripplanner.gtfs.graphbuilder.GtfsBundle.getFeedId(GtfsBundle.java:95)
	at org.opentripplanner.ext.emissions.EmissionsGraphBuilder.buildGraph(EmissionsGraphBuilder.java:49)
	at org.opentripplanner.graph_builder.GraphBuilder.run(GraphBuilder.java:202)
	at org.opentripplanner.standalone.OTPMain.startOTPServer(OTPMain.java:144)
	at org.opentripplanner.standalone.OTPMain.main(OTPMain.java:56)

The GTFS data I used can be downloaded from here

/**
* This class allows updating the graph with emissions data from external emissions data files.
*/
public class EmissionsGraphBuilder implements GraphBuilderModule {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this Gram vs Grams dicussion. Should we call all these classes EmissionSomething or EmissionsSomething?

Copy link
Member Author

@t2gran t2gran Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue for this #6571. I think we should use EmissionSomeThing and not EmissionsSomeThing. I would like to do it in a separate PR.

Co-authored-by: Joel Lappalainen <[email protected]>
@t2gran t2gran changed the title Cleanup co2 emissions Cleanup CO₂ emissions Mar 25, 2025
@t2gran
Copy link
Member Author

t2gran commented Mar 25, 2025

The failure (stacktrace) is caused by the zip-file being closed after the GTFS data is read, before the emissions data is loaded. I will look into how this should be resolved. The best solution is probably to clone and open DataSource, but we need to be careful - opening two connections to a connection based data-source (Google Cloud) might cause problems.

@t2gran
Copy link
Member Author

t2gran commented Mar 25, 2025

The failure was a bug in the ZipFileDataSource the wrapper is perfect for keeping information about the open/closed connections - so if two graph modules uses the same CompositeDataSources then it should be reopened the second time it is accessed. I checked all the implementation we have, it was only the ZipFile version who had this error. It does not clean up its internal state when closed.

@t2gran t2gran requested a review from optionsome March 25, 2025 22:19
t2gran added 2 commits March 25, 2025 23:21
…odule

The ZipFileDataSource did not cleanup its internal state when it was closed, so
opening it a second time failed. The OTP DataSource framework is tailored to
address this kind of features, so the fix is simple. None of the other composite
data sources has the same bug.
@t2gran t2gran force-pushed the add_co2_emmistions branch from d63570a to 6c5d37a Compare March 25, 2025 22:22
return store.stopConsolidation();
}

private ConfiguredDataSource<OsmExtractParameters> mapOsmData(DataSource dataSource) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be lower in the file as it's private. The diff is already difficult to read so it shouldn't matter much if you move one more method a bit.

optionsome
optionsome previously approved these changes Mar 28, 2025
Copy link
Member

@optionsome optionsome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just fix the formatting issue.

@leonardehrenfried
Copy link
Member

@t2gran you need to merge dev-2.x because a test has moved and the merge of your branch and dev-2.x doesn't compile anymore.

Copy link
Contributor

@vpaturet vpaturet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, no additional comment from my side.

@t2gran t2gran merged commit 9d3ff48 into opentripplanner:dev-2.x Apr 10, 2025
6 checks passed
t2gran pushed a commit that referenced this pull request Apr 10, 2025
@t2gran t2gran deleted the add_co2_emmistions branch April 11, 2025 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump serialization id Add this label if you want the serialization id automatically bumped after merging the PR Skip Changelog Technical Debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What does OTP Deployment look like?
4 participants