Search in sources :

Example 1 with ContainerMetadata

use of io.pravega.segmentstore.server.ContainerMetadata in project pravega by pravega.

the class OperationMetadataUpdater method getOrCreateTransaction.

private ContainerMetadataUpdateTransaction getOrCreateTransaction() {
    if (this.transactions.isEmpty() || this.transactions.peekLast().isSealed()) {
        // No transactions or last transaction is sealed. Create a new one.
        ContainerMetadata previous = this.metadata;
        if (!this.transactions.isEmpty()) {
            previous = this.transactions.peekLast();
        }
        ContainerMetadataUpdateTransaction txn = new ContainerMetadataUpdateTransaction(previous, this.metadata, this.nextTransactionId);
        this.nextTransactionId++;
        this.transactions.addLast(txn);
    }
    return this.transactions.peekLast();
}
Also used : ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata)

Example 2 with ContainerMetadata

use of io.pravega.segmentstore.server.ContainerMetadata in project pravega by pravega.

the class OperationLogTestBase method createStreamSegmentsWithOperations.

/**
 * Creates a number of StreamSegments in the given Metadata and OperationLog.
 */
HashSet<Long> createStreamSegmentsWithOperations(int streamSegmentCount, ContainerMetadata containerMetadata, OperationLog durableLog, Storage storage) {
    StreamSegmentMapper mapper = new StreamSegmentMapper(containerMetadata, durableLog, new InMemoryStateStore(), NO_OP_METADATA_CLEANUP, storage, ForkJoinPool.commonPool());
    HashSet<Long> result = new HashSet<>();
    for (int i = 0; i < streamSegmentCount; i++) {
        String name = getStreamSegmentName(i);
        long streamSegmentId = mapper.createNewStreamSegment(name, null, Duration.ZERO).thenCompose(v -> mapper.getOrAssignStreamSegmentId(name, Duration.ZERO)).join();
        result.add(streamSegmentId);
    }
    return result;
}
Also used : Storage(io.pravega.segmentstore.storage.Storage) AssertExtensions(io.pravega.test.common.AssertExtensions) StreamSegmentMapper(io.pravega.segmentstore.server.containers.StreamSegmentMapper) ProbeOperation(io.pravega.segmentstore.server.logs.operations.ProbeOperation) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) SequencedItemList(io.pravega.common.util.SequencedItemList) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) ReadResultEntryContents(io.pravega.segmentstore.contracts.ReadResultEntryContents) Duration(java.time.Duration) Map(java.util.Map) Operation(io.pravega.segmentstore.server.logs.operations.Operation) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) UUID(java.util.UUID) StreamSegmentNameUtils(io.pravega.shared.segment.StreamSegmentNameUtils) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Futures(io.pravega.common.concurrent.Futures) ReadResult(io.pravega.segmentstore.contracts.ReadResult) ObjectClosedException(io.pravega.common.ObjectClosedException) IllegalContainerStateException(io.pravega.segmentstore.server.IllegalContainerStateException) Exceptions(io.pravega.common.Exceptions) InMemoryStateStore(io.pravega.segmentstore.server.containers.InMemoryStateStore) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) DurableDataLogException(io.pravega.segmentstore.storage.DurableDataLogException) SequenceInputStream(java.io.SequenceInputStream) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) MetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.MetadataCheckpointOperation) OperationLog(io.pravega.segmentstore.server.OperationLog) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractMap(java.util.AbstractMap) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) ReadIndex(io.pravega.segmentstore.server.ReadIndex) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) Assert(org.junit.Assert) MergeTransactionOperation(io.pravega.segmentstore.server.logs.operations.MergeTransactionOperation) Collections(java.util.Collections) StreamSegmentSealOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentSealOperation) InputStream(java.io.InputStream) InMemoryStateStore(io.pravega.segmentstore.server.containers.InMemoryStateStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamSegmentMapper(io.pravega.segmentstore.server.containers.StreamSegmentMapper) HashSet(java.util.HashSet)

Example 3 with ContainerMetadata

use of io.pravega.segmentstore.server.ContainerMetadata in project pravega by pravega.

the class OperationLogTestBase method createTransactionsWithOperations.

/**
 * Creates a number of Transaction Segments in the given Metadata and OperationLog.
 */
AbstractMap<Long, Long> createTransactionsWithOperations(HashSet<Long> streamSegmentIds, int transactionsPerStreamSegment, ContainerMetadata containerMetadata, OperationLog durableLog, Storage storage) {
    HashMap<Long, Long> result = new HashMap<>();
    StreamSegmentMapper mapper = new StreamSegmentMapper(containerMetadata, durableLog, new InMemoryStateStore(), NO_OP_METADATA_CLEANUP, storage, ForkJoinPool.commonPool());
    for (long streamSegmentId : streamSegmentIds) {
        String streamSegmentName = containerMetadata.getStreamSegmentMetadata(streamSegmentId).getName();
        for (int i = 0; i < transactionsPerStreamSegment; i++) {
            long transactionId = mapper.createNewTransactionStreamSegment(streamSegmentName, UUID.randomUUID(), null, Duration.ZERO).thenCompose(v -> mapper.getOrAssignStreamSegmentId(v, Duration.ZERO)).join();
            result.put(transactionId, streamSegmentId);
        }
    }
    return result;
}
Also used : Storage(io.pravega.segmentstore.storage.Storage) AssertExtensions(io.pravega.test.common.AssertExtensions) StreamSegmentMapper(io.pravega.segmentstore.server.containers.StreamSegmentMapper) ProbeOperation(io.pravega.segmentstore.server.logs.operations.ProbeOperation) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) SequencedItemList(io.pravega.common.util.SequencedItemList) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) ReadResultEntryContents(io.pravega.segmentstore.contracts.ReadResultEntryContents) Duration(java.time.Duration) Map(java.util.Map) Operation(io.pravega.segmentstore.server.logs.operations.Operation) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) UUID(java.util.UUID) StreamSegmentNameUtils(io.pravega.shared.segment.StreamSegmentNameUtils) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Futures(io.pravega.common.concurrent.Futures) ReadResult(io.pravega.segmentstore.contracts.ReadResult) ObjectClosedException(io.pravega.common.ObjectClosedException) IllegalContainerStateException(io.pravega.segmentstore.server.IllegalContainerStateException) Exceptions(io.pravega.common.Exceptions) InMemoryStateStore(io.pravega.segmentstore.server.containers.InMemoryStateStore) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) DurableDataLogException(io.pravega.segmentstore.storage.DurableDataLogException) SequenceInputStream(java.io.SequenceInputStream) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) MetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.MetadataCheckpointOperation) OperationLog(io.pravega.segmentstore.server.OperationLog) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractMap(java.util.AbstractMap) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) ReadIndex(io.pravega.segmentstore.server.ReadIndex) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) Assert(org.junit.Assert) MergeTransactionOperation(io.pravega.segmentstore.server.logs.operations.MergeTransactionOperation) Collections(java.util.Collections) StreamSegmentSealOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentSealOperation) InputStream(java.io.InputStream) InMemoryStateStore(io.pravega.segmentstore.server.containers.InMemoryStateStore) HashMap(java.util.HashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamSegmentMapper(io.pravega.segmentstore.server.containers.StreamSegmentMapper)

Example 4 with ContainerMetadata

use of io.pravega.segmentstore.server.ContainerMetadata in project pravega by pravega.

the class OperationMetadataUpdaterTests method testCommit.

/**
 * Tests the ability to successively commit update transactions to the base metadata.
 */
@Test
public void testCommit() throws Exception {
    // Commit 1 at a time, then 2, then 3, then 4, etc until we have nothing left to commit.
    // At each step verify that the base metadata has been properly updated.
    val referenceMetadata = createBlankMetadata();
    val metadata = createBlankMetadata();
    val updater = new OperationMetadataUpdater(metadata);
    val lastSegmentId = new AtomicLong(-1);
    val lastSegmentTxnId = new AtomicLong(-1);
    long lastCommittedTxnId = -1;
    int txnGroupSize = 1;
    val updateTransactions = new ArrayList<Map.Entry<Long, ContainerMetadata>>();
    while (updateTransactions.size() < TRANSACTION_COUNT) {
        populateUpdateTransaction(updater, referenceMetadata, lastSegmentId, lastSegmentTxnId);
        long utId = updater.sealTransaction();
        if (updateTransactions.size() > 0) {
            long prevUtId = updateTransactions.get(updateTransactions.size() - 1).getKey();
            Assert.assertEquals("UpdateTransaction.Id is not sequential and increasing.", prevUtId + 1, utId);
        }
        updateTransactions.add(new AbstractMap.SimpleImmutableEntry<>(utId, clone(referenceMetadata)));
    }
    ContainerMetadata previousMetadata = null;
    for (val t : updateTransactions) {
        val utId = t.getKey();
        val expectedMetadata = t.getValue();
        // Check to see if it's time to commit.
        if (utId - lastCommittedTxnId >= txnGroupSize) {
            if (previousMetadata != null) {
                // Verify no changes to the metadata prior to commit.
                ContainerMetadataUpdateTransactionTests.assertMetadataSame("Before commit " + utId, previousMetadata, metadata);
            }
            // Commit and verify.
            updater.commit(utId);
            ContainerMetadataUpdateTransactionTests.assertMetadataSame("After commit " + utId, expectedMetadata, metadata);
            lastCommittedTxnId = utId;
            txnGroupSize++;
            previousMetadata = expectedMetadata;
        }
    }
}
Also used : lombok.val(lombok.val) AbstractMap(java.util.AbstractMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) StreamSegmentContainerMetadata(io.pravega.segmentstore.server.containers.StreamSegmentContainerMetadata) Test(org.junit.Test)

Example 5 with ContainerMetadata

use of io.pravega.segmentstore.server.ContainerMetadata in project pravega by pravega.

the class OperationMetadataUpdaterTests method testRollback.

/**
 * Tests the ability to rollback update transactions.
 */
@Test
public void testRollback() throws Exception {
    // 2 out of 3 UpdateTransactions are failed (to verify multi-failure).
    // Commit the rest and verify final metadata is as it should.
    final int failEvery = 3;
    Predicate<Integer> isIgnored = index -> index % failEvery > 0;
    Predicate<Integer> shouldFail = index -> index % failEvery == failEvery - 1;
    val referenceMetadata = createBlankMetadata();
    val metadata = createBlankMetadata();
    val updater = new OperationMetadataUpdater(metadata);
    val lastSegmentId = new AtomicLong(-1);
    val lastSegmentTxnId = new AtomicLong(-1);
    val updateTransactions = new ArrayList<Map.Entry<Long, ContainerMetadata>>();
    for (int i = 0; i < TRANSACTION_COUNT; i++) {
        // Check to see if this UpdateTransaction is going to end up being rolled back. If so, we should not update
        // the reference metadata at all.
        UpdateableContainerMetadata txnReferenceMetadata = isIgnored.test(i) ? null : referenceMetadata;
        populateUpdateTransaction(updater, txnReferenceMetadata, lastSegmentId, lastSegmentTxnId);
        if (shouldFail.test(i)) {
            long prevUtId = updateTransactions.get(updateTransactions.size() - 1).getKey();
            updater.rollback(prevUtId + 1);
        } else if (txnReferenceMetadata != null) {
            // Not failing and not ignored: this UpdateTransaction will survive, so record it.
            long utId = updater.sealTransaction();
            if (updateTransactions.size() > 0) {
                long prevUtId = updateTransactions.get(updateTransactions.size() - 1).getKey();
                Assert.assertEquals("Unexpected UpdateTransaction.Id.", prevUtId + failEvery - 1, utId);
            }
            updateTransactions.add(new AbstractMap.SimpleImmutableEntry<>(utId, clone(txnReferenceMetadata)));
        }
    }
    ContainerMetadata previousMetadata = null;
    for (val t : updateTransactions) {
        val utId = t.getKey();
        val expectedMetadata = t.getValue();
        // Check to see if it's time to commit.
        if (previousMetadata != null) {
            // Verify no changes to the metadata prior to commit.
            ContainerMetadataUpdateTransactionTests.assertMetadataSame("Before commit " + utId, previousMetadata, metadata);
        }
        // Commit and verify.
        updater.commit(utId);
        ContainerMetadataUpdateTransactionTests.assertMetadataSame("After commit " + utId, expectedMetadata, metadata);
        previousMetadata = expectedMetadata;
    }
}
Also used : Arrays(java.util.Arrays) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) HashMap(java.util.HashMap) LogAddress(io.pravega.segmentstore.storage.LogAddress) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Operation(io.pravega.segmentstore.server.logs.operations.Operation) StreamSegmentMapOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentMapOperation) Attributes(io.pravega.segmentstore.contracts.Attributes) Predicate(java.util.function.Predicate) lombok.val(lombok.val) Test(org.junit.Test) UUID(java.util.UUID) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractMap(java.util.AbstractMap) Rule(org.junit.Rule) StreamSegmentContainerMetadata(io.pravega.segmentstore.server.containers.StreamSegmentContainerMetadata) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) Assert(org.junit.Assert) MergeTransactionOperation(io.pravega.segmentstore.server.logs.operations.MergeTransactionOperation) StreamSegmentSealOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentSealOperation) lombok.val(lombok.val) ArrayList(java.util.ArrayList) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) StreamSegmentContainerMetadata(io.pravega.segmentstore.server.containers.StreamSegmentContainerMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) Test(org.junit.Test)

Aggregations

ContainerMetadata (io.pravega.segmentstore.server.ContainerMetadata)6 UpdateableContainerMetadata (io.pravega.segmentstore.server.UpdateableContainerMetadata)6 AbstractMap (java.util.AbstractMap)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 lombok.val (lombok.val)5 AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)4 AttributeUpdateType (io.pravega.segmentstore.contracts.AttributeUpdateType)4 SegmentMetadata (io.pravega.segmentstore.server.SegmentMetadata)4 MergeTransactionOperation (io.pravega.segmentstore.server.logs.operations.MergeTransactionOperation)4 Operation (io.pravega.segmentstore.server.logs.operations.Operation)4 StreamSegmentAppendOperation (io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation)4 StreamSegmentSealOperation (io.pravega.segmentstore.server.logs.operations.StreamSegmentSealOperation)4 UUID (java.util.UUID)4 Supplier (java.util.function.Supplier)4 Assert (org.junit.Assert)4 StreamSegmentContainerMetadata (io.pravega.segmentstore.server.containers.StreamSegmentContainerMetadata)3 Test (org.junit.Test)3