Search in sources :

Example 1 with ReadResultEntryType

use of io.pravega.segmentstore.contracts.ReadResultEntryType in project pravega by pravega.

the class ReadTest method testReadDirectlyFromStore.

@Test(timeout = 10000)
public void testReadDirectlyFromStore() throws Exception {
    String segmentName = "testReadFromStore";
    final int entries = 10;
    final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    UUID clientId = UUID.randomUUID();
    StreamSegmentStore segmentStore = SERVICE_BUILDER.createStreamSegmentService();
    fillStoreForSegment(segmentName, data, entries, segmentStore);
    @Cleanup ReadResult result = segmentStore.read(segmentName, 0, entries * data.length, Duration.ZERO).get();
    int index = 0;
    while (result.hasNext()) {
        ReadResultEntry entry = result.next();
        ReadResultEntryType type = entry.getType();
        assertTrue(type == ReadResultEntryType.Cache || type == ReadResultEntryType.Future);
        // Each ReadResultEntryContents may be of an arbitrary length - we should make no assumptions.
        // Also put a timeout when fetching the response in case we get back a Future read and it never completes.
        BufferView contents = entry.getContent().get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        @Cleanup InputStream contentStream = contents.getReader();
        byte next;
        while ((next = (byte) contentStream.read()) != -1) {
            byte expected = data[index % data.length];
            assertEquals(expected, next);
            index++;
        }
    }
    assertEquals(entries * data.length, index);
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) BufferView(io.pravega.common.util.BufferView) InputStream(java.io.InputStream) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) ReadResultEntryType(io.pravega.segmentstore.contracts.ReadResultEntryType) ReadResult(io.pravega.segmentstore.contracts.ReadResult) UUID(java.util.UUID) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 2 with ReadResultEntryType

use of io.pravega.segmentstore.contracts.ReadResultEntryType in project pravega by pravega.

the class ContainerReadIndexTests method testStorageFailedCacheInsert.

/**
 * Tests the ability to handle Cache/Index Update failures post a successful Storage Read.
 */
