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 am...@apache.org on 2018/10/18 04:48:19 UTC

svn commit: r1844201 - in /jackrabbit/oak/branches/1.6: ./ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/ oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/

Author: amitj
Date: Thu Oct 18 04:48:18 2018
New Revision: 1844201

URL: http://svn.apache.org/viewvc?rev=1844201&view=rev
Log:
OAK-7798: Return stream for downloaded files rather than directly from backend

Merge r1842677, r1843222, r1843231 from trunk

Modified:
    jackrabbit/oak/branches/1.6/   (props changed)
    jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.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/CachingDataStoreTest.java

Propchange: jackrabbit/oak/branches/1.6/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct 18 04:48:18 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,1785161,1785172,1785283,1785652,1785838,1785916-1785917,1785919,1785946,1786122,1787074,1787145,1787151,1787217,1787425,1788056,1788378,1788387-1788389,1788463,1788476,1788850,1789056,1789534,1789925,1789940,1789987,1790006,1790013,1790069,1790077,1790079,1790382,1790502-1790503,1792049,1792463,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,1798832,1798834,1799219,1799389,1799393,1799924,1800244,1800269,1800606,1800613,1800974,1801011,1801013,1801118-1801119
 ,1801675,1802260,1802262,1802286,1802548,1802905,1802934,1802938,1802973,1803026,1803247-1803249,1803951,1803953-1803955,1804437,1805851-1805852,1806668,1807308,1807688,1808022,1808053,1808125,1808128,1808142,1808240,1808246,1808731,1809024,1809026,1809131,1809163,1809178-1809179,1809253,1809255-1809256,1809289,1809745,1811071-1811072,1811155,1811380,1811655,1811952,1811963,1811986,1813192,1813538,1814189,1814332,1814397,1814475,1815201,1815438,1815926,1817326,1817919,1817987-1817988,1817990,1818038,1818042,1818056,1818124,1818137,1818554,1818576,1818645,1819048,1819050,1821237,1821325,1821358,1821495,1821516,1821847,1822207,1822850,1823172,1823655,1824896,1825471,1825654,1826237,1826338,1826532,1826640,1826932,1826957,1827472,1827486,1827977,1828502,1829527,1829569,1829587,1829665,1829854,1829864,1829987,1829998,1830019,1830160,1830239,1830748,1831190,1831374,1832379,1832535,1833308,1834648-1834649,1834681,1835060,1837475,1837998,1838637,1839746,1840024
+/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,1785161,1785172,1785283,1785652,1785838,1785916-1785917,1785919,1785946,1786122,1787074,1787145,1787151,1787217,1787425,1788056,1788378,1788387-1788389,1788463,1788476,1788850,1789056,1789534,1789925,1789940,1789987,1790006,1790013,1790069,1790077,1790079,1790382,1790502-1790503,1792049,1792463,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,1798832,1798834,1799219,1799389,1799393,1799924,1800244,1800269,1800606,1800613,1800974,1801011,1801013,1801118-1801119
 ,1801675,1802260,1802262,1802286,1802548,1802905,1802934,1802938,1802973,1803026,1803247-1803249,1803951,1803953-1803955,1804437,1805851-1805852,1806668,1807308,1807688,1808022,1808053,1808125,1808128,1808142,1808240,1808246,1808731,1809024,1809026,1809131,1809163,1809178-1809179,1809253,1809255-1809256,1809289,1809745,1811071-1811072,1811155,1811380,1811655,1811952,1811963,1811986,1813192,1813538,1814189,1814332,1814397,1814475,1815201,1815438,1815926,1817326,1817919,1817987-1817988,1817990,1818038,1818042,1818056,1818124,1818137,1818554,1818576,1818645,1819048,1819050,1821237,1821325,1821358,1821495,1821516,1821847,1822207,1822850,1823172,1823655,1824896,1825471,1825654,1826237,1826338,1826532,1826640,1826932,1826957,1827472,1827486,1827977,1828502,1829527,1829569,1829587,1829665,1829854,1829864,1829987,1829998,1830019,1830160,1830239,1830748,1831190,1831374,1832379,1832535,1833308,1834648-1834649,1834681,1835060,1837475,1837998,1838637,1839746,1840024,1842677
 /jackrabbit/trunk:1345480

