danny0405 commented on code in PR #17802: URL: https://github.com/apache/hudi/pull/17802#discussion_r2674774337
########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/partitioner/index/RecordIndexCache.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.sink.partitioner.index; + +import org.apache.hudi.common.config.HoodieCommonConfig; +import org.apache.hudi.common.model.HoodieRecordGlobalLocation; +import org.apache.hudi.common.serialization.DefaultSerializer; +import org.apache.hudi.common.util.DefaultSizeEstimator; +import org.apache.hudi.common.util.ValidationUtils; +import org.apache.hudi.common.util.VisibleForTesting; +import org.apache.hudi.common.util.collection.ExternalSpillableMap; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.configuration.FlinkOptions; +import org.apache.hudi.util.FlinkWriteClients; + +import lombok.Getter; +import org.apache.flink.configuration.Configuration; + +import java.io.Closeable; +import java.io.IOException; +import java.util.Comparator; +import java.util.NavigableMap; +import java.util.TreeMap; + +/** + * Cache to hold the in-flight record level index entries which are not committed to metadata table yet. + * <p> + * todo: use map backed by flink managed memory. + */ +public class RecordIndexCache implements Closeable { + @VisibleForTesting + @Getter + private final TreeMap<Long, ExternalSpillableMap<String, HoodieRecordGlobalLocation>> caches; + private final HoodieWriteConfig writeConfig; + private final long maxCacheSizeInBytes; + + public RecordIndexCache(Configuration conf, long initCheckpointId) { + this.caches = new TreeMap<>(Comparator.reverseOrder()); + this.writeConfig = FlinkWriteClients.getHoodieClientConfig(conf, false, false); + this.maxCacheSizeInBytes = conf.get(FlinkOptions.BUCKET_ASSIGN_INFLIGHT_INDEX_CACHE_SIZE) * 1024 * 1024; + addCheckpointCache(initCheckpointId); + } + + /** + * Add a new checkpoint cache for the given checkpoint ID. + * + * @param checkpointId the checkpoint ID + */ + public void addCheckpointCache(long checkpointId) { + try { + // Create a new ExternalSpillableMap for this checkpoint + ExternalSpillableMap<String, HoodieRecordGlobalLocation> newCache = + new ExternalSpillableMap<>( + maxCacheSizeInBytes, + writeConfig.getSpillableMapBasePath(), + new DefaultSizeEstimator<>(), + new DefaultSizeEstimator<>(), + writeConfig.getCommonConfig().getSpillableDiskMapType(), + new DefaultSerializer<>(), Review Comment: might need to fire a sub-task to introduce efficient serializer in the future. -- 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]
