Search in sources :

Example 11 with LogAddress

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

the class InMemoryDurableDataLog method append.

@Override
public CompletableFuture<LogAddress> append(ArrayView data, Duration timeout) {
    ensurePreconditions();
    CompletableFuture<LogAddress> result;
    try {
        Entry entry = new Entry(data);
        synchronized (this.entries) {
            entry.sequenceNumber = this.offset;
            this.entries.add(entry, clientId);
            // Only update internals after a successful add.
            this.offset += entry.data.length;
        }
        result = CompletableFuture.completedFuture(new InMemoryLogAddress(entry.sequenceNumber));
    } catch (Throwable ex) {
        return Futures.failedFuture(ex);
    }
    Duration delay = this.appendDelayProvider.get();
    if (delay.compareTo(Duration.ZERO) <= 0) {
        // No delay, execute right away.
        return result;
    } else {
        // Schedule the append after the given delay.
        return result.thenComposeAsync(logAddress -> Futures.delayedFuture(delay, this.executorService).thenApply(ignored -> logAddress), this.executorService);
    }
}
Also used : Getter(lombok.Getter) Exceptions(io.pravega.common.Exceptions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) LogAddress(io.pravega.segmentstore.storage.LogAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayView(io.pravega.common.util.ArrayView) SequencedItemList(io.pravega.common.util.SequencedItemList) ByteArrayInputStream(java.io.ByteArrayInputStream) Duration(java.time.Duration) DataLogInitializationException(io.pravega.segmentstore.storage.DataLogInitializationException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DurableDataLogException(io.pravega.segmentstore.storage.DurableDataLogException) CloseableIterator(io.pravega.common.util.CloseableIterator) DataLogDisabledException(io.pravega.segmentstore.storage.DataLogDisabledException) DurableDataLog(io.pravega.segmentstore.storage.DurableDataLog) Iterator(java.util.Iterator) CompletionException(java.util.concurrent.CompletionException) ThreadSafe(javax.annotation.concurrent.ThreadSafe) UUID(java.util.UUID) DataLogWriterNotPrimaryException(io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException) GuardedBy(javax.annotation.concurrent.GuardedBy) AtomicLong(java.util.concurrent.atomic.AtomicLong) QueueStats(io.pravega.segmentstore.storage.QueueStats) Preconditions(com.google.common.base.Preconditions) Futures(io.pravega.common.concurrent.Futures) InputStream(java.io.InputStream) LogAddress(io.pravega.segmentstore.storage.LogAddress) Duration(java.time.Duration)

Example 12 with LogAddress

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

the class DurableLog method truncate.

@Override
public CompletableFuture<Void> truncate(long upToSequenceNumber, Duration timeout) {
    ensureRunning();
    Preconditions.checkArgument(this.metadata.isValidTruncationPoint(upToSequenceNumber), "Invalid Truncation Point. Must refer to a MetadataCheckpointOperation.");
    // The SequenceNumber we were given points directly to a MetadataCheckpointOperation. We must not remove it!
    // Instead, it must be the first operation that does survive, so we need to adjust our SeqNo to the one just
    // before it.
    long actualTruncationSequenceNumber = upToSequenceNumber - 1;
    // Find the closest Truncation Marker (that does not exceed it).
    LogAddress truncationFrameAddress = this.metadata.getClosestTruncationMarker(actualTruncationSequenceNumber);
    if (truncationFrameAddress == null) {
        // Nothing to truncate.
        return CompletableFuture.completedFuture(null);
    }
    TimeoutTimer timer = new TimeoutTimer(timeout);
    log.info("{}: Truncate (OperationSequenceNumber = {}, DataFrameAddress = {}).", this.traceObjectId, upToSequenceNumber, truncationFrameAddress);
    // info will be readily available upon recovery without delay.
    return add(new StorageMetadataCheckpointOperation(), timer.getRemaining()).thenComposeAsync(v -> this.durableDataLog.truncate(truncationFrameAddress, timer.getRemaining()), this.executor).thenRunAsync(() -> {
        // Truncate InMemory Transaction Log.
        int count = this.inMemoryOperationLog.truncate(actualTruncationSequenceNumber);
        // Remove old truncation markers.
        this.metadata.removeTruncationMarkers(actualTruncationSequenceNumber);
        this.operationProcessor.getMetrics().operationLogTruncate(count);
    }, this.executor);
}
Also used : ObjectClosedException(io.pravega.common.ObjectClosedException) IllegalContainerStateException(io.pravega.segmentstore.server.IllegalContainerStateException) SneakyThrows(lombok.SneakyThrows) Retry(io.pravega.common.util.Retry) StorageMetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.StorageMetadataCheckpointOperation) ProbeOperation(io.pravega.segmentstore.server.logs.operations.ProbeOperation) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) LogAddress(io.pravega.segmentstore.storage.LogAddress) StreamingException(io.pravega.segmentstore.contracts.StreamingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SequencedItemList(io.pravega.common.util.SequencedItemList) Duration(java.time.Duration) AbstractService(com.google.common.util.concurrent.AbstractService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Operation(io.pravega.segmentstore.server.logs.operations.Operation) DataLogDisabledException(io.pravega.segmentstore.storage.DataLogDisabledException) LoggerHelpers(io.pravega.common.LoggerHelpers) Services(io.pravega.common.concurrent.Services) ContainerOfflineException(io.pravega.segmentstore.server.ContainerOfflineException) DurableDataLog(io.pravega.segmentstore.storage.DurableDataLog) Iterator(java.util.Iterator) TimeoutTimer(io.pravega.common.TimeoutTimer) MetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.MetadataCheckpointOperation) Set(java.util.Set) OperationLog(io.pravega.segmentstore.server.OperationLog) CompletionException(java.util.concurrent.CompletionException) ThreadSafe(javax.annotation.concurrent.ThreadSafe) GuardedBy(javax.annotation.concurrent.GuardedBy) Timer(io.pravega.common.Timer) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ReadIndex(io.pravega.segmentstore.server.ReadIndex) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) DurableDataLogFactory(io.pravega.segmentstore.storage.DurableDataLogFactory) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) LogAddress(io.pravega.segmentstore.storage.LogAddress) StorageMetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.StorageMetadataCheckpointOperation) TimeoutTimer(io.pravega.common.TimeoutTimer)

