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 ch...@apache.org on 2015/03/19 11:44:50 UTC

svn commit: r1667696 - in /jackrabbit/oak/trunk/oak-lucene/src: main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopier.java test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopierTest.java

Author: chetanm
Date: Thu Mar 19 10:44:50 2015
New Revision: 1667696

URL: http://svn.apache.org/r1667696
Log:
OAK-2649 - IndexCopier might create empty files in case of error occuring while copying

Modified:
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopier.java
    jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopierTest.java

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopier.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopier.java?rev=1667696&r1=1667695&r2=1667696&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopier.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopier.java Thu Mar 19 10:44:50 2015
@@ -188,9 +188,12 @@ class IndexCopier implements CopyOnReadS
                 @Override
                 public void run() {
                     String name = reference.name;
+                    boolean success = false;
+                    boolean copyAttempted = false;
                     try {
                         if (!local.fileExists(name)) {
                             long start = System.currentTimeMillis();
+                            copyAttempted = true;
                             remote.copy(local, name, name, IOContext.READ);
                             reference.markValid();
                             downloadTime.addAndGet(System.currentTimeMillis() - start);
@@ -210,11 +213,22 @@ class IndexCopier implements CopyOnReadS
                                 reference.markValid();
                             }
                         }
+                        success = true;
                     } catch (IOException e) {
                         //TODO In case of exception there would not be any other attempt
                         //to download the file. Look into support for retry
                         log.warn("Error occurred while copying file [{}] " +
                                 "from {} to {}", name, remote, local, e);
+                    } finally {
+                        if (copyAttempted && !success){
+                            try {
+                                if (local.fileExists(name)) {
+                                    local.deleteFile(name);
+                                }
+                            } catch (IOException e) {
+                                log.warn("Error occurred while deleting corrupted file [{}] from [{}]", name, local, e);
+                            }
+                        }
                     }
                 }
             });

Modified: jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopierTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopierTest.java?rev=1667696&r1=1667695&r2=1667696&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopierTest.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexCopierTest.java Thu Mar 19 10:44:50 2015
@@ -43,6 +43,7 @@ import static org.apache.jackrabbit.oak.
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class IndexCopierTest {
     private Random rnd = new Random();
@@ -153,6 +154,34 @@ public class IndexCopierTest {
     }
 
     @Test
+    public void deleteCorruptedFile() throws Exception{
+        Directory baseDir = new RAMDirectory();
+        IndexDefinition defn = new IndexDefinition(root, builder.getNodeState());
+        RAMIndexCopier c1 = new RAMIndexCopier(baseDir, sameThreadExecutor(), getWorkDir());
+
+        Directory remote = new RAMDirectory(){
+            @Override
+            public IndexInput openInput(String name, IOContext context) throws IOException {
+                throw new IllegalStateException("boom");
+            }
+        };
+
+        String fileName = "failed.txt";
+        Directory wrapped = c1.wrap("/foo" , defn, remote);
+
+        byte[] t1 = writeFile(remote , fileName);
+
+        try {
+            readAndAssert(wrapped, fileName, t1);
+            fail("Read of file should have failed");
+        } catch (IllegalStateException ignore){
+
+        }
+
+        assertFalse(c1.baseDir.fileExists(fileName));
+    }
+
+    @Test
     public void deletesOnClose() throws Exception{
         //Use a close safe dir. In actual case the FSDir would
         //be opened on same file system hence it can retain memory