Author: amitj
Date: Tue Feb  6 11:08:54 2018
New Revision: 1823298

URL: http://svn.apache.org/viewvc?rev=1823298&view=rev
Log:
OAK-7223: Files could be kept partially in case of disconnection from backends
Merged r1822850 from trunk

Modified:
    jackrabbit/oak/branches/1.6/   (props changed)
    
jackrabbit/oak/branches/1.6/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
    
jackrabbit/oak/branches/1.6/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/FileIOUtilsTest.java
    
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
    
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
    
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java

Propchange: jackrabbit/oak/branches/1.6/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Feb  6 11:08:54 2018
@@ -1,3 +1,3 @@
 /jackrabbit/oak/branches/1.0:1665962
-/jackrabbit/oak/trunk:1781068,1781075,1781248,1781386,1781846,1781907,1782000,1782029,1782196,1782447,1782476,1782770,1782945,1782966,1782973,1782990,1783061,1783066,1783089,1783104-1783105,1783110,1783619,1783720,1783731,1783733,1783738,1783742,1783773,1783855,1783891,1784023,1784034,1784130,1784162,1784251,1784401,1784551,1784574,1784689,1785095,1785108,1785283,1785838,1785917,1785919,1785946,1786122,1787074,1787145,1787151,1787217,1787425,1788056,1788378,1788387-1788389,1788463,1788476,1788850,1789056,1789534,1790382,1790502-1790503,1792049,1792463,1792483,1792742,1792746,1793013,1793088,1793618,1793627,1793644,1794393,1794417,1794683,1795138,1795314,1795330,1795475,1795488,1795491,1795502,1795594,1795613,1795618,1796144,1796230,1796239,1796274,1796278,1796988,1797378,1798035,1798834,1799219,1799389,1799393,1799924,1800244,1800269,1800606,1800613,1800974,1801011,1801013,1801118-1801119,1801675,1802260,1802262,1802286,1802548,1802934,1802938,1802973,1803026,1803247-1803249,1803951
 
,1803953-1803955,1805851-1805852,1806668,1807308,1807688,1808022,1808125,1808128,1808142,1808240,1808246,1809024,1809026,1809131,1809163,1809253,1809255-1809256,1809289,1809745,1811071-1811072,1811155,1811380,1811655,1811952,1811963,1811986,1813192,1814189,1814332,1814397,1815201,1815426,1815438,1815926,1817326,1817919,1817987-1817988,1817990,1818038,1818042,1818056,1818124,1818554,1818576,1818645,1819048,1819050,1821325,1821358,1821495,1821516
+/jackrabbit/oak/trunk:1781068,1781075,1781248,1781386,1781846,1781907,1782000,1782029,1782196,1782447,1782476,1782770,1782945,1782966,1782973,1782990,1783061,1783066,1783089,1783104-1783105,1783110,1783619,1783720,1783731,1783733,1783738,1783742,1783773,1783855,1783891,1784023,1784034,1784130,1784162,1784251,1784401,1784551,1784574,1784689,1785095,1785108,1785283,1785838,1785917,1785919,1785946,1786122,1787074,1787145,1787151,1787217,1787425,1788056,1788378,1788387-1788389,1788463,1788476,1788850,1789056,1789534,1790382,1790502-1790503,1792049,1792463,1792483,1792742,1792746,1793013,1793088,1793618,1793627,1793644,1794393,1794417,1794683,1795138,1795314,1795330,1795475,1795488,1795491,1795502,1795594,1795613,1795618,1796144,1796230,1796239,1796274,1796278,1796988,1797378,1798035,1798834,1799219,1799389,1799393,1799924,1800244,1800269,1800606,1800613,1800974,1801011,1801013,1801118-1801119,1801675,1802260,1802262,1802286,1802548,1802934,1802938,1802973,1803026,1803247-1803249,1803951
 
,1803953-1803955,1805851-1805852,1806668,1807308,1807688,1808022,1808125,1808128,1808142,1808240,1808246,1809024,1809026,1809131,1809163,1809253,1809255-1809256,1809289,1809745,1811071-1811072,1811155,1811380,1811655,1811952,1811963,1811986,1813192,1814189,1814332,1814397,1815201,1815426,1815438,1815926,1817326,1817919,1817987-1817988,1817990,1818038,1818042,1818056,1818124,1818554,1818576,1818645,1819048,1819050,1821325,1821358,1821495,1821516,1822850
 /jackrabbit/trunk:1345480

