codeconsole commented on code in PR #16495:
URL: https://github.com/apache/grails-core/pull/16495#discussion_r4175882366


##########
grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java:
##########
@@ -315,10 +318,29 @@ public void newConnectionSource(final 
ConnectionSource<MongoClient, MongoConnect
                     MongoDatastore childDatastore = 
createChildDatastore(mappingContext, eventPublisher, parent, 
singletonConnectionSources);
                     
datastoresByConnectionSource.put(connectionSource.getName(), childDatastore);
                     registerAllEntitiesWithEnhancer();
-                    // Registered first and then checked: either close() has 
not started, and will find this
-                    // child when it walks the map, or it has, and the build 
is never started.
-                    if (!closed) {
-                        childDatastore.buildIndexAutomatically();
+                    // Decided under the lifecycle monitor, so a connection 
registered while start() or stop() is
+                    // under way waits for it and is then treated as the 
datastore now is.
+                    synchronized (lifecycleMonitor) {
+                        // Registered first and then checked: either close() 
has not started, and will find this
+                        // child when it walks the map, or it has, and the 
build is never started.
+                        if (closed) {
+                            return;
+                        }
+                        if (running) {
+                            // Unless the start() this waited for found it in 
the map, and has connected and
+                            // built it already.
+                            if (!childDatastore.startupBuildDone) {
+                                
childDatastore.startClient(connectionSources.getFactory(), false);
+                                childDatastore.buildIndexAutomatically();
+                                childDatastore.startupBuildDone = true;
+                            }
+                        }
+                        else if (stopped && childDatastore.ownsClient()) {
+                            // Refused, like the others, until start() 
connects it and builds its indexes.
+                            childDatastore.stopClient();

Review Comment:
   Thanks, fixed in e115da9b27. The listener now leaves a connection added 
while the datastore is stopped as `stop()` left the others: `stopIndexBuild()` 
always, and `stopClient()` only when GORM owns the client, the same check 
`stop()` uses. A `buildIndex()` on it is now put off and runs on `start()`. I 
updated the comment in `replaceStoppedIndexBuildExecutor()` that named this 
connection.
   
   The added-while-stopped feature in `BuildIndexesPerConnectionSpec` now 
requests a build while stopped, checks it is put off, and checks nothing logs 
an error. A new feature covers `buildIndexes = false`: the requested build runs 
when the datastore starts. Both fail without the change.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to