Author: frm
Date: Wed Nov 23 14:08:33 2016
New Revision: 1770972

URL: http://svn.apache.org/viewvc?rev=1770972&view=rev
Log:
OAK-5135 - Use a lock consistently when accessing the journal file

Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/Scheduler.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/Scheduler.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/Scheduler.java?rev=1770972&r1=1770971&r2=1770972&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/Scheduler.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/Scheduler.java
 Wed Nov 23 14:08:33 2016
@@ -133,13 +133,7 @@ public class Scheduler implements Closea
             if (executor.awaitTermination(60, SECONDS)) {
                 LOG.debug("The scheduler {} was successfully shut down", name);
             } else {
-                LOG.warn("The scheduler {} takes too long to shut down, 
forcing termination", name);
-                executor.shutdownNow();
-                if (executor.awaitTermination(60, SECONDS)) {
-                    LOG.debug("The scheduler {} was successfully shut down", 
name);
-                } else {
-                    LOG.error("The scheduler {} takes too long to shutdown", 
name);
-                }
+                LOG.warn("The scheduler {} takes too long to shut down", name);
             }
         } catch (InterruptedException e) {
             LOG.warn("Interrupt while shutting down he scheduler {}", name, e);

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java?rev=1770972&r1=1770971&r2=1770972&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java
 Wed Nov 23 14:08:33 2016
@@ -65,14 +65,22 @@ public class TarRevisions implements Rev
 
     public static final String JOURNAL_FILE_NAME = "journal.log";
 
+    /**
+     * The lock protecting {@link #journalFile}.
+     */
+    private final Lock journalFileLock = new ReentrantLock();
+
     @Nonnull
     private final AtomicReference<RecordId> head;
 
     @Nonnull
     private final File directory;
 
-    @Nonnull
-    private final RandomAccessFile journalFile;
+    /**
+     * The journal file. It is protected by {@link #journalFileLock}. It 
becomes
+     * {@code null} after it's closed.
+     */
+    private RandomAccessFile journalFile;
 
     /**
      * The persisted head of the root journal, used to determine whether the
@@ -183,8 +191,6 @@ public class TarRevisions implements Rev
         checkState(head.get() != null, "Revisions not bound to a store");
     }
 
-    private final Lock flushLock = new ReentrantLock();
-
     /**
      * Flush the id of the current head to the journal after a call to
      * {@code persisted}. This method does nothing and returns immediately if
@@ -198,27 +204,35 @@ public class TarRevisions implements Rev
         if (head.get() == null) {
             return;
         }
-        if (flushLock.tryLock()) {
+        if (journalFileLock.tryLock()) {
+            if (journalFile == null) {
+                return;
+            }
             try {
-                RecordId before = persistedHead.get();
-                RecordId after = getHead();
-                if (!after.equals(before)) {
-                    persisted.call();
-
-                    LOG.debug("TarMK journal update {} -> {}", before, after);
-                    journalFile.writeBytes(after.toString10() + " root " + 
System.currentTimeMillis() + "\n");
-                    journalFile.getChannel().force(false);
-                    persistedHead.set(after);
-                }
-            } catch (Exception e) {
-                propagateIfInstanceOf(e, IOException.class);
-                propagate(e);
+                doFlush(persisted);
             } finally {
-                flushLock.unlock();
+                journalFileLock.unlock();
             }
         }
     }
 
+    private void doFlush(Callable<Void> persisted) throws IOException {
+        try {
+            RecordId before = persistedHead.get();
+            RecordId after = getHead();
+            if (!after.equals(before)) {
+                persisted.call();
+                LOG.debug("TarMK journal update {} -> {}", before, after);
+                journalFile.writeBytes(after.toString10() + " root " + 
System.currentTimeMillis() + "\n");
+                journalFile.getChannel().force(false);
+                persistedHead.set(after);
+            }
+        } catch (Exception e) {
+            propagateIfInstanceOf(e, IOException.class);
+            propagate(e);
+        }
+    }
+
     @Nonnull
     @Override
     public RecordId getHead() {
@@ -321,6 +335,16 @@ public class TarRevisions implements Rev
      */
     @Override
     public void close() throws IOException {
-        journalFile.close();
+        journalFileLock.lock();
+        try {
+            if (journalFile == null) {
+                return;
+            }
+            journalFile.close();
+            journalFile = null;
+        } finally {
+            journalFileLock.unlock();
+        }
     }
+
 }


Reply via email to