jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1550394 - in /jackrabbit/oak/trunk/oak-core/src/main: java/org/apache/jackrabbit/oak/plugins/mongomk/ java/org/apache/jackrabbit/oak/plugins/mongomk/util/ resources/OSGI-INF/metatype/
Date Thu, 12 Dec 2013 10:37:11 GMT
Author: chetanm
Date: Thu Dec 12 10:37:11 2013
New Revision: 1550394

URL: http://svn.apache.org/r1550394
Log:
OAK-1209 - Consolidate Mongo client connection creation logic in MongoMK

Refactored the logic to use Mongo URI to create connection. Also merged
MongoNodeStore and MongoMicroKernal service to one. The MongoNodeStore
service would register MK interface if `useMK` config property is set.  Otherwise
 it would default to register NodeStore service. More details in bug notes

Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/MongoMicroKernelService.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/MongoNodeStoreService.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/util/MongoConnection.java
    jackrabbit/oak/trunk/oak-core/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/MongoNodeStoreService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/MongoNodeStoreService.java?rev=1550394&r1=1550393&r2=1550394&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/MongoNodeStoreService.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/MongoNodeStoreService.java
Thu Dec 12 10:37:11 2013
@@ -18,19 +18,21 @@
  */
 package org.apache.jackrabbit.oak.plugins.mongomk;
 
-import static org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils.registerMBean;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
 import com.mongodb.DB;
-
+import com.mongodb.MongoClient;
+import com.mongodb.MongoClientOptions;
+import com.mongodb.MongoClientURI;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Property;
+import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean;
 import org.apache.jackrabbit.oak.osgi.ObserverTracker;
 import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard;
@@ -41,10 +43,11 @@ import org.apache.jackrabbit.oak.spi.whi
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.component.annotations.Deactivate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils.registerMBean;
+
 /**
  * The OSGi service to start/stop a MongoNodeStore instance.
  */
@@ -53,58 +56,111 @@ import org.slf4j.LoggerFactory;
         description = "%oak.mongons.description",
         policy = ConfigurationPolicy.REQUIRE
 )
