Search in sources :

Example 6 with SegmentProperties

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

the class StreamSegmentContainerTests method checkStorage.

private static void checkStorage(HashMap<String, ByteArrayOutputStream> segmentContents, HashMap<String, Long> lengths, TestContext context) {
    for (String segmentName : segmentContents.keySet()) {
        // 1. Deletion status
        SegmentProperties sp = null;
        try {
            sp = context.container.getStreamSegmentInfo(segmentName, false, TIMEOUT).join();
        } catch (Exception ex) {
            if (!(Exceptions.unwrap(ex) instanceof StreamSegmentNotExistsException)) {
                throw ex;
            }
        }
        if (sp == null) {
            Assert.assertFalse("Segment is marked as deleted in metadata but was not deleted in Storage " + segmentName, context.storage.exists(segmentName, TIMEOUT).join());
            // No need to do other checks.
            continue;
        }
        // 2. Seal Status
        SegmentProperties storageProps = context.storage.getStreamSegmentInfo(segmentName, TIMEOUT).join();
        Assert.assertEquals("Segment seal status disagree between Metadata and Storage for segment " + segmentName, sp.isSealed(), storageProps.isSealed());
        // 3. Contents.
        long expectedLength = lengths.get(segmentName);
        Assert.assertEquals("Unexpected Storage length for segment " + segmentName, expectedLength, storageProps.getLength());
        byte[] expectedData = segmentContents.get(segmentName).toByteArray();
        byte[] actualData = new byte[expectedData.length];
        val readHandle = context.storage.openRead(segmentName).join();
        int actualLength = context.storage.read(readHandle, 0, actualData, 0, actualData.length, TIMEOUT).join();
        Assert.assertEquals("Unexpected number of bytes read from Storage for segment " + segmentName, expectedLength, actualLength);
        Assert.assertArrayEquals("Unexpected data written to storage for segment " + segmentName, expectedData, actualData);
    }
}
Also used : lombok.val(lombok.val) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) TooManyActiveSegmentsException(io.pravega.segmentstore.contracts.TooManyActiveSegmentsException) ConfigurationException(io.pravega.common.util.ConfigurationException) TimeoutException(java.util.concurrent.TimeoutException) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) ContainerOfflineException(io.pravega.segmentstore.server.ContainerOfflineException) DataLogWriterNotPrimaryException(io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) IntentionalException(io.pravega.test.common.IntentionalException) StreamSegmentMergedException(io.pravega.segmentstore.contracts.StreamSegmentMergedException) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)

Example 7 with SegmentProperties

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

the class StreamSegmentContainerTests method waitForSegmentsInStorage.

private CompletableFuture<Void> waitForSegmentsInStorage(Collection<String> segmentNames, TestContext context) {
    ArrayList<CompletableFuture<Void>> segmentsCompletion = new ArrayList<>();
    for (String segmentName : segmentNames) {
        SegmentProperties sp = context.container.getStreamSegmentInfo(segmentName, false, TIMEOUT).join();
        segmentsCompletion.add(waitForSegmentInStorage(sp, context));
    }
    return Futures.allOf(segmentsCompletion);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties)

Example 8 with SegmentProperties

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

the class StreamSegmentContainerTests method tryActivate.

/**
 * Attempts to activate the targetSegment in the given Container. Since we do not have access to the internals of the
 * Container, we need to trigger this somehow, hence the need for this complex code. We need to trigger a truncation,
 * so we need an 'appendSegment' to which we continuously append so that the DurableDataLog is truncated. After truncation,
 * the Metadata should have enough leeway in making room for new activation.
 *
 * @return A Future that will complete either with an exception (failure) or SegmentProperties for the targetSegment.
 */
