Search in sources :

Example 6 with SegmentRollingPolicy

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

the class ChunkedSegmentStorageTests method testParallelReadRequestsOnSingleSegmentWithReentry.

private void testParallelReadRequestsOnSingleSegmentWithReentry(int numberOfRequests, int threadPoolSize, boolean shouldBlock) throws Exception {
    String testSegmentName = "testSegment";
    // Force rollover after every 2 byte.
    SegmentRollingPolicy policy = new SegmentRollingPolicy(2);
    @Cleanup TestContext testContext = getTestContext(ChunkedSegmentStorageConfig.DEFAULT_CONFIG.toBuilder().indexBlockSize(3).build());
    if (!(testContext.metadataStore instanceof InMemoryMetadataStore)) {
        return;
    }
    CompletableFuture<Void> futureToWaitOn = shouldBlock ? new CompletableFuture<Void>() : CompletableFuture.completedFuture(null);
    // Step 1: Populate dummy system segment segment.
    // Write some data to system segment so that we can read it back in call back.
    val systemSegment = "SystemSegment";
    val hSystem = testContext.chunkedSegmentStorage.create(systemSegment, policy, null).get();
    testContext.chunkedSegmentStorage.write(hSystem, 0, new ByteArrayInputStream(new byte[1]), 1, null).join();
    // Step 2: Create test segment.
    val h = testContext.chunkedSegmentStorage.create(testSegmentName, policy, null).get();
    Assert.assertEquals(h.getSegmentName(), testSegmentName);
    Assert.assertFalse(h.isReadOnly());
    // Step 3: Write some data to test segment.
    long writeAt = 0;
    for (int i = 1; i < 5; i++) {
        testContext.chunkedSegmentStorage.write(h, writeAt, new ByteArrayInputStream(new byte[i]), i, null).join();
        writeAt += i;
    }
    TestUtils.checkSegmentLayout(testContext.metadataStore, testSegmentName, 2, 5);
    TestUtils.checkSegmentBounds(testContext.metadataStore, testSegmentName, 0, 10);
    TestUtils.checkReadIndexEntries(testContext.chunkedSegmentStorage, testContext.metadataStore, testSegmentName, 0, 10, true);
    TestUtils.checkChunksExistInStorage(testContext.chunkStorage, testContext.metadataStore, testSegmentName);
    // Step 4: Setup call backs that read system segment on each read.
    // Set up a call back which will be invoked during get call.
    ((InMemoryMetadataStore) testContext.metadataStore).setReadCallback(transactionData -> {
        // Make sure we don't invoke read for system segment itself.
        if (!transactionData.getKey().equals(systemSegment)) {
            return futureToWaitOn.thenComposeAsync(v -> checkDataReadAsync(systemSegment, testContext, 0, 1, executorService()), executorService()).thenApplyAsync(v -> null, executorService());
        }
        return CompletableFuture.completedFuture(null);
    });
    // Step 5: Read back data concurrently.
    @Cleanup("shutdownNow") ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
    CompletableFuture[] futures = new CompletableFuture[numberOfRequests];
    for (int i = 0; i < numberOfRequests; i++) {
        CompletableFuture<Void> f = testContext.chunkedSegmentStorage.getStreamSegmentInfo(testSegmentName, null).thenComposeAsync(info -> {
            Assert.assertFalse(info.isSealed());
            Assert.assertFalse(info.isDeleted());
            Assert.assertEquals(info.getName(), testSegmentName);
            Assert.assertEquals(info.getLength(), 10);
            Assert.assertEquals(info.getStartOffset(), 0);
            return checkDataReadAsync(testSegmentName, testContext, 0, 10, executor);
        }, executor);
        futures[i] = f;
    }
    if (shouldBlock) {
        futureToWaitOn.complete(null);
    }
    CompletableFuture.allOf(futures).join();
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Getter(lombok.Getter) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) StorageFullException(io.pravega.segmentstore.storage.StorageFullException) InMemoryTaskQueueManager(io.pravega.segmentstore.storage.mocks.InMemoryTaskQueueManager) ArrayList(java.util.ArrayList) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) HashSet(java.util.HashSet) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) AbstractInMemoryChunkStorage(io.pravega.segmentstore.storage.mocks.AbstractInMemoryChunkStorage) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) NoOpChunkStorage(io.pravega.segmentstore.storage.noop.NoOpChunkStorage) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ChunkMetadata(io.pravega.segmentstore.storage.metadata.ChunkMetadata) StreamSegmentTruncatedException(io.pravega.segmentstore.contracts.StreamSegmentTruncatedException) Longs(com.google.common.primitives.Longs) Executor(java.util.concurrent.Executor) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) UUID(java.util.UUID) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) ChunkMetadataStore(io.pravega.segmentstore.storage.metadata.ChunkMetadataStore) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) Preconditions(com.google.common.base.Preconditions) Assert(org.junit.Assert) Futures(io.pravega.common.concurrent.Futures) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ByteArrayInputStream(java.io.ByteArrayInputStream) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 7 with SegmentRollingPolicy

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