Modified: 
jackrabbit/oak/branches/1.6/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java?rev=1823298&r1=1823297&r2=1823298&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
 Tue Feb  6 11:08:54 2018
@@ -51,7 +51,6 @@ import static com.google.common.io.Files
 import static com.google.common.io.Files.move;
 import static com.google.common.io.Files.newWriter;
 import static java.io.File.createTempFile;
-import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
 import static org.apache.commons.io.FileUtils.forceDelete;
 import static org.apache.commons.io.IOUtils.closeQuietly;
 import static org.apache.commons.io.IOUtils.copyLarge;
@@ -292,6 +291,26 @@ public final class FileIOUtils {
     }
 
     /**
+     *
+     * Copy the input stream to the given file. Delete the file in case of 
exception.
+     *
+     * @param source the input stream source
+     * @param destination the file to write to
+     * @throws IOException
+     */
+    public static void copyInputStreamToFile(final InputStream source, final 
File destination) throws IOException {
+        boolean success = false;
+        try {
+            FileUtils.copyInputStreamToFile(source, destination);
+            success = true;
+        } finally {
+            if (!success) {
+                forceDelete(destination);
+            }
+        }
+    }
+
+    /**
      * Decorates the given comparator and applies the function before 
delegating to the decorated
      * comparator.
      */

Modified: 
jackrabbit/oak/branches/1.6/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/FileIOUtilsTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/FileIOUtilsTest.java?rev=1823298&r1=1823297&r2=1823298&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/FileIOUtilsTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/FileIOUtilsTest.java
 Tue Feb  6 11:08:54 2018
@@ -22,6 +22,7 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -346,6 +347,32 @@ public class FileIOUtilsTest {
         assertTrue(!f.exists());
     }
 
+    @Test
+    public void copyStreamToFile() throws Exception {
+        Set<String> added = newHashSet("a", "z", "e", "b");
+        File f = assertWrite(added.iterator(), false, added.size());
+
+        File f2 = folder.newFile();
+        FileIOUtils.copyInputStreamToFile(new FileInputStream(f), f2);
+        assertEquals(added, readStringsAsSet(new FileInputStream(f), false));
+        assertTrue(f.exists());
+    }
+
+    @Test
+    public void copyStreamToFileNoPartialCreation() throws Exception {
+        File f = folder.newFile();
+        FileIOUtils.copyInputStreamToFile(randomStream(12, 8192), f);
+
+        File f2 = folder.newFile();
+        try {
+            FileIOUtils.copyInputStreamToFile(new ErrorInputStream(f, 4096), 
f2);
+            Assert.fail("Should have failed with IOException");
+        } catch (Exception e) {}
+
+        assertTrue(f.exists());
+        assertTrue(!f2.exists());
+    }
+
     private static List<String> getLineBreakStrings() {
         return newArrayList("ab\nc\r", "ab\\z", "a\\\\z\nc",
             "/a", "/a/b\nc", "/a/b\rd", "/a/b\r\ne", "/a/c");
@@ -401,4 +428,26 @@ public class FileIOUtilsTest {
         r.nextBytes(data);
         return new ByteArrayInputStream(data);
     }
+
+    /**
+     * Throws error after reading partially defined by max
+     */
+    private static class ErrorInputStream extends FileInputStream {
+        private long bytesread;
+        private long max;
+
+        ErrorInputStream(File file, long max) throws FileNotFoundException {
+            super(file);
+            this.max = max;
+        }
+
+        @Override
+        public int read(byte b[]) throws IOException {
+            bytesread += b.length;
+            if (bytesread > max) {
+                throw new IOException("Disconnected");
+            }
+            return super.read(b);
+        }
+    }
 }

Modified: 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java?rev=1823298&r1=1823297&r2=1823298&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
 Tue Feb  6 11:08:54 2018
@@ -51,8 +51,8 @@ import org.apache.jackrabbit.oak.commons
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
 import static org.apache.commons.io.FilenameUtils.normalizeNoEndSeparator;
+import static 
org.apache.jackrabbit.oak.commons.FileIOUtils.copyInputStreamToFile;
 
 /**
  */