private CompletableFuture<SegmentProperties> tryActivate(MetadataCleanupContainer localContainer, String targetSegment, String appendSegment) {
    CompletableFuture<SegmentProperties> successfulMap = new CompletableFuture<>();
    // Append continuously to an existing segment in order to trigger truncations (these are necessary for forced evictions).
    val appendFuture = localContainer.appendRandomly(appendSegment, false, () -> !successfulMap.isDone());
    Futures.exceptionListener(appendFuture, successfulMap::completeExceptionally);
    // Repeatedly try to get info on 'segment1' (activate it), until we succeed or time out.
    TimeoutTimer remaining = new TimeoutTimer(TIMEOUT);
    Futures.loop(() -> !successfulMap.isDone(), () -> Futures.delayedFuture(Duration.ofMillis(250), executorService()).thenCompose(v -> localContainer.getStreamSegmentInfo(targetSegment, false, TIMEOUT)).thenAccept(successfulMap::complete).exceptionally(ex -> {
        if (!(Exceptions.unwrap(ex) instanceof TooManyActiveSegmentsException)) {
            // Some other error.
            successfulMap.completeExceptionally(ex);
        } else if (!remaining.hasRemaining()) {
            // Waited too long.
            successfulMap.completeExceptionally(new TimeoutException("No successful activation could be done in the allotted time."));
        }
        // Try again.
        return null;
    }), executorService());
    return successfulMap;
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Storage(io.pravega.segmentstore.storage.Storage) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) Cleanup(lombok.Cleanup) StorageWriterFactory(io.pravega.segmentstore.server.writer.StorageWriterFactory) Future(java.util.concurrent.Future) ReadResultEntryContents(io.pravega.segmentstore.contracts.ReadResultEntryContents) InMemoryStorageFactory(io.pravega.segmentstore.storage.mocks.InMemoryStorageFactory) Duration(java.time.Duration) Map(java.util.Map) AsyncReadResultProcessor(io.pravega.segmentstore.server.reading.AsyncReadResultProcessor) ContainerReadIndexFactory(io.pravega.segmentstore.server.reading.ContainerReadIndexFactory) InMemoryDurableDataLogFactory(io.pravega.segmentstore.storage.mocks.InMemoryDurableDataLogFactory) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) DurableLogFactory(io.pravega.segmentstore.server.logs.DurableLogFactory) Attributes(io.pravega.segmentstore.contracts.Attributes) DurableLogConfig(io.pravega.segmentstore.server.logs.DurableLogConfig) Writer(io.pravega.segmentstore.server.Writer) SegmentContainerFactory(io.pravega.segmentstore.server.SegmentContainerFactory) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) SyncStorage(io.pravega.segmentstore.storage.SyncStorage) Futures(io.pravega.common.concurrent.Futures) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TooManyActiveSegmentsException(io.pravega.segmentstore.contracts.TooManyActiveSegmentsException) Exceptions(io.pravega.common.Exceptions) StorageFactory(io.pravega.segmentstore.storage.StorageFactory) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) Runnables(com.google.common.util.concurrent.Runnables) ReadIndexConfig(io.pravega.segmentstore.server.reading.ReadIndexConfig) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) ConfigurationException(io.pravega.common.util.ConfigurationException) StreamHelpers(io.pravega.common.io.StreamHelpers) WriterFactory(io.pravega.segmentstore.server.WriterFactory) Properties(java.util.Properties) DurableDataLog(io.pravega.segmentstore.storage.DurableDataLog) Executor(java.util.concurrent.Executor) lombok.val(lombok.val) OperationLog(io.pravega.segmentstore.server.OperationLog) Test(org.junit.Test) Service(com.google.common.util.concurrent.Service) AtomicLong(java.util.concurrent.atomic.AtomicLong) OperationLogFactory(io.pravega.segmentstore.server.OperationLogFactory) SegmentContainer(io.pravega.segmentstore.server.SegmentContainer) Assert(org.junit.Assert) WriterConfig(io.pravega.segmentstore.server.writer.WriterConfig) SneakyThrows(lombok.SneakyThrows) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) ReadIndexFactory(io.pravega.segmentstore.server.ReadIndexFactory) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) AbstractService(com.google.common.util.concurrent.AbstractService) CacheFactory(io.pravega.segmentstore.storage.CacheFactory) ServiceListeners(io.pravega.segmentstore.server.ServiceListeners) ContainerOfflineException(io.pravega.segmentstore.server.ContainerOfflineException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ReadResultEntryType(io.pravega.segmentstore.contracts.ReadResultEntryType) UUID(java.util.UUID) DataLogWriterNotPrimaryException(io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException) Collectors(java.util.stream.Collectors) SegmentMetadataComparer(io.pravega.segmentstore.server.SegmentMetadataComparer) StreamSegmentNameUtils(io.pravega.shared.segment.StreamSegmentNameUtils) List(java.util.List) InMemoryCacheFactory(io.pravega.segmentstore.storage.mocks.InMemoryCacheFactory) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) DurableDataLogFactory(io.pravega.segmentstore.storage.DurableDataLogFactory) ReadResult(io.pravega.segmentstore.contracts.ReadResult) Setter(lombok.Setter) Getter(lombok.Getter) ConfigHelpers(io.pravega.segmentstore.server.ConfigHelpers) AsyncStorageWrapper(io.pravega.segmentstore.storage.AsyncStorageWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HashSet(java.util.HashSet) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) ExecutorService(java.util.concurrent.ExecutorService) ExecutorServiceHelpers.newScheduledThreadPool(io.pravega.common.concurrent.ExecutorServiceHelpers.newScheduledThreadPool) TimeoutTimer(io.pravega.common.TimeoutTimer) RollingStorage(io.pravega.segmentstore.storage.rolling.RollingStorage) IntentionalException(io.pravega.test.common.IntentionalException) StreamSegmentMergedException(io.pravega.segmentstore.contracts.StreamSegmentMergedException) TestReadResultHandler(io.pravega.segmentstore.server.reading.TestReadResultHandler) TestDurableDataLogFactory(io.pravega.segmentstore.server.TestDurableDataLogFactory) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Rule(org.junit.Rule) TypedProperties(io.pravega.common.util.TypedProperties) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) ReadIndex(io.pravega.segmentstore.server.ReadIndex) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) TooManyActiveSegmentsException(io.pravega.segmentstore.contracts.TooManyActiveSegmentsException) TimeoutTimer(io.pravega.common.TimeoutTimer) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with SegmentProperties

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

