jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1466437 - in /jackrabbit/oak/trunk/oak-mongomk/src: main/java/org/apache/jackrabbit/mongomk/impl/ main/java/org/apache/jackrabbit/mongomk/prototype/ test/java/org/apache/jackrabbit/mongomk/ test/java/org/apache/jackrabbit/mongomk/prototype/
Date Wed, 10 Apr 2013 11:58:14 GMT
Author: thomasm
Date: Wed Apr 10 11:58:14 2013
New Revision: 1466437

URL: http://svn.apache.org/r1466437
Log:
OAK-731 The cluster id should be automatically generated

Modified:
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/ClusterNodeInfo.java
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoMK.java
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/AbstractMongoConnectionTest.java
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/BaseMongoMKTest.java
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/ClusterTest.java
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/SimpleTest.java

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/impl/MongoConnection.java
Wed Apr 10 11:58:14 2013
@@ -52,8 +52,8 @@ public class MongoConnection {
     /**
      * Closes the underlying Mongo instance
      */
-    public void close(){
-        if (mongo != null){
+    public void close() {
+        if (mongo != null) {
             mongo.close();
         }
     }

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/ClusterNodeInfo.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/ClusterNodeInfo.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/ClusterNodeInfo.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/ClusterNodeInfo.java
Wed Apr 10 11:58:14 2013
@@ -85,10 +85,10 @@ public class ClusterNodeInfo {
     private static final String WORKING_DIR = System.getProperty("user.dir", "");
     
     /**
-     * The initial number of milliseconds for a lease (1 minute). This value is
-     * only used until the first renewal.
+     * The number of milliseconds for a lease (1 minute by default, and
+     * initially).
      */
-    private static final long LEASE_INITIAL = 1000 * 60;
+    private long leaseTime = 1000 * 60;
     
     /**
      * The assigned cluster id.
@@ -137,6 +137,16 @@ public class ClusterNodeInfo {
     public int getId() {
         return id;
     }
+    
+    /**
+     * Create a cluster node info instance for the store, with the 
+     * 
+     * @param store the document store (for the lease)
+     * @return the cluster node info
+     */
+    public static ClusterNodeInfo getInstance(DocumentStore store) {
+        return getInstance(store, MACHINE_ID, WORKING_DIR);
+    }
 
     /**
      * Create a cluster node info instance for the store.
@@ -159,7 +169,7 @@ public class ClusterNodeInfo {
             update.set(ID_KEY, "" + clusterNode.id);
             update.set(MACHINE_ID_KEY, clusterNode.machineId);
             update.set(INSTANCE_ID_KEY, clusterNode.instanceId);
-            update.set(LEASE_END_KEY, System.currentTimeMillis() + LEASE_INITIAL);
+            update.set(LEASE_END_KEY, System.currentTimeMillis() + clusterNode.leaseTime);
             update.set(INFO_KEY, clusterNode.toString());
             boolean success = store.create(Collection.CLUSTER_NODES, Collections.singletonList(update));
             if (success) {
@@ -227,11 +237,19 @@ public class ClusterNodeInfo {
             return;
         }
         UpdateOp update = new UpdateOp(null, "" + id, true);
-        leaseEndTime = now + nextCheckMillis;
+        leaseEndTime = now + leaseTime;
         update.set(LEASE_END_KEY, leaseEndTime);
         store.createOrUpdate(Collection.CLUSTER_NODES, update);
     }
     
+    public void setLeaseTime(long leaseTime) {
+        this.leaseTime = leaseTime;
+    }
+    
+    public long getLeaseTime() {
+        return leaseTime;
+    }
+    
     public void dispose() {
         UpdateOp update = new UpdateOp(null, "" + id, true);
         update.set(LEASE_END_KEY, null);

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoMK.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoMK.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoMK.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/MongoMK.java
Wed Apr 10 11:58:14 2013
@@ -105,11 +105,16 @@ public class MongoMK implements MicroKer
      * The MongoDB blob store.
      */
     private final BlobStore blobStore;
+    
+    /**
+     * The cluster node info.
+     */
+    private final ClusterNodeInfo clusterNodeInfo;
 
     /**
      * The unique cluster id, similar to the unique machine id in MongoDB.
      */
-    private int clusterId;
+    private final int clusterId;
 
     /**
      * The node cache.
@@ -159,8 +164,15 @@ public class MongoMK implements MicroKer
     MongoMK(Builder builder) {
         this.store = builder.getDocumentStore();
         this.blobStore = builder.getBlobStore();
-        this.clusterId = builder.getClusterId();
-        clusterId = Integer.getInteger("oak.mongoMK.clusterId", clusterId);
+        int cid = builder.getClusterId();
+        cid = Integer.getInteger("oak.mongoMK.clusterId", cid);
+        if (cid == 0) {
+            clusterNodeInfo = ClusterNodeInfo.getInstance(store);
+            cid = clusterNodeInfo.getId();
+        } else {
+            clusterNodeInfo = null;
+        }
+        this.clusterId = cid;
         this.asyncDelay = builder.getAsyncDelay();
 
         //TODO Use size based weigher
@@ -235,10 +247,14 @@ public class MongoMK implements MicroKer
         if (isDisposed.get()) {
             return;
         }
+        backgroundRenewClusterIdLease();
         if (simpleRevisionCounter != null) {
             // only when using timestamp
             return;
         }
+        if (!ENABLE_BACKGROUND_OPS) {
+            return;
+        }
         synchronized (this) {
             try {
                 backgroundWrite();
@@ -251,6 +267,13 @@ public class MongoMK implements MicroKer
             }
         }
     }
+
+    private void backgroundRenewClusterIdLease() {
+        if (clusterNodeInfo == null) {
+            return;
+        }
+        clusterNodeInfo.renewLease(asyncDelay);
+    }
     
     private void backgroundRead() {
         String id = Utils.getIdFromPath("/");
@@ -327,6 +350,9 @@ public class MongoMK implements MicroKer
             } catch (InterruptedException e) {
                 // ignore
             }
+            if (clusterNodeInfo != null) {
+                clusterNodeInfo.dispose();
+            }
             store.dispose();
         }
     }
@@ -1230,7 +1256,7 @@ public class MongoMK implements MicroKer
                     }
                 }
                 MongoMK mk = ref.get();
-                if (mk != null && ENABLE_BACKGROUND_OPS) {
+                if (mk != null) {
                     mk.runBackgroundOperations();
                     delay = mk.getAsyncDelay();
                 }
@@ -1299,7 +1325,8 @@ public class MongoMK implements MicroKer
         }
 
         /**
-         * Set the cluster id to use. By default, 0 is used.
+         * Set the cluster id to use. By default, 0 is used, meaning the cluster
+         * id is automatically generated.
          * 
          * @param clusterId the cluster id
          * @return this
@@ -1339,4 +1366,8 @@ public class MongoMK implements MicroKer
         }
     }
 
+    public ClusterNodeInfo getClusterInfo() {
+        return clusterNodeInfo;
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/AbstractMongoConnectionTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/AbstractMongoConnectionTest.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/AbstractMongoConnectionTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/AbstractMongoConnectionTest.java
Wed Apr 10 11:58:14 2013
@@ -72,6 +72,9 @@ public class AbstractMongoConnectionTest
 
     @After
     public void tearDownConnection() throws Exception {
+        // the db might already be closed
+        mongoConnection.close();
+        mongoConnection = new MongoConnection(HOST, PORT, DB);
         dropCollections(mongoConnection.getDB());
         mongoConnection.close();
     }

Modified: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/BaseMongoMKTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/BaseMongoMKTest.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/BaseMongoMKTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/BaseMongoMKTest.java
Wed Apr 10 11:58:14 2013
@@ -35,7 +35,7 @@ public class BaseMongoMKTest extends Bas
 
     @Override
     public void tearDownConnection() throws Exception {
-        super.tearDownConnection();
         ((MongoMK) mk).dispose();
+        super.tearDownConnection();
     }
 }

Modified: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/ClusterTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/ClusterTest.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/ClusterTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/ClusterTest.java
Wed Apr 10 11:58:14 2013
@@ -43,6 +43,7 @@ public class ClusterTest {
         ClusterNodeInfo c1, c2;
         c1 = ClusterNodeInfo.getInstance(store, "m1", null);
         assertEquals(1, c1.getId());
+        c1.setLeaseTime(1);
         // this will quickly expire
         c1.renewLease(1);
         Thread.sleep(10);
@@ -51,6 +52,16 @@ public class ClusterTest {
     }
     
     @Test
+    public void clusterNodeId() {
+        MongoMK mk1 = createMK(0);
+        MongoMK mk2 = createMK(0);
+        assertEquals(1, mk1.getClusterInfo().getId());
+        assertEquals(2, mk2.getClusterInfo().getId());
+        mk1.dispose();
+        mk2.dispose();
+    }    
+    
+    @Test
     public void clusterNodeInfo() {
         MemoryDocumentStore store = new MemoryDocumentStore();
         ClusterNodeInfo c1, c2, c3, c4;

Modified: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/SimpleTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/SimpleTest.java?rev=1466437&r1=1466436&r2=1466437&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/SimpleTest.java
(original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/SimpleTest.java
Wed Apr 10 11:58:14 2013
@@ -106,25 +106,25 @@ public class SimpleTest {
         String rev4 = mk.commit("/test", "^\"a/x\":1", null, null);
         
         String r0 = mk.getNodes("/", rev0, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/@r0000001000-0\",\":childNodeCount\":0}", r0);
+        assertEquals("{\":id\":\"/@r0000001000-1\",\":childNodeCount\":0}", r0);
         String r1 = mk.getNodes("/", rev1, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/@r0000002000-0\",\"test\":{},\":childNodeCount\":1}", r1);
+        assertEquals("{\":id\":\"/@r0000002000-1\",\"test\":{},\":childNodeCount\":1}", r1);
         String r2 = mk.getNodes("/", rev2, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/@r0000003000-0\",\"test\":{},\":childNodeCount\":1}", r2);
+        assertEquals("{\":id\":\"/@r0000003000-1\",\"test\":{},\":childNodeCount\":1}", r2);
         String r3;
         r3 = mk.getNodes("/", rev3, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/@r0000004000-0\",\"test\":{},\":childNodeCount\":1}", r3);
+        assertEquals("{\":id\":\"/@r0000004000-1\",\"test\":{},\":childNodeCount\":1}", r3);
         r3 = mk.getNodes("/test", rev3, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/test@r0000004000-0\",\"a\":{},\"b\":{},\":childNodeCount\":2}",
r3);
+        assertEquals("{\":id\":\"/test@r0000004000-1\",\"a\":{},\"b\":{},\":childNodeCount\":2}",
r3);
         String r4;
         r4 = mk.getNodes("/", rev4, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/@r0000005000-0\",\"test\":{},\":childNodeCount\":1}", r4);
+        assertEquals("{\":id\":\"/@r0000005000-1\",\"test\":{},\":childNodeCount\":1}", r4);
         r4 = mk.getNodes("/test", rev4, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/test@r0000005000-0\",\"a\":{},\"b\":{},\":childNodeCount\":2}",
r4);
+        assertEquals("{\":id\":\"/test@r0000005000-1\",\"a\":{},\"b\":{},\":childNodeCount\":2}",
r4);
         r4 = mk.getNodes("/test/a", rev4, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/test/a@r0000005000-0\",\"x\":1,\":childNodeCount\":0}",
r4);
+        assertEquals("{\":id\":\"/test/a@r0000005000-1\",\"x\":1,\":childNodeCount\":0}",
r4);
         r4 = mk.getNodes("/test/b", rev4, 0, 0, Integer.MAX_VALUE, ":id");
-        assertEquals("{\":id\":\"/test/b@r0000004000-0\",\":childNodeCount\":0}", r4);
+        assertEquals("{\":id\":\"/test/b@r0000004000-1\",\":childNodeCount\":0}", r4);
         
         mk.dispose();        
     }



Mime
View raw message