Repository navigation
Keep ${random.*} values stable when a property source is processed - #14109
Conversation
|
|
||
| if (value != null && entries != null) { | ||
| // remembered apart from the placeholder cache, which a processed property source invalidates | ||
| value = withRandomValues(currentState, name, value, lookupRevision); |
There was a problem hiding this comment.
this is the wrong fix IMO, right now the design of PropertySourcePropertyResolver is such that it has no awareness of "random" and what it means.
this is encapsulated by RandomPropertyExpressionResolver which implements PropertyExpressionResolver
so RandomPropertyExpressionResolver needs to take part in remembering these values such that the implementation split is correct.
This change hard codes of randomness and duplicates all the patterns defined in RandomPropertyExpressionResolver here
There was a problem hiding this comment.
Agreed. I'll rework it so that RandomPropertyExpressionResolver does the remembering and PropertySourcePropertyResolver knows nothing about random values:
RandomPropertyExpressionResolverremembers what it draws, per placeholder and per expression of a fallback chain, and converts the remembered value to the requested type on each lookup.- It can't remember by the expression alone, since
a=${random.port}andb=${random.port}would then get the same port. SoPropertySourcePropertyResolverpasses a context throughDefaultPropertyPlaceholderResolverto the expression resolvers, through an internal hook: where the text was found (the property name, and the index of a list element or the key of a map value), and a memory that a refresh replaces and that adding or removing a property source at run time clears, as before. Other expression resolvers are called as they are now. PropertySourcePropertyResolverno longer parses anything: fallbacks such as${app.port:random.port:8080}and the order of errors follow from resolving the placeholders as usual.
#14108 (4.10.x) gets the same change, which also moves the random memory that #13896 added to PropertySourcePropertyResolver on 4.10.x into RandomPropertyExpressionResolver.
Let me know if you had a different split in mind.
There was a problem hiding this comment.
Reworked along these lines. RandomPropertyExpressionResolver now remembers what it draws, through a new internal RememberingPropertyExpressionResolver that DefaultPropertyPlaceholderResolver passes a memory for each expression. PropertySourcePropertyResolver only passes where the text was found and keeps the memory with its state; it no longer knows about random values or parses placeholders. The description is updated.
Since 5.2.0 processing a property source clears the placeholder cache, which was what kept a
random value stable. A lookup after any property source had been processed drew a new value,
and a property source that read a random property while the property sources were processed,
when the environment starts or refreshes, got a value that later lookups did not return.
As on 5.1.x, the values now stay the same. RandomPropertyExpressionResolver remembers what it
draws, through the new internal RememberingPropertyExpressionResolver. When the property resolver
resolves the placeholders of a property, it passes such a resolver a memory for each expression:
kept in the state of the resolver by the place of the text, the name it was looked up by and its
place in a list or in a map in a list, and by the position of the expression in the text. The
property resolver itself knows nothing of random values, and the placeholders are resolved as
before. A random fallback, such as ${app.port:random.port:8080}, is only drawn when the property
before it is not set, and keeps the type of its value. A text that also names a property, such as
http://${app.host}:${random.port}/, keeps its random values while the property is resolved again.
The memory is kept when a property source is processed, drawn again when the text changes, and
forgotten when a property source is added to or removed from a running environment, as before
5.2.0. A refresh publishes another state, so it draws the random values again, as before. The
state holds one entry per looked-up name or list element whose text holds a random expression.
Like the caches, the memory only takes a new text from a lookup that has read the catalogs as
they still are, from the thread that is processing a property source, or when nothing is
remembered yet, so a lookup that a replaced text overtakes does not replace what a later lookup
has remembered. A running environment forgets the values before it changes the revision, so no
lookup caches a value for the new revision that is forgotten after it.
Fixes #13913
ab22ee5 to
bacaa2a
Compare
|



Fixes #13913. The 4.10.x counterpart is #14108.
Why
A property whose value holds a random expression (
${random.uuid},${random.port}, …) is meant to resolve to the same value on every lookup until the environment is refreshed. The cache of resolved placeholders was what kept it stable.Since 5.2.0, #12864 clears that cache after every property source the resolver processes. With #14080 (5.2.18) a cached value is also only used for the revision it was read for, and processing a property source changes the revision. So the next lookup after any property source has been processed resolves the expression again and draws a new value.
Since when, by symptom
PropertySourcePropertyResolverreturns another random value once an unrelated property source is added (issue, step 1)Environmenton start and on every refreshhttp://${app.host}:${random.port}/['${random.uuid}']${app.port:random.port:8080}withapp.portnot setMeasured with the issue's steps and probes on each version. #12864 is in every 5.2.x release and in no 5.1.x release. 4.10.32 brought the same cache clear to 4.10.x, with a memory for texts made only of random expressions; #14108 covers the last three rows on 4.10.x. The current 5.3.x branch behaves like 5.2.x on the issue's steps.
These behave as before, on every version above:
getProperties()or a placeholder resolved directly, a new value is drawn every time.Change
RandomPropertyExpressionResolverremembers what it draws. It implements the new internalRememberingPropertyExpressionResolver: aPropertyExpressionResolverthatDefaultPropertyPlaceholderResolverpasses a memory for each expression of a placeholder it resolves for a property. Other expression resolvers are called as before.PropertySourcePropertyResolverknows nothing of random values. When it resolves the placeholders of a property, it passes where the text was found: the name it was looked up by, and the index of a list element or the key of a map value in a list. The places are kept as structured keys, so map keys such as1and"1", or a keyid[0]next to a listid, stay apart.${app.port:random.port:8080}is only drawn whenapp.portis not set, andapp.portstill wins once it is. A random expression that cannot be resolved fails as before, after a missing property before it, and nothing is remembered for it.${random.uuid}, or${missing:random.uuid:default}, read as anObjectis aUUID, as before.${app.host}above, is resolved again by every lookup that does not find the value cached, so it follows the property sources.ConcurrentHashMap.compute, so another lookup at the same place cannot get in between.addPropertySourceandremovePropertySourceon a running environment clear the memory, as they cleared the cache before 5.2.0. They clear it before they change the revision, so a lookup that reads the new revision finds the memory already cleared, and the value it caches is the one the memory keeps.Memory
The original incident was an
OutOfMemoryErrorunder a refresh every minute. The memory holds one entry per looked-up name, or list element, whose text holds a random expression: the same names the placeholder cache keeps. A refresh replaces it with the state.Probe: 5,000
refreshAndDiff()with-Xmx64m. Between the refreshes, a property source is replaced by the same name with a different random text each time, and property sources are added and removed by eight names. A second thread looks the random properties up without pause.Tests
PropertySourcePropertyResolverSpec:ConcurrentModificationException, also when the list holds no random expression;DefaultEnvironmentSpec:5.2.x, and the race tests fail on earlier versions of this change. The ones that check behaviour kept from before (a list in a list, an incomplete placeholder, a random fallback not drawn while the property before it is set, a property source added or removed at run time) pass on5.2.xas well.micronaut-inject: 510 tests;micronaut-inject-java: 5,370 tests (9 skipped);micronaut-context: 58 tests (1 skipped);micronaut-runtime: 112 tests (3 skipped);checkstyleMainfor inject, which the build disables, run with it enabled: no new violation.