the class StreamSegmentContainerTests method checkActiveSegments.

private void checkActiveSegments(SegmentContainer container, int expectedCount) {
    val initialActiveSegments = container.getActiveSegments();
    Assert.assertEquals("Unexpected result from getActiveSegments with freshly created segments.", expectedCount, initialActiveSegments.size());
    for (SegmentProperties sp : initialActiveSegments) {
        val expectedSp = container.getStreamSegmentInfo(sp.getName(), false, TIMEOUT).join();
        Assert.assertEquals("Unexpected length (from getActiveSegments) for segment " + sp.getName(), expectedSp.getLength(), sp.getLength());
        Assert.assertEquals("Unexpected sealed (from getActiveSegments) for segment " + sp.getName(), expectedSp.isSealed(), sp.isSealed());
        Assert.assertEquals("Unexpected deleted (from getActiveSegments) for segment " + sp.getName(), expectedSp.isDeleted(), sp.isDeleted());
        SegmentMetadataComparer.assertSameAttributes("Unexpected attributes (from getActiveSegments) for segment " + sp.getName(), expectedSp.getAttributes(), sp);
    }
}
Also used : lombok.val(lombok.val) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties)

Example 10 with SegmentProperties

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

the class StreamSegmentContainerTests method testSegmentDelete.

/**
 * Tests the ability to delete StreamSegments.
 */
