924060929 commented on code in PR #70830:
URL: https://github.com/apache/doris/pull/70830#discussion_r4665616715


##########
fe/fe-connector/fe-connector-lance/src/main/java/org/apache/doris/connector/lance/LanceCatalogClient.java:
##########
@@ -202,26 +259,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())

Review Comment:
   [P2] Can we skip the catalog-wide Session for accesses with vended storage 
options? A REST namespace can return a fresh session token on each describe. 
The zero TTL handles the Java cache, but every open here still uses the same 
native Session. Lance 12's object-store registry keys entries by the full 
options, holds the keys strongly, and only removes an expired weak store on 
lookup of that same key. A rotated token leaves its old options in native 
memory until the catalog Session closes. The 64/128 MiB cache limits do not 
cover this registry.
   
   I checked with lance-core 12.0.0 on macOS, opening the same local dataset 
and closing it after every read. With 48 reads using one synthetic token, the 
number of live native allocations in the token's size class stayed flat. With 
48 distinct tokens it grew by 96, then fell back after closing the Session. 
Carrying the vended-options flag from describe to open would let these reads 
use a dataset-owned Session while ordinary reads keep the shared cache. [Lance 
registry lookup and 
insertion](https://github.com/lance-format/lance/blob/cbeec97cb893a66ad6b3db87f25a57ab0093f358/rust/lance-io/src/object_store/providers.rs#L279-L325)



##########
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:
   I followed the stale index through the BE reader using the new test's V1 
dataset. After recreating the same URI at version 2 without the index, I called 
`invalidateTable` and planned `WHERE id = 101` in a fresh statement. The FE 
range still contained the deleted index's segment UUID for fragment 0. Passing 
that range to lance-c failed with `scalar index segment … is absent from the 
dataset snapshot`. The missing-UUID check runs before fallback. After 
`invalidateAll`, FE planned the same fragment without the UUID and the scanner 
returned the expected one row.
   
   This makes an ordinary filtered query fail after an explicit table refresh. 
Could table/database invalidation also invalidate the affected native index 
metadata, or use a fresh Session for the affected table's pinned index lookup? 
The existing fixture could assert that the next filtered scan succeeds after 
`invalidateTable` as well. [lance-c segment 
validation](https://github.com/lance-format/lance-c/blob/a51ab47a9c814a746cb1714c99fad93dd3a188b8/src/scalar_segment.rs#L187-L203)



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