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 06:37:59 UTC

svn commit: r1844206 - in /jackrabbit/oak/branches/1.8: ./ oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/ oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/

Author: amitj
Date: Thu Oct 18 06:37:59 2018
New Revision: 1844206

URL: http://svn.apache.org/viewvc?rev=1844206&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.8/   (props changed)
    jackrabbit/oak/branches/1.8/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
    jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
    jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java

Propchange: jackrabbit/oak/branches/1.8/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct 18 06:37:59 2018
@@ -1,3 +1,3 @@
 /jackrabbit/oak/branches/1.0:1665962
-/jackrabbit/oak/trunk:1820660-1820661,1820729,1820734,1820859,1820861,1820878,1820888,1820947,1821027,1821130,1821140-1821141,1821178,1821237,1821240,1821249,1821258,1821325,1821358,1821361-1821362,1821370,1821375,1821393,1821477,1821487,1821516,1821617,1821663,1821665,1821668,1821681,1821847,1821975-1821983,1822121,1822201,1822207,1822527,1822723,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826932,1826957,1827423,1827472,1827486,1827816,1827977,1828349,1828439,1828502,1828529,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019,1830048,1830160,1830171,1830197,1830209,1830239,1830347,1830748,1830911,1830923,1831157
 -1831158,1831163,1831190,1831374,1831560,1831689,1832258,1832376,1832379,1832535,1833308,1833347,1833833,1834112,1834117,1834287,1834291,1834302,1834326,1834328,1834336,1834428,1834468,1834483,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835060,1835518,1835521,1835635,1835642,1835780,1835819,1836082,1836121,1836487,1836493,1837057,1837274,1837296,1837326,1837475,1837503,1837547,1837569,1837600,1837657,1837718,1837998,1838076,1838637,1839549,1839570,1839637,1839746,1840024,1840455,1840574,1841314,1841352,1843398
+/jackrabbit/oak/trunk:1820660-1820661,1820729,1820734,1820859,1820861,1820878,1820888,1820947,1821027,1821130,1821140-1821141,1821178,1821237,1821240,1821249,1821258,1821325,1821358,1821361-1821362,1821370,1821375,1821393,1821477,1821487,1821516,1821617,1821663,1821665,1821668,1821681,1821847,1821975-1821983,1822121,1822201,1822207,1822527,1822723,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826932,1826957,1827423,1827472,1827486,1827816,1827977,1828349,1828439,1828502,1828529,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019,1830048,1830160,1830171,1830197,1830209,1830239,1830347,1830748,1830911,1830923,1831157
 -1831158,1831163,1831190,1831374,1831560,1831689,1832258,1832376,1832379,1832535,1833308,1833347,1833833,1834112,1834117,1834287,1834291,1834302,1834326,1834328,1834336,1834428,1834468,1834483,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835060,1835518,1835521,1835635,1835642,1835780,1835819,1836082,1836121,1836487,1836493,1837057,1837274,1837296,1837326,1837475,1837503,1837547,1837569,1837600,1837657,1837718,1837998,1838076,1838637,1839549,1839570,1839637,1839746,1840024,1840455,1840574,1841314,1841352,1842677,1843222,1843231,1843398
 /jackrabbit/trunk:1345480

Modified: jackrabbit/oak/branches/1.8/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java?rev=1844206&r1=1844205&r2=1844206&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java (original)
+++ jackrabbit/oak/branches/1.8/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java Thu Oct 18 06:37:59 2018
@@ -39,6 +39,7 @@ import com.google.common.base.Stopwatch;
 import com.google.common.cache.CacheLoader;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterators;
+import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
@@ -53,11 +54,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;
 
 /**
@@ -130,7 +133,7 @@ public abstract class AbstractSharedCach
     /**
      * DataStore cache
      */
-    private CompositeDataStoreCache cache;
+    protected CompositeDataStoreCache cache;
 
     /**
      * The delegate backend
@@ -201,13 +204,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);
             }
@@ -296,12 +299,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;
         }
@@ -324,7 +329,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.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java?rev=1844206&r1=1844205&r2=1844206&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java (original)
+++ jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java Thu Oct 18 06:37:59 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;
@@ -112,7 +113,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");
@@ -170,6 +171,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);
         }
@@ -417,7 +427,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.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java?rev=1844206&r1=1844205&r2=1844206&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java (original)
+++ jackrabbit/oak/branches/1.8/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/CachingDataStoreTest.java Thu Oct 18 06:37:59 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,78 @@ 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);
+            }
+
+            @Override public void adopt(File f, File moved) throws IOException {
+                FileUtils.moveFile(f, moved);
+            }
+        }, 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 +568,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);
+            }
         }
     }