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);
}
}
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);
}
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);
}
}
Aggregations