Search in sources :

Example 36 with SegmentHandle

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

the class SimpleStorageTests method testSimpleReadWrite.

@Test
public void testSimpleReadWrite() throws Exception {
    String segmentName = createSegmentName("testSimpleReadWrite");
    try (Storage s = createStorage()) {
        s.initialize(DEFAULT_EPOCH);
        createSegment(segmentName, s);
        Assert.assertTrue("Expected the segment to exist.", s.exists(segmentName, null).join());
        SegmentHandle writeHandle = s.openWrite(segmentName).join();
        Assert.assertEquals(segmentName, writeHandle.getSegmentName());
        Assert.assertEquals(false, writeHandle.isReadOnly());
        byte[] writeBuffer = new byte[10];
        populate(writeBuffer);
        s.write(writeHandle, 0, new ByteArrayInputStream(writeBuffer), writeBuffer.length, null).join();
        SegmentHandle readHandle = s.openRead(segmentName).join();
        Assert.assertEquals(segmentName, readHandle.getSegmentName());
        Assert.assertEquals(true, readHandle.isReadOnly());
        byte[] readBuffer = new byte[writeBuffer.length];
        int bytesRead = s.read(readHandle, 0, readBuffer, 0, writeBuffer.length, null).get();
        Assert.assertEquals(writeBuffer.length, bytesRead);
        Assert.assertArrayEquals(writeBuffer, readBuffer);
    }
}
Also used : Storage(io.pravega.segmentstore.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) Test(org.junit.Test)

Example 37 with SegmentHandle

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

the class SimpleStorageTests method testFencing.

/**
 * Tests fencing abilities. We create two different Storage objects with different owner ids.
 * Part 1: Creation:
 * * We create the Segment on Storage1:
 * ** We verify that Storage1 can execute all operations.
 * ** We verify that Storage2 can execute only read-only operations.
 * * We open the Segment on Storage2:
 * ** We verify that Storage1 can execute only read-only operations.
 * ** We verify that Storage2 can execute all operations.
 */
@Test
@Override
public void testFencing() throws Exception {
    final long epoch1 = 1;
    final long epoch2 = 2;
    final String segmentName = "segment";
    try (val storage1 = createStorage();
        val storage2 = createStorage()) {
        storage1.initialize(epoch1);
        storage2.initialize(epoch2);
        // Create segment in Storage1 (thus Storage1 owns it for now).
        storage1.create(segmentName, TIMEOUT).join();
        // Storage1 should be able to execute all operations.
        SegmentHandle handle1 = storage1.openWrite(segmentName).join();
        verifyWriteOperationsSucceed(handle1, storage1);
        verifyReadOnlyOperationsSucceed(handle1, storage1);
        // Open the segment in Storage2 (thus Storage2 owns it for now).
        SegmentHandle handle2 = storage2.openWrite(segmentName).join();
        // Storage1 should be able to execute only read-only operations.
        verifyWriteOperationsFail(handle1, storage1);
        verifyReadOnlyOperationsSucceed(handle1, storage1);
        // Storage2 should be able to execute all operations.
        verifyReadOnlyOperationsSucceed(handle2, storage2);
        verifyWriteOperationsSucceed(handle2, storage2);
        // Seal and Delete (these should be run last, otherwise we can't run our test).
        verifyFinalWriteOperationsFail(handle1, storage1);
        verifyFinalWriteOperationsSucceed(handle2, storage2);
    }
}
Also used : lombok.val(lombok.val) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) Test(org.junit.Test)

Example 38 with SegmentHandle

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

the class SimpleStorageTests method testConsecutiveWrites.

