Search in sources :

Example 21 with ChunkMetadata

use of io.pravega.segmentstore.storage.metadata.ChunkMetadata in project pravega by pravega.

the class SystemJournal method createSystemSnapshotRecord.

private CompletableFuture<SystemSnapshotRecord> createSystemSnapshotRecord(MetadataTransaction txn, boolean validateSegment, boolean validateChunks) {
    val systemSnapshot = SystemSnapshotRecord.builder().epoch(epoch).fileIndex(currentFileIndex.get()).segmentSnapshotRecords(new ArrayList<>()).build();
    val futures = Collections.synchronizedList(new ArrayList<CompletableFuture<Void>>());
    for (val systemSegment : systemSegments) {
        // Find segment metadata.
        val future = txn.get(systemSegment).thenComposeAsync(metadata -> {
            val segmentMetadata = (SegmentMetadata) metadata;
            segmentMetadata.checkInvariants();
            val segmentSnapshot = SegmentSnapshotRecord.builder().segmentMetadata(segmentMetadata).chunkMetadataCollection(new ArrayList<>()).build();
            // Enumerate all chunks.
            val currentChunkName = new AtomicReference<>(segmentMetadata.getFirstChunk());
            val dataSize = new AtomicLong();
            val chunkCount = new AtomicLong();
            // For each chunk
            return Futures.loop(() -> null != currentChunkName.get(), () -> txn.get(currentChunkName.get()).thenComposeAsync(m -> {
                val currentChunkMetadata = (ChunkMetadata) m;
                CompletableFuture<Void> f;
                Preconditions.checkState(null != currentChunkMetadata, "currentChunkMetadata must not be null");
                if (validateChunks) {
                    f = chunkStorage.getInfo(currentChunkName.get()).thenAcceptAsync(chunkInfo -> Preconditions.checkState(chunkInfo.getLength() >= currentChunkMetadata.getLength(), "Wrong chunk length chunkInfo=%d, currentMetadata=%d.", chunkInfo.getLength(), currentChunkMetadata.getLength()), executor);
                } else {
                    f = CompletableFuture.completedFuture(null);
                }
                return f.thenAcceptAsync(v -> {
                    chunkCount.getAndIncrement();
                    dataSize.addAndGet(currentChunkMetadata.getLength());
                    segmentSnapshot.chunkMetadataCollection.add(currentChunkMetadata);
                    // move to next chunk
                    currentChunkName.set(currentChunkMetadata.getNextChunk());
                }, executor);
            }, executor), executor).thenAcceptAsync(v -> {
                // Validate
                if (validateSegment) {
                    Preconditions.checkState(chunkCount.get() == segmentMetadata.getChunkCount(), "Wrong chunk count. Segment=%s", segmentMetadata);
                    Preconditions.checkState(dataSize.get() == segmentMetadata.getLength() - segmentMetadata.getFirstChunkStartOffset(), "Data size does not match dataSize (%s). Segment=%s", dataSize.get(), segmentMetadata);
                }
                // Add to the system snapshot.
                synchronized (systemSnapshot) {
                    systemSnapshot.segmentSnapshotRecords.add(segmentSnapshot);
                }
            }, executor);
        }, executor);
        futures.add(future);
    }
    return Futures.allOf(futures).thenApplyAsync(vv -> {
        systemSnapshot.checkInvariants();
        return systemSnapshot;
    }, executor);
}
Also used : lombok.val(lombok.val) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) ChunkMetadata(io.pravega.segmentstore.storage.metadata.ChunkMetadata) Getter(lombok.Getter) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Exceptions(io.pravega.common.Exceptions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) RevisionDataInput(io.pravega.common.io.serialization.RevisionDataInput) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) MultiKeySequentialProcessor(io.pravega.common.concurrent.MultiKeySequentialProcessor) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MetadataTransaction(io.pravega.segmentstore.storage.metadata.MetadataTransaction) RevisionDataOutput(io.pravega.common.io.serialization.RevisionDataOutput) VersionedSerializer(io.pravega.common.io.serialization.VersionedSerializer) ChunkMetadata(io.pravega.segmentstore.storage.metadata.ChunkMetadata) NameUtils(io.pravega.shared.NameUtils) Executor(java.util.concurrent.Executor) NonNull(lombok.NonNull) Collection(java.util.Collection) lombok.val(lombok.val) Set(java.util.Set) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) EqualsAndHashCode(lombok.EqualsAndHashCode) Timer(io.pravega.common.Timer) EOFException(java.io.EOFException) ObjectBuilder(io.pravega.common.ObjectBuilder) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ChunkMetadataStore(io.pravega.segmentstore.storage.metadata.ChunkMetadataStore) Builder(lombok.Builder) Data(lombok.Data) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 22 with ChunkMetadata