the class ChunkedSegmentStorageTests method testWrite.

/**
 * Test Write.
 *
 * @throws Exception Exception if any.
 */
@Test
public void testWrite() throws Exception {
    String testSegmentName = "foo";
    @Cleanup TestContext testContext = getTestContext();
    // Force rollover after every 2 byte.
    SegmentRollingPolicy policy = new SegmentRollingPolicy(2);
    // Create
    val hWrite = testContext.chunkedSegmentStorage.create(testSegmentName, policy, null).get();
    HashSet<String> chunksBefore = new HashSet<>();
    chunksBefore.addAll(TestUtils.getChunkNameList(testContext.metadataStore, testSegmentName));
    // Write some data.
    long writeAt = 0;
    for (int i = 1; i < 5; i++) {
        testContext.chunkedSegmentStorage.write(hWrite, writeAt, new ByteArrayInputStream(new byte[i]), i, null).join();
        writeAt += i;
    }
    HashSet<String> chunksAfter = new HashSet<>();
    chunksAfter.addAll(TestUtils.getChunkNameList(testContext.metadataStore, testSegmentName));
    TestUtils.checkGarbageCollectionQueue(testContext.chunkedSegmentStorage, chunksBefore, chunksAfter);
    int total = 10;
    checkDataRead(testSegmentName, testContext, 0, total);
}
Also used : lombok.val(lombok.val) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) ByteArrayInputStream(java.io.ByteArrayInputStream) Cleanup(lombok.Cleanup) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with SegmentRollingPolicy

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

the class ChunkedSegmentStorageTests method testSimpleScenario.

/**
 * Test simple scenario.
 *
 * @throws Exception Exception if any.
 */
@Test
public void testSimpleScenario() throws Exception {
    String testSegmentName = "foo";
    // Force rollover after every 2 byte.
    SegmentRollingPolicy policy = new SegmentRollingPolicy(2);
    @Cleanup TestContext testContext = getTestContext();
    testSimpleScenario(testSegmentName, policy, testContext);
}
Also used : SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 9 with SegmentRollingPolicy

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

the class ChunkedSegmentStorageTests method testParallelSegmentOperationsWithReentry.

