Repository navigation
Close and replace every MongoDB client across a CRaC checkpoint, and fix named-connection routing - #16368
Close and replace every MongoDB client across a CRaC checkpoint, and fix named-connection routing#16368codeconsole wants to merge 8 commits into
Conversation
MongoDatastore.stop() closed only the default connection's client, and start() replaced only that one, in the datastore's own field. The clients of the connections under grails.mongodb.connections, and of any added at runtime, stayed open through the checkpoint. Anything that read a client from its connection source rather than from the datastore went on using the one stop() had closed: SCHEMA multi-tenancy listing its tenants, MongoConnectionSources recording a connection added at runtime, and the Spring Data MongoDatabaseFactory, which kept the client it was created with. stop() now closes the client of every connection GORM created, and start() replaces each from the same settings. MongoConnectionSourceFactory creates a MongoConnectionSource, whose client can be replaced, so the replacement is handed out wherever the original was, and close() after a restore closes the replacements. The Spring Data factory asks the datastore for its client on every use. A datastore built from a MongoClientSettings.Builder builds its replacement default client with those options as well; it had been built from the configuration alone. Fixes apache#16366
The guide shows Book.withConnection('moreBooks') { Book.list() } and says
that everything in the closure uses the alternate connection. Only the
calls that did not name the class did: the closure's delegate is that
connection's static API, but Book.list(), a dynamic finder or
book.save() look their API up in the registry, which returned the
default connection's. A save meant for the named connection was written
to the default one.
GormRegistry.withConnectionScope routes an entity's unqualified static,
instance and validation API lookups to a connection while a closure
runs, and MongoEntity.withConnection runs its closure in one. A lookup
that names its connection, such as Book.moreBooks, is unaffected;
scopes nest, and are undone when the closure throws.
…ections A named connection's driver options are built with the default connection's Config as their fallback, and every value of it was passed on, the driver's own defaults included. Some of those are sentinels the builder's setters reject: eventLoopThreads() is 0 until set, and withEventLoopThreads(0) throws, so a datastore with any named connection failed to start. Only the values the default connection customized are inherited now.
Neo4jGormApiFactory built every static API without its qualifier, so Book.other reported the default connection. It read through the named connection's datastore, but the operations it hands to the instance API, save and delete among them, went to the default connection's instead: a write reached the default server, or failed with NoTransactionException inside the named connection's own transaction. The factory now passes the qualifier, resolver and registry through, as MongoDB's does.
Neo4jEntity.withConnection had the same gap as MongoDB's: only the calls that did not name the class used the named connection, and the guide told applications not to write Book.list() inside the block for that reason. It now runs its closure in GormRegistry.withConnectionScope, so static calls on the class and instance calls such as save() use the block's connection too. An operation that names its connection, such as Book.'default'.list(), keeps it.
The release notes gain an 8.0.x section for the two named-connection fixes, and the configuration guide says which driver options a named connection inherits: the ones the default connection sets.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 8.0.x #16368 +/- ##
==================================================
+ Coverage 56.3406% 56.4024% +0.0618%
- Complexity 21863 21920 +57
==================================================
Files 2127 2129 +2
Lines 103460 103586 +126
Branches 18536 18568 +32
==================================================
+ Hits 58290 58425 +135
+ Misses 36887 36877 -10
- Partials 8283 8284 +1
🚀 New features to boost your workflow:
|
Review FindingsHead What I ran on the head, all green:
The CRaC part, the Neo4j startup fix and the Neo4j qualifier fix are all correct as far as I can verify, and each of the claims in the description that I could check holds (details under Confirmed). The findings are about the connection scope in [P2] A scope named
|
The scoped lookups went straight to the scope's connection API, ahead
of the multi-tenant resolution. For a scope on the default connection
and a DATABASE or SCHEMA mode entity that is not what Book.'default'
does: naming the default connection resolves the current tenant's API,
while the scope returned the default connection's own. Inside
Book.withConnection('default') { } the delegated list() and Book.list()
then read different stores.
A lookup that names no connection now takes the scope as its qualifier
and goes through the ordinary resolution, so a scope is what naming the
connection would be by construction. The javadoc says that for a
multi-tenant entity the scope's connection takes precedence over the
current tenant, as a named connection does, and that it belongs to the
calling thread.
Both withConnection pages say the routing applies on the calling thread and takes precedence over the current tenant for a multi-tenant class. The Neo4j release notes quote the two errors the named-connection fixes remove, so they can be searched for, and a MongoDatastore comment gets its missing word.
|
@matrei thanks. Addressed in b34471f and 5e5b35a:
|
🚨 TestLens detected 2 failed tests 🚨Here is what you can do:
Test SummaryCI / Functional Tests (Java 25, indy=false) > :grails-test-examples-scaffolding:integrationTest
Groovy Snapshot Canary Build / Build Grails (shard 2) > :grails-test-examples-scaffolding:integrationTest
🏷️ Commit: 5e5b35a Test FailuresUserControllerSpec > User list (:grails-test-examples-scaffolding:integrationTest in Groovy Snapshot Canary Build / Build Grails (shard 2))UserControllerSpec > User list (:grails-test-examples-scaffolding:integrationTest in CI / Functional Tests (Java 25, indy=false))Rerun ControlsNote Checks are currently running using the configuration below. Select tests to mute in this pull request: 🔲 UserControllerSpec > User list Reuse successful test results: 🔲 ♻️ Only rerun the tests that failed or were muted before Click the checkbox to trigger a rerun: 🔲 Rerun jobs Learn more about TestLens at testlens.app/docs. |
|
Closing in favour of #16369, which contains every commit here, including the review changes, and is now the single PR for all of it. Its description covers both. |
Fixes #16366.
MongoDatastoretakes part in the Spring lifecycle so an application can be checkpointed with CRaC and restored (#16095). Until now only the default connection'sMongoClientwas covered. This makes checkpoint and restore cover every client GORM creates. The later commits fix named-connection bugs found along the way:withConnectionrouting in MongoDB and Neo4j, and Neo4j named connections, which could not start.Checkpoint and restore cover every connection
stop()closes the client of the default connection, of each connection undergrails.mongodb.connections, and of any added at runtime.start()builds a replacement for each from the same settings.MongoConnectionSourceFactorynow creates aMongoConnectionSource, aDefaultConnectionSourcewhose client can be replaced. Code that reads the client from the datastore's connection sources gets the new one. This fixes SCHEMA multi-tenancy, whereeachTenantlisted databases through the closed client, andMongoConnectionSources, which recorded a connection added after a restore through the closed client.close()after a restore closes the replacements, on every connection.MongoDatabaseFactoryasks the datastore for its client on every use, soMongoTemplateand repositories keep working after a restore.MongoClientSettings.Builderbuilds its replacement default client with those options. Before, the replacement was built from the configuration alone and lost them.The guide gains a "Checkpoint and Restore (CRaC)" section under Advanced Configuration, plus a release-notes entry and a line on the Spring Data interop page.
withConnectionroutes the class's own callsThe guide shows
Book.withConnection('moreBooks') { Book.list() }, but only the calls made without naming the class, such aslist(), used the named connection.Book.list(), dynamic finders,wherequeries andbook.save()went to the default connection, so a save inside the block was written to the wrong database. They now use the block's connection:An operation that names its own connection, such as
Book.moreBooks.count(), keeps it, and the default connection applies again after the block, including when it throws. The routing lives inGormRegistry.withConnectionScope, whichMongoEntity.withConnectionandNeo4jEntity.withConnectionuse. Both guides' pages forwithConnectionnow say exactly what the block covers. The Neo4j guide had documented the old behaviour ("should not be prefixed withBook."), so its upgrade notes cover the change.Neo4j named connections work
Config, and every value was copied, the driver's own defaults included.eventLoopThreads()is0until set, andwithEventLoopThreads(0)throws, so any datastore with a connection undergrails.neo4j.connectionsfailed to start. Only options the default connection customizes are inherited now.Neo4jGormApiFactorybuilt static APIs without their qualifier, soBook.moreBooks.save(book)handed the save to the default connection's instance API: it reached the default server, or failed withNoTransactionExceptioninsideBook.moreBooks.withTransaction { }. The factory now passes the qualifier through, as MongoDB's does.Limitations
MongoConnectionSourceFactorywhosecreatereturns a plainDefaultConnectionSourcehas its clients closed and replaced in the datastore, but those connection sources keep handing out the closed originals.MongoDbGormAutoConfigurationcreates the client itself (aMongoPropertiesbean but noMongoClientbean), it passes that client to the datastore as supplied, so a checkpoint leaves it open, as before.MongoClientthat application code obtained before the checkpoint is closed; code keeping one has to obtain it again after the restore.Book.moreBooks.withNewSession { }andBook.moreBooks.withTransaction { }are not scopes:book.save()inside them still uses the entity's default connection. UsewithConnection, or the qualified API itself (Book.moreBooks.save(book)).