hudi-agent commented on code in PR #18821: URL: https://github.com/apache/hudi/pull/18821#discussion_r3291595985
########## hudi-common/src/main/java/org/apache/hudi/common/util/collection/RocksDBDAOFactory.java: ########## @@ -0,0 +1,114 @@ +/* + * 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.hudi.common.util.collection; + +import org.apache.hudi.common.serialization.CustomSerializer; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Factory that creates and caches {@link RocksDBDAO} singletons keyed by {@code (basePath, rocksDBBasePath)}. + * + * <p>Multiple callers that supply identical parameters share a single underlying RocksDB instance. + * The factory tracks a reference count; the instance is physically closed and evicted only when + * every holder has called {@link #release}. + * + * <p>Callers <em>must</em> call {@link #release} (not {@link RocksDBDAO#close}) when they are + * done with the instance, so that the reference count is decremented correctly. + * + * <p>Note: column-family serializers and the {@code disableWAL} flag are applied only on the + * first creation for a given key. Subsequent callers that pass different values for these + * parameters will share the instance created by the first caller. + */ +public class RocksDBDAOFactory { + private static final Object LOCK = new Object(); + private static final Map<String, Entry> INSTANCES = new HashMap<>(); + + private RocksDBDAOFactory() { + } + + /** + * Returns a shared {@link RocksDBDAO} for the given parameters, creating one if absent. + * + * <p>Increments the internal reference count. Callers must invoke {@link #release} with the Review Comment: 🤖 The doc note that subsequent callers' `columnFamilySerializers`/`disableWAL` are silently ignored is a real footgun — if two callers ever pass different serializers for the same column family, reads/writes via the second caller would silently use the first's format. For the current `RocksDBIndexBackend` use, `isPartitionedTable` is table-scoped so this is OK, but if two jobs in the same TM share a path with different partitioning, data could be silently corrupted. Have you considered failing fast when subsequent callers pass conflicting parameters, or making serializer registration explicit per caller? <sub><i>- AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> -- 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]