-public class MongoNodeStoreService {
-
-    private static final String DEFAULT_HOST = "localhost";
-    private static final int DEFAULT_PORT = 27017;
-    private static final String DEFAULT_DB = "oak";
+public class MongoNodeStoreService{
+    private static final String DEFAULT_URI = "mongodb://localhost:27017/oak";
     private static final int DEFAULT_CACHE = 256;
+    private static final String DEFAULT_DB = "oak";
 
-    @Property(value = DEFAULT_HOST)
-    private static final String PROP_HOST = "host";
+    /**
+     * Name of framework property to configure Mongo Connection URI
+     */
+    private static final String FWK_PROP_URI = "oak.mongo.uri";
+
+    /**
+     * Name of framework property to configure Mongo Database name
+     * to use
+     */
+    private static final String FWK_PROP_DB = "oak.mongo.db";
+
+    //MongoMK would be done away with so better not
+    //to expose this setting in config ui
+    @Property(boolValue = false, propertyPrivate = true)
+    private static final String PROP_USE_MK = "useMK";
 
-    @Property(intValue = DEFAULT_PORT)
-    private static final String PROP_PORT = "port";
+    @Property(value = DEFAULT_URI)
+    private static final String PROP_URI = "mongouri";
 
     @Property(value = DEFAULT_DB)
     private static final String PROP_DB = "db";
 
     @Property(intValue = DEFAULT_CACHE)
     private static final String PROP_CACHE = "cache";
+
     private static final long MB = 1024 * 1024;
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     private ServiceRegistration reg;
+    private final List<Registration> registrations = new ArrayList<Registration>();
     private MongoNodeStore store;
     private ObserverTracker observerTracker;
-    private final List<Registration> registrations = new ArrayList<Registration>();
 
     @Activate
-    private void activate(BundleContext context, Map<String, ?> config)
-            throws Exception {
-        String host = PropertiesUtil.toString(config.get(PROP_HOST), DEFAULT_HOST);
-        int port = PropertiesUtil.toInteger(config.get(PROP_PORT), DEFAULT_PORT);
+    protected void activate(BundleContext context, Map<String, ?> config) throws Exception
{
+        String uri = PropertiesUtil.toString(config.get(PROP_URI), DEFAULT_URI);
+        if(context.getProperty(FWK_PROP_URI) != null){
+            uri = context.getProperty(FWK_PROP_URI);
+        }
+
         String db = PropertiesUtil.toString(config.get(PROP_DB), DEFAULT_DB);
+        if(context.getProperty(FWK_PROP_DB) != null){
+            db = context.getProperty(FWK_PROP_DB);
+        }
+
         int cacheSize = PropertiesUtil.toInteger(config.get(PROP_CACHE), DEFAULT_CACHE);
+        boolean useMK = PropertiesUtil.toBoolean(config.get(PROP_USE_MK), false);
 
-        logger.info("Starting MongoDB NodeStore with host={}, port={}, db={}",
-                new Object[] {host, port, db});
+        MongoClientOptions.Builder builder = MongoConnection.getDefaultBuilder();
+        MongoClientURI mongoURI = new MongoClientURI(uri,builder);
 
-        MongoConnection connection = new MongoConnection(host, port, db);
-        DB mongoDB = connection.getDB();
+        if(logger.isInfoEnabled()){
+            //Take care around not logging the uri directly as it
+            //might contain passwords
+            String type = useMK ? "MicroKernel" : "NodeStore" ;
+            logger.info("Starting MongoDB {} with host={}, db={}",
+                    new Object[] {type,mongoURI.getHosts(), db});
+            logger.info("Mongo Connection details {}",MongoConnection.toString(mongoURI.getOptions()));
+        }
 
-        logger.info("Connected to database {}", mongoDB);
+        MongoClient client = new MongoClient(mongoURI);
+        DB mongoDB = client.getDB(db);
 
         MongoMK mk = new MongoMK.Builder()
                 .memoryCacheSize(cacheSize * MB)
                 .setMongoDB(mongoDB)
                 .open();
-        store = mk.getNodeStore();
+
+        logger.info("Connected to database {}", mongoDB);
 
         registerJMXBeans(mk, context);
-        observerTracker = new ObserverTracker(store);
-        reg = context.registerService(NodeStore.class.getName(), store, new Properties());
+        store = mk.getNodeStore();
+
+        if(useMK){
+            reg  = context.registerService(MicroKernel.class.getName(), mk, new Properties());
+        }else{
+            observerTracker = new ObserverTracker(store);
+            observerTracker.start(context);
+            reg = context.registerService(NodeStore.class.getName(), store, new Properties());
+        }
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        if(observerTracker != null){
+            observerTracker.stop();
+        }
+
+        for (Registration r : registrations) {
+            r.unregister();
+        }
+
+        if (reg != null) {
+            reg.unregister();
+        }
+
+        if (store != null) {
+            store.dispose();
+        }
     }
 
     private void registerJMXBeans(MongoMK mk, BundleContext context) {
@@ -150,20 +206,4 @@ public class MongoNodeStoreService {
             );
         }
     }
-
-    @Deactivate
-    private void deactivate() {
-        observerTracker.stop();
-        for (Registration r : registrations) {
-            r.unregister();
-        }
-
-        if (reg != null) {
-            reg.unregister();
-        }
-
-        if (store != null) {
-            store.dispose();
-        }
-    }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/util/MongoConnection.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/util/MongoConnection.java?rev=1550394&r1=1550393&r2=1550394&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/util/MongoConnection.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/mongomk/util/MongoConnection.java
Thu Dec 12 10:37:11 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.mongomk.util;
 
+import com.google.common.base.Objects;
 import com.mongodb.DB;
 import com.mongodb.MongoClient;
 import com.mongodb.MongoClientOptions;
@@ -38,8 +39,7 @@ public class MongoConnection {
      * @throws Exception If an error occurred while trying to connect.
      */
     public MongoConnection(String host, int port, String database) throws Exception {
-        MongoClientOptions options = new MongoClientOptions.Builder().
-                threadsAllowedToBlockForConnectionMultiplier(100).build();
+        MongoClientOptions options = getDefaultBuilder().build();
         ServerAddress serverAddress = new ServerAddress(host, port);
         mongo = new MongoClient(serverAddress, options);
         db = mongo.getDB(database);
@@ -58,8 +58,35 @@ public class MongoConnection {
      * Closes the underlying Mongo instance
      */
     public void close() {
-        if (mongo != null) {
-            mongo.close();
-        }
+        mongo.close();
+    }
+
+    //--------------------------------------< Utility Methods >
+
+    /**
+     * Constructs a builder with default options set. These can be overridden later
+     *
+     * @return builder with default options set
+     */
+    public static MongoClientOptions.Builder getDefaultBuilder() {
+        return new MongoClientOptions.Builder()
+                .description("MongoConnection for Oak MongoMK")
+                .threadsAllowedToBlockForConnectionMultiplier(100);
+    }
+
+    public static String toString(MongoClientOptions opts){
+        return Objects.toStringHelper(opts)
+                .add("connectionsPerHost", opts.getConnectionsPerHost())
+                .add("connectTimeout", opts.getConnectTimeout())
+                .add("socketTimeout", opts.getSocketTimeout())
+                .add("socketKeepAlive", opts.isSocketKeepAlive())
+                .add("autoConnectRetry", opts.isAutoConnectRetry())
+                .add("maxAutoConnectRetryTime", opts.getMaxAutoConnectRetryTime())
+                .add("maxWaitTime", opts.getMaxWaitTime())
+                .add("threadsAllowedToBlockForConnectionMultiplier",
+                        opts.getThreadsAllowedToBlockForConnectionMultiplier())
+                .add("readPreference",opts.getReadPreference().getName())
+                .add("writeConcern", opts.getWriteConcern())
+                .toString();
     }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-core/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1550394&r1=1550393&r2=1550394&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/resources/OSGI-INF/metatype/metatype.properties
Thu Dec 12 10:37:11 2013
@@ -17,9 +17,7 @@
 # under the License.
 #
 
-oak.mongomk.label=Apache Jackrabbit Oak MongoDB MicroKernel Service
-oak.mongomk.description= Configure an instance of the MongoDB \
-  based MicroKernel implementation
+# suppress inspection "UnusedProperty" for whole file
 
 oak.mongons.label=Apache Jackrabbit Oak MongoNodeStore Service
 oak.mongons.description= Configure an instance of the MongoDB \
@@ -32,7 +30,14 @@ port.name = MongoDB Port
 port.description = The port to connect to.
 
 db.name = MongoDB Database
-db.description = The database to use.
+db.description = The database to use. It can be overridden through framework property \
+  'oak.mongo.db'
 
 cache.name = Cache Size (MB)
-cache.description = Cache Size (MB)
\ No newline at end of file
+cache.description = Cache Size (MB)
+
+mongouri.name = Mongo URI
+mongouri.description = Represents a URI which can be used to create a MongoClient instance.
\
+  The URI describes the hosts to be used and options. Refer to \
+  http://www.mongodb.org/display/DOCS/Connections for more details on format and options.
It \
+  can be overridden through framework property 'oak.mongo.uri'
\ No newline at end of file



Mime
View raw message