Repository navigation
8.0 (fix): Cache with the class's own cache manager where a parameter is named like it - #17151
Conversation
…ike it The code the cache annotations generate read the cache manager and the key generator by their property names, grailsCacheManager and customCacheKeyGenerator. A parameter of the annotated method with one of those names hides the property in the method, so the method cached in the cache manager passed to it, or failed with a NullPointerException where the key generator passed was null. Both are now read through their getters, which a parameter cannot hide. ShadowedPropertiesSpec compiles a class, statically and dynamically, whose @Cacheable, @cACHEpUT and @CacheEvict methods take parameters of both names, and checks they act on the class's own cache manager; before the change both modes failed, and with only the key generator read through its getter they cached in the cache manager passed.
matrei
left a comment
There was a problem hiding this comment.
Review of #17151 at 41e5267ac2 (base 8.0.x): one commit on 5bdcbc2e82. Since #16980 merged, it conflicts with the current tip, fca95de32c. The conflict is only in the imports of AbstractCacheTransformation: keep both block and callThisX.
What I ran, on Java 21:
- The fix, A/B at this head: with the four transformations of
8.0.xput back, both features ofShadowedPropertiesSpecfail, statically and dynamically, with theNullPointerExceptionon the key generator passed asnull. With the PR they pass. - A merge with the current
8.0.x, with the conflict resolved as above: thegrails-cachesuite, without the build cache, 124 tests, no failures. That includes the 2 features ofShadowedPropertiesSpec, the 4 ofMissingCacheSpecand the 70 ofGrailsCacheManagerAliasMatrixSpec. A probe of the case in point 1 fails there. - That merge with the line from point 1: the same 124 tests and the probe pass, as do the
grails-test-examples/cacheapp'stest(19) andintegrationTest(25), and theCachingSpecofgrails-test-examples/app1(20).
@codeconsole, thanks for splitting this out. The change is right; it only needs the merge with #16980 finished.
1. The missing-cache report still reads grailsCacheManager by name
#16980 merged first, so this is the PR that switches the report over, as its description says. After the merge, whereCacheExists still passes varX(GRAILS_CACHE_MANAGER_PROPERTY_NAME) to MissingCacheReporter.report (AbstractCacheTransformation.groovy:200 on 8.0.x). So in a method whose cache the class's cache manager doesn't have, a parameter named grailsCacheManager is what gets reported. A probe at the merge, statically and dynamically:
- passed
null, the call fails withNullPointerException: Cannot invoke "Object.getClass()" because "cacheManager" is null, fromMissingCacheReporter.report; - passed another cache manager, the warning names that one (
NoOpCacheManager), not the class's, and the reporter records the cache as reported for the wrong cache manager.
None of the existing tests reaches this, so the merge passes without the change. The fix is one line:
args(grailsCacheManagerExpression(), constX(cacheName), classX(classNode.plainNodeReference)))A feature like this in ShadowedPropertiesSpec covers it. It fails without the line, in both modes, with the NullPointerException above, and passes with it:
@Unroll
void 'a cache the class\'s cache manager does not have is reported for that cache manager, compiled #mode'() {
given: 'the class created as a bean beside its cache manager, which has no cache named missing'
CacheManager cacheManager = new ConcurrentMapCacheManager('configured')
GenericApplicationContext context = new GenericApplicationContext()
AnnotationConfigUtils.registerAnnotationConfigProcessors(context)
context.registerBean('grailsCacheManager', CacheManager, { -> cacheManager } as Supplier<CacheManager>)
context.registerBean('service', (Class<Object>) compile(mode))
context.refresh()
def service = context.getBean('service')
LogCapture log = new LogCapture(MissingCacheReporter, Level.WARN)
when: 'the method is called with no cache manager, then with another one'
List<Object> results = [service.uncached(1, null, null), service.uncached(1, new NoOpCacheManager(), null)]
then: 'it ran uncached both times, and the class\'s cache manager was reported once'
results == [2, 2]
service.calls == 2
log.events*.formattedMessage == ["ShadowingService does not cache with missing: its cache manager, ${ConcurrentMapCacheManager.name}, ".toString() +
'has no cache of that name, so the methods run uncached. Configure the cache in that cache manager.']
cleanup:
log?.close()
context?.close()
where:
mode << ['statically', 'dynamically']
}with this method in ShadowingService:
@Cacheable('missing')
int uncached(int x, CacheManager grailsCacheManager, GrailsCacheKeyGenerator customCacheKeyGenerator) {
calls++
x * 2
}and imports of Level, NoOpCacheManager, LogCapture and MissingCacheReporter, as MissingCacheSpec has them.
2. FYI: 7.0.x has the same code
On 7.0.x, the four transformations and the trait are identical to those of 8.0.x before #16980. So the bug is there too, and this commit applies there as it is. Nobody has reported it, and a parameter with one of those names is rare, so I'm fine with fixing it on 8.0.x only. If you'd rather have it on 7.0.x, the merge-up into 8.0.x then needs the line from point 1.
Verified as correct
- Both getters come from
GrailsCacheManagerAware, which is unchanged, and a parameter can't hide a method called onthis.AbstractMethodDecoratingTransformationcompiles the generated method statically in both modes (compileMethodStatically), so where no parameter has one of those names, the old property read resolved to the same getters. The existing tests pass unchanged. - The signature of
declareCachetaking aVariableExpressionstays, and so do the public constants, so a subclass compiled against 8.0.0 still links. After point 1, neitherGRAILS_CACHE_MANAGER_PROPERTY_NAMEnorGRAILS_CACHE_KEY_GENERATOR_PROPERTY_NAMEis used any more; keeping them is right on a patch line. - It's a bug fix: nothing documents that a parameter picks the cache manager or the key generator, so it needs no upgrade note.
- The one failure TestLens reports,
RedirectWithAndWithoutParamsFunctionalSpecinhibernate7-app1on the Groovy snapshot canary, is unrelated tograils-cache.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A service with its own cache manager still caches in the bean of that name.
| * @return the call of the getter on {@code this} | ||
| */ | ||
| protected static Expression grailsCacheManagerExpression() { | ||
| callThisX('getGrailsCacheManager') |
There was a problem hiding this comment.
A service keeps its own cache manager in a field of that name, and a method parameter has the same name. A call passes a third manager. The entry is stored in the bean of that name, not in the service field. Use the service field.
There was a problem hiding this comment.
Thanks, you're right, and it went further than a parameter. Where a class declares its own private or protected grailsCacheManager field, it has no getter of its own. So getGrailsCacheManager() is the trait's and returned the bean, even with no parameter of that name, where 8.0.0's bare name read the class's field. The same went for a private customCacheKeyGenerator field. A Groovy property was already right, since its own getter wins.
In 98ed17c the generated code reads this.grailsCacheManager and this.customCacheKeyGenerator instead. That's the class's own field where it declares one, otherwise the trait's getter, and a parameter can't hide it. Without a parameter, it matches 8.0.0 in every case I compared. That includes a superclass that declares the field, private, protected or as a property, which reads the bean in both.
ShadowedPropertiesSpec compiles each of those classes statically and dynamically, in f86a22c and 320d430. Each calls @Cacheable, @CachePut, @CacheEvict by key and of every entry, and a missing cache, without the parameters and with them, passed another cache manager and key generator. It checks where the entries land and which are evicted, which key generator made each key, and which cache manager is reported. Before 98ed17c the private and protected cache-manager rows and the private key-generator row fail in both modes.
…-properties-8.0.x # Conflicts: # grails-cache/src/ast/groovy/org/grails/plugin/cache/compiler/AbstractCacheTransformation.groovy
…meter is named like it The report of a cache the cache manager does not have, which came with the merge of 8.0.x, read grailsCacheManager by its name, so a parameter of that name was reported in place of the class's cache manager: passed null, the call failed with a NullPointerException, and passed another cache manager, that one was reported and remembered as reported. It now reads the cache manager the way the rest of the generated code does. ShadowedPropertiesSpec calls a method whose cache is missing with no cache manager and with another one, statically and dynamically, and checks the class's cache manager is reported once.
The code the cache annotations generate read the cache manager and the key generator through the getters of GrailsCacheManagerAware, so a class that declares its own private or protected grailsCacheManager field cached in the bean of that name, with or without a parameter of that name, and one with its own private customCacheKeyGenerator field never used it. 8.0.0 read the bare names, which were the class's fields there. They are now read as properties of this, which are the class's field where it declares one, and otherwise the trait's getter, and which a parameter cannot hide. ShadowedPropertiesSpec compiles classes, statically and dynamically, that declare a private, a protected or a property cache manager, a private or a property key generator, or whose superclass declares a private cache manager, and calls their methods with and without parameters of those names, passed another cache manager and key generator. It checks where they cache, which key generator makes the keys, and which cache manager a missing cache is reported for. Before the change the private and protected cache managers and the private key generator failed, in both modes.
ShadowedPropertiesSpec's feature for a class that declares its own cache manager or key generator also puts and evicts every entry, with parameters of those names, and checks both act on the cache manager the class means and that the put's key comes from the key generator it means. With only @cACHEpUT and @CacheEvict reading the cache manager through the trait's getter, the private and protected cache manager rows fail.
…s protected field or property ShadowedPropertiesSpec's feature for a class that declares its own cache manager or key generator now calls each annotation without the parameters and with them: @Cacheable, @cACHEpUT, @CacheEvict by key and of every entry, and a missing cache. Each key comes from a key closure, so the eviction by key is checked to remove just its entries, which run again, while the ones put still come from the cache; the key generator the class means is checked to have made every key, and the passed one none. The cache manager passed is of a class of its own, so a report of it is told apart from one of the bean in every row. Rows for a superclass's protected field and property join the private one, and read the bean, as in 8.0.0. The feature for parameters of both names also evicts by key, passed a null key generator, which failed with a NullPointerException while the generated code read the key generator by its bare name.
|
Thanks @matrei.
|
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. A class uses its own cache manager, and a parameter of that name no longer hides it.
sbglasius
left a comment
There was a problem hiding this comment.
This is an AI assisted review
Reading both through this fixes the shadowing cleanly, and the spec covers every annotation, both compile modes, and own vs. superclass fields. LGTM.
matrei
left a comment
There was a problem hiding this comment.
Second round, at 320d430 (base 8.0.x). It merges cleanly with today's 8.0.x (99aedeb), which changed nothing in grails-cache after your merge.
Round 1's point is fixed: 766ae27 reports the class's cache manager, and the feature from my review is in. James's case also holds. In round 1 I wrote that the old read resolved to the same getters wherever no parameter has those names. That was wrong for a class that declares its own field. With the getters of 766ae27, a class with its own private or protected grailsCacheManager cached in the bean, and one with its own private customCacheKeyGenerator made its keys with the trait's generator, without any parameter of those names. 98ed17c fixes that. Thanks for following it up.
What I ran, on Java 21, with the build cache off:
- The suite: the
grails-cachetests at the head, 142, all pass. ShadowedPropertiesSpecwith the older transformations:- With those of 8.0.x, all 20 features fail.
- With those of 766ae27, the 6 features for a private or protected cache manager field and a private key generator field fail.
- A temporary spec that compiles 17 class shapes, statically and dynamically. Each one calls
@Cacheablewith a key closure and without,@CachePut,@CacheEvictby key and of every entry, and a method whose cache is missing. It calls them once without parameters and once with parameters namedgrailsCacheManagerandcustomCacheKeyGenerator, passed another cache manager and another key generator. It records which cache manager each call acts on, which key generator makes the keys, and which cache manager is reported.- The shapes: besides the rows of
ShadowedPropertiesSpec, it has a protected key generator field, a private static cache manager field, a getter the class declares for each of the two, a superclass's key generator fields and property, and a superclass's getter. - Without the parameters, the head gives the same results as 8.0.x in all 34 cases.
- With the parameters, 8.0.x acts on the cache manager and the key generator passed. The head acts as it does without them.
- With the getters of 766ae27, the 10 cases of a private, protected or static field went to the bean or to the trait's key generator.
- The shapes: besides the rows of
CI: 89 checks pass, 9 skipped.
Optional
- The Javadoc of
grailsCacheManagerExpression()and the description say "a field of that name the class declares, otherwise whatGrailsCacheManagerAwareholds, through its getter". A class that declares its owngetGrailsCacheManager(), without a field, caches with what that getter returns, here and on 8.0.x. The same goes forgetCustomCacheKeyGenerator(). So maybe: "otherwise its getter, which is the trait's unless the class declares its own". - The
propXimport goes afterparams.
🔎 No tests executed 🔎🏷️ Commit: 910124b Learn more about TestLens at testlens.app/docs. |
|
@matrei Thanks for the second review. Both optional suggestions are addressed in 910124b. The Javadoc and PR description now explain that property resolution can use a class-defined getter, with the trait providing the default getter. The |
The code the cache annotations generate read the class's cache manager and key generator by their bare names,
grailsCacheManagerandcustomCacheKeyGenerator. A parameter of the annotated method with one of those names hides the name inside the method. So a method like this cached in the cache manager it was passed, not the class's own:A parameter named
customCacheKeyGeneratorthat was passednullmade every call that makes a key fail with aNullPointerException. So did a parameter namedgrailsCacheManagerpassednullto a method whose cache the cache manager doesn't have, in the report of that missing cache.The generated code now reads both as properties of
this:this.grailsCacheManagerandthis.customCacheKeyGenerator, which a parameter cannot hide. That is what the bare names meant without such a parameter:getGrailsCacheManager()orgetCustomCacheKeyGenerator(). The correspondingGrailsCacheManagerAwaregetter supplies the value by default, including when a superclass only declares the field, as in 8.0.0.declareCachenow takes any expression for the cache manager. The signature taking aVariableExpressionstays, as do the property-name constants, for subclasses compiled against 8.0.0.Tests
ShadowedPropertiesSpeccompiles cache-aware classes statically and dynamically.@Cacheable,@CachePut,@CacheEvictby key and of every entry take them and act on the class's cache manager, not the one passed in, with anullkey generator passed. Before the change both modes failed. With only the key generator read the new way, they cached in the cache manager passed in.nulland with another cache manager, runs uncached and reports the class's cache manager once.All
grails-cachetests pass, as do thegrails-test-examples/cacheapp's tests and theCachingSpecofgrails-test-examples/app1.