You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2015/03/18 12:08:03 UTC

svn commit: r1667498 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java

Author: mreutegg
Date: Wed Mar 18 11:08:03 2015
New Revision: 1667498

URL: http://svn.apache.org/r1667498
Log:
OAK-2642: DocumentNodeStore.dispose() may leave repository in an inconsistent state

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1667498&r1=1667497&r2=1667498&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java Wed Mar 18 11:08:03 2015
@@ -459,40 +459,50 @@ public final class DocumentNodeStore
     }
 
     public void dispose() {
-        runBackgroundOperations();
-        if (!isDisposed.getAndSet(true)) {
-            synchronized (isDisposed) {
-                isDisposed.notifyAll();
-            }
+        if (isDisposed.getAndSet(true)) {
+            // only dispose once
+            return;
+        }
+        // notify background threads waiting on isDisposed
+        synchronized (isDisposed) {
+            isDisposed.notifyAll();
+        }
+        try {
+            backgroundThread.join();
+        } catch (InterruptedException e) {
+            // ignore
+        }
+
+        // do a final round of background operations after
+        // the background thread stopped
+        internalRunBackgroundOperations();
+
+        if (leaseUpdateThread != null) {
             try {
-                backgroundThread.join();
+                leaseUpdateThread.join();
             } catch (InterruptedException e) {
                 // ignore
             }
-            if (leaseUpdateThread != null) {
-                try {
-                    leaseUpdateThread.join();
-                } catch (InterruptedException e) {
-                    // ignore
-                }
-            }
-            if (clusterNodeInfo != null) {
-                clusterNodeInfo.dispose();
-            }
-            store.dispose();
-            LOG.info("Disposed DocumentNodeStore with clusterNodeId: {}", clusterId);
-
-            if (blobStore instanceof Closeable) {
-                try {
-                    ((Closeable) blobStore).close();
-                } catch (IOException ex) {
-                    LOG.debug("Error closing blob store " + blobStore, ex);
-                }
+        }
+
+        // now mark this cluster node as inactive by
+        // disposing the clusterNodeInfo
+        if (clusterNodeInfo != null) {
+            clusterNodeInfo.dispose();
+        }
+        store.dispose();
+
+        if (blobStore instanceof Closeable) {
+            try {
+                ((Closeable) blobStore).close();
+            } catch (IOException ex) {
+                LOG.debug("Error closing blob store " + blobStore, ex);
             }
         }
         if (persistentCache != null) {
             persistentCache.close();
         }
+        LOG.info("Disposed DocumentNodeStore with clusterNodeId: {}", clusterId);
     }
 
     Revision setHeadRevision(@Nonnull Revision newHead) {
@@ -544,6 +554,7 @@ public final class DocumentNodeStore
             base = headRevision;
         }
         backgroundOperationLock.readLock().lock();
+        checkOpen();
         boolean success = false;
         Commit c;
         try {
@@ -573,6 +584,7 @@ public final class DocumentNodeStore
             base = headRevision;
         }
         backgroundOperationLock.readLock().lock();
+        checkOpen();
         boolean success = false;
         MergeCommit c;
         try {
@@ -1502,48 +1514,49 @@ public final class DocumentNodeStore
 
     //----------------------< background operations >---------------------------
 
-    public synchronized void runBackgroundOperations() {
+    public void runBackgroundOperations() {
         if (isDisposed.get()) {
             return;
         }
-        if (simpleRevisionCounter != null) {
-            // only when using timestamp
-            return;
-        }
         try {
-            long start = clock.getTime();
-            long time = start;
-            // clean orphaned branches and collisions
-            cleanOrphanedBranches();
-            cleanCollisions();
-            long cleanTime = clock.getTime() - time;
-            time = clock.getTime();
-            // split documents (does not create new revisions)
-            backgroundSplit();
-            long splitTime = clock.getTime() - time;
-            time = clock.getTime();
-            // write back pending updates to _lastRev
-            backgroundWrite();
-            long writeTime = clock.getTime() - time;
-            time = clock.getTime();
-            // pull in changes from other cluster nodes
-            BackgroundReadStats readStats = backgroundRead(true);
-            long readTime = clock.getTime() - time;
-            String msg = "Background operations stats (clean:{}, split:{}, write:{}, read:{} {})";
-            if (clock.getTime() - start > TimeUnit.SECONDS.toMillis(10)) {
-                // log as info if it took more than 10 seconds
-                LOG.info(msg, cleanTime, splitTime, writeTime, readTime, readStats);
-            } else {
-                LOG.debug(msg, cleanTime, splitTime, writeTime, readTime, readStats);
-            }
+            internalRunBackgroundOperations();
         } catch (RuntimeException e) {
             if (isDisposed.get()) {
+                LOG.warn("Background operation failed: " + e.toString(), e);
                 return;
             }
             throw e;
         }
     }
 
+    private synchronized void internalRunBackgroundOperations() {
+        long start = clock.getTime();
+        long time = start;
+        // clean orphaned branches and collisions
+        cleanOrphanedBranches();
+        cleanCollisions();
+        long cleanTime = clock.getTime() - time;
+        time = clock.getTime();
+        // split documents (does not create new revisions)
+        backgroundSplit();
+        long splitTime = clock.getTime() - time;
+        time = clock.getTime();
+        // write back pending updates to _lastRev
+        backgroundWrite();
+        long writeTime = clock.getTime() - time;
+        time = clock.getTime();
+        // pull in changes from other cluster nodes
+        BackgroundReadStats readStats = backgroundRead(true);
+        long readTime = clock.getTime() - time;
+        String msg = "Background operations stats (clean:{}, split:{}, write:{}, read:{} {})";
+        if (clock.getTime() - start > TimeUnit.SECONDS.toMillis(10)) {
+            // log as info if it took more than 10 seconds
+            LOG.info(msg, cleanTime, splitTime, writeTime, readTime, readStats);
+        } else {
+            LOG.debug(msg, cleanTime, splitTime, writeTime, readTime, readStats);
+        }
+    }
+
     /**
      * Renews the cluster lease if necessary.
      *
@@ -1771,6 +1784,19 @@ public final class DocumentNodeStore
 
     //-----------------------------< internal >---------------------------------
 
+    /**
+     * Checks if this store is still open and throws an
+     * {@link IllegalStateException} if it is already disposed (or a dispose
+     * is in progress).
+     *
+     * @throws IllegalStateException if this store is disposed.
+     */
+    private void checkOpen() throws IllegalStateException {
+        if (isDisposed.get()) {
+            throw new IllegalStateException("This DocumentNodeStore is disposed");
+        }
+    }
+
     private boolean dispatch(@Nonnull String jsonDiff,
                              @Nonnull DocumentNodeState node,
                              @Nonnull DocumentNodeState base,

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java?rev=1667498&r1=1667497&r2=1667498&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java Wed Mar 18 11:08:03 2015
@@ -77,7 +77,6 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
 import org.apache.jackrabbit.oak.stats.Clock;
 import org.junit.After;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class DocumentNodeStoreTest {
@@ -1123,7 +1122,6 @@ public class DocumentNodeStoreTest {
     }
 
     // OAK-2642
-    @Ignore
     @Test
     public void dispose() throws CommitFailedException, InterruptedException {
         final BlockingQueue<String> updates = new ArrayBlockingQueue<String>(1);
@@ -1179,8 +1177,9 @@ public class DocumentNodeStoreTest {
             builder = store.getRoot().builder();
             builder.child("test").child("node").child("child-2");
             merge(store, builder);
-        } catch (Exception e) {
-            // ignore
+            fail("Merge must fail with CommitFailedException");
+        } catch (CommitFailedException e) {
+            // expected
         }
 
         // drain updates until dispose finished
@@ -1208,8 +1207,6 @@ public class DocumentNodeStoreTest {
             node.child("child-2");
             merge(store2, builder);
         }
-
-        store2.dispose();
     }
 
     private void doSomeChange(NodeStore ns) throws CommitFailedException {