Savonitar commented on code in PR #30190: URL: https://github.com/apache/flink/pull/30190#discussion_r4771797362
########## flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/state/rocksdb/PrefixBoundedIteratorFactory.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.flink.state.rocksdb; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.util.ExceptionUtils; +import org.apache.flink.util.IOUtils; +import org.apache.flink.util.Preconditions; + +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.DirectSlice; +import org.rocksdb.ReadOptions; +import org.rocksdb.RocksDB; +import org.rocksdb.RocksIterator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.nio.ByteBuffer; +import java.util.function.IntFunction; + +/** + * Creates prefix-bounded iterators for MapState scans. RocksDB borrows a pointer to the upper bound + * for the lifetime of the iterator. Once that iterator closes, the next scan can reuse its slice. + * Overlapping iterators and bounds that exceed the buffer capacity get a slice of their own. + * + * <p>The factory is not thread-safe. Only the thread that accesses the state may use it. + */ +final class PrefixBoundedIteratorFactory implements AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(PrefixBoundedIteratorFactory.class); + + @VisibleForTesting static final int MAX_REUSED_BOUND_LENGTH = 256; + + private final ReadOptions readOptions; + private final IntFunction<ByteBuffer> boundAllocator; + @Nullable private ByteBuffer reusedBound; + @Nullable private DirectSlice reusedBoundSlice; + @Nullable private ReusedBoundIterator boundOwner; + private boolean directMemoryUnavailable; + private boolean closed; + + PrefixBoundedIteratorFactory(ReadOptions readOptions) { + this(readOptions, ByteBuffer::allocateDirect); + } + + @VisibleForTesting + PrefixBoundedIteratorFactory(ReadOptions readOptions, IntFunction<ByteBuffer> boundAllocator) { + this.readOptions = readOptions; + this.boundAllocator = boundAllocator; + } + + RocksIteratorWrapper newIterator( + RocksDB db, ColumnFamilyHandle columnFamilyHandle, byte[] prefix) { + Preconditions.checkState(!closed, "The iterator factory is closed."); + final int length = RocksDBOperationUtils.getPrefixEndLength(prefix); + if (length == 0 + || boundOwner != null + || length > MAX_REUSED_BOUND_LENGTH + || !ensureReusedBound(length)) { + return RocksDBOperationUtils.getRocksIteratorBoundedByPrefix( + db, columnFamilyHandle, readOptions, prefix); + } + + if (length != reusedBound.limit()) { + reusedBoundSlice.setLength(length); + reusedBound.limit(length); + } + reusedBound.rewind(); + reusedBound.put(prefix, 0, length - 1); + reusedBound.put((byte) (prefix[length - 1] + 1)); + + final ReadOptions options = + RocksDBOperationUtils.copyForPrefixBoundedIterators(readOptions, reusedBoundSlice); + try { + boundOwner = + new ReusedBoundIterator(db.newIterator(columnFamilyHandle, options), options); + return boundOwner; + } catch (RuntimeException | Error e) { + IOUtils.closeQuietly(options); + throw e; + } Review Comment: Agree, Done. As you mentioned, the cached copy will not see later changes and a snapshot could be released while the copy still refers to it. I documented that. I also updated the tests to check that settings applied before the first scan reach both the reused-bound and private-bound iterators. In the local mapIsEmpty benchmark, the extra time over an unbounded scan fell from 244 ns on master to 112 ns with slice reuse and 50 ns with both cached. That's with Long keys. When the effective bound length changes, we still call `setLength().` -- 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]
