You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2013/05/07 07:53:47 UTC

svn commit: r1479768 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/core/ lucene/core/src/test/org/apache/lucene/store/ lucene/test-framework/ lucene/test-framework/src/java/org/apache/lucene/store/

Author: shaie
Date: Tue May  7 05:53:47 2013
New Revision: 1479768

URL: http://svn.apache.org/r1479768
Log:
LUCENE-4982: Make MockIndexOutputWrapper check disk full on copyBytes

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/lucene/core/   (props changed)
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestMockDirectoryWrapper.java
    lucene/dev/branches/branch_4x/lucene/test-framework/   (props changed)
    lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestMockDirectoryWrapper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestMockDirectoryWrapper.java?rev=1479768&r1=1479767&r2=1479768&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestMockDirectoryWrapper.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestMockDirectoryWrapper.java Tue May  7 05:53:47 2013
@@ -51,4 +51,42 @@ public class TestMockDirectoryWrapper ex
     iw.close();
     dir.close();
   }
+  
+  public void testDiskFull() throws IOException {
+    // test writeBytes
+    MockDirectoryWrapper dir = newMockDirectory();
+    dir.setMaxSizeInBytes(2);
+    final byte[] bytes = new byte[] { 1, 2};
+    IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT);
+    out.writeBytes(bytes, bytes.length); // first write should succeed
+    // flush() to ensure the written bytes are not buffered and counted
+    // against the directory size
+    out.flush();
+    try {
+      out.writeBytes(bytes, bytes.length);
+      fail("should have failed on disk full");
+    } catch (IOException e) {
+      // expected
+    }
+    out.close();
+    dir.close();
+    
+    // test copyBytes
+    dir = newMockDirectory();
+    dir.setMaxSizeInBytes(2);
+    out = dir.createOutput("foo", IOContext.DEFAULT);
+    out.copyBytes(new ByteArrayDataInput(bytes), bytes.length); // first copy should succeed
+    // flush() to ensure the written bytes are not buffered and counted
+    // against the directory size
+    out.flush();
+    try {
+      out.copyBytes(new ByteArrayDataInput(bytes), bytes.length);
+      fail("should have failed on disk full");
+    } catch (IOException e) {
+      // expected
+    }
+    out.close();
+    dir.close();
+  }
+  
 }

Modified: lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java?rev=1479768&r1=1479767&r2=1479768&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java (original)
+++ lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java Tue May  7 05:53:47 2013
@@ -43,6 +43,50 @@ public class MockIndexOutputWrapper exte
     this.delegate = delegate;
   }
 
+  private void checkCrashed() throws IOException {
+    // If MockRAMDir crashed since we were opened, then don't write anything
+    if (dir.crashed) {
+      throw new IOException("MockRAMDirectory was crashed; cannot write to " + name);
+    }
+  }
+  
+  private void checkDiskFull(byte[] b, int offset, DataInput in, long len) throws IOException {
+    long freeSpace = dir.maxSize == 0 ? 0 : dir.maxSize - dir.sizeInBytes();
+    long realUsage = 0;
+
+    // Enforce disk full:
+    if (dir.maxSize != 0 && freeSpace < len) {
+      // Compute the real disk free.  This will greatly slow
+      // down our test but makes it more accurate:
+      realUsage = dir.getRecomputedActualSizeInBytes();
+      freeSpace = dir.maxSize - realUsage;
+    }
+
+    if (dir.maxSize != 0 && freeSpace < len) {
+      if (freeSpace > 0) {
+        realUsage += freeSpace;
+        if (b != null) {
+          delegate.writeBytes(b, offset, (int) freeSpace);
+        } else {
+          delegate.copyBytes(in, len);
+        }
+      }
+      if (realUsage > dir.maxUsedSize) {
+        dir.maxUsedSize = realUsage;
+      }
+      String message = "fake disk full at " + dir.getRecomputedActualSizeInBytes() + " bytes when writing " + name + " (file length=" + delegate.length();
+      if (freeSpace > 0) {
+        message += "; wrote " + freeSpace + " of " + len + " bytes";
+      }
+      message += ")";
+      if (LuceneTestCase.VERBOSE) {
+        System.out.println(Thread.currentThread().getName() + ": MDW: now throw fake disk full");
+        new Throwable().printStackTrace(System.out);
+      }
+      throw new IOException(message);
+    }
+  }
+  
   @Override
   public void close() throws IOException {
     try {
@@ -75,48 +119,16 @@ public class MockIndexOutputWrapper exte
   
   @Override
   public void writeBytes(byte[] b, int offset, int len) throws IOException {
-    long freeSpace = dir.maxSize == 0 ? 0 : dir.maxSize - dir.sizeInBytes();
-    long realUsage = 0;
-    // If MockRAMDir crashed since we were opened, then
-    // don't write anything:
-    if (dir.crashed)
-      throw new IOException("MockRAMDirectory was crashed; cannot write to " + name);
-
-    // Enforce disk full:
-    if (dir.maxSize != 0 && freeSpace <= len) {
-      // Compute the real disk free.  This will greatly slow
-      // down our test but makes it more accurate:
-      realUsage = dir.getRecomputedActualSizeInBytes();
-      freeSpace = dir.maxSize - realUsage;
-    }
-
-    if (dir.maxSize != 0 && freeSpace <= len) {
-      if (freeSpace > 0) {
-        realUsage += freeSpace;
-        delegate.writeBytes(b, offset, (int) freeSpace);
-      }
-      if (realUsage > dir.maxUsedSize) {
-        dir.maxUsedSize = realUsage;
-      }
-      String message = "fake disk full at " + dir.getRecomputedActualSizeInBytes() + " bytes when writing " + name + " (file length=" + delegate.length();
-      if (freeSpace > 0) {
-        message += "; wrote " + freeSpace + " of " + len + " bytes";
-      }
-      message += ")";
-      if (LuceneTestCase.VERBOSE) {
-        System.out.println(Thread.currentThread().getName() + ": MDW: now throw fake disk full");
-        new Throwable().printStackTrace(System.out);
-      }
-      throw new IOException(message);
+    checkCrashed();
+    checkDiskFull(b, offset, null, len);
+    
+    if (dir.randomState.nextInt(200) == 0) {
+      final int half = len/2;
+      delegate.writeBytes(b, offset, half);
+      Thread.yield();
+      delegate.writeBytes(b, offset+half, len-half);
     } else {
-      if (dir.randomState.nextInt(200) == 0) {
-        final int half = len/2;
-        delegate.writeBytes(b, offset, half);
-        Thread.yield();
-        delegate.writeBytes(b, offset+half, len-half);
-      } else {
-        delegate.writeBytes(b, offset, len);
-      }
+      delegate.writeBytes(b, offset, len);
     }
 
     dir.maybeThrowDeterministicException();
@@ -151,8 +163,10 @@ public class MockIndexOutputWrapper exte
 
   @Override
   public void copyBytes(DataInput input, long numBytes) throws IOException {
+    checkCrashed();
+    checkDiskFull(null, 0, input, numBytes);
+    
     delegate.copyBytes(input, numBytes);
-    // TODO: we may need to check disk full here as well
     dir.maybeThrowDeterministicException();
   }