github-actions[bot] commented on code in PR #70830:
URL: https://github.com/apache/doris/pull/70830#discussion_r4661513530


##########
fe/fe-connector/fe-connector-lance/src/main/java/org/apache/doris/connector/lance/LanceCatalogClient.java:
##########
@@ -202,26 +258,48 @@ private <T> T readTable(String dbName, String tableName, 
OptionalLong version, T
         }, "load Lance table metadata for " + what);
     }
 
-    private static LanceTableMetadata readMetadata(Dataset dataset, 
LanceTableAccess access) {
-        List<LanceFragmentInfo> fragments = new ArrayList<>();
-        for (Fragment fragment : dataset.getFragments()) {
-            fragments.add(new 
LanceFragmentInfo(Integer.toUnsignedLong(fragment.getId()),
-                    fragment.metadata().getNumRows(), 
fragment.metadata().getPhysicalRows()));
+    private static LanceTableMetadata readMetadata(Dataset dataset, 
LanceTableAccess access,
+            LanceMetadataMetrics.Timer timer) {
+        Schema schema = timer.measure(Stage.SCHEMA, dataset::getSchema);
+        List<LanceFragmentInfo> fragments = timer.measure(Stage.FRAGMENTS, () 
-> {
+            List<LanceFragmentInfo> infos = new ArrayList<>();
+            for (Fragment fragment : dataset.getFragments()) {
+                infos.add(new 
LanceFragmentInfo(Integer.toUnsignedLong(fragment.getId()),
+                        fragment.metadata().getNumRows(), 
fragment.metadata().getPhysicalRows()));
+            }
+            return infos;
+        });
+        return new LanceTableMetadata(access, dataset.version(), schema, 
fragments);
+    }
+
+    private static long versionAtOrBefore(Dataset latest, long 
timestampMillis, String requestedText,
+            String what) {
+        try {
+            return 
LanceSnapshotResolver.versionAtOrBefore(latest.listVersions(), timestampMillis, 
requestedText);
+        } catch (LanceSnapshotResolver.NoVersionAtOrBeforeException e) {
+            throw new UserFacingException("Lance table " + what + " has no 
version at or before '"
+                    + requestedText + "'");
+        } catch (LanceSnapshotResolver.HistoryRemovedException e) {
+            throw new UserFacingException("Lance cannot resolve FOR TIME AS OF 
'" + requestedText
+                    + "' on " + what + ": version " + e.getVersion()
+                    + ", which may hold the state at that time, no longer 
exists");
         }
-        return new LanceTableMetadata(access, dataset.version(), 
dataset.getSchema(), fragments);
     }
 
-    private static Dataset openDataset(BufferAllocator readAllocator, 
LanceTableAccess access,
-            OptionalLong version) {
+    /**
+     * Opens the dataset with the generation's Session: its caches, not the 
dataset, decide how much metadata is
+     * kept, and the next open of the same version reads it from them.
+     */
+    private static Dataset openDataset(Generation generation, BufferAllocator 
readAllocator,
+            LanceTableAccess access, OptionalLong version, 
LanceMetadataMetrics.Timer timer) {
         ReadOptions.Builder options = new ReadOptions.Builder()
                 .setStorageOptions(access.getStorageOptions())
-                .setIndexCacheSizeBytes(0)
-                .setMetadataCacheSizeBytes(METADATA_CACHE_SIZE_BYTES);
+                .setSession(generation.session);

Review Comment:
   [P1] Invalidate the Lance Session caches when refreshing a table/database. 
`invalidateTableAccess()` swaps only the namespace access cache while the 
generation Session remains warm, and every `Dataset.open()` reuses that 
Session. Lance's URI-scoped metadata/index caches retain version-keyed entries; 
if a table is dropped/recreated or replaced at the same URI and its version 
restarts, a REFRESH TABLE/DATABASE can reopen the new URI but serve cached 
state from the previous incarnation. FE refresh/replay uses these methods, 
while only REFRESH CATALOG creates a cold Session. Retire/reset the affected 
Session (or otherwise partition/clear its caches) on table/database 
invalidation and add a same-URI replacement test.



##########
fe/fe-connector/fe-connector-lance/src/main/java/org/apache/doris/connector/lance/LanceCatalogClient.java:
##########
@@ -148,40 +176,68 @@ LanceIndexMetadata loadIndexMetadata(String dbName, 
String tableName, long versi
      * the commit times the manifests record; see {@link 
LanceSnapshotResolver#versionAtOrBefore}.
      */
     LanceTableMetadata loadTableMetadataAtOrBefore(String dbName, String 
tableName, long timestampMillis,
-            String requestedText) {
+            String requestedText, LanceMetadataMetrics metrics) {
         String what = dbName + "." + tableName;
-        return readTable(dbName, tableName, OptionalLong.empty(), (access, 
readAllocator) -> {
-            long version;
-            try (Dataset latest = openDataset(readAllocator, access, 
OptionalLong.empty())) {
-                try {
-                    version = 
LanceSnapshotResolver.versionAtOrBefore(latest.listVersions(), timestampMillis,
-                            requestedText);
-                } catch (LanceSnapshotResolver.NoVersionAtOrBeforeException e) 
{
-                    throw new UserFacingException("Lance table " + what + " 
has no version at or before '"
-                            + requestedText + "'");
-                } catch (LanceSnapshotResolver.HistoryRemovedException e) {
-                    throw new UserFacingException("Lance cannot resolve FOR 
TIME AS OF '" + requestedText
-                            + "' on " + what + ": version " + e.getVersion()
-                            + ", which may hold the state at that time, no 
longer exists");
-                }
-            }
-            LOG.debug("Resolved Lance FOR TIME AS OF '{}' to version {} from 
manifest commit times",
-                    requestedText, version);
-            try (Dataset dataset = openDataset(readAllocator, access, 
OptionalLong.of(version))) {
-                return readMetadata(dataset, access);
-            }
-        });
+        return readTable(dbName, tableName, OptionalLong.empty(), metrics,
+                (generation, access, readAllocator, timer) -> {
+                    long version;
+                    try (Dataset latest = openDataset(generation, 
readAllocator, access, OptionalLong.empty(),
+                            timer)) {
+                        version = timer.measure(Stage.VERSION_RESOLVE,
+                                () -> versionAtOrBefore(latest, 
timestampMillis, requestedText, what));
+                    }
+                    LOG.debug("Resolved Lance FOR TIME AS OF '{}' to version 
{} from manifest commit times",
+                            requestedText, version);
+                    try (Dataset dataset = openDataset(generation, 
readAllocator, access, OptionalLong.of(version),
+                            timer)) {
+                        return readMetadata(dataset, access, timer);
+                    }
+                });
+    }
+
+    /**
+     * REFRESH CATALOG: retires the current generation, so that the next call 
opens a new namespace, allocator and
+     * Session. Opens nothing and waits for nothing: calls in flight finish on 
the retired generation, and the
+     * last of them releases it.
+     */
+    void refresh() {
+        Generation idle = retireCurrent();

Review Comment:
   [P2] Bound overlapping retired generations during REFRESH CATALOG. 
`refresh()` detaches the current generation and relies on its in-flight calls 
to drain before closing it, but a new call can open G2 while G1 is still 
blocked and the next refresh can retire G2 as well. Each generation allocates 
up to 192 MiB of native Session caches outside the Arrow limit, with no cap, so 
repeated refresh/new-call cycles can retain one cache set per slow call until 
they finish and exhaust FE native memory. Coalesce or bound retired 
generations, or otherwise prevent unbounded Session allocation.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to