Aitozi commented on code in PR #7699:
URL: https://github.com/apache/paimon/pull/7699#discussion_r3214323613


##########
paimon-common/src/main/java/org/apache/paimon/fs/cache/CachingSeekableInputStream.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.paimon.fs.cache;
+
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.fs.SeekableInputStream;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+
+/** A {@link SeekableInputStream} that caches reads at block granularity on 
local disk. */
+public class CachingSeekableInputStream extends SeekableInputStream {

Review Comment:
   Has the performance improvement of CachingInputStream been measured yet?



##########
paimon-common/src/main/java/org/apache/paimon/fs/cache/LocalDiskCacheManager.java:
##########
@@ -0,0 +1,287 @@
+/*
+ * 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.paimon.fs.cache;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+/** Block-level local disk cache with LRU eviction. Thread-safe. */
+public class LocalDiskCacheManager implements LocalCacheManager {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(LocalDiskCacheManager.class);
+
+    private static final Map<CacheKey, LocalDiskCacheManager> SHARED_CACHES =
+            new ConcurrentHashMap<>();
+
+    private final File cacheDir;
+    private final long maxSizeBytes;
+    private final int blockSize;
+    private final Object lock = new Object();
+    private long currentSize;
+
+    public LocalDiskCacheManager(String cacheDir, long maxSizeBytes, int 
blockSize) {
+        this.cacheDir = new File(cacheDir);
+        this.maxSizeBytes = maxSizeBytes;
+        this.blockSize = blockSize;
+        this.cacheDir.mkdirs();
+        this.currentSize = scanSize();
+    }
+
+    /** Returns a shared instance for the given parameters, creating one if 
needed. */
+    public static LocalDiskCacheManager getOrCreate(
+            String cacheDir, long maxSizeBytes, int blockSize) {
+        CacheKey key = new CacheKey(cacheDir, maxSizeBytes, blockSize);
+        return SHARED_CACHES.computeIfAbsent(
+                key, k -> new LocalDiskCacheManager(cacheDir, maxSizeBytes, 
blockSize));
+    }
+
+    public int blockSize() {
+        return blockSize;
+    }
+
+    public byte[] getBlock(String filePath, int blockIndex) {
+        File path = cachePath(filePath, blockIndex);
+        if (!path.exists()) {
+            return null;
+        }
+        try {
+            byte[] data = Files.readAllBytes(path.toPath());
+            // touch to update mtime for LRU
+            path.setLastModified(System.currentTimeMillis());
+            return data;
+        } catch (IOException e) {
+            LOG.debug("Failed to read cache block: {}", path, e);
+            return null;
+        }
+    }
+
+    public void putBlock(String filePath, int blockIndex, byte[] data) {
+        File path = cachePath(filePath, blockIndex);
+        if (path.exists()) {
+            return;
+        }
+
+        File subDir = path.getParentFile();
+        subDir.mkdirs();
+
+        File tmpFile =
+                new File(
+                        path.getParent(),
+                        path.getName() + ".tmp." + 
Thread.currentThread().getId());
+        try {
+            try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
+                fos.write(data);
+            }
+            if (!tmpFile.renameTo(path)) {
+                tmpFile.delete();
+                // another thread won the race — don't update currentSize
+                return;
+            }
+        } catch (IOException e) {
+            tmpFile.delete();
+            LOG.debug("Failed to write cache block: {}", path, e);
+            return;
+        }
+
+        boolean needEvict = false;
+        synchronized (lock) {
+            currentSize += data.length;
+            needEvict = maxSizeBytes < Long.MAX_VALUE && currentSize > 
maxSizeBytes;
+        }
+        if (needEvict) {
+            evict();
+        }
+    }
+
+    private void evict() {
+        List<CacheEntry> entries = new ArrayList<>();
+        File[] prefixDirs = cacheDir.listFiles();

Review Comment:
   Why aren't we maintaining the sizes/access time of these cache files in 
memory to avoid these frequent list operations?



##########
paimon-common/src/main/java/org/apache/paimon/fs/cache/LocalDiskCacheManager.java:
##########
@@ -0,0 +1,287 @@
+/*
+ * 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.paimon.fs.cache;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+/** Block-level local disk cache with LRU eviction. Thread-safe. */
+public class LocalDiskCacheManager implements LocalCacheManager {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(LocalDiskCacheManager.class);
+
+    private static final Map<CacheKey, LocalDiskCacheManager> SHARED_CACHES =
+            new ConcurrentHashMap<>();
+
+    private final File cacheDir;
+    private final long maxSizeBytes;
+    private final int blockSize;
+    private final Object lock = new Object();
+    private long currentSize;
+
+    public LocalDiskCacheManager(String cacheDir, long maxSizeBytes, int 
blockSize) {
+        this.cacheDir = new File(cacheDir);
+        this.maxSizeBytes = maxSizeBytes;
+        this.blockSize = blockSize;
+        this.cacheDir.mkdirs();
+        this.currentSize = scanSize();
+    }
+
+    /** Returns a shared instance for the given parameters, creating one if 
needed. */
+    public static LocalDiskCacheManager getOrCreate(
+            String cacheDir, long maxSizeBytes, int blockSize) {
+        CacheKey key = new CacheKey(cacheDir, maxSizeBytes, blockSize);
+        return SHARED_CACHES.computeIfAbsent(
+                key, k -> new LocalDiskCacheManager(cacheDir, maxSizeBytes, 
blockSize));
+    }
+
+    public int blockSize() {
+        return blockSize;
+    }
+
+    public byte[] getBlock(String filePath, int blockIndex) {
+        File path = cachePath(filePath, blockIndex);
+        if (!path.exists()) {
+            return null;
+        }
+        try {
+            byte[] data = Files.readAllBytes(path.toPath());
+            // touch to update mtime for LRU
+            path.setLastModified(System.currentTimeMillis());
+            return data;
+        } catch (IOException e) {
+            LOG.debug("Failed to read cache block: {}", path, e);
+            return null;
+        }
+    }
+
+    public void putBlock(String filePath, int blockIndex, byte[] data) {
+        File path = cachePath(filePath, blockIndex);

Review Comment:
   Here, we are storing each block as an individual file. Will this lead to an 
excessive number of local files? AFAIK, engines like StarRocks handle this 
differently; they generally store serval blocks to a big single file.
   
   Another issue to consider here is the access pattern of these files. For 
example, data files might have only certain blocks accessed due to column 
pruning, But regarding other files like the Manifest and Index, will we only 
access a portion of them? I'm not entirely sure.If we aren't accessing only a 
portion of the data, does it still make sense for us to break this down into 
blocks?
   
   Internally, we adopted a different approach. For Meta files, we store a 
one-to-one mapping between the local file and the remote metadata file. 
https://github.com/apache/paimon/issues/5820



-- 
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]

Reply via email to