jordepic commented on code in PR #4658: URL: https://github.com/apache/datafusion-comet/pull/4658#discussion_r3723210691
########## spark/src/main/scala/org/apache/spark/sql/comet/IcebergCommitExec.scala: ########## @@ -0,0 +1,125 @@ +/* + * 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.spark.sql.comet + +import org.apache.spark.internal.Logging +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.connector.write.{BatchWrite, Write, WriterCommitMessage} +import org.apache.spark.sql.execution.{SparkPlan, SQLExecution, UnaryExecNode} +import org.apache.spark.sql.execution.datasources.v2.V2CommandExec +import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics} + +import org.apache.comet.iceberg.IcebergDriverMetricsShim + +/** + * Driver-side committer for Comet's split-operator Iceberg V2 write. + */ +case class IcebergCommitExec( + // None of these fields are serialized, this is all run on the driver. + @transient batchWrite: BatchWrite, + @transient write: Write, + @transient refreshCache: IcebergCommitExec.RefreshCache, + child: SparkPlan) + extends V2CommandExec + with UnaryExecNode + with Logging { + + override def output: Seq[Attribute] = Nil + + override lazy val metrics: Map[String, SQLMetric] = + Map( + "numCommittedMessages" -> SQLMetrics + .createMetric(sparkContext, "number of task commit messages")) ++ + write + .supportedCustomMetrics() + .map(m => m.name -> SQLMetrics.createV2CustomMetric(sparkContext, m)) + + // Exactly-once relies on V2CommandExec memoizing run() via its `result` lazy val and on + // the writer executing once inside the AQE bubble anchored by IcebergWriteLogical; pinned + // by the AQE re-plan test in CometIcebergWriteActionSuite. + override protected def run(): Seq[InternalRow] = { + try { + collectAndCommit() + } finally { + postDriverMetrics() + } + } + + private def collectAndCommit(): Seq[InternalRow] = { + val messages: Array[WriterCommitMessage] = + try { + child.executeCollect().map { row => + IcebergWriteExec.deserializeMessage(row.getBinary(0)) + } + } catch { + case cause: Throwable => + // The write job failed; the BatchWrite contract still expects a job-level abort. + try batchWrite.abort(Array.empty[WriterCommitMessage]) Review Comment: Thanks @parthchandra ! I wasn't sure if that was part of the write "contract" (the result bytes) of spark/iceberg, and assumed that some folks would be running procedures to remove orphaned files anyways (https://iceberg.apache.org/javadoc/1.5.0/org/apache/iceberg/actions/DeleteOrphanFiles.html) See #5277 . I did a little bit more research to find the divergence here, and as far as I can tell. 1) Task level failures (executors writing parquet) and still cleaned up, with the exception of successful writes on iceberg 1.5.2/spark 3.4 (some writes succeed, some writes fail, the successful ones are not cleaned) 2) Job level failures (commit issues) are semantically identical So I really do think this is pretty minor, and again, is still not reflected in actual results visible to a user. The issue is filed nonetheless. ``` - Iceberg ≥ 1.8 (our Spark 3.5/4.0/4.1 profiles): cleanupOnAbort initializes to false and is only set true when the commit itself threw a CleanableFailure (changed in apache/iceberg#10373, c67c9124d). On a write-stage failure the commit never ran, so stock Spark's carefully assembled message array is ignored anyway — Iceberg logs "Skipping cleanup of written files" and deletes nothing. Our empty array is behaviorally identical here. Both designs orphan the committed-but-never-published files. - Iceberg 1.5.2 (Spark 3.4 profile): cleanupOnAbort defaulted to true (only flipped off on CommitStateUnknownException). Stock Spark would delete the successful tasks' data files on a write-stage job abort; we can't, so on that old version we leak files stock Spark would have removed. Job-level abort on commit failure No divergence. By the time IcebergWriteSummaryShim.commit runs we hold all messages, and on failure we call batchWrite.abort(messages) exactly like Spark (IcebergCommitExec.scala:88-96), suppressing any abort exception into the original cause. Iceberg then decides: CleanableFailure (e.g. CommitFailedException after retry exhaustion) → delete all the job's data files; CommitStateUnknownException → keep them, since a snapshot may actually reference them. What happens to the orphans Neither design deletes anything itself — physical deletion always happens inside Iceberg (SparkCleanupUtil). Files that escape cleanup (our empty-array case on 1.5.2, and every pre-commit job failure on ≥1.8 in both designs) are never referenced by table metadata, so they're invisible to readers and get reclaimed by remove_orphan_files maintenance. The one concrete behavioral gap we introduce is the Iceberg 1.5.2 write-stage-failure case, and it's a hygiene gap (extra orphans), not a correctness one. One minor ordering difference worth noting: stock Spark calls batchWrite.onDataWriterCommit(msg) per task as results stream back; we call it for all messages after the collect succeeds. Iceberg's SparkWrite doesn't override it (interface default is a no-op), so this is inert for Iceberg tables. ``` -- 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]
