This is an automated email from the ASF dual-hosted git repository.

joscorbe pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new bf7bc97069 OAK-11834: Add delay to getDefaultWriteConcern to avoid 
UNKNOWN ClusterType (#2422)
bf7bc97069 is described below

commit bf7bc97069832ecb4d6d9dc77f0aa63508ab3f63
Author: José Andrés Cordero Benítez <[email protected]>
AuthorDate: Sun Aug 3 15:54:07 2025 +0200

    OAK-11834: Add delay to getDefaultWriteConcern to avoid UNKNOWN ClusterType 
(#2422)
    
    * OAK-11834: Add delay to getDefaultWriteConcern to avoid UNKNOWN 
ClusterType
    * OAK-11834: Cover Sharded clusterTypes too
---
 .../plugins/document/mongo/MongoDBConnection.java  |  6 ++-
 .../oak/plugins/document/util/MongoConnection.java | 56 +++++++++++++++++++---
 2 files changed, 54 insertions(+), 8 deletions(-)

diff --git 
a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java
 
b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java
index 200c7f1716..a8bd2fa675 100644
--- 
a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java
+++ 
b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java
@@ -27,6 +27,7 @@ import com.mongodb.client.MongoClient;
 import com.mongodb.client.MongoClients;
 import com.mongodb.client.MongoCollection;
 import com.mongodb.client.MongoDatabase;
+import com.mongodb.WriteConcern;
 
 import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection;
 import org.jetbrains.annotations.NotNull;
@@ -83,7 +84,10 @@ final class MongoDBConnection {
         serverMonitorListener.addListener(status);
         MongoDatabase db = client.getDatabase(name);
         if (!MongoConnection.hasWriteConcern(uri)) {
-            db = 
db.withWriteConcern(MongoConnection.getDefaultWriteConcern(client));
+            WriteConcern defaultWriteConcern = 
MongoConnection.getDefaultWriteConcern(client);
+            LOG.info("Setting default write concern: {} for cluster type: {}", 
+                    defaultWriteConcern, 
client.getClusterDescription().getType());
+            db = db.withWriteConcern(defaultWriteConcern);
         }
         if (status.isMajorityReadConcernSupported()
                 && status.isMajorityReadConcernEnabled()
diff --git 
a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java
 
b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java
index 11e62c2e20..e6bd807cbd 100644
--- 
a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java
+++ 
b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java
@@ -41,11 +41,16 @@ import static java.util.Objects.requireNonNull;
 
 import org.jetbrains.annotations.NotNull;
 
+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+
 /**
  * The {@code MongoConnection} abstracts connection to the {@code MongoDB}.
  */
 public class MongoConnection {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(MongoConnection.class);
+
     public static final String MONGODB_PREFIX = "mongodb://";
 
     private static final int DEFAULT_MAX_WAIT_TIME = (int) 
TimeUnit.MINUTES.toMillis(1);
@@ -54,6 +59,12 @@ public class MongoConnection {
     private final ConnectionString mongoURI;
     private final MongoClient mongo;
 
+    // MongoDB cluster description timeout configuration
+    private static final String PROP_CLUSTER_DESCRIPTION_TIMEOUT_MS = 
"oak.mongo.clusterDescriptionTimeoutMs";
+    private static final String PROP_CLUSTER_DESCRIPTION_INTERVAL_MS = 
"oak.mongo.clusterDescriptionIntervalMs";
+    private static final long DEFAULT_CLUSTER_DESCRIPTION_TIMEOUT_MS = 5000L;
+    private static final long DEFAULT_CLUSTER_DESCRIPTION_INTERVAL_MS = 100L;
+
     /**
      * Constructs a new connection using the specified MongoDB connection 
string.
      * See also http://docs.mongodb.org/manual/reference/connection-string/
@@ -227,14 +238,45 @@ public class MongoConnection {
      * @return the default write concern to use for Oak.
      */
     public static WriteConcern getDefaultWriteConcern(@NotNull MongoClient 
client) {
-        WriteConcern w;
-        ClusterDescription clusterDescription = client.getClusterDescription();
-        if (clusterDescription.getType() == ClusterType.REPLICA_SET) {
-            w = WriteConcern.MAJORITY;
-        } else {
-            w = WriteConcern.ACKNOWLEDGED;
+        long timeoutMs = Long.getLong(PROP_CLUSTER_DESCRIPTION_TIMEOUT_MS, 
DEFAULT_CLUSTER_DESCRIPTION_TIMEOUT_MS);
+        long intervalMs = Long.getLong(PROP_CLUSTER_DESCRIPTION_INTERVAL_MS, 
DEFAULT_CLUSTER_DESCRIPTION_INTERVAL_MS);
+        
+        // If timeout is 0, disable the retry functionality and use immediate 
detection
+        if (timeoutMs == 0) {
+            WriteConcern w;
+            ClusterDescription clusterDescription = 
client.getClusterDescription();
+            if (clusterDescription.getType() == ClusterType.REPLICA_SET) {
+                w = WriteConcern.MAJORITY;
+            } else {
+                w = WriteConcern.ACKNOWLEDGED;
+            }
+            return w;
+        }
+        
+        long startTime = System.currentTimeMillis();
+        long endTime = startTime + timeoutMs;
+        int attempts = 0;
+        
+        while (System.currentTimeMillis() < endTime) {
+            attempts++;
+            ClusterDescription clusterDescription = 
client.getClusterDescription();
+            
+            if (clusterDescription.getType() == ClusterType.REPLICA_SET || 
+                clusterDescription.getType() == ClusterType.SHARDED) {
+                return WriteConcern.MAJORITY;
+            } else if (clusterDescription.getType() == ClusterType.STANDALONE) 
{
+                return WriteConcern.ACKNOWLEDGED;
+            }
+            
+            try {
+                Thread.sleep(intervalMs);
+            } catch (InterruptedException e) {}
         }
-        return w;
+        
+        // In case of timeout, default to ACKNOWLEDGED
+        LOG.warn("Cluster description timeout after {}ms ({} attempts). 
Defaulting to ACKNOWLEDGED write concern.", 
+                timeoutMs, attempts);
+        return WriteConcern.ACKNOWLEDGED;
     }
 
     /**

Reply via email to