Example 13 with LogAddress

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

the class StreamSegmentContainerMetadataTests method testTruncationMarkers.

/**
 * Tests the Truncation Marker functionality (truncation points is tested separately).
 */
@Test
public void testTruncationMarkers() {
    final long maxSeqNo = 1000;
    final int markerFrequency = 13;
    Function<Long, LogAddress> getFrameAddress = seqNo -> new TestLogAddress(Integer.MAX_VALUE + seqNo * seqNo);
    final UpdateableContainerMetadata m = new MetadataBuilder(CONTAINER_ID).build();
    // Record some truncation markers, starting a few steps after initial.
    for (long seqNo = markerFrequency; seqNo <= maxSeqNo; seqNo += markerFrequency) {
        m.recordTruncationMarker(seqNo, getFrameAddress.apply(seqNo));
    }
    // Verify them.
    for (long seqNo = 0; seqNo < maxSeqNo + markerFrequency; seqNo++) {
        LogAddress expectedTruncationMarker = null;
        if (seqNo >= markerFrequency) {
            long input = seqNo > maxSeqNo ? maxSeqNo : seqNo;
            expectedTruncationMarker = getFrameAddress.apply(input - input % markerFrequency);
        }
        LogAddress truncationMarker = m.getClosestTruncationMarker(seqNo);
        Assert.assertEquals("Unexpected truncation marker value for Op Sequence Number " + seqNo, expectedTruncationMarker, truncationMarker);
    }
    // Remove some truncation markers & verify again.
    for (long seqNo = 0; seqNo < maxSeqNo + markerFrequency; seqNo++) {
        m.removeTruncationMarkers(seqNo);
        // Check that the removal actually made sense (it should return -1 now).
        LogAddress truncationMarker = m.getClosestTruncationMarker(seqNo);
        Assert.assertNull("Unexpected truncation marker value after removal for Op Sequence Number " + seqNo, truncationMarker);
        // Check that the next higher up still works.
        long input = seqNo + markerFrequency;
        input = input > maxSeqNo ? maxSeqNo : input;
        LogAddress expectedTruncationMarker;
        if (seqNo > maxSeqNo - markerFrequency) {
            // We have already removed all possible truncation markers, so expect the result to be -1.
            expectedTruncationMarker = null;
        } else {
            expectedTruncationMarker = getFrameAddress.apply(input - input % markerFrequency);
        }
        truncationMarker = m.getClosestTruncationMarker(input);
        Assert.assertEquals("Unexpected truncation marker value for Op Sequence Number " + input + " after removing marker at Sequence Number " + seqNo, expectedTruncationMarker, truncationMarker);
    }
}
Also used : MetadataBuilder(io.pravega.segmentstore.server.MetadataBuilder) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Collection(java.util.Collection) lombok.val(lombok.val) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) HashMap(java.util.HashMap) Test(org.junit.Test) LogAddress(io.pravega.segmentstore.storage.LogAddress) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) List(java.util.List) Rule(org.junit.Rule) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Assert(org.junit.Assert) LogAddress(io.pravega.segmentstore.storage.LogAddress) MetadataBuilder(io.pravega.segmentstore.server.MetadataBuilder) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) Test(org.junit.Test)

Aggregations

LogAddress (io.pravega.segmentstore.storage.LogAddress)13 Test (org.junit.Test)7 DurableDataLog (io.pravega.segmentstore.storage.DurableDataLog)6 ArrayList (java.util.ArrayList)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 UpdateableContainerMetadata (io.pravega.segmentstore.server.UpdateableContainerMetadata)4 ObjectClosedException (io.pravega.common.ObjectClosedException)3 SequencedItemList (io.pravega.common.util.SequencedItemList)3 Operation (io.pravega.segmentstore.server.logs.operations.Operation)3 ProbeOperation (io.pravega.segmentstore.server.logs.operations.ProbeOperation)3 lombok.val (lombok.val)3 Preconditions (com.google.common.base.Preconditions)2 Exceptions (io.pravega.common.Exceptions)2 Timer (io.pravega.common.Timer)2 Futures (io.pravega.common.concurrent.Futures)2 ArrayView (io.pravega.common.util.ArrayView)2 CloseableIterator (io.pravega.common.util.CloseableIterator)2 MetadataBuilder (io.pravega.segmentstore.server.MetadataBuilder)2 DataLogDisabledException (io.pravega.segmentstore.storage.DataLogDisabledException)2 Duration (java.time.Duration)2