singhpk234 commented on code in PR #8644: URL: https://github.com/apache/iceberg/pull/8644#discussion_r1336483112
########## core/src/main/java/org/apache/iceberg/io/AsyncFileIO.java: ########## @@ -0,0 +1,269 @@ +/* + * 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.iceberg.io; + +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.github.benmanes.caffeine.cache.RemovalListener; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import org.apache.iceberg.Files; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.relocated.com.google.common.hash.Hashing; +import org.apache.iceberg.relocated.com.google.common.io.ByteStreams; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.SerializableMap; +import org.apache.iceberg.util.ThreadPools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** A {@link FileIO} implementation that caches files when an {@link InputFile} is created. */ +public class AsyncFileIO extends ResolvingFileIO { + private static final Logger LOG = LoggerFactory.getLogger(AsyncFileIO.class); + + private static final String FILE_PREFIX = "file:"; + private static final String ASYNC_ENABLED = "async.enabled"; + private static final String CACHE_LOCATION = "async.cache-location"; + + // Using a holder instead of double-checked locking + // see https://stackoverflow.com/questions/6189099/ + private static final class SharedPools { + static final ExecutorService DOWNLOAD_POOL = ThreadPools.newWorkerPool("async-file-io"); + } + + private SerializableMap<String, String> properties = null; + private FileIO local = null; + private boolean enabled = true; + private String cacheLocation = null; + + private transient LoadingCache<String, InputFile> lazyCache = null; + + // detect when this has been serialized using a transient variable + private transient Boolean isRemote = false; + + @Override + public InputFile newInputFile(String path) { + if (enabled /*&& null == isRemote*/) { + return cache().get(path); + } + + return super.newInputFile(path); + } + + @Override + public OutputFile newOutputFile(String path) { + return super.newOutputFile(path); + } + + @Override + public void deleteFile(String path) { + super.deleteFile(path); + } + + @Override + public Map<String, String> properties() { + return properties.immutableMap(); + } + + @Override + public void initialize(Map<String, String> props) { + super.initialize(props); + + this.properties = SerializableMap.copyOf(props); + this.enabled = PropertyUtil.propertyAsBoolean(props, ASYNC_ENABLED, true); + + String location = props.getOrDefault(CACHE_LOCATION, System.getProperty("java.io.tmpdir")); Review Comment: should we control how many file we want to be present in the disc at any point of time to avoid filling up the temp dir ? As per my understanding we would hold the references of Input File obj created in the hashmap till the BaseReader class is GC ed ########## core/src/main/java/org/apache/iceberg/io/AsyncFileIO.java: ########## @@ -0,0 +1,269 @@ +/* + * 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.iceberg.io; + +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.github.benmanes.caffeine.cache.RemovalListener; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import org.apache.iceberg.Files; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.relocated.com.google.common.hash.Hashing; +import org.apache.iceberg.relocated.com.google.common.io.ByteStreams; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.SerializableMap; +import org.apache.iceberg.util.ThreadPools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** A {@link FileIO} implementation that caches files when an {@link InputFile} is created. */ +public class AsyncFileIO extends ResolvingFileIO { + private static final Logger LOG = LoggerFactory.getLogger(AsyncFileIO.class); + + private static final String FILE_PREFIX = "file:"; + private static final String ASYNC_ENABLED = "async.enabled"; + private static final String CACHE_LOCATION = "async.cache-location"; + + // Using a holder instead of double-checked locking + // see https://stackoverflow.com/questions/6189099/ + private static final class SharedPools { + static final ExecutorService DOWNLOAD_POOL = ThreadPools.newWorkerPool("async-file-io"); + } + + private SerializableMap<String, String> properties = null; + private FileIO local = null; + private boolean enabled = true; + private String cacheLocation = null; + + private transient LoadingCache<String, InputFile> lazyCache = null; + + // detect when this has been serialized using a transient variable + private transient Boolean isRemote = false; + + @Override + public InputFile newInputFile(String path) { + if (enabled /*&& null == isRemote*/) { Review Comment: deletes could also benefit from it, as table.io() would be shared and repeated reads of deletes could serve from local ? -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org