private void testParallelSegmentOperationsWithReentry(int numberOfRequests, int threadPoolSize, boolean shouldBlock) throws Exception {
    // Force rollover after every 2 byte.
    SegmentRollingPolicy policy = new SegmentRollingPolicy(2);
    @Cleanup TestContext testContext = getTestContext();
    if (!(testContext.metadataStore instanceof InMemoryMetadataStore)) {
        return;
    }
    CompletableFuture<Void> futureToWaitOn = shouldBlock ? new CompletableFuture<Void>() : CompletableFuture.completedFuture(null);
    // Step 1: Populate system segment.
    // Write some data to system segment so that we can read it back in call back.
    val systemSegment = "SystemSegment";
    val h = testContext.chunkedSegmentStorage.create(systemSegment, policy, null).get();
    testContext.chunkedSegmentStorage.write(h, 0, new ByteArrayInputStream(new byte[1]), 1, null).join();
    // Step 2: Setup call backs.
    // Set up a call back which will be invoked during get call.
    ((InMemoryMetadataStore) testContext.metadataStore).setReadCallback(transactionData -> {
        // Make sure we don't invoke read for system segment itself.
        if (!transactionData.getKey().equals(systemSegment)) {
            return futureToWaitOn.thenComposeAsync(v -> checkDataReadAsync(systemSegment, testContext, 0, 1, executorService()), executorService()).thenApplyAsync(v -> null, executorService());
        }
        return CompletableFuture.completedFuture(null);
    });
    // Set up a call back which will be invoked during writeAll call.
    ((InMemoryMetadataStore) testContext.metadataStore).setWriteCallback(transactionDataList -> {
        // Make sure we don't invoke read for system segment itself.
        if (transactionDataList.stream().filter(t -> !t.getKey().equals(systemSegment)).findAny().isPresent()) {
            return futureToWaitOn.thenComposeAsync(v -> checkDataReadAsync(systemSegment, testContext, 0, 1, executorService()), executorService()).thenApplyAsync(v -> null, executorService());
        }
        return CompletableFuture.completedFuture(null);
    });
    // Step 3: Perform operations on multiple segments.
    @Cleanup("shutdownNow") ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
    CompletableFuture[] futures = new CompletableFuture[numberOfRequests];
    for (int i = 0; i < numberOfRequests; i++) {
        String testSegmentName = "test" + i;
        val f = testSimpleScenarioAsync(testSegmentName, policy, testContext, executor);
        futures[i] = f;
    }
    if (shouldBlock) {
        futureToWaitOn.complete(null);
    }
    CompletableFuture.allOf(futures).join();
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Getter(lombok.Getter) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) StorageFullException(io.pravega.segmentstore.storage.StorageFullException) InMemoryTaskQueueManager(io.pravega.segmentstore.storage.mocks.InMemoryTaskQueueManager) ArrayList(java.util.ArrayList) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) HashSet(java.util.HashSet) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) AbstractInMemoryChunkStorage(io.pravega.segmentstore.storage.mocks.AbstractInMemoryChunkStorage) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) NoOpChunkStorage(io.pravega.segmentstore.storage.noop.NoOpChunkStorage) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ChunkMetadata(io.pravega.segmentstore.storage.metadata.ChunkMetadata) StreamSegmentTruncatedException(io.pravega.segmentstore.contracts.StreamSegmentTruncatedException) Longs(com.google.common.primitives.Longs) Executor(java.util.concurrent.Executor) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) UUID(java.util.UUID) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) ChunkMetadataStore(io.pravega.segmentstore.storage.metadata.ChunkMetadataStore) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) Preconditions(com.google.common.base.Preconditions) Assert(org.junit.Assert) Futures(io.pravega.common.concurrent.Futures) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ByteArrayInputStream(java.io.ByteArrayInputStream) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 10 with SegmentRollingPolicy

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

the class ChunkedSegmentStorageTests method testRepeatedTruncatesAtFullLength.