use of io.pravega.segmentstore.storage.metadata.ChunkMetadata in project pravega by pravega.

the class SystemJournal method validateSegment.

/**
 * Validate that all information for given segment in given snapshot exists in transaction.
 */
private CompletableFuture<Void> validateSegment(MetadataTransaction txn, String segmentName) {
    return txn.get(segmentName).thenComposeAsync(m -> {
        val segmentMetadata = (SegmentMetadata) m;
        Preconditions.checkState(null != segmentMetadata, "Segment metadata must not be null.");
        val chunkName = new AtomicReference<>(segmentMetadata.getFirstChunk());
        return Futures.loop(() -> chunkName.get() != null, () -> txn.get(chunkName.get()).thenAcceptAsync(mm -> {
            Preconditions.checkState(null != mm, "Chunk metadata must not be null.");
            val chunkMetadata = (ChunkMetadata) mm;
            chunkName.set(chunkMetadata.getNextChunk());
        }, executor), executor);
    }, executor);
}
Also used : lombok.val(lombok.val) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) Getter(lombok.Getter) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Exceptions(io.pravega.common.Exceptions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) RevisionDataInput(io.pravega.common.io.serialization.RevisionDataInput) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) MultiKeySequentialProcessor(io.pravega.common.concurrent.MultiKeySequentialProcessor) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MetadataTransaction(io.pravega.segmentstore.storage.metadata.MetadataTransaction) RevisionDataOutput(io.pravega.common.io.serialization.RevisionDataOutput) VersionedSerializer(io.pravega.common.io.serialization.VersionedSerializer) ChunkMetadata(io.pravega.segmentstore.storage.metadata.ChunkMetadata) NameUtils(io.pravega.shared.NameUtils) Executor(java.util.concurrent.Executor) NonNull(lombok.NonNull) Collection(java.util.Collection) lombok.val(lombok.val) Set(java.util.Set) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) EqualsAndHashCode(lombok.EqualsAndHashCode) Timer(io.pravega.common.Timer) EOFException(java.io.EOFException) ObjectBuilder(io.pravega.common.ObjectBuilder) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ChunkMetadataStore(io.pravega.segmentstore.storage.metadata.ChunkMetadataStore) Builder(lombok.Builder) Data(lombok.Data) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

ChunkMetadata (io.pravega.segmentstore.storage.metadata.ChunkMetadata)22 lombok.val (lombok.val)20 SegmentMetadata (io.pravega.segmentstore.storage.metadata.SegmentMetadata)17 Preconditions (com.google.common.base.Preconditions)12 ArrayList (java.util.ArrayList)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Slf4j (lombok.extern.slf4j.Slf4j)12 Exceptions (io.pravega.common.Exceptions)11 Futures (io.pravega.common.concurrent.Futures)11 MetadataTransaction (io.pravega.segmentstore.storage.metadata.MetadataTransaction)11 Collections (java.util.Collections)11 Callable (java.util.concurrent.Callable)11 CompletionException (java.util.concurrent.CompletionException)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 List (java.util.List)10 NameUtils (io.pravega.shared.NameUtils)9 HashSet (java.util.HashSet)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 ChunkMetadataStore (io.pravega.segmentstore.storage.metadata.ChunkMetadataStore)8