@@ -137,6 +137,9 @@ public class FileCache extends AbstractC
                             is = loader.load(key);
                             copyInputStreamToFile(is, cachedFile);
                             threw = false;
+                        } catch (Exception e) {
+                            LOG.warn("Error reading object for id [{}] from 
backend", key, e);
+                            throw e;
                         } finally {
                             Closeables.close(is, threw);
                         }

Modified: 
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java?rev=1823298&r1=1823297&r2=1823298&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
 Tue Feb  6 11:08:54 2018
@@ -94,7 +94,7 @@ public class AbstractDataStoreCacheTest
 
 
     static class TestCacheLoader<S, I> extends CacheLoader<String, 
FileInputStream> {
-        private final File root;
+        protected final File root;
 
         public TestCacheLoader(File dir) {
             this.root = new File(dir, "datastore");
@@ -117,6 +117,46 @@ public class AbstractDataStoreCacheTest
         }
     }
 
+
+    /**
+     * Throws error after reading partially defined by max
+     */
+    private static class ErrorInputStream extends FileInputStream {
+        private long bytesread;
+        private long max;
+
+        ErrorInputStream(File file, long max) throws FileNotFoundException {
+            super(file);
+            this.max = max;
+        }
+
+        @Override
+        public int read(byte b[]) throws IOException {
+            bytesread += b.length;
+            if (bytesread > max) {
+                throw new IOException("Disconnected");
+            }
+            return super.read(b);
+        }
+    }
+
+
+    /**
+     * Test loader which uses the ErrorInputStream for load
+     */
+    static class TestErrorCacheLoader<S, I> extends TestCacheLoader<String, 
FileInputStream> {
+        private long max;
+
+        public TestErrorCacheLoader(File dir, long max) {
+            super(dir);
+            this.max = max;
+        }
+
+        @Override public FileInputStream load(@Nonnull String key) throws 
Exception {
+            return new ErrorInputStream(getFile(key, root), max);
+        }
+    }
+
     static class TestPoolExecutor extends ThreadPoolExecutor {
         private final CountDownLatch beforeLatch;
         private final CountDownLatch afterLatch;

Modified: 
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java?rev=1823298&r1=1823297&r2=1823298&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java
 Tue Feb  6 11:08:54 2018
@@ -38,6 +38,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.junit.rules.TestName;
 
@@ -59,6 +60,9 @@ public class FileCacheTest extends Abstr
     private Closer closer;
 
     @Rule
+    public ExpectedException expectedEx = ExpectedException.none();
+
+    @Rule
     public TemporaryFolder folder = new TemporaryFolder(new File("target"));
 
     @Rule
@@ -111,6 +115,25 @@ public class FileCacheTest extends Abstr
         LOG.info("Finished zeroCache");
     }
 
+    @Test
+    public void loadError() throws Exception {
+        LOG.info("Started loadError");
+
+        loader = new TestErrorCacheLoader<String, 
InputStream>(folder.newFolder(), 8192);
+        cache = FileCache.build(12 * 1024/* KB */, root, loader, null);
+        closer.register(cache);
+        createFile(0, loader, cache, folder, 12 * 1024);
+        try {
+            cache.get(ID_PREFIX + 0);
+        } catch (IOException e) {
+        }
+
+        expectedEx.expect(IOException.class);
+        cache.get(ID_PREFIX + 0);
+
+        LOG.info("Finished loadError");
+    }
+
     /**
      * Load and get from cache.
      * @throws Exception
@@ -503,9 +526,14 @@ public class FileCacheTest extends Abstr
         assertTrue(Files.equal(f, cached));
     }
 
+    private static File createFile(int seed, TestCacheLoader loader, FileCache 
cache, TemporaryFolder folder)
+        throws Exception {
+        return createFile(seed, loader, cache, folder, 4 * 1024);
+    }
+
     private static File createFile(int seed, TestCacheLoader loader, FileCache 
cache,
-        TemporaryFolder folder) throws Exception {
-        File f = copyToFile(randomStream(0, 4 * 1024),
+        TemporaryFolder folder, int size) throws Exception {
+        File f = copyToFile(randomStream(0, size),
             folder.newFile());
         loader.write(ID_PREFIX + seed, f);
         assertNull(cache.getIfPresent(ID_PREFIX + seed));


Reply via email to