@Test
public void testStorageFailedCacheInsert() throws Exception {
    final int segmentLength = 1024;
    // Create a segment and write some data in Storage for it.
    @Cleanup TestContext context = new TestContext();
    ArrayList<Long> segmentIds = createSegments(context);
    createSegmentsInStorage(context);
    val testSegmentId = segmentIds.get(0);
    UpdateableSegmentMetadata sm = context.metadata.getStreamSegmentMetadata(testSegmentId);
    sm.setStorageLength(segmentLength);
    sm.setLength(segmentLength);
    context.storage.openWrite(sm.getName()).thenCompose(handle -> context.storage.write(handle, 0, new ByteArrayInputStream(new byte[segmentLength]), segmentLength, TIMEOUT)).join();
    // Keep track of inserted/deleted calls to the Cache, and "fail" the insert call.
    val inserted = new ReusableLatch();
    val insertedAddress = new AtomicInteger(CacheStorage.NO_ADDRESS);
    val deletedAddress = new AtomicInteger(Integer.MAX_VALUE);
    context.cacheStorage.insertCallback = address -> {
        // Immediately delete this data (prevent leaks).
        context.cacheStorage.delete(address);
        Assert.assertTrue(insertedAddress.compareAndSet(CacheStorage.NO_ADDRESS, address));
        inserted.release();
        throw new IntentionalException();
    };
    context.cacheStorage.deleteCallback = deletedAddress::set;
    // Trigger a read. The first read call will be served with data directly from Storage, so we expect it to be successful.
    @Cleanup ReadResult readResult = context.readIndex.read(testSegmentId, 0, segmentLength, TIMEOUT);
    ReadResultEntry entry = readResult.next();
    Assert.assertEquals("Unexpected ReadResultEntryType.", ReadResultEntryType.Storage, entry.getType());
    entry.requestContent(TIMEOUT);
    // This should complete without issues.
    entry.getContent().get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    // Verify that the cache insert attempt has been made
    inserted.await();
    Assert.assertNotEquals("Expected an insert attempt to have been made.", CacheStorage.NO_ADDRESS, insertedAddress.get());
    AssertExtensions.assertEventuallyEquals(CacheStorage.NO_ADDRESS, deletedAddress::get, TIMEOUT.toMillis());
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) SneakyThrows(lombok.SneakyThrows) AssertExtensions(io.pravega.test.common.AssertExtensions) ReadOnlyStorage(io.pravega.segmentstore.storage.ReadOnlyStorage) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) Random(java.util.Random) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) CachePolicy(io.pravega.segmentstore.server.CachePolicy) TestCacheManager(io.pravega.segmentstore.server.TestCacheManager) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) InMemoryStorage(io.pravega.segmentstore.storage.mocks.InMemoryStorage) CompletionException(java.util.concurrent.CompletionException) ReadResultEntryType(io.pravega.segmentstore.contracts.ReadResultEntryType) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StreamSegmentMetadata(io.pravega.segmentstore.server.containers.StreamSegmentMetadata) List(java.util.List) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) DirectMemoryCache(io.pravega.segmentstore.storage.cache.DirectMemoryCache) TestUtils(io.pravega.test.common.TestUtils) Futures(io.pravega.common.concurrent.Futures) ReadResult(io.pravega.segmentstore.contracts.ReadResult) TestStorage(io.pravega.segmentstore.server.TestStorage) ObjectClosedException(io.pravega.common.ObjectClosedException) MetadataBuilder(io.pravega.segmentstore.server.MetadataBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Getter(lombok.Getter) Exceptions(io.pravega.common.Exceptions) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) CacheStorage(io.pravega.segmentstore.storage.cache.CacheStorage) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EvictableMetadata(io.pravega.segmentstore.server.EvictableMetadata) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) CacheState(io.pravega.segmentstore.storage.cache.CacheState) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) Timeout(org.junit.rules.Timeout) ReusableLatch(io.pravega.common.util.ReusableLatch) StreamSegmentTruncatedException(io.pravega.segmentstore.contracts.StreamSegmentTruncatedException) NameUtils(io.pravega.shared.NameUtils) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Assert(org.junit.Assert) Collections(java.util.Collections) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) ReadResult(io.pravega.segmentstore.contracts.ReadResult) Cleanup(lombok.Cleanup) IntentionalException(io.pravega.test.common.IntentionalException) ReusableLatch(io.pravega.common.util.ReusableLatch) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Aggregations

BufferView (io.pravega.common.util.BufferView)2 ReadResult (io.pravega.segmentstore.contracts.ReadResult)2 ReadResultEntry (io.pravega.segmentstore.contracts.ReadResultEntry)2 ReadResultEntryType (io.pravega.segmentstore.contracts.ReadResultEntryType)2 UUID (java.util.UUID)2 Cleanup (lombok.Cleanup)2 Test (org.junit.Test)2 Exceptions (io.pravega.common.Exceptions)1 ObjectClosedException (io.pravega.common.ObjectClosedException)1 Futures (io.pravega.common.concurrent.Futures)1 ByteArraySegment (io.pravega.common.util.ByteArraySegment)1 ReusableLatch (io.pravega.common.util.ReusableLatch)1 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)1 StreamSegmentSealedException (io.pravega.segmentstore.contracts.StreamSegmentSealedException)1 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)1 StreamSegmentTruncatedException (io.pravega.segmentstore.contracts.StreamSegmentTruncatedException)1 CachePolicy (io.pravega.segmentstore.server.CachePolicy)1 EvictableMetadata (io.pravega.segmentstore.server.EvictableMetadata)1 MetadataBuilder (io.pravega.segmentstore.server.MetadataBuilder)1 SegmentMetadata (io.pravega.segmentstore.server.SegmentMetadata)1