@Test
public void testConsecutiveWrites() throws Exception {
    String segmentName = createSegmentName("testConsecutiveWrites");
    try (Storage s = createStorage()) {
        s.initialize(DEFAULT_EPOCH);
        createSegment(segmentName, s);
        Assert.assertTrue("Expected the segment to exist.", s.exists(segmentName, null).join());
        SegmentHandle writeHandle = s.openWrite(segmentName).join();
        Assert.assertEquals(segmentName, writeHandle.getSegmentName());
        Assert.assertFalse(writeHandle.isReadOnly());
        byte[] writeBuffer = new byte[15];
        populate(writeBuffer);
        int totalBytesWritten = 0;
        for (int i = 1; i <= 5; i++) {
            s.write(writeHandle, totalBytesWritten, new ByteArrayInputStream(writeBuffer, totalBytesWritten, i), i, null).join();
            totalBytesWritten += i;
        }
        SegmentHandle readHandle = s.openRead(segmentName).join();
        Assert.assertEquals(segmentName, readHandle.getSegmentName());
        Assert.assertTrue(readHandle.isReadOnly());
        byte[] readBuffer = new byte[writeBuffer.length];
        int bytesRead = s.read(readHandle, 0, readBuffer, 0, writeBuffer.length, null).get();
        Assert.assertEquals(writeBuffer.length, bytesRead);
        Assert.assertArrayEquals(writeBuffer, readBuffer);
    }
}
Also used : Storage(io.pravega.segmentstore.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) Test(org.junit.Test)

Example 39 with SegmentHandle

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

the class SimpleStorageTests method testNormalRead.

/**
 * Tests a read scenario with no issues or failures.
 */
@Test
public void testNormalRead() throws Exception {
    // Write data.
    String segmentName = "foo_open";
    try (Storage s = createStorage()) {
        s.initialize(DEFAULT_EPOCH);
        createSegment(segmentName, s);
        SegmentHandle handle = s.openWrite(segmentName).join();
        long expectedLength = 0;
        ByteArrayOutputStream writtenData = new ByteArrayOutputStream();
        for (int i = 0; i < WRITE_COUNT; i++) {
            byte[] data = new byte[i + 1];
            populate(data);
            s.write(handle, expectedLength, new ByteArrayInputStream(data), data.length, null).join();
            writtenData.write(data);
            expectedLength += data.length;
        }
        // Check written data via a Read Operation, from every offset from 0 to length/2
        byte[] expectedData = writtenData.toByteArray();
        val readHandle = s.openRead(segmentName).join();
        for (int startOffset = 0; startOffset < expectedLength / 2; startOffset++) {
            int readLength = (int) (expectedLength - 2 * startOffset);
            byte[] actualData = new byte[readLength];
            int readBytes = s.read(readHandle, startOffset, actualData, 0, actualData.length, null).join();
            Assert.assertEquals("Unexpected number of bytes read with start offset " + startOffset, actualData.length, readBytes);
            AssertExtensions.assertArrayEquals("Unexpected data read back with start offset " + startOffset, expectedData, startOffset, actualData, 0, readLength);
        }
    }
}
Also used : lombok.val(lombok.val) Storage(io.pravega.segmentstore.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) Test(org.junit.Test)

Example 40 with SegmentHandle

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

the class SystemJournalTests method testSimpleBootstrapWithMultipleFailovers.

