smengcl commented on code in PR #10484: URL: https://github.com/apache/ozone/pull/10484#discussion_r3532834266
########## hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/db/LatestVersionedKWayMergeIterator.java: ########## @@ -0,0 +1,553 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.utils.db; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.primitives.UnsignedLong; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.PriorityQueue; +import org.apache.hadoop.hdds.utils.IOUtils; +import org.apache.hadoop.hdds.utils.db.managed.ManagedOptions; +import org.apache.hadoop.ozone.util.ClosableIterator; + +/** + * Dual-heap k-way merge over RocksDB SST files for snapshot diff. + * <p> + * Two heaps track non-tombstones and tombstones separately. For each user key, + * all versions are drained from both heaps, then snapshot-diff emit rules apply: + * emit the latest tombstone and/or latest value, including both when a delete is + * followed by a newer recreate. + * <p> + * When constructed with an exclusive minimum sequence number {@code S}, each SST + * source skips entries whose sequence is {@code <= S} while advancing. + */ +public final class LatestVersionedKWayMergeIterator implements + ClosableIterator<LatestVersionedKWayMergeIterator.MergedKeyValue> { + + /** RocksDB {@code ValueType::kTypeValue}. */ + public static final int ROCKS_TYPE_VALUE = 1; + private static final int DEFAULT_READ_AHEAD_SIZE = 2 * 1024 * 1024; + + private final ManagedOptions options; + private final List<ClosableIterator<? extends MergeHead>> iterators; + private final Long exclusiveMinSequenceNumber; + + private final PriorityQueue<HeapEntry> valueHeap; + private final PriorityQueue<HeapEntry> tombstoneHeap; + + private List<MergedKeyValue> emitQueue; + private boolean initialized; + + public static LatestVersionedKWayMergeIterator overRawSstFiles(Collection<Path> sstFiles) + throws IOException { + return overRawSstFiles(sstFiles, DEFAULT_READ_AHEAD_SIZE, null); + } + + public static LatestVersionedKWayMergeIterator overRawSstFiles(Collection<Path> sstFiles, + int readAheadSizePerFile) throws IOException { + return overRawSstFiles(sstFiles, readAheadSizePerFile, null); + } + + /** + * Opens one iterator per SST file and merges them. + * + * @param exclusiveMinSequenceNumber when non-null, each source skips entries with + * sequence {@code <=} this value while advancing; when null, no entries are skipped + */ + public static LatestVersionedKWayMergeIterator overRawSstFiles(Collection<Path> sstFiles, + int readAheadSizePerFile, Long exclusiveMinSequenceNumber) throws IOException { + Objects.requireNonNull(sstFiles, "sstFiles cannot be null"); + ManagedOptions options = new ManagedOptions(); + List<ClosableIterator<? extends MergeHead>> sources = new ArrayList<>(sstFiles.size()); + for (Path file : sstFiles) { + sources.add(new RawSstIterator(options, file, readAheadSizePerFile)); + } Review Comment: If new RawSstIterator() throws (e.g. due to missing/corrupt SST), those opened readers and options would leak. Can we wrap it with try-catch like: ```java try { for (Path file : sstFiles) { sources.add(new RawSstIterator(options, file, readAheadSizePerFile)); } } catch (RuntimeException e) { IOUtils.closeQuietly(sources); options.close(); throw e; } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
