Gabriel39 commented on code in PR #70869: URL: https://github.com/apache/doris/pull/70869#discussion_r4676768738
########## fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java: ########## @@ -0,0 +1,414 @@ +// 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.TableMetadata; +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 java.io.IOException; +import java.io.Serializable; +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; + +/** 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 Map<String, MetadataLocation> taskLocations; + private final transient TableMetadata metadata; + 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, Map.of()); + } + + private IcebergV4FileIO(FileIO delegate, String tableLocation, + TableMetadata metadata, Map<String, MetadataLocation> taskLocations) { + this.delegate = Objects.requireNonNull(delegate, "delegate"); + this.tableLocation = tableLocation; + this.metadata = metadata; + this.taskLocations = Map.copyOf(taskLocations); + } + + public FileIO delegate() { + return delegate; + } + + public IcebergV4FileIO withDelegate(FileIO replacement) { + IcebergV4FileIO view = new IcebergV4FileIO(replacement, tableLocation, metadata, taskLocations); + // Per-consumer authentication changes the IO delegate, not this statement's frozen path provenance. + view.metadataLocations = locations(); + return view; + } + + /** Bind just this task's concrete reference; neither metadata JSON nor snapshot history crosses the wire. */ + public IcebergV4FileIO forManifest(ManifestFile file) { + prepareManifest(file); + MetadataLocation location = locations().get(file.path()); + return new IcebergV4FileIO(delegate, tableLocation, null, + location == null ? Map.of() : Map.of(file.path(), location)); + } + + public IcebergV4FileIO forManifestList(ManifestListFile file, long snapshotId) { + if (!file.location().startsWith("/")) { + return new IcebergV4FileIO(delegate, tableLocation); + } + snapshotHeader(file.location(), snapshotId); + return new IcebergV4FileIO(delegate, tableLocation, null, + Map.of(file.location(), locations().get(file.location()))); + } + + 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<>(taskLocations); + } + 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 -> { + Snapshot snapshot = metadata == null ? null : metadata.snapshot(id); + String path = snapshot == null ? null : snapshot.manifestListLocation(); + return path == null ? null : snapshotHeader(path, id); + }); + } + return snapshotHeaders; + } + + private MetadataHeader snapshotHeader(String path, long snapshotId) { + String relative = hasBase() || isAbsolute(path) ? resolve(tableLocation, path) : null; + MetadataHeader header = relative == null ? null : 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 = hasBase() ? resolve(tableLocation, path) : null; + if (relative != null && 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); + } + // A retained manifest keeps its immutable descriptor even after its creator expires or metadata moves. + // Validate the concrete rooted file, rather than guessing its identity from today's directory or ADDED rows. + InputFile rooted = existingInput(path); + if (rooted != null) { + if (rooted.getLength() != file.length()) { + throw new IllegalArgumentException("Rooted manifest length does not match its descriptor: " + path); + } + return remember(path, path, file.snapshotId()); + } + if (relative == null) { + resolve(tableLocation, path); + } + throw new NotFoundException("Cannot find manifest at %s or retained rooted location %s", relative, path); + } + + private boolean hasBase() { + return tableLocation != null && !tableLocation.isEmpty(); + } + + private InputFile existingInput(String location) { + try { + InputFile input = delegate.newInputFile(location); + return input.exists() ? input : null; + } catch (NotFoundException missing) { + return null; + } + } + + private MetadataHeader existingHeader(String location, FileFormat format) { + InputFile input = existingInput(location); + return input == null ? null : readHeader(input, format); + } + + private static MetadataHeader readHeader(InputFile input, FileFormat format) { + try { + Map<String, String> metadata; + try (CloseableIterable<?> header = InternalData.read(format, input) + .project(new Schema()).build()) { + metadata = ((AvroIterable<?>) header).getMetadata(); + } + String snapshot = metadata.get("snapshot-id"); + return new MetadataHeader(Integer.parseInt(metadata.getOrDefault("format-version", "1")), + snapshot == null || snapshot.equals("null") ? null : Long.valueOf(snapshot), input.location()); + } catch (IOException failure) { + throw new UncheckedIOException("Failed to read Iceberg metadata header", failure); + } + } + + private static final class MetadataHeader { + private final int version; + private final Long snapshotId; + private final String location; + + private MetadataHeader(int version, Long snapshotId, String location) { + this.version = version; + this.snapshotId = snapshotId; + this.location = location; + } + } + + private static final class MetadataLocation implements Serializable { + private static final long serialVersionUID = 1L; + private final String location; + private final Long snapshotId; + + private MetadataLocation(String location, Long snapshotId) { + this.location = location; + this.snapshotId = snapshotId; + } + } + + @Override + public InputFile newInputFile(String path) { + return delegate.newInputFile(inputLocation(path)); + } + + @Override + public InputFile newInputFile(String path, long length) { + return delegate.newInputFile(inputLocation(path), length); + } + + private String inputLocation(String path) { + if (path.startsWith("/")) { + MetadataLocation bound = locations().get(path); + if (bound != null) { + return bound.location; + } + } + // Static metadata-table tasks size the frozen metadata JSON using its original pointer, not a V4 + // content descriptor. Preserve an existing rooted JSON pointer without relaxing content-file resolution. + if (path.startsWith("/") && metadata != null && path.equals(metadata.metadataFileLocation()) Review Comment: Addressed in d6c0bc87e313. Slash-leading metadata JSON now tries the table-relative object first, so an unrelated rooted object cannot override it and scheme-requiring delegates never see a premature rooted probe. A rooted compatibility fallback is accepted only when the complete SDK-normalized metadata matches the frozen generation. The comparison streams into SHA-256 rather than materializing additional full-history JSON strings. Regression tests reproduce both original failures and now pass. Additional positive/negative controls accept matching rooted metadata and reject a property-only change with the same UUID and snapshot. The final approved reactor selections passed 948 tests, including 88 V4 and 20 JNI cases; Checkstyle passed in all three changed Java modules. ########## fe/fe-iceberg-common/src/main/java/org/apache/doris/connector/iceberg/IcebergV4FileIO.java: ########## @@ -0,0 +1,414 @@ +// 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.TableMetadata; +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 java.io.IOException; +import java.io.Serializable; +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; + +/** 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 Map<String, MetadataLocation> taskLocations; + private final transient TableMetadata metadata; + 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, Map.of()); + } + + private IcebergV4FileIO(FileIO delegate, String tableLocation, + TableMetadata metadata, Map<String, MetadataLocation> taskLocations) { + this.delegate = Objects.requireNonNull(delegate, "delegate"); + this.tableLocation = tableLocation; + this.metadata = metadata; + this.taskLocations = Map.copyOf(taskLocations); + } + + public FileIO delegate() { + return delegate; + } + + public IcebergV4FileIO withDelegate(FileIO replacement) { + IcebergV4FileIO view = new IcebergV4FileIO(replacement, tableLocation, metadata, taskLocations); + // Per-consumer authentication changes the IO delegate, not this statement's frozen path provenance. + view.metadataLocations = locations(); + return view; + } + + /** Bind just this task's concrete reference; neither metadata JSON nor snapshot history crosses the wire. */ + public IcebergV4FileIO forManifest(ManifestFile file) { + prepareManifest(file); + MetadataLocation location = locations().get(file.path()); + return new IcebergV4FileIO(delegate, tableLocation, null, + location == null ? Map.of() : Map.of(file.path(), location)); + } + + public IcebergV4FileIO forManifestList(ManifestListFile file, long snapshotId) { + if (!file.location().startsWith("/")) { + return new IcebergV4FileIO(delegate, tableLocation); + } + snapshotHeader(file.location(), snapshotId); + return new IcebergV4FileIO(delegate, tableLocation, null, + Map.of(file.location(), locations().get(file.location()))); + } + + 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) Review Comment: Addressed in d6c0bc87e313 by making V4 data writes and maintenance explicitly unsupported in this read-support PR. Doris refuses upgrade-then-rewrite before invoking raw SDK mutations. V1-V3 writes retain their existing behavior. The V4 format contract requires a writer to qualify inherited pre-V4 scheme-less paths with a URI scheme when copying them into V4 manifests: https://github.com/apache/iceberg/blob/main/format/spec.md . After producer identity has been replaced, guessing from a slash prefix would also misclassify valid V4 relative paths. Recovery of provenance already lost by an external writer remains outside this read-support scope. Four real-SDK V2/V3 x Avro/Parquet rewrite cases explicitly qualify the inherited paths and verify cached/uncached planning reads them correctly. Four authenticated/plain upgrade-then-rewrite refusal cases also pass, alongside the existing V1-V3 write/procedure suites. All 948 selected unit/JNI tests pass. ########## fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergManifestReader.java: ########## @@ -77,7 +77,7 @@ CompletionStage<ManifestCacheValue> readFuture(ManifestFile manifest, String sta try (ReadLease ignored = retain()) { boolean privateRead = encryptedTable || manifest.keyMetadata() != null; // A statement reader is shared by scans with and without optional EXPLAIN accounting. - cache.recordStatementAccess(manifest, statsQueryId, !privateRead); Review Comment: Addressed in d6c0bc87e313. ReadKey now uses the same IcebergManifestCache.manifestKey factory as completed cache entries, preserving V4 table-base, snapshot, sequence and first-row-ID inheritance. Its existing length, partition-spec and encryption-key separation remains intact. The new latch-controlled shared-reader test overlaps two reads of identical manifest bytes with different descriptors. It reproduces sequence 2 inheriting sequence 1 on the baseline; the fix preserves each descriptor's data/file sequence and row ID independently. The final selected run passed all 948 unit/JNI cases, including the manifest reader/cache/prefetch and statement-scope suites. ########## fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergConnectorMetadata.java: ########## @@ -797,7 +797,7 @@ private Table resolveTableForRead(ConnectorSession session, IcebergTableHandle h value -> IcebergConnector.cachedTableCleanup(value, catalogProps.getFlavor())); } IcebergEncryptionRead.check(table, catalogProps, context); - return table; Review Comment: Addressed in d6c0bc87e313 with explicit V4 DML refusal. Metadata DELETE now throws the unsupported-write error rather than returning an empty fallback. Data transaction begin, row-level mode validation, synchronous procedures and distributed rewrite planning share the V4 write guard, before unsupported SDK mutation. V1-V3 writes remain supported. The stock regression runner passed the complete test_iceberg_v4_read suite: four whole-table/filtered DELETE refusal checks across cache off/on, followed by all 80 existing golden read queries proving the fixture rows remain unchanged. It used freshly built current FE/BE Java packages with a pre-existing ASAN native BE, so it is not a fresh matching native-build acceptance. All 948 selected unit/JNI tests, official Java packaging and explicit Checkstyle also pass. Live KDC/HDFS and object-store doubled-separator integration remain unverified. -- 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]