private void testSimpleBootstrapWithMultipleFailovers(int containerId, ChunkStorage chunkStorage, ChunkedSegmentStorageConfig config, Consumer<Long> faultInjection) throws Exception {
    @Cleanup CleanupHelper cleanupHelper = new CleanupHelper();
    String systemSegmentName = SystemJournal.getChunkStorageSystemSegments(containerId)[0];
    long epoch = 0;
    val data = new InMemorySnapshotInfoStore();
    val snapshotInfoStore = new SnapshotInfoStore(containerId, snapshotId -> data.setSnapshotId(containerId, snapshotId), () -> data.getSnapshotId(containerId));
    long offset = 0;
    ChunkedSegmentStorage oldChunkedSegmentStorage = null;
    SegmentHandle oldHandle = null;
    for (int i = 1; i < 10; i++) {
        // Epoch 2
        epoch++;
        ChunkMetadataStore metadataStoreAfterCrash = getMetadataStore();
        cleanupHelper.add(metadataStoreAfterCrash);
        ChunkedSegmentStorage segmentStorageInLoop = new ChunkedSegmentStorage(containerId, chunkStorage, metadataStoreAfterCrash, executorService(), config);
        cleanupHelper.add(segmentStorageInLoop);
        segmentStorageInLoop.initialize(epoch);
        segmentStorageInLoop.getGarbageCollector().initialize(new InMemoryTaskQueueManager()).join();
        segmentStorageInLoop.bootstrap(snapshotInfoStore, null).join();
        deleteGarbage(segmentStorageInLoop);
        checkSystemSegmentsLayout(segmentStorageInLoop);
        val h = segmentStorageInLoop.openWrite(systemSegmentName).join();
        if (null != oldChunkedSegmentStorage) {
            oldChunkedSegmentStorage.write(oldHandle, offset, new ByteArrayInputStream("junk".getBytes()), 4, null).join();
        }
        val b1 = "Test".getBytes();
        segmentStorageInLoop.write(h, offset, new ByteArrayInputStream(b1), b1.length, null).join();
        offset += b1.length;
        val b2 = Integer.toString(i).getBytes();
        segmentStorageInLoop.write(h, offset, new ByteArrayInputStream(b2), b2.length, null).join();
        offset += b2.length;
        oldChunkedSegmentStorage = segmentStorageInLoop;
        oldHandle = h;
    }
    if (null != faultInjection) {
        faultInjection.accept(epoch);
    }
    epoch++;
    @Cleanup ChunkMetadataStore metadataStoreFinal = getMetadataStore();
    @Cleanup ChunkedSegmentStorage segmentStorageFinal = new ChunkedSegmentStorage(containerId, chunkStorage, metadataStoreFinal, executorService(), config);
    segmentStorageFinal.initialize(epoch);
    segmentStorageFinal.getGarbageCollector().initialize(new InMemoryTaskQueueManager()).join();
    segmentStorageFinal.bootstrap(snapshotInfoStore, null).join();
    deleteGarbage(segmentStorageFinal);
    checkSystemSegmentsLayout(segmentStorageFinal);
    val info = segmentStorageFinal.getStreamSegmentInfo(systemSegmentName, null).join();
    Assert.assertEquals(offset, info.getLength());
    byte[] out = new byte[Math.toIntExact(offset)];
    val hr = segmentStorageFinal.openRead(systemSegmentName).join();
    segmentStorageFinal.read(hr, 0, out, 0, Math.toIntExact(offset), null).join();
    val expected = "Test1Test2Test3Test4Test5Test6Test7Test8Test9";
    val actual = new String(out);
    Assert.assertEquals(expected, actual);
}
Also used : lombok.val(lombok.val) Cleanup(lombok.Cleanup) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) InMemoryTaskQueueManager(io.pravega.segmentstore.storage.mocks.InMemoryTaskQueueManager) ByteArrayInputStream(java.io.ByteArrayInputStream) InMemorySnapshotInfoStore(io.pravega.segmentstore.storage.mocks.InMemorySnapshotInfoStore) InMemorySnapshotInfoStore(io.pravega.segmentstore.storage.mocks.InMemorySnapshotInfoStore) ChunkMetadataStore(io.pravega.segmentstore.storage.metadata.ChunkMetadataStore)

Aggregations

SegmentHandle (io.pravega.segmentstore.storage.SegmentHandle)43 lombok.val (lombok.val)27 Test (org.junit.Test)26 Storage (io.pravega.segmentstore.storage.Storage)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)14 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)13 StreamSegmentSealedException (io.pravega.segmentstore.contracts.StreamSegmentSealedException)12 StorageNotPrimaryException (io.pravega.segmentstore.storage.StorageNotPrimaryException)12 Exceptions (io.pravega.common.Exceptions)10 Cleanup (lombok.Cleanup)10 CompletableFuture (java.util.concurrent.CompletableFuture)9 Futures (io.pravega.common.concurrent.Futures)8 StreamSegmentExistsException (io.pravega.segmentstore.contracts.StreamSegmentExistsException)8 NameUtils (io.pravega.shared.NameUtils)8 List (java.util.List)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Preconditions (com.google.common.base.Preconditions)7 StreamSegmentInformation (io.pravega.segmentstore.contracts.StreamSegmentInformation)7 SegmentRollingPolicy (io.pravega.segmentstore.storage.SegmentRollingPolicy)7