@Test
public void testRepeatedTruncatesAtFullLength() throws Exception {
    @Cleanup TestContext testContext = getTestContext();
    String testSegmentName = "testSegmentName";
    SegmentRollingPolicy policy = new SegmentRollingPolicy(2);
    // create.
    val h1 = testContext.chunkedSegmentStorage.create(testSegmentName, policy, null).get();
    long expectedLength = 0;
    for (int i = 1; i < 5; i++) {
        val info1 = testContext.chunkedSegmentStorage.getStreamSegmentInfo(testSegmentName, null).get();
        Assert.assertEquals(expectedLength, info1.getLength());
        Assert.assertEquals(info1.getLength(), info1.getStartOffset());
        // Write some data.
        byte[] buffer = new byte[i];
        testContext.chunkedSegmentStorage.write(h1, info1.getStartOffset(), new ByteArrayInputStream(buffer), buffer.length, null).join();
        val info2 = testContext.chunkedSegmentStorage.getStreamSegmentInfo(testSegmentName, null).get();
        expectedLength += i;
        Assert.assertEquals(expectedLength, info2.getLength());
        HashSet<String> chunksBefore = new HashSet<>();
        chunksBefore.addAll(TestUtils.getChunkNameList(testContext.metadataStore, testSegmentName));
        // Now truncate
        testContext.chunkedSegmentStorage.truncate(h1, info2.getLength(), null).join();
        // Validate info
        val metadata = TestUtils.getSegmentMetadata(testContext.metadataStore, testSegmentName);
        Assert.assertEquals(expectedLength, metadata.getLength());
        Assert.assertEquals(expectedLength, metadata.getStartOffset());
        Assert.assertEquals(expectedLength, metadata.getFirstChunkStartOffset());
        Assert.assertEquals(expectedLength, metadata.getLastChunkStartOffset());
        Assert.assertEquals(null, metadata.getLastChunk());
        Assert.assertEquals(null, metadata.getFirstChunk());
        TestUtils.checkChunksExistInStorage(testContext.chunkStorage, testContext.metadataStore, testSegmentName);
        HashSet<String> chunksAfter = new HashSet<>();
        chunksAfter.addAll(TestUtils.getChunkNameList(testContext.metadataStore, testSegmentName));
        TestUtils.checkGarbageCollectionQueue(testContext.chunkedSegmentStorage, chunksBefore, chunksAfter);
        // Validate Exceptions.
        val expectedLength2 = expectedLength;
        val h = testContext.chunkedSegmentStorage.openRead(testSegmentName).get();
        AssertExtensions.assertFutureThrows("read succeeded on invalid offset.", testContext.chunkedSegmentStorage.read(h, expectedLength - 1, new byte[1], 0, 1, null), ex -> ex instanceof StreamSegmentTruncatedException && ((StreamSegmentTruncatedException) ex).getStartOffset() == expectedLength2);
        AssertExtensions.assertFutureThrows("read succeeded on invalid offset.", testContext.chunkedSegmentStorage.read(h, expectedLength, new byte[1], 0, 1, null), ex -> ex instanceof IllegalArgumentException);
    }
}
Also used : lombok.val(lombok.val) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) StreamSegmentTruncatedException(io.pravega.segmentstore.contracts.StreamSegmentTruncatedException) Cleanup(lombok.Cleanup) ByteArrayInputStream(java.io.ByteArrayInputStream) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SegmentRollingPolicy (io.pravega.segmentstore.storage.SegmentRollingPolicy)50 Cleanup (lombok.Cleanup)46 lombok.val (lombok.val)45 Test (org.junit.Test)39 ByteArrayInputStream (java.io.ByteArrayInputStream)32 InMemoryTaskQueueManager (io.pravega.segmentstore.storage.mocks.InMemoryTaskQueueManager)21 ChunkMetadataStore (io.pravega.segmentstore.storage.metadata.ChunkMetadataStore)19 InMemoryChunkStorage (io.pravega.segmentstore.storage.mocks.InMemoryChunkStorage)17 InMemorySnapshotInfoStore (io.pravega.segmentstore.storage.mocks.InMemorySnapshotInfoStore)11 HashSet (java.util.HashSet)10 ArrayList (java.util.ArrayList)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)7 InMemoryMetadataStore (io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore)7 NoOpChunkStorage (io.pravega.segmentstore.storage.noop.NoOpChunkStorage)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)6 StorageFullException (io.pravega.segmentstore.storage.StorageFullException)6 StorageNotPrimaryException (io.pravega.segmentstore.storage.StorageNotPrimaryException)6