Repository navigation
Process a property source added just before a refresh reads only once, in its order - #14171
Conversation
…, in its order Since 5.2.0 a refresh holds the catalog lock for its whole read. A property source that another thread added just before the refresh copied the registered ones waited for the lock, was processed by the refresh in its order, and was then processed again on top once the refresh had published. Until the next refresh it overrode property sources of a higher order, such as environment variables or system properties. A start or a refresh now numbers the snapshot it takes of the registered property sources. Once it has processed all of them into the catalogs it noted itself on, those catalogs keep its snapshot, so the snapshot is published, kept and dropped with them: a refresh that fails keeps the published catalogs and their snapshot, and a stop resets them without one. An add reads the count of snapshots when it registers its property source, under the monitor of the registered property sources, which the read also holds for its snapshot. It leaves the property source as processed when the catalogs it writes into keep a snapshot taken after that, which holds this instance, and processes it as before otherwise. Adds still wait for a refresh, as before. Fixes #14159
e405130 to
abadbc6
Compare
graemerocher
left a comment
There was a problem hiding this comment.
Thanks, this looks correct to me. Keeping the snapshot on ResolverState so it is published, kept and dropped with the catalogs is a good fit for 5.2.x, where a start writes straight into the published catalogs and can have them replaced mid-read. I walked through the cases in the table (successful/failed refresh, reset() inside and outside a refresh, a start overtaken by a stop or refresh, the copy made by addToCopy during a start) and the number > snapshotsWhenRegistered && contains(instance) rule holds in each. The lock order is unchanged: processAddedPropertySource takes only catalogLock, never under the propertySources monitor, and re-entering it in addToCopy is fine. The tests wait on BLOCKED plus registration rather than sleeping, which is what I'd want for this kind of race.
A few small points inline, none blocking.
…ves its snapshot outside a finally The add logged that it was processing its property source also when the read it waited for had processed it already. It now logs that in the branch that processes, and the skip at debug with the snapshot that processed it. The snapshot's Javadoc says that it keeps the property sources it holds reachable while its catalogs are published.
|



Fixes #14159
The 5.2.x version of #14163. It follows the same rule, but keeps the record in a different place, because 5.2.x reads its property sources differently: see How it differs from 4.10.x.
The problem
Since 5.2.0 (#13079), a refresh holds the catalog lock for its whole rebuild, and builds the new catalogs apart from the published ones. Another thread can add a property source just before the refresh copies the registered property sources. When that happens:
Until the next refresh, a property source of a lower order then overrides ones of a higher order, such as environment variables or system properties.
The fix
Snapshots are numbered. Each start or refresh numbers the copy it takes of the registered property sources. The snapshot holds the property sources it took from there and processes, by identity.
The catalogs keep the snapshot they were filled from. Before a read processes its property sources, it notes its snapshot on the catalogs it writes into, and clears the one they kept. Once it has processed all of them into those same catalogs, they keep its snapshot.
An add skips only what those catalogs already hold. When the add gets the catalog lock, it leaves its property source as processed only if the catalogs it writes into keep a snapshot that:
In every other case the add processes the property source as before.
Registration and snapshot can't interleave. An add already registers its property source under the monitor of the registered property sources map, and a read already copies them under it. The add now reads the snapshot count there, and the read counts there. So either the snapshot holds the source, or the add's count already includes the snapshot. No new lock.
The rule is that the snapshot the catalogs keep always describes those catalogs. Because it lives on the catalogs (
ResolverState), it is published, kept and dropped with them, with no code at those places:state = writeStatewriteState = statereset()outside a refresh, which is a plainstop()A
stop()or a refresh inside a refresh resets only the catalogs being built, so the published ones keep their snapshot.How it differs from 4.10.x
registrationLockisn't needed.DefaultEnvironmentholds the resolver rather than extending it, so the add calls a new package-privateprocessAddedPropertySourceinstead of overriding a hook.What does not change
PropertySourcePropertyResolvernever keeps a snapshot.javap -protectedofPropertySourcePropertyResolverandDefaultEnvironmentis identical to 5.2.x.getConvention()before it takes the catalog lock, as 5.2.x did before Keep lookups from failing while a property source is added to the running environment #14167.Tests
AddBeforeRefreshReadsSpechas 9 cases. Each holds the catalog lock through the resolver'sdiff, lets another thread's add register its property source and wait for the lock, and then runs the refresh, stop or start.4 reproduce #14159. They fail on 5.2.x, each with the lower-order property source on top, and pass with the fix:
refresh()and withrefreshAndDiff();5 guard existing behaviour. They pass on 5.2.x and with the fix:
Also run, with JDK 25:
io.micronaut.context.env.*: 313 pass.:micronaut-context:test: 58 pass (1 skipped).:micronaut-runtime:test: 112 pass (3 skipped).A behaviour comparison with 5.2.x
To check that the fix changes nothing besides #14159, I used a local recorder (not committed), as on #14163. It runs 90 scenarios on 5.2.x and on this branch and writes one outcome per scenario:
refresh()orrefreshAndDiff().refreshAndDiff()reports;The expected differences were written down before the change. The result is exactly those 12 differences, all of them #14159 itself: the cases where the added source has the lower order and 5.2.x processes it a second time on top.
In each of them, only the competing key changes, to the higher-order value. Everything else, and the state after one more refresh, is identical to 5.2.x. The recorders that #14162 and #14167 used give the same outcomes on 5.2.x and on this branch.
Known limit
Two adds of different instances under one name around a refresh: the first registers X and waits, and the second replaces it with Y before the refresh copies the registered property sources, then waits too. The refresh reads Y in its order, Y's add leaves it as read, and X's add processes X on top. On 5.2.x the two adds processed X and Y on top in whichever order they got the lock, so either could win; now X does. Making the add of a replaced property source process nothing belongs to the refresh redesign. #14163 behaves the same.
With #14167
#14167 merged first, and this branch is rebased on it. Since #14167, an add writes into a copy of the catalogs,
new ResolverState(published), and publishes it:pendingSnapshotandprocessedSnapshot, as the catalogs it holds are the same ones, written on. Without them, the next add would see no snapshot and process its property source on top again.Not in this PR
These are related refresh issues on 5.2.x, kept separate so that this PR stays small:
Environment#stop()orstart()called from another thread during a refresh waits for the whole refresh, and can deadlock with it #14141