Modified: jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java?rev=1844201&r1=1844200&r2=1844201&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java Thu Oct 18 04:48:18 2018
@@ -52,11 +52,13 @@ import org.apache.jackrabbit.oak.spi.blo
 import org.apache.jackrabbit.oak.spi.blob.AbstractSharedBackend;
 import org.apache.jackrabbit.oak.spi.blob.BlobOptions;
 import org.apache.jackrabbit.oak.stats.StatisticsProvider;
+import org.apache.jackrabbit.util.LazyFileInputStream;
 import org.apache.jackrabbit.util.TransientFileFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.jackrabbit.oak.commons.FileIOUtils.copyInputStreamToFile;
 import static org.apache.jackrabbit.oak.spi.blob.BlobOptions.UploadType.SYNCHRONOUS;
 
 /**
@@ -129,7 +131,7 @@ public abstract class AbstractSharedCach
     /**
      * DataStore cache
      */
-    private CompositeDataStoreCache cache;
+    protected CompositeDataStoreCache cache;
 
     /**
      * The delegate backend
@@ -196,13 +198,13 @@ public abstract class AbstractSharedCach
         File cached = cache.getIfPresent(dataIdentifier.toString());
         if (cached != null && cached.exists()) {
             return new FileCacheDataRecord(this, backend, dataIdentifier, cached.length(),
-                cached.lastModified());
+                tmp, cached.lastModified());
         } else {
             // Return the metadata from backend and lazily load the stream
             try {
                 DataRecord rec = backend.getRecord(dataIdentifier);
                 return new FileCacheDataRecord(this, backend, dataIdentifier, rec.getLength(),
-                    rec.getLastModified());
+                    tmp, rec.getLastModified());
             } catch (Exception e) {
                 LOG.error("Error retrieving record [{}]", dataIdentifier, e);
             }
@@ -291,12 +293,14 @@ public abstract class AbstractSharedCach
         private final long length;
         private final long lastModified;
         private final AbstractSharedCachingDataStore store;
+        private final File temp;
 
         public FileCacheDataRecord(AbstractSharedCachingDataStore store, AbstractSharedBackend backend,
-            DataIdentifier identifier, long length,
+            DataIdentifier identifier, long length, File temp,
             long lastModified) {
             super(backend, identifier);
             this.length = length;
+            this.temp = temp;
             this.lastModified = lastModified;
             this.store = store;
         }
@@ -319,7 +323,16 @@ public abstract class AbstractSharedCach
             try {
                 // If cache configured to 0 will return null
                 if (cached == null || !cached.exists()) {
-                    return backend.getRecord(getIdentifier()).getStream();
+                    InputStream in = null;
+                    try {
+                        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
+                        File tmpFile = fileFactory.createTransientFile("temp0cache", null, temp);
+                        in = backend.getRecord(getIdentifier()).getStream();
+                        copyInputStreamToFile(in, tmpFile);
+                        return new LazyFileInputStream(tmpFile);
+                    } finally {
+                        Closeables.close(in, false);
+                    }
                 } else {
                     return new FileInputStream(cached);
                 }

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=1844201&r1=1844200&r2=1844201&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 Thu Oct 18 04:48:18 2018
@@ -57,6 +57,7 @@ import org.apache.jackrabbit.core.data.D
 import org.apache.jackrabbit.core.data.DataRecord;
 import org.apache.jackrabbit.core.data.DataStoreException;
 import org.apache.jackrabbit.core.data.util.NamedThreadFactory;
+import org.apache.jackrabbit.oak.commons.FileIOUtils;
 import org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord;
 import org.apache.jackrabbit.oak.spi.blob.AbstractSharedBackend;
 import org.slf4j.Logger;
@@ -94,7 +95,7 @@ public class AbstractDataStoreCacheTest
 
 
     static class TestCacheLoader<S, I> extends CacheLoader<String, FileInputStream> {
-        protected final File root;
+        protected File root;
 
         public TestCacheLoader(File dir) {
             this.root = new File(dir, "datastore");
@@ -152,6 +153,15 @@ public class AbstractDataStoreCacheTest
             this.max = max;
         }
 
+        public TestErrorCacheLoader(File dir, long max, boolean override) {
+            super(dir);
+            if (override) {
+                this.root = dir;
+            }
+
+            this.max = max;
+        }
+
         @Override public FileInputStream load(@Nonnull String key) throws Exception {
             return new ErrorInputStream(getFile(key, root), max);
         }
@@ -399,7 +409,7 @@ public class AbstractDataStoreCacheTest
     }
 
     static File copyToFile(InputStream stream, File file) throws IOException {
-        FileUtils.copyInputStreamToFile(stream, file);
+        FileIOUtils.copyInputStreamToFile(stream, file);
         return file;
     }
 

Modified: jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java?rev=1844201&r1=1844200&r2=1844201&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java (original)
+++ jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java Thu Oct 18 04:48:18 2018
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.DigestOutputStream;
 import java.security.MessageDigest;
+import java.util.Collection;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -33,7 +34,9 @@ import com.google.common.collect.Iterato
 import com.google.common.io.Closer;
 import com.google.common.io.Files;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.filefilter.FileFilterUtils;
 import org.apache.commons.io.output.NullOutputStream;
 import org.apache.jackrabbit.core.data.DataIdentifier;
 import org.apache.jackrabbit.core.data.DataRecord;
@@ -43,6 +46,7 @@ import org.apache.jackrabbit.oak.spi.blo
 import org.apache.jackrabbit.oak.spi.blob.BlobOptions;
 import org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider;
 import org.apache.jackrabbit.oak.stats.StatisticsProvider;
+import org.apache.jackrabbit.util.LazyFileInputStream;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -83,6 +87,10 @@ public class CachingDataStoreTest extend
     private ScheduledExecutorService scheduledExecutor;
     private AbstractSharedCachingDataStore dataStore;
     private TestMemoryBackend backend;
+    private StatisticsProvider statsProvider;
+    private TestExecutor listeningExecutor;
+    private String dsPath;
+    private File backendRoot;
 
     @Before
     public void setup() throws Exception {
@@ -97,18 +105,18 @@ public class CachingDataStoreTest extend
         taskLatch = new CountDownLatch(1);
         callbackLatch = new CountDownLatch(1);
         afterExecuteLatch = new CountDownLatch(i);
-        TestExecutor listeningExecutor = new TestExecutor(1, taskLatch, callbackLatch, afterExecuteLatch);
+        listeningExecutor = new TestExecutor(1, taskLatch, callbackLatch, afterExecuteLatch);
 
         // stats
         ScheduledExecutorService statsExecutor = Executors.newSingleThreadScheduledExecutor();
         closer.register(new ExecutorCloser(statsExecutor, 500, TimeUnit.MILLISECONDS));
-        StatisticsProvider statsProvider = new DefaultStatisticsProvider(statsExecutor);
+        statsProvider = new DefaultStatisticsProvider(statsExecutor);
 
         scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
         closer.register(new ExecutorCloser(scheduledExecutor, 500, TimeUnit.MILLISECONDS));
 
-        final File datastoreRoot = folder.newFolder();
-        final TestMemoryBackend testBackend = new TestMemoryBackend(datastoreRoot);
+        backendRoot = folder.newFolder();
+        final TestMemoryBackend testBackend = new TestMemoryBackend(backendRoot);
         this.backend = testBackend;
 
         dataStore = new AbstractSharedCachingDataStore() {
@@ -126,11 +134,74 @@ public class CachingDataStoreTest extend
         dataStore.listeningExecutor = listeningExecutor;
         dataStore.schedulerExecutor = scheduledExecutor;
         dataStore.executor = sameThreadExecutor();
+        dsPath = new File(root.getAbsolutePath(), "ds").getAbsolutePath();
+        dataStore.setPath(dsPath);
         dataStore.init(root.getAbsolutePath());
 
         LOG.info("Finished init");
     }
 
+    @Test
+    public void loadCacheErrorDirectTemp() throws Exception {
+        LOG.info("Started loadCacheErrorDirectTemp");
+        loadDirectBackendTemp(64 * 1024 * 1024);
+        LOG.info("Finished loadCacheErrorDirectTemp");
+    }
+
+    @Test
+    public void cacheZeroDirectTemp() throws Exception {
+        LOG.info("Started cacheZeroDirectTemp");
+        loadDirectBackendTemp(0);
+        LOG.info("Finished cacheZeroDirectTemp");
+    }
+
+    public void loadDirectBackendTemp(long cacheSize) throws Exception {
+        LOG.info("Started loadDirectBackendTemp");
+        dataStore.close();
+        init(1, (int) cacheSize, 0);
+        String path = FilenameUtils
+            .normalizeNoEndSeparator(new File(dsPath).getAbsolutePath());
+        String home = FilenameUtils.normalizeNoEndSeparator(new File(root.getAbsolutePath()).getAbsolutePath());
+
+        dataStore.cache = new CompositeDataStoreCache(path , new File(home), cacheSize, 0,
+            0,
+            new TestErrorCacheLoader(backendRoot, 40, true), new StagingUploader() {
+            @Override public void write(String id, File file) throws DataStoreException {
+                backend.write(new DataIdentifier(id), file);
+            }
+        }, statsProvider, listeningExecutor, scheduledExecutor, dataStore.executor, 300,
+            600);
+
+        File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
+        String id = getIdForInputStream(f);
+
+        DataRecord rec;
+        if (cacheSize != 0) {
+            backend.write(new DataIdentifier(id), f);
+            rec = dataStore.getRecordIfStored(new DataIdentifier(id));
+        } else {
+            FileInputStream fin = new FileInputStream(f);
+            closer.register(fin);
+            rec = dataStore.addRecord(fin);
+        }
+        assertEquals(id, rec.getIdentifier().toString());
+        InputStream is = rec.getStream();
+        closer.register(is);
+
+        assertNotNull(is);
+        assertTrue(is instanceof LazyFileInputStream);
+        ((LazyFileInputStream)is).open();
+
+        File tmp = new File(new File(path), "tmp");
+        Collection<File> temp0cacheFiles =
+            FileUtils.listFiles(tmp, FileFilterUtils.prefixFileFilter("temp0cache"), null);
+        assertEquals(1, temp0cacheFiles.size());
+
+        assertFile(is, f, folder, false);
+
+        LOG.info("Finished loadDirectBackendTemp");
+    }
+
     /**
      * Add, get, delete when zero cache size.
      * @throws Exception
@@ -493,14 +564,20 @@ public class CachingDataStoreTest extend
         dataStore.close();
     }
 
-    private static void assertFile(InputStream is, File org, TemporaryFolder folder)
+    private static void assertFile(InputStream is, File org, TemporaryFolder folder) throws IOException {
+        assertFile(is, org, folder, true);
+    }
+
+    private static void assertFile(InputStream is, File org, TemporaryFolder folder, boolean close)
         throws IOException {
         try {
             File ret = folder.newFile();
-            FileUtils.copyInputStreamToFile(is, ret);
+            copyToFile(is, ret);
             assertTrue(Files.equal(org, ret));
         } finally {
-            IOUtils.closeQuietly(is);
+            if (close) {
+                IOUtils.closeQuietly(is);
+            }
         }
     }