Skip to content

Share the GML reader proxy, not the whole reader factory - #404

Merged
JornC merged 2 commits into
aerius:mainfrom
JornC:reader-factory-per-importer
Nov 24, 2026
Merged

JornC merged 2 commits into
aerius:mainfrom
JornC:reader-factory-per-importer

Conversation

@JornC

@JornC JornC commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

This refactors the part where the cheap factory, instead of the expensive reader proxy, is turned into a singleton.

When running calculator's integration tests one after another in the same JVM (i.e. reuseForks=true), the factory would be created once, and for different helpers would return the same factory, using the first helper. This caused tests to fail because those helpers (a class which provides emission categories/factors etc.) would be different in the tests (they are not different in production, hence it leads to no issues there).

This changes this factory to turn the GMLReaderProxy into a singleton, rather than itself, which makes all calls work with the helper they pass, even when they pass helpers which are actually different.

This change enables running integration tests on the same JVM, creating a ~4 minute speedup (which brings it in the ballpark of where running them for each PR is somewhat feasible)


LLM-generated technical summary

Before this change, GMLReaderFactory.getFactory(gmlHelper) stored the whole factory in a static field on the first call, including that first GMLHelper, and returned the same instance on every later call. createReader builds each GMLReader with the factory's helper, and the reader uses it for feature conversion (GMLConversionData: sector ids, default characteristics, inland shipping) and for enforceEmissions; createValidationHelper() also comes from it. ImaerImporter(GMLHelper) calls getFactory, so every importer in a JVM converted, computed emissions and validated with the helper of the first importer, whatever helper it was given.

The static field now holds only the GMLReaderProxy: the 12 version reader factories, each of which compiles its XSD schema and builds a legacy code converter from the helper's legacy code methods. That is the costly part, and it is still built once per JVM. getFactory returns a new GMLReaderFactory(gmlHelper, sharedReaderProxy) per call, which only sets two fields. The legacy code conversions still come from the first helper; the Javadoc says so. In AERIUS Calculator they come from the same database tables for every helper. No public signatures change. getFactory now returns a new instance per call; its callers (ImaerImporter and two tests) do not rely on getting the same instance.

No effect is expected in production. Calculator creates a helper per import, with NL or EN categories depending on the locale. Those two sets differ only in description text, and the reader only uses ids, codes and emission factors.

Found with Maven Failsafe reusing one JVM per module in calculator: SRMSourceConversionVerificationIT, which uses categories built in code with different emission factors, failed whenever ImporterFacadeIT, RemovedVehicleCodeIT or ImportSummaryGeneratorJsonIT had run earlier in the same JVM. With this change installed locally, it passes after each of those three, and all 10 integration test classes of aerius-commons-imaer pass in three random orders. The imaer-java test suite passes (1,213 tests).

Calculator uses 6.0.2-2-SNAPSHOT, which is what main builds, so it picks this up without a version bump once the snapshot is published.

Written at 8864259.

@JornC
JornC requested a review from BertScholten November 17, 2026 06:49

@BertScholten BertScholten left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it now be OK to get rid of the public constructor (and get rid of the whole cache thing in AssertGML), and perhaps make the remaining constructor private to avoid someone using it that way?

@JornC

JornC commented Nov 24, 2026

Copy link
Copy Markdown
Member Author
  • Removed the public constructor
  • Tried the cache removal but tests kept failing, I'd still like to do it but not here
  • The last remaining constructor can only be package-private atm because tests use it

@JornC
JornC requested a review from BertScholten November 24, 2026 11:49

@BertScholten BertScholten left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@JornC
JornC merged commit 1028133 into aerius:main Nov 24, 2026
1 check passed
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.

2 participants