@Test
public void testSegmentDelete() throws Exception {
    final int appendsPerSegment = 1;
    @Cleanup TestContext context = new TestContext();
    context.container.startAsync().awaitRunning();
    // 1. Create the StreamSegments.
    ArrayList<String> segmentNames = createSegments(context);
    HashMap<String, ArrayList<String>> transactionsBySegment = createTransactions(segmentNames, context);
    // 2. Add some appends.
    ArrayList<CompletableFuture<Void>> appendFutures = new ArrayList<>();
    for (int i = 0; i < appendsPerSegment; i++) {
        for (String segmentName : segmentNames) {
            appendFutures.add(context.container.append(segmentName, getAppendData(segmentName, i), null, TIMEOUT));
            for (String transactionName : transactionsBySegment.get(segmentName)) {
                appendFutures.add(context.container.append(transactionName, getAppendData(transactionName, i), null, TIMEOUT));
            }
        }
    }
    Futures.allOf(appendFutures).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    // 3. Delete the first half of the segments.
    ArrayList<CompletableFuture<Void>> deleteFutures = new ArrayList<>();
    for (int i = 0; i < segmentNames.size() / 2; i++) {
        String segmentName = segmentNames.get(i);
        deleteFutures.add(context.container.deleteStreamSegment(segmentName, TIMEOUT));
    }
    Futures.allOf(deleteFutures).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    // 4. Verify that only the first half of the segments (and their Transactions) were deleted, and not the others.
    for (int i = 0; i < segmentNames.size(); i++) {
        ArrayList<String> toCheck = new ArrayList<>();
        toCheck.add(segmentNames.get(i));
        toCheck.addAll(transactionsBySegment.get(segmentNames.get(i)));
        boolean expectedDeleted = i < segmentNames.size() / 2;
        if (expectedDeleted) {
            // Verify the segments and their Transactions are not there anymore.
            for (String sn : toCheck) {
                AssertExtensions.assertThrows("getStreamSegmentInfo did not throw expected exception when called on a deleted StreamSegment.", context.container.getStreamSegmentInfo(sn, false, TIMEOUT)::join, ex -> ex instanceof StreamSegmentNotExistsException);
                AssertExtensions.assertThrows("append did not throw expected exception when called on a deleted StreamSegment.", context.container.append(sn, "foo".getBytes(), null, TIMEOUT)::join, ex -> ex instanceof StreamSegmentNotExistsException);
                AssertExtensions.assertThrows("read did not throw expected exception when called on a deleted StreamSegment.", context.container.read(sn, 0, 1, TIMEOUT)::join, ex -> ex instanceof StreamSegmentNotExistsException);
                Assert.assertFalse("Segment not deleted in storage.", context.storage.exists(sn, TIMEOUT).join());
            }
        } else {
            // Verify the segments and their Transactions are still there.
            for (String sn : toCheck) {
                SegmentProperties props = context.container.getStreamSegmentInfo(sn, false, TIMEOUT).join();
                Assert.assertFalse("Not-deleted segment (or one of its Transactions) was marked as deleted in metadata.", props.isDeleted());
                // Verify we can still append and read from this segment.
                context.container.append(sn, "foo".getBytes(), null, TIMEOUT).join();
                @Cleanup ReadResult rr = context.container.read(sn, 0, 1, TIMEOUT).join();
                // Verify the segment still exists in storage.
                context.storage.getStreamSegmentInfo(sn, TIMEOUT).join();
            }
        }
    }
    context.container.stopAsync().awaitTerminated();
}
Also used : ArrayList(java.util.ArrayList) ReadResult(io.pravega.segmentstore.contracts.ReadResult) Cleanup(lombok.Cleanup) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) CompletableFuture(java.util.concurrent.CompletableFuture) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Test(org.junit.Test)

Aggregations

SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)43 Test (org.junit.Test)24 Cleanup (lombok.Cleanup)22 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)18 lombok.val (lombok.val)18 HashMap (java.util.HashMap)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 ArrayList (java.util.ArrayList)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 SegmentMetadata (io.pravega.segmentstore.server.SegmentMetadata)13 BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)11 UUID (java.util.UUID)11 Exceptions (io.pravega.common.Exceptions)10 Duration (java.time.Duration)10 Map (java.util.Map)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 StorageOperation (io.pravega.segmentstore.server.logs.operations.StorageOperation)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Futures (io.pravega.common.concurrent.Futures)8