github-actions[bot] commented on code in PR #70869:
URL: https://github.com/apache/doris/pull/70869#discussion_r4667934782
##########
fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergScanPlanProvider.java:
##########
@@ -967,7 +968,7 @@ private List<ConnectorScanRange> planScanInternal(
? extractVendedToken(table, restVendedCredentialsEnabled()) :
Collections.emptyMap();
// Derive the vended storage config ONCE per scan (the token is
scan-invariant) and reuse it for every
// per-file path normalization below, instead of rebuilding it per
data/delete file (C3).
Review Comment:
[P1] Route every direct V4 delete-manifest probe through the partition-spec
fallback. `getScanNodeProperties` calls
`cachedApplicableEqualityDeleteFieldIds`, COUNT capability/pushdown calls
`livePositionDeleteRowCount` (including streaming estimation), and the count
encryption probe calls `ManifestFiles.read` directly; these pass
`table.specs()` and bypass `IcebergManifestLoader.manifestReadSpecs`. For an
upgraded V4 Parquet manifest retaining a spec whose source column was expired,
these probes fail before the header schema can be recovered, so scan
properties, count support, or streaming planning rejects a table that normal
manifest loading can read. Share the helper and add coverage for each probe.
##########
fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java:
##########
@@ -0,0 +1,416 @@
+// 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.doris.connector.iceberg;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.InternalData;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.ManifestListFile;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.avro.AvroIterable;
+import org.apache.iceberg.exceptions.NotFoundException;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.types.Types;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/** Serializable, borrowed read view for table-relative format v4 locations. */
+public final class IcebergV4FileIO implements FileIO {
+ private static final long serialVersionUID = 1L;
+ private static final Pattern SCHEME =
Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*:.*", Pattern.DOTALL);
+ private final FileIO delegate;
+ private final String tableLocation;
+ private final String metadataFileLocation;
+ private final Map<Long, String> inlineSnapshotLists;
+ private transient volatile TableMetadata metadata;
+ private transient LoadingCache<String, TableMetadata> metadataFiles;
+ private transient LoadingCache<String, Integer> manifestVersions;
+ private transient LoadingCache<Long, MetadataHeader> snapshotHeaders;
+ private transient ConcurrentMap<String, MetadataLocation>
metadataLocations;
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation) {
+ this(delegate, tableLocation, null);
+ }
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation,
TableMetadata metadata) {
+ this(delegate, tableLocation, metadata, metadata == null ? null :
metadata.metadataFileLocation(),
+ metadata != null && metadata.metadataFileLocation() == null ?
metadata.snapshots().stream()
+ .filter(snapshot -> snapshot.manifestListLocation() !=
null)
+ .collect(Collectors.toMap(Snapshot::snapshotId,
Snapshot::manifestListLocation)) : Map.of());
+ }
+
+ private IcebergV4FileIO(FileIO delegate, String tableLocation,
+ TableMetadata metadata, String metadataFileLocation, Map<Long,
String> inlineSnapshotLists) {
+ this.delegate = Objects.requireNonNull(delegate, "delegate");
+ this.tableLocation = tableLocation;
+ this.metadata = metadata;
+ this.metadataFileLocation = metadataFileLocation;
+ // REST responses may supply inline metadata without a metadata-file
identity to reopen on the BE.
+ this.inlineSnapshotLists = Map.copyOf(inlineSnapshotLists);
+ }
+
+ public FileIO delegate() {
+ return delegate;
+ }
+
+ public IcebergV4FileIO withDelegate(FileIO replacement) {
+ return new IcebergV4FileIO(replacement, tableLocation, metadata,
metadataFileLocation, inlineSnapshotLists);
+ }
+
+ private TableMetadata metadata() {
+ if (metadata == null && metadataFileLocation != null) {
+ metadata = metadataFiles().get(metadataFileLocation);
+ }
+ return metadata;
+ }
+
+ private synchronized LoadingCache<String, TableMetadata> metadataFiles() {
+ if (metadataFiles == null) {
+ // Transport only the immutable metadata-file identity, not all
snapshot history in every JNI task.
+ // The cache coalesces lazy remote loads without doing I/O under
this initialization lock.
+ metadataFiles = Caffeine.newBuilder().maximumSize(1)
+ .build(path -> TableMetadataParser.read(delegate, path));
+ }
+ return metadataFiles;
+ }
+
+ public static String resolve(String tableLocation, String path) {
+ Objects.requireNonNull(path, "path");
+ if (isAbsolute(path)) {
+ return path;
+ }
+ if (tableLocation == null || tableLocation.isEmpty()) {
+ throw new IllegalArgumentException("Relative Iceberg v4 path
requires a table location: " + path);
+ }
+ // Duplicate separators are part of an object-store key, including at
the concatenation boundary.
+ return tableLocation + "/" + path;
+ }
+
+ private static boolean isAbsolute(String path) {
+ return SCHEME.matcher(path).matches();
+ }
+
+ public String resolveLocation(String path) {
+ return resolve(tableLocation, path);
+ }
+
+ public String resolveContentFile(ContentFile<?> file) {
+ return isAbsolute(file.location()) || isLegacyEntry(file)
+ ? file.location() : resolve(tableLocation, file.location());
+ }
+
+ private boolean isLegacyEntry(ContentFile<?> file) {
+ String manifest = file.manifestLocation();
+ // Encoding alone is insufficient: the SDK can also write a V4
manifest using Avro.
+ return manifest != null && FileFormat.fromFileName(manifest) ==
FileFormat.AVRO
+ && versions().get(manifest) < 4;
+ }
+
+ private synchronized LoadingCache<String, Integer> versions() {
+ if (manifestVersions == null) {
+ // Keep only small version numbers; never serialize a query's
cache into every metadata task.
+ manifestVersions =
Caffeine.newBuilder().maximumSize(256).build(this::readManifestVersion);
+ }
+ return manifestVersions;
+ }
+
+ private int readManifestVersion(String manifest) {
+ MetadataLocation reference = manifest.startsWith("/") ?
locations().get(manifest) : null;
+ if (manifest.startsWith("/") && reference == null) {
+ throw new IllegalStateException("Manifest reference has not been
bound: " + manifest);
+ }
+ String location = reference == null ? resolve(tableLocation, manifest)
: reference.location;
+ return readHeader(delegate.newInputFile(location),
FileFormat.AVRO).version;
+ }
+
+ /** Bind provenance even on a parsed-manifest cache hit, where the SDK
will not open the manifest again. */
+ public void prepareManifest(ManifestFile file) {
+ if (file.path().startsWith("/")) {
+ metadataLocation(file);
+ }
+ }
+
+ /** A cached-only prefetch probe runs under its queue lock and must never
bind provenance through I/O. */
+ public boolean isManifestPrepared(ManifestFile file) {
+ if (!file.path().startsWith("/")) {
+ return true;
+ }
+ MetadataLocation bound = locations().get(file.path());
+ return bound != null && Objects.equals(bound.snapshotId,
file.snapshotId());
+ }
+
+ private synchronized ConcurrentMap<String, MetadataLocation> locations() {
+ if (metadataLocations == null) {
+ // Query-owned provenance, not a cross-query cache. Retain only
ambiguous slash-leading references.
+ metadataLocations = new ConcurrentHashMap<>();
+ }
+ return metadataLocations;
+ }
+
+ private String remember(String path, String location, Long snapshotId) {
+ MetadataLocation previous = locations().putIfAbsent(path, new
MetadataLocation(location, snapshotId));
+ if (previous != null && !previous.location.equals(location)) {
+ throw new IllegalArgumentException("Ambiguous legacy and V4
metadata reference: " + path);
+ }
+ return location;
+ }
+
+ private synchronized LoadingCache<Long, MetadataHeader> snapshots() {
+ if (snapshotHeaders == null) {
+ snapshotHeaders = Caffeine.newBuilder().maximumSize(256).build(id
-> {
+ TableMetadata current = metadata();
+ Snapshot snapshot = current == null ? null :
current.snapshot(id);
+ String path = snapshot == null ? inlineSnapshotLists.get(id) :
snapshot.manifestListLocation();
+ return path == null ? null : snapshotHeader(path, id);
+ });
+ }
+ return snapshotHeaders;
+ }
+
+ private MetadataHeader snapshotHeader(String path, long snapshotId) {
+ String relative = resolve(tableLocation, path);
+ MetadataHeader header = existingHeader(relative, FileFormat.AVRO);
+ if (header != null && Objects.equals(header.snapshotId, snapshotId)) {
+ if (path.startsWith("/")) {
+ remember(path, relative, snapshotId);
+ }
+ return header;
+ }
+ if (path.startsWith("/")) {
+ header = existingHeader(path, FileFormat.AVRO);
+ if (header != null && Objects.equals(header.snapshotId,
snapshotId)) {
+ remember(path, path, snapshotId);
+ return header;
+ }
+ }
+ throw new NotFoundException("Cannot find manifest list for snapshot
%s: %s", snapshotId, path);
+ }
+
+ private String metadataLocation(ManifestFile file) {
+ String path = file.path();
+ MetadataLocation bound = locations().get(path);
+ if (bound != null && Objects.equals(bound.snapshotId,
file.snapshotId())) {
+ return bound.location;
+ }
+ MetadataHeader source = file.snapshotId() == null ? null :
snapshots().get(file.snapshotId());
+ // Only a pre-V4 producer proves that a slash-leading reference is
filesystem-rooted.
+ // New V4 references remain relative, including when an unrelated root
path also exists.
+ if (source != null && source.version < 4) {
+ return remember(path, path, file.snapshotId());
+ }
+ String relative = resolve(tableLocation, path);
Review Comment:
[P1] Do not require a table base before trying an inherited rooted path. For
a V4 metadata document without `location`, `metadataLocation` calls
`resolve(null, "/metadata/old.avro")` and throws before its
`existingInput(path)` rooted fallback. This breaks a retained V2/V3 rooted
manifest after its producing snapshot expires even though the file is a valid
absolute filesystem path; probe rooted paths first (or preserve their
provenance) and cover the no-location upgrade case.
##########
fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java:
##########
@@ -0,0 +1,416 @@
+// 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.doris.connector.iceberg;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.InternalData;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.ManifestListFile;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.avro.AvroIterable;
+import org.apache.iceberg.exceptions.NotFoundException;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.types.Types;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/** Serializable, borrowed read view for table-relative format v4 locations. */
+public final class IcebergV4FileIO implements FileIO {
+ private static final long serialVersionUID = 1L;
+ private static final Pattern SCHEME =
Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*:.*", Pattern.DOTALL);
+ private final FileIO delegate;
+ private final String tableLocation;
+ private final String metadataFileLocation;
+ private final Map<Long, String> inlineSnapshotLists;
+ private transient volatile TableMetadata metadata;
+ private transient LoadingCache<String, TableMetadata> metadataFiles;
+ private transient LoadingCache<String, Integer> manifestVersions;
+ private transient LoadingCache<Long, MetadataHeader> snapshotHeaders;
+ private transient ConcurrentMap<String, MetadataLocation>
metadataLocations;
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation) {
+ this(delegate, tableLocation, null);
+ }
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation,
TableMetadata metadata) {
+ this(delegate, tableLocation, metadata, metadata == null ? null :
metadata.metadataFileLocation(),
+ metadata != null && metadata.metadataFileLocation() == null ?
metadata.snapshots().stream()
+ .filter(snapshot -> snapshot.manifestListLocation() !=
null)
+ .collect(Collectors.toMap(Snapshot::snapshotId,
Snapshot::manifestListLocation)) : Map.of());
+ }
+
+ private IcebergV4FileIO(FileIO delegate, String tableLocation,
+ TableMetadata metadata, String metadataFileLocation, Map<Long,
String> inlineSnapshotLists) {
+ this.delegate = Objects.requireNonNull(delegate, "delegate");
+ this.tableLocation = tableLocation;
+ this.metadata = metadata;
+ this.metadataFileLocation = metadataFileLocation;
+ // REST responses may supply inline metadata without a metadata-file
identity to reopen on the BE.
+ this.inlineSnapshotLists = Map.copyOf(inlineSnapshotLists);
+ }
+
+ public FileIO delegate() {
+ return delegate;
+ }
+
+ public IcebergV4FileIO withDelegate(FileIO replacement) {
+ return new IcebergV4FileIO(replacement, tableLocation, metadata,
metadataFileLocation, inlineSnapshotLists);
+ }
+
+ private TableMetadata metadata() {
+ if (metadata == null && metadataFileLocation != null) {
+ metadata = metadataFiles().get(metadataFileLocation);
Review Comment:
[P1] Resolve the metadata-file identity before reopening a serialized V4
view. The constructor stores `metadata.metadataFileLocation()` verbatim, but
after serialization `metadata()` calls `TableMetadataParser.read(delegate,
metadataFileLocation)` directly. For a valid no-scheme metadata pointer, V4
path semantics require `tableLocation + metadataFileLocation`; the BE instead
opens the unqualified path and cannot recover snapshot/manifest provenance.
Apply the same absolute-versus-relative resolution used by the rest of this
FileIO and add a serialized relative-metadata test.
##########
fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java:
##########
@@ -0,0 +1,416 @@
+// 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.doris.connector.iceberg;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.InternalData;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.ManifestListFile;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.avro.AvroIterable;
+import org.apache.iceberg.exceptions.NotFoundException;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.types.Types;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/** Serializable, borrowed read view for table-relative format v4 locations. */
+public final class IcebergV4FileIO implements FileIO {
+ private static final long serialVersionUID = 1L;
+ private static final Pattern SCHEME =
Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*:.*", Pattern.DOTALL);
+ private final FileIO delegate;
+ private final String tableLocation;
+ private final String metadataFileLocation;
+ private final Map<Long, String> inlineSnapshotLists;
+ private transient volatile TableMetadata metadata;
+ private transient LoadingCache<String, TableMetadata> metadataFiles;
+ private transient LoadingCache<String, Integer> manifestVersions;
+ private transient LoadingCache<Long, MetadataHeader> snapshotHeaders;
+ private transient ConcurrentMap<String, MetadataLocation>
metadataLocations;
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation) {
+ this(delegate, tableLocation, null);
+ }
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation,
TableMetadata metadata) {
+ this(delegate, tableLocation, metadata, metadata == null ? null :
metadata.metadataFileLocation(),
Review Comment:
[P2] Avoid serializing the entire inline snapshot history with every
metadata task. The `metadataFileLocation() == null` constructor branch eagerly
collects every snapshot's manifest-list path into `inlineSnapshotLists`, and
that map is serialized through the FileIO carried by each JNI task. Large
inline REST histories therefore multiply task payload and FE/BE memory by the
number of tasks; retain only the needed provenance or a compact lazy identity
and add a large-history serialization test.
##########
fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergManifestEntryKey.java:
##########
@@ -25,20 +25,31 @@
/**
* Cache key for one iceberg manifest entry (T08).
*
- * <p>Ported verbatim from the legacy fe-core
- * {@code org.apache.doris.datasource.iceberg.IcebergManifestEntryKey}. The
key carries only stable identity
- * dimensions — the manifest path plus its content type — so two tables that
share a manifest path hit the same
- * cached payload, and a table-level invalidation (REFRESH TABLE)
intentionally does NOT drop it (legacy
- * {@code testInvalidateTableKeepsManifestCache} parity). Runtime loader
context (the manifest/table instances)
- * must not be stored here.
+ * <p>Legacy keys retain the manifest path and content type. V4 keys also
identify the table base, raw reference
+ * and descriptor fields inherited by parsed files. Physical aliases alone
cannot identify those cached values.
+ * Table-level invalidation (REFRESH TABLE) intentionally does not drop
immutable manifest entries. Runtime
+ * loader context (the manifest/table instances) must not be stored here.
*/
public class IcebergManifestEntryKey {
private final String manifestPath;
private final ManifestContent content;
+ private final String tableLocation;
Review Comment:
[P2] Account for the new V4 key fields in the cache weight.
`IcebergManifestEntryKey` now retains `tableLocation` plus three boxed
provenance values, but `estimateManifestKey` still counts only `manifestPath`;
`instanceSize` is shallow, so the shared byte-bounded manifest cache can exceed
its configured budget as V4 keys accumulate. Include the non-null referents in
the estimate and add a V4 admission-size test.
##########
fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java:
##########
@@ -0,0 +1,416 @@
+// 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.doris.connector.iceberg;
+
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DeleteFile;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.InternalData;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.ManifestListFile;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.avro.AvroIterable;
+import org.apache.iceberg.exceptions.NotFoundException;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.types.Types;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/** Serializable, borrowed read view for table-relative format v4 locations. */
+public final class IcebergV4FileIO implements FileIO {
+ private static final long serialVersionUID = 1L;
+ private static final Pattern SCHEME =
Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*:.*", Pattern.DOTALL);
+ private final FileIO delegate;
+ private final String tableLocation;
+ private final String metadataFileLocation;
+ private final Map<Long, String> inlineSnapshotLists;
+ private transient volatile TableMetadata metadata;
+ private transient LoadingCache<String, TableMetadata> metadataFiles;
+ private transient LoadingCache<String, Integer> manifestVersions;
+ private transient LoadingCache<Long, MetadataHeader> snapshotHeaders;
+ private transient ConcurrentMap<String, MetadataLocation>
metadataLocations;
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation) {
+ this(delegate, tableLocation, null);
+ }
+
+ public IcebergV4FileIO(FileIO delegate, String tableLocation,
TableMetadata metadata) {
+ this(delegate, tableLocation, metadata, metadata == null ? null :
metadata.metadataFileLocation(),
+ metadata != null && metadata.metadataFileLocation() == null ?
metadata.snapshots().stream()
+ .filter(snapshot -> snapshot.manifestListLocation() !=
null)
+ .collect(Collectors.toMap(Snapshot::snapshotId,
Snapshot::manifestListLocation)) : Map.of());
+ }
+
+ private IcebergV4FileIO(FileIO delegate, String tableLocation,
+ TableMetadata metadata, String metadataFileLocation, Map<Long,
String> inlineSnapshotLists) {
+ this.delegate = Objects.requireNonNull(delegate, "delegate");
+ this.tableLocation = tableLocation;
+ this.metadata = metadata;
+ this.metadataFileLocation = metadataFileLocation;
+ // REST responses may supply inline metadata without a metadata-file
identity to reopen on the BE.
+ this.inlineSnapshotLists = Map.copyOf(inlineSnapshotLists);
+ }
+
+ public FileIO delegate() {
+ return delegate;
+ }
+
+ public IcebergV4FileIO withDelegate(FileIO replacement) {
+ return new IcebergV4FileIO(replacement, tableLocation, metadata,
metadataFileLocation, inlineSnapshotLists);
+ }
+
+ private TableMetadata metadata() {
+ if (metadata == null && metadataFileLocation != null) {
+ metadata = metadataFiles().get(metadataFileLocation);
+ }
+ return metadata;
+ }
+
+ private synchronized LoadingCache<String, TableMetadata> metadataFiles() {
+ if (metadataFiles == null) {
+ // Transport only the immutable metadata-file identity, not all
snapshot history in every JNI task.
+ // The cache coalesces lazy remote loads without doing I/O under
this initialization lock.
+ metadataFiles = Caffeine.newBuilder().maximumSize(1)
+ .build(path -> TableMetadataParser.read(delegate, path));
+ }
+ return metadataFiles;
+ }
+
+ public static String resolve(String tableLocation, String path) {
+ Objects.requireNonNull(path, "path");
+ if (isAbsolute(path)) {
+ return path;
+ }
+ if (tableLocation == null || tableLocation.isEmpty()) {
+ throw new IllegalArgumentException("Relative Iceberg v4 path
requires a table location: " + path);
+ }
+ // Duplicate separators are part of an object-store key, including at
the concatenation boundary.
+ return tableLocation + "/" + path;
+ }
+
+ private static boolean isAbsolute(String path) {
+ return SCHEME.matcher(path).matches();
+ }
+
+ public String resolveLocation(String path) {
+ return resolve(tableLocation, path);
+ }
+
+ public String resolveContentFile(ContentFile<?> file) {
+ return isAbsolute(file.location()) || isLegacyEntry(file)
+ ? file.location() : resolve(tableLocation, file.location());
+ }
+
+ private boolean isLegacyEntry(ContentFile<?> file) {
+ String manifest = file.manifestLocation();
+ // Encoding alone is insufficient: the SDK can also write a V4
manifest using Avro.
+ return manifest != null && FileFormat.fromFileName(manifest) ==
FileFormat.AVRO
+ && versions().get(manifest) < 4;
+ }
+
+ private synchronized LoadingCache<String, Integer> versions() {
+ if (manifestVersions == null) {
+ // Keep only small version numbers; never serialize a query's
cache into every metadata task.
+ manifestVersions =
Caffeine.newBuilder().maximumSize(256).build(this::readManifestVersion);
+ }
+ return manifestVersions;
+ }
+
+ private int readManifestVersion(String manifest) {
+ MetadataLocation reference = manifest.startsWith("/") ?
locations().get(manifest) : null;
+ if (manifest.startsWith("/") && reference == null) {
+ throw new IllegalStateException("Manifest reference has not been
bound: " + manifest);
+ }
+ String location = reference == null ? resolve(tableLocation, manifest)
: reference.location;
+ return readHeader(delegate.newInputFile(location),
FileFormat.AVRO).version;
+ }
+
+ /** Bind provenance even on a parsed-manifest cache hit, where the SDK
will not open the manifest again. */
+ public void prepareManifest(ManifestFile file) {
+ if (file.path().startsWith("/")) {
+ metadataLocation(file);
+ }
+ }
+
+ /** A cached-only prefetch probe runs under its queue lock and must never
bind provenance through I/O. */
+ public boolean isManifestPrepared(ManifestFile file) {
+ if (!file.path().startsWith("/")) {
+ return true;
+ }
+ MetadataLocation bound = locations().get(file.path());
+ return bound != null && Objects.equals(bound.snapshotId,
file.snapshotId());
+ }
+
+ private synchronized ConcurrentMap<String, MetadataLocation> locations() {
+ if (metadataLocations == null) {
+ // Query-owned provenance, not a cross-query cache. Retain only
ambiguous slash-leading references.
+ metadataLocations = new ConcurrentHashMap<>();
+ }
+ return metadataLocations;
+ }
+
+ private String remember(String path, String location, Long snapshotId) {
+ MetadataLocation previous = locations().putIfAbsent(path, new
MetadataLocation(location, snapshotId));
+ if (previous != null && !previous.location.equals(location)) {
+ throw new IllegalArgumentException("Ambiguous legacy and V4
metadata reference: " + path);
+ }
+ return location;
+ }
+
+ private synchronized LoadingCache<Long, MetadataHeader> snapshots() {
+ if (snapshotHeaders == null) {
+ snapshotHeaders = Caffeine.newBuilder().maximumSize(256).build(id
-> {
+ TableMetadata current = metadata();
+ Snapshot snapshot = current == null ? null :
current.snapshot(id);
+ String path = snapshot == null ? inlineSnapshotLists.get(id) :
snapshot.manifestListLocation();
+ return path == null ? null : snapshotHeader(path, id);
+ });
+ }
+ return snapshotHeaders;
+ }
+
+ private MetadataHeader snapshotHeader(String path, long snapshotId) {
+ String relative = resolve(tableLocation, path);
+ MetadataHeader header = existingHeader(relative, FileFormat.AVRO);
+ if (header != null && Objects.equals(header.snapshotId, snapshotId)) {
+ if (path.startsWith("/")) {
+ remember(path, relative, snapshotId);
+ }
+ return header;
+ }
+ if (path.startsWith("/")) {
+ header = existingHeader(path, FileFormat.AVRO);
+ if (header != null && Objects.equals(header.snapshotId,
snapshotId)) {
+ remember(path, path, snapshotId);
+ return header;
+ }
+ }
+ throw new NotFoundException("Cannot find manifest list for snapshot
%s: %s", snapshotId, path);
+ }
+
+ private String metadataLocation(ManifestFile file) {
+ String path = file.path();
+ MetadataLocation bound = locations().get(path);
+ if (bound != null && Objects.equals(bound.snapshotId,
file.snapshotId())) {
+ return bound.location;
+ }
+ MetadataHeader source = file.snapshotId() == null ? null :
snapshots().get(file.snapshotId());
+ // Only a pre-V4 producer proves that a slash-leading reference is
filesystem-rooted.
+ // New V4 references remain relative, including when an unrelated root
path also exists.
+ if (source != null && source.version < 4) {
+ return remember(path, path, file.snapshotId());
+ }
+ String relative = resolve(tableLocation, path);
+ if (existingInput(relative) != null) {
+ return remember(path, relative, file.snapshotId());
+ }
+ if (source != null && source.version >= 4 &&
!source.location.startsWith("/")) {
+ throw new NotFoundException("Cannot find relative V4 manifest:
%s", relative);
+ }
+ // SDK 1.12 retains unqualified write.metadata.path locations after
subsequent V4 commits.
+ // Rewrites may contain only EXISTING entries, so their snapshot ID
cannot be proved from ADDED rows.
+ // A physically rooted list in the same directory establishes the
SDK's filesystem metadata convention.
+ InputFile root = existingInput(path);
+ if (root != null && (hasRootedMetadataDirectory(path)
Review Comment:
[P1] Retained rooted manifests can fail after the metadata directory moves.
Once the producing snapshot is expired, `metadataLocation` falls through to
`hasRootedMetadataDirectory`, which only accepts a rooted path when it shares
the current snapshot's manifest-list directory (or contains an ADDED row). A
valid V2/V3 manifest retained by a later V4 rewrite with only EXISTING entries
in an older `write.metadata.path` therefore has an existing file but is
rejected as unresolved. Preserve provenance for every reachable metadata
directory (or validate the retained manifest/list relationship) and add an
expiration test that changes the metadata output location.
--
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]