Search in sources :

Example 11 with StorageNotPrimaryException

use of io.pravega.segmentstore.storage.StorageNotPrimaryException in project pravega by pravega.

the class OpenWriteOperationTests method testConcurrentFenceOutLower.

/**
 * Tests the case when the OpenWriteOperation thinks it is the highest epoch, fences out, but then it finds out
 * it was beaten to it by a higher epoch instance.
 * Expected outcome: StorageNotPrimaryException and no side effects (it should back off).
 */
@Test
public void testConcurrentFenceOutLower() throws Exception {
    @Cleanup val fs = new MockFileSystem();
    val context1 = newContext(1, fs);
    new CreateOperation(SEGMENT_NAME, context1).call();
    val context2 = newContext(context1.epoch + 1, fs);
    val context3 = newContext(context2.epoch + 1, fs);
    Path survivingFilePath = context3.getFileName(SEGMENT_NAME, 0);
    fs.setOnCreate(path -> fs.new CreateNewFileAction(survivingFilePath));
    AssertExtensions.assertThrows("OpenWrite did not fail when a concurrent higher epoch file was created.", new OpenWriteOperation(SEGMENT_NAME, context2)::call, ex -> ex instanceof StorageNotPrimaryException);
    // In a real-world situation, we'd have just one surviving file. However we were not able to successfully carry
    // out the fencing operation, hence no cleanup could be done (testConcurrentFenceOutHigher should check this though).
    Assert.assertEquals("Unexpected number of files in the file system.", 2, fs.getFileCount());
    Assert.assertTrue("Original file was deleted.", fs.exists(context1.getFileName(SEGMENT_NAME, 0)));
    Assert.assertTrue("Higher epoch file was deleted.", fs.exists(survivingFilePath));
}
Also used : lombok.val(lombok.val) Path(org.apache.hadoop.fs.Path) Cleanup(lombok.Cleanup) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Test(org.junit.Test)

Example 12 with StorageNotPrimaryException

use of io.pravega.segmentstore.storage.StorageNotPrimaryException in project pravega by pravega.

the class SealOperationTests method testLastFileEmptyNonReadOnlyFencedOut.

/**
 * Tests the case when the last file was thought to be read-only but it got changed in the background and
 * fenced out by a higher-epoch instance.
 * Expected outcome: StorageNotPrimaryException.
 */
@Test
public void testLastFileEmptyNonReadOnlyFencedOut() throws Exception {
    final String emptySegment = SEGMENT_NAME;
    final String nonEmptySegment = SEGMENT_NAME + "nonempty";
    @Cleanup val fs = new MockFileSystem();
    val context1 = newContext(1, fs);
    val context2 = newContext(context1.epoch + 1, fs);
    new CreateOperation(emptySegment, context1).call();
    val emptySegmentHandle1 = new OpenWriteOperation(emptySegment, context1).call();
    // Part 1: empty segment first file (it will be deleted when OpenWrite is invoked with epoch 2)
    new OpenWriteOperation(emptySegment, context2).call();
    AssertExtensions.assertThrows("SealOperation did not fail when it was fenced out (empty segment).", new SealOperation(emptySegmentHandle1, context1)::run, ex -> ex instanceof StorageNotPrimaryException);
    Assert.assertFalse("Last file in emptySegmentHandle1 was marked as read-only.", emptySegmentHandle1.getLastFile().isReadOnly());
    Assert.assertFalse("Last file in file system (empty segment) was set as 'sealed'.", fs.exists(emptySegmentHandle1.getLastFile().getPath()));
    // Part 1: non-empty segment first file (it will be marked read-only when OpenWrite is invoked with epoch 2)
    new CreateOperation(nonEmptySegment, context1).call();
    val nonEmptySegmentHandle1 = new OpenWriteOperation(nonEmptySegment, context1).call();
    new WriteOperation(nonEmptySegmentHandle1, 0, new ByteArrayInputStream(new byte[1]), 1, context1).run();
    new OpenWriteOperation(nonEmptySegment, context2).call();
    AssertExtensions.assertThrows("SealOperation did not fail when it was fenced out (non-empty segment).", new SealOperation(nonEmptySegmentHandle1, context1)::run, ex -> ex instanceof StorageNotPrimaryException);
    Assert.assertFalse("Last file in nonEmptySegmentHandle1 was marked as read-only.", emptySegmentHandle1.getLastFile().isReadOnly());
    Assert.assertFalse("Last file in file system (empty segment) was set as 'sealed'.", context1.isSealed(nonEmptySegmentHandle1.getLastFile()));
}
Also used : lombok.val(lombok.val) ByteArrayInputStream(java.io.ByteArrayInputStream) Cleanup(lombok.Cleanup) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Test(org.junit.Test)

Example 13 with StorageNotPrimaryException

use of io.pravega.segmentstore.storage.StorageNotPrimaryException in project pravega by pravega.

the class WriteOperationTests method testFenceOutMissingFile.

/**
 * Tests the case when the current file (previously empty) has disappeared due to it being fenced out.
 * Expected behavior: StorageNotPrimaryException with no side effects.
 */
@Test
public void testFenceOutMissingFile() throws Exception {
    @Cleanup val fs = new MockFileSystem();
    val context1 = newContext(1, fs);
    new CreateOperation(SEGMENT_NAME, context1).call();
    val handle1 = new OpenWriteOperation(SEGMENT_NAME, context1).call();
    val context2 = newContext(2, fs);
    val handle2 = new OpenWriteOperation(SEGMENT_NAME, context2).call();
    AssertExtensions.assertThrows("WriteOperation did not fail when it was fenced out by removing a file.", new WriteOperation(handle1, 0, new ByteArrayInputStream(new byte[1]), 1, context1)::run, ex -> ex instanceof StorageNotPrimaryException);
    Assert.assertEquals("Unexpected number of files in the filesystem.", 1, fs.getFileCount());
    Assert.assertEquals("Unexpected size of the file in the filesystem.", 0, fs.getFileStatus(handle2.getLastFile().getPath()).getLen());
}
Also used : lombok.val(lombok.val) ByteArrayInputStream(java.io.ByteArrayInputStream) Cleanup(lombok.Cleanup) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Test(org.junit.Test)

Example 14 with StorageNotPrimaryException

use of io.pravega.segmentstore.storage.StorageNotPrimaryException in project pravega by pravega.

the class WriteOperationTests method testFenceOutReadOnlyFile.

/**
 * Tests the case when the current file (non-empty) has been marked as read-only due to it being fenced out.
 * Expected behavior: StorageNotPrimaryException with no side effects.
 */
@Test
public void testFenceOutReadOnlyFile() throws Exception {
    @Cleanup val fs = new MockFileSystem();
    val context1 = newContext(1, fs);
    new CreateOperation(SEGMENT_NAME, context1).call();
    val handle1 = new OpenWriteOperation(SEGMENT_NAME, context1).call();
    new WriteOperation(handle1, 0, new ByteArrayInputStream(new byte[1]), 1, context1).run();
    val context2 = newContext(2, fs);
    val handle2 = new OpenWriteOperation(SEGMENT_NAME, context2).call();
    AssertExtensions.assertThrows("WriteOperation did not fail when it was fenced out by making a file read-only.", new WriteOperation(handle1, 1, new ByteArrayInputStream(new byte[1]), 1, context1)::run, ex -> ex instanceof StorageNotPrimaryException);
    Assert.assertEquals("Unexpected number of files in the filesystem.", 2, fs.getFileCount());
    Assert.assertEquals("Unexpected size of the first file in the filesystem.", 1, fs.getFileStatus(handle1.getLastFile().getPath()).getLen());
    Assert.assertEquals("Unexpected size of the last file in the filesystem.", 0, fs.getFileStatus(handle2.getLastFile().getPath()).getLen());
}
Also used : lombok.val(lombok.val) ByteArrayInputStream(java.io.ByteArrayInputStream) Cleanup(lombok.Cleanup) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Test(org.junit.Test)

Example 15 with StorageNotPrimaryException

use of io.pravega.segmentstore.storage.StorageNotPrimaryException in project pravega by pravega.

the class RollingStorage method updateHandle.

private void updateHandle(RollingSegmentHandle handle, byte[] data) throws StreamSegmentException {
    try {
        this.baseStorage.write(handle.getHeaderHandle(), handle.getHeaderLength(), new ByteArrayInputStream(data), data.length);
        handle.increaseHeaderLength(data.length);
        log.debug("Header for '{}' updated with {} bytes for a length of {}.", handle.getSegmentName(), data.length, handle.getHeaderLength());
    } catch (BadOffsetException ex) {
        // If we get BadOffsetException when writing the Handle, it means it was modified externally.
        throw new StorageNotPrimaryException(handle.getSegmentName(), ex);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException)

Aggregations

StorageNotPrimaryException (io.pravega.segmentstore.storage.StorageNotPrimaryException)15 lombok.val (lombok.val)13 Cleanup (lombok.Cleanup)9 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Path (org.apache.hadoop.fs.Path)3 BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)2 ByteArraySegment (io.pravega.common.util.ByteArraySegment)1 FileNotFoundException (java.io.FileNotFoundException)1 FileAlreadyExistsException (org.apache.hadoop.fs.FileAlreadyExistsException)1