xx789633 commented on code in PR #2409: URL: https://github.com/apache/fluss/pull/2409#discussion_r2735539020
########## fluss-server/src/main/java/org/apache/fluss/server/kv/scan/ScannerManager.java: ########## @@ -0,0 +1,161 @@ +/* + * 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.fluss.server.kv.scan; + +import org.apache.fluss.config.ConfigOptions; +import org.apache.fluss.config.Configuration; +import org.apache.fluss.rpc.protocol.Errors; +import org.apache.fluss.server.kv.KvTablet; +import org.apache.fluss.server.kv.rocksdb.RocksDBKv; +import org.apache.fluss.utils.AutoCloseableAsync; +import org.apache.fluss.utils.MapUtils; +import org.apache.fluss.utils.clock.Clock; +import org.apache.fluss.utils.clock.SystemClock; +import org.apache.fluss.utils.concurrent.ExecutorThreadFactory; +import org.apache.fluss.utils.concurrent.FutureUtils; + +import org.rocksdb.ReadOptions; +import org.rocksdb.RocksIterator; +import org.rocksdb.Snapshot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** A manager for scanners. */ +public class ScannerManager implements AutoCloseableAsync { + private static final Logger LOG = LoggerFactory.getLogger(ScannerManager.class); + + private final Map<ByteBuffer, ScannerContext> scanners = MapUtils.newConcurrentHashMap(); + private final ScheduledExecutorService cleanupExecutor; + private final Clock clock; + private final long scannerTtlMs; + + public ScannerManager(Configuration conf) { + this(conf, SystemClock.getInstance()); + } + + public ScannerManager(Configuration conf, Clock clock) { + this.clock = clock; + this.scannerTtlMs = conf.get(ConfigOptions.SERVER_SCANNER_TTL).toMillis(); + this.cleanupExecutor = + Executors.newSingleThreadScheduledExecutor( + new ExecutorThreadFactory("scanner-cleanup-thread")); + this.cleanupExecutor.scheduleWithFixedDelay( + this::cleanupExpiredScanners, + scannerTtlMs / 2, + scannerTtlMs / 2, + TimeUnit.MILLISECONDS); + } + + public byte[] createScanner(KvTablet kvTablet, long limit) { + RocksDBKv rocksDBKv = kvTablet.getRocksDBKv(); + Snapshot snapshot = rocksDBKv.getSnapshot(); + ReadOptions readOptions = new ReadOptions().setSnapshot(snapshot); + RocksIterator iterator = rocksDBKv.newIterator(readOptions); + iterator.seekToFirst(); + + ScannerContext context = + new ScannerContext(rocksDBKv, iterator, readOptions, snapshot, limit, clock); + byte[] scannerId = generateScannerId(); + scanners.put(ByteBuffer.wrap(scannerId), context); + return scannerId; + } + + public ScannerContext getScanner(byte[] scannerId) { + ScannerContext context = scanners.get(ByteBuffer.wrap(scannerId)); + if (context != null) { + context.updateLastAccessTime(clock.milliseconds()); + } + return context; + } + + public void keepAlive(byte[] scannerId) { + ScannerContext context = scanners.get(ByteBuffer.wrap(scannerId)); + if (context != null) { + context.updateLastAccessTime(clock.milliseconds()); Review Comment: I think every scanner should have an access lock protection, just like what kudu does: https://github.com/apache/kudu/blob/master/src/kudu/tserver/scanners.h#L384 -- 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]
