Repository navigation
9.0: Cache annotations use grailsCacheManager, as the cache tags do - #17157
codeconsole wants to merge 3 commits into
Conversation
…tags do The cache annotations and GrailsCacheAdminService were given a cache manager by type, so a cache manager of the application's own marked primary or with a @priority took them from grailsCacheManager, which the cache tags and the plugin's startup hook use by name, and with grails.cache.enabled: false they took whatever single or primary cache manager the context had, such as Spring Security's cacheUsers one. The trait's field is now qualified with the name grailsCacheManager, so Spring injects the bean of that name, or the bean with that alias, and no other, and creates no other to choose. Without one the annotated methods run uncached. Spring still does all of the injection, so the field keeps its place among the members it injects, on every path it injects. A class compiled against Grails 8 keeps its unqualified field until it is compiled again, and the alias registrar of 8.0.1 stays for it. GrailsCacheManagerQualifierSpec checks, over the arrangements now shared in CacheManagerArrangementsSpec, that what a class compiled against this version gets depends only on how grailsCacheManager is defined, with the alias and without it, and covers autowired methods, autowireBean, a provider's alias, resolvable dependencies and code generated ahead of time. GrailsCacheManagerAliasMatrixSpec now injects a class compiled against the Grails 8 trait, and keeps 8.0.0 as its oracle. CacheAutoConfigurationSpec covers the changed cases. The cache guide, section 32 of the upgrade guide and the grails-upgrade skill describe it. Closes apache#17152
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 9.0.x #17157 +/- ##
==================================================
- Coverage 65.7675% 65.7617% -0.0058%
+ Complexity 33106 33104 -2
==================================================
Files 2448 2448
Lines 121519 121519
Branches 22550 22550
==================================================
- Hits 79920 79913 -7
- Misses 31514 31522 +8
+ Partials 10085 10084 -1
🚀 New features to boost your workflow:
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A cache annotation can still use a manager other than the one named grailsCacheManager.
…ager The trait's field was qualified with Spring's @qualifier('grailsCacheManager'), which Spring also matches to a bean that declares that qualifier itself, on its class, its @bean method or its bean definition. Such a bean marked primary was taken for the cache annotations over the bean named grailsCacheManager, which the cache tags and the plugin's startup hook use, and with no grailsCacheManager it was injected where the annotated methods should run uncached. The field is now qualified with GrailsCacheManagerQualifier, a qualifier that can only be put on fields, so no class or @bean method can declare it. Its value, grailsCacheManager, still selects the bean of that name or alias, and Spring still does all of the injection. The @qualifier that marks it as a qualifier has no value, so it matches no bean by name either. GrailsCacheManagerQualifierSpec covers a cache manager declaring the qualifier in each of the three ways, primary, with a priority and plain, with and without the alias, for the bean, its prototypes, autowireBean and code generated ahead of time, and with no grailsCacheManager. It also pins that a primary cache manager given the alias grailsCacheManager beside a bean of that name is what the field and a lookup by name both get. CacheAutoConfigurationSpec covers the case through the auto-configuration. The cache guide, the upgrade guide and the grails-upgrade skill say that declaring the qualifier does not select a cache manager.
The feature run from the code Spring's AOT processing generates checked a cache manager declaring Spring's qualifier grailsCacheManager only on its class, only marked primary, and only with the alias registrar. It now runs each declaration, on the class, the @bean method and the bean definition, primary, with a priority and plain, with the alias and without it, and with no grailsCacheManager, where the field must stay empty. With Spring's @qualifier('grailsCacheManager') on the trait's field, the primary rows and the rows with no grailsCacheManager fail.
✅ All tests passed ✅🏷️ Commit: 2469a80 Learn more about TestLens at testlens.app/docs. |
matrei
left a comment
There was a problem hiding this comment.
Review of #17157 at 2469a805ec (base 9.0.x): three commits on e551bd3f88, which has #16980 merged up. 9.0.x is at 814ef026fc now, with nothing new in grails-cache or in the cache and upgrade chapters of the guide, and the PR merges cleanly.
What I ran, on Java 21, without the build cache:
-
The
grails-cachesuite at this head: 126 tests, no failures. -
A/B on the trait's field:
- With Spring's
@Qualifier('grailsCacheManager')in place of@GrailsCacheManagerQualifier, 19 features ofGrailsCacheManagerQualifierSpecand 2 ofCacheAutoConfigurationSpecfail. They are every row with a primary cache manager that declares that qualifier, every row with nograilsCacheManager, and the check of the field's annotation. - With no qualifier, 47 and 12 fail.
Both counts are the ones the description gives.
- With Spring's
-
The
grails-test-examples/cacheapp:test(19) andintegrationTest(25), and theCachingSpecofgrails-test-examples/app1(20), at this head, no failures. -
At
8d05864997, the first commit, I also checked two things in a running context:cacheUsers, as on #16980:spring-security/ui/simplewithgrails-cacheadded starts, andGrailsCacheAdminServicehasgrailsCacheManager.- The alias route that the upgrade note gives, through the auto-configuration: a
GrailsCacheManagerdefined as@Bean(name = ['appCacheManager', 'grailsCacheManager']), beside a primary cache manager. The plugin'sgrailsCacheManagerbacks off, the cache-aware bean andGrailsCacheAdminServiceget the application's, and the startup hook runs.
The later commits change only which annotation qualifies the field, and the alias rows of the spec cover that, so I didn't repeat these two.
@codeconsole, thanks for writing this up as #17152 and following it so closely. A qualifier is the simplest form the rule could take: one annotation, and Spring does all the rest. It's on 9.0.x, as a change of behaviour has to be, with the upgrade note and the skill entry. James's case holds: with Spring's @Qualifier, a primary cache manager that declares that qualifier takes the field from grailsCacheManager, as the A/B shows. The qualifier that can go only on a field closes that gap without taking any of the choice away from Spring. Nothing below blocks this.
1. A row for a parent context's grailsCacheManager (optional)
The test plan in #17152 lists "an alias grailsCacheManager, and a parent context's". The alias is covered, but the parent context's no longer is: this PR removes the feature of GrailsCacheManagerAliasMatrixSpec that covered it, "the bean named grailsCacheManager is the one a child bean factory sees by that name". It behaves right at this head. This feature passes in GrailsCacheManagerQualifierSpec, with the imports that the spec already has:
void 'a child context gets the grailsCacheManager it sees by that name beside a primary cache manager of its own: #where'() {
given:
GenericApplicationContext parent = new GenericApplicationContext()
CacheManager parentNamed = new ConcurrentMapCacheManager()
parent.registerBean(NAMED, CacheManager, { -> parentNamed } as Supplier<CacheManager>)
parent.refresh()
GenericApplicationContext child = new GenericApplicationContext(parent)
AnnotationConfigUtils.registerAnnotationConfigProcessors(child)
CacheManager childNamed = new ConcurrentMapCacheManager()
child.registerBean('primaryCacheManager', CacheManager, { -> new ConcurrentMapCacheManager() } as Supplier<CacheManager>,
{ BeanDefinition definition -> definition.primary = true } as BeanDefinitionCustomizer)
if (childHasOne) {
child.registerBean(NAMED, CacheManager, { -> childNamed } as Supplier<CacheManager>)
}
child.registerBean('probe', CacheAware)
child.refresh()
expect:
child.getBean('probe', CacheAware).grailsCacheManager.is(childHasOne ? childNamed : parentNamed)
cleanup:
child?.close()
parent?.close()
where:
where | childHasOne
'the parent\'s' | false
'the child\'s own, beside the parent\'s' | true
}2. What an application's own cache manager needs (optional)
On 8.0.x, the guide says that the annotations cache with an application's own cache manager that is marked primary. That is often a plain Spring one, and the guide itself names JCacheCacheManager. Under section 32, such an application names it grailsCacheManager or gives it that alias, and with caching enabled "it must then implement GrailsCacheManager". The note doesn't tell the reader how much work that is. Half a sentence would show that it's small:
it must then implement
GrailsCacheManager, as the plugin's startup hook requires: for a Spring cache manager such asJCacheCacheManager, a subclass that implementscacheExistsanddestroyCache.
3. FYI: #17074 also adds a section 32
#17074, open against 9.0.x, adds "32. A Sort by an Alias That the Query Does Not Define Is Rejected" to upgrading90x.adoc. Whichever merges second renumbers; nothing to do here now.
Verified as correct
- How Spring matches the qualifier:
- Spring documents a bean's name as its default qualifier value.
QualifierAnnotationAutowireCandidateResolvermatches the value of@GrailsCacheManagerQualifierthroughBeanDefinitionHolder.matchesName, which includes the aliases. So the bean namedgrailsCacheManagerand a bean with that alias both qualify. - Spring also takes a bean that declares the same qualifier annotation on its class, its
@Beanmethod or its bean definition. With@Target(ElementType.FIELD), only a bean definition could declare this one, by namingGrailsCacheManagerQualifierin anAutowireCandidateQualifier, which no application does by accident. - The
@Qualifierwithout a value that marks the annotation matches no bean of its own. At this head a primary cache manager that declares Spring's qualifier, in any of the three ways, loses tograilsCacheManager, in a context and in the code that AOT processing generates.
- Spring documents a bean's name as its default qualifier value.
- The field's annotation: Groovy carries
@GrailsCacheManagerQualifierover to the field that it creates in the implementing class, as the first feature ofGrailsCacheManagerQualifierSpecchecks. A class compiled against the Grails 8 trait keeps its field without one. The fixture is identical to the trait ofv8.0.0, and the registrar still serves those classes against 8.0.0 as the oracle. - The features removed from
GrailsCacheManagerAliasMatrixSpec: the 20 features injected classes compiled in the spec. Those classes now have the qualifier, so the features no longer exercised the alias. They checked the fallback and alias designs against 8.0.0 on paths where Grails code once took part. Spring now handles those on its own, andGrailsCacheManagerQualifierSpeccovers the ones that still matter for the qualified field: autowired methods,autowireBean, resolvable dependencies and AOT. Point 1 is the one left out. - Caching disabled: with
grails.cache.enabled: false, the whole auto-configuration is off. There is nograilsCacheManager, and the annotations run uncached, also beside a cache manager that only declares the qualifier. An application's own cache manager namedgrailsCacheManageris still used. That's what the guide, the note and the skill say. - A cache-aware
grailsCacheManageris given itself. It still starts in an application, sinceGrailsAppdefaultsspring.main.allow-circular-referencestotrue. - The docs:
- The guide, section 32, the skill and the trait's Javadoc describe the rule as the code has it, including that declaring Spring's
@Qualifier("grailsCacheManager")doesn't select a cache manager. - The required actions match what the tests show: the
BeanCurrentlyInCreationExceptionfor agrailsCacheManagerbuilt from a cache-aware bean, and the name or alias route. - The guide doesn't name
GrailsCacheManagerQualifier, which lives inorg.grails.plugin.cachebeside the trait.
- The guide, section 32, the skill and the trait's Javadoc describe the rule as the code has it, including that declaring Spring's
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A cache annotation can still use a manager other than the one named grailsCacheManager.
|
|
||
| /** The bean named {@code grailsCacheManager}, as the cache tags and the plugin use it. */ | ||
| @Autowired(required = false) | ||
| @GrailsCacheManagerQualifier |
There was a problem hiding this comment.
Two cache managers exist. One is named grailsCacheManager. The other is primary and also answers to that name. A cache annotation uses the other one. Use the one that is named grailsCacheManager.
Closes #17152.
The cache annotations and
GrailsCacheAdminServiceget their cache manager through theGrailsCacheManagerAwaretrait, which injected aCacheManagerby type. So:@Priority, took the annotations away fromgrailsCacheManager, while the cache tags and the plugin's startup hook still usedgrailsCacheManagerby name;grails.cache.enabled: false, the annotations took whatever single or primary cache manager the context had, such as the one the Spring Security plugin defines whencacheUsersis set.#16980 kept that by-type choice on 8.0.x, as a patch release must, and added an alias so Spring takes
grailsCacheManageronly where it cannot choose. This changes the rule for 9.0.0.The change
The trait's field is qualified with the bean name:
GrailsCacheManagerQualifieris a Spring qualifier annotation whosevaluedefaults tograilsCacheManager. Spring then injects the bean namedgrailsCacheManager, or the bean with that alias, and no other:grailsCacheManagergrailsCacheManager@PrioritygrailsCacheManager@Qualifier("grailsCacheManager")grailsCacheManagercacheUsersgrailsCacheManager(8.0.1)grailsCacheManagergrailsCacheManagerat all, as withgrails.cache.enabled: false@Qualifier('grailsCacheManager'). Spring matches that to a bean's name or alias, and also to a bean that declares the same qualifier itself, on its class, its@Beanmethod or its bean definition. Marked primary, such a bean would take the field from the bean namedgrailsCacheManager, and with nograilsCacheManagerit would be injected.GrailsCacheManagerQualifiercan only be put on fields, so no class or@Beanmethod can declare it, and the@Qualifierthat marks it has no value, so a bean matches it only by its name or alias.autowireBean(GORM data services), prototypes and the code Spring's AOT processing generates behave as Spring makes them behave. No other cache manager is created to choose one, so a lazy one stays uncreated.grailsCacheManageror give it that alias; declaring@Qualifier("grailsCacheManager")on it is not enough. With caching enabled it then replaces the plugin's, which a cache provider plugin does too, and must implementGrailsCacheManager, as the startup hook requires.grailsCacheManagerbeside a bean of that name: whichever was registered last owns the name, and the annotations and a lookup by name, as the tags do, get the same one.grailsCacheManagercreated from a cache-aware bean no longer starts, as that bean asks for it while it is created. Any rule that takesgrailsCacheManagerfirst has this.grailsCacheManagerthat is cache-aware itself is given itself, as Spring gives a bean its only candidate. Where circular references are not allowed, it does not start.CacheManagerregistered withregisterResolvableDependencyis still injected where there is nograilsCacheManager, as Spring applies no qualifier to resolvable dependencies. BesidegrailsCacheManagerit is passed over.GrailsCacheManagerAliasRegistrarstays, so they keep the behaviour of Grails 8.0.1.The cache guide, section 32 of the upgrade guide and the
grails-upgradeskill describe the rule and what to do.Tests
GrailsCacheManagerQualifierSpecruns the 1,596 arrangements of cache managers from 8.0 (fix): Start with another CacheManager next to the Grails cache plugin's #16980, now shared inCacheManagerArrangementsSpec, with the alias registrar and without it.grailsCacheManageris defined: it getsgrailsCacheManagerwhere that is plain, primary, lazy, a fallback bean, not a default candidate, with a@Priorityor cache-aware itself, and nothing where it is absent, not an autowire candidate, of the wrong type or Spring's null bean. WheregrailsCacheManageris created from the cache-aware bean, the context does not start.grailsCacheManageralone, and nothing else is created for the field.@Qualifier('grailsCacheManager')on its class, its@Beanmethod or its bean definition, primary, with a priority or plain, with the alias and without it, is not taken for the bean, its prototypes,autowireBeanor the code Spring's AOT processing generates, compiled and started. With nograilsCacheManagerthe field stays empty, in a context and in generated code.autowireBean; a provider's cache manager with the alias; a primary cache manager aliasedgrailsCacheManagerbeside a bean of that name, in both orders; resolvable dependencies; and generated code beside a plain primary cache manager.GrailsCacheManagerAliasMatrixSpecnow injects a class compiled against the Grails 8 trait, from a fixture identical to the trait of v8.0.0. It keeps 8.0.0 as its oracle over the same arrangements, with the known differences of 8.0.1, and the registrar's own features.CacheAutoConfigurationSpeccovers the changed cases through the auto-configuration:@Prioritycache manager beside the plugin's, and a primary one whose@Beanmethod declares@Qualifier("grailsCacheManager");grailsCacheManager, and with one that only declares the qualifier;grailsCacheManagerbuilt from a cache-aware bean;grailsCacheManager, with circular references allowed and not.GrailsCacheManagerQualifierSpecand 12 ofCacheAutoConfigurationSpecfail. With Spring's@Qualifier('grailsCacheManager')in its place, 19 and 2 fail.grails-cachetests pass, as do thegrails-test-examples/cacheapp's tests and theCachingSpecofgrails-test-examples/app1andhibernate7/app1.