LuciferYang commented on code in PR #2751: URL: https://github.com/apache/uniffle/pull/2751#discussion_r3298591278
########## client-spark/spark4/src/main/java-spark4_1/org/apache/spark/shuffle/compat/Spark4Compat.java: ########## @@ -0,0 +1,62 @@ +/* + * 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.shuffle.compat; + +import scala.Option; +import scala.math.Ordering; + +import org.apache.spark.Aggregator; +import org.apache.spark.Partitioner; +import org.apache.spark.TaskContext; +import org.apache.spark.scheduler.MapStatus; +import org.apache.spark.serializer.Serializer; +import org.apache.spark.shuffle.checksum.RowBasedChecksum; +import org.apache.spark.storage.BlockManagerId; +import org.apache.spark.util.collection.ExternalSorter; + +/** + * Compatibility shim for Spark 4.1.x. Selected by the build via the + * {@code src/main/java-spark4_1} source root under the {@code spark4.1} profile. + * + * <p>Spark 4.1 added a {@code checksumVal} parameter to {@code MapStatus.apply} and a + * {@code rowBasedChecksums} parameter to {@link ExternalSorter}'s constructor. Both have Scala + * defaults but are required from Java; pass disabled defaults (0L and an empty array). + * + * <p>Mirror of {@code java-spark4_0/.../Spark4Compat.java}; keep the public surface in lock-step. + */ +public final class Spark4Compat { + + private Spark4Compat() {} + + public static MapStatus mapStatus( + BlockManagerId loc, long[] uncompressedSizes, long mapTaskId) { + return MapStatus.apply(loc, uncompressedSizes, mapTaskId, 0L); + } + + public static <K, V, C> ExternalSorter<K, V, C> newExternalSorter( + TaskContext context, + Option<Aggregator<K, V, C>> aggregator, + Option<Partitioner> partitioner, + Option<Ordering<K>> ordering, + Serializer serializer) { + // Spark 4.1's ExternalSorter requires a non-null RowBasedChecksum[]; pass an empty + // array to disable row-based checksums. + return new ExternalSorter<>( + context, aggregator, partitioner, ordering, serializer, new RowBasedChecksum[0]); Review Comment: ## Mechanism ### SPARK-54663 / apache/spark#50230 — Writer-side computation Config: `spark.sql.shuffle.orderIndependentChecksum.enabled` (off by default). Also `spark.sql.shuffle.orderIndependentChecksum.enableFullRetryOnMismatch`; either flag turns injection on. Core class `org.apache.spark.shuffle.checksum.RowBasedChecksum`: ```scala abstract class RowBasedChecksum() extends Serializable with Logging { private val ROTATE_POSITIONS = 27 private var hasError: Boolean = false private var checksumXor: Long = 0 private var checksumSum: Long = 0 ... def update(key: Any, value: Any): Unit = { val v = calculateRowChecksum(key, value) checksumXor = checksumXor ^ v checksumSum += v } def getValue: Long = if (hasError) 0L else checksumXor ^ rotateLeft(checksumSum) } ``` XOR and addition are commutative and associative, so the accumulator is row-order independent. `rotateLeft` is applied once on the final aggregated `checksumSum`. On error, `getValue` returns 0 to skip comparison for that partition. Each map task keeps one `RowBasedChecksum` per partition during the writer phase. The companion `getAggregatedChecksumValue` folds them into a single scalar via `acc = acc * 31L + c.getValue` for reporting. `ExternalSorter` adds a 6th parameter: ```scala // ExternalSorter.scala (Spark 4.1) private[spark] class ExternalSorter[K, V, C]( ... rowBasedChecksums: Array[RowBasedChecksum] = Array.empty) ... if (rowBasedChecksums.nonEmpty) rowBasedChecksums(partitionId).update(k, v) ``` The `nonEmpty` guard makes an empty array equivalent to switching the feature off. `MapStatus` adds the reporting field in SPARK-54663; the trait exposes `def checksumValue: Long`: ```scala def apply( loc: BlockManagerId, uncompressedSizes: Array[Long], mapTaskId: Long, checksumVal: Long = 0): MapStatus = ... ``` `ShuffleDependency.rowBasedChecksums: Array[RowBasedChecksum]` is injected by `ShuffleExchangeExec` via `UnsafeRowChecksum.createUnsafeRowChecksums` before the shuffle write. Injection is gated by the two SQL configs above. Injection only happens in `ShuffleExchangeExec`, so only the SQL `UnsafeRow` path gets it automatically; non-SQL and non-`UnsafeRow` paths never see a non-empty array. ### SPARK-53575 / apache/spark#52336 — Consumer-side detection The driver compares `checksumValue` reported by different attempts of the same map task; on mismatch, the whole consumer stage is re-run, avoiding partial-recompute corruption from non-deterministic stages. Previously this relied on the static `stage.isIndeterminate` flag and conservatively re-ran the whole stage. With round-robin partitioning, for instance, output distribution shifts on retry, so what downstream already consumed disagrees with the new attempt. Triggering retry on a runtime checksum mismatch is more precise than the static `isIndeterminate` decision. This piece lives entirely in the driver (`MapOutputTracker` / `DAGScheduler`) and does not go through RSS, so Uniffle does not need to adapt. ## Where Uniffle stands | Field / hook | Location | Current value | Assessment | |---|---|---|---| | Reader-side `rowBasedChecksums` of `ExternalSorter` | `RssShuffleReader.java:227` → `Spark4Compat.newExternalSorter` | `new RowBasedChecksum[0]` | Semantically correct off-switch | | Writer-side `MapStatus.checksumVal` | `RssShuffleWriter.java:724` → `Spark4Compat.mapStatus` | `0L` | Reporting channel placeholder; writer-side computation missing | The two sides are asymmetric: - Reader side: native `BlockStoreShuffleReader` also doesn't pass `rowBasedChecksums`; the default is `Array.empty`. Uniffle passing an empty array matches upstream. - Writer side: `MapStatus.checksumVal` is the actual reporting channel for the row-based checksum. Uniffle's writer goes through `WriteBufferManager.addRecord(partition, key, value)` directly to the shuffle server, bypassing Spark's `ExternalSorter`, so there's no hook to compute the checksum — that's why this slot is a placeholder. ## What it takes to support this Plug into the writer path and reuse upstream plumbing. The `shuffleDependency` held by `RssShuffleWriter` is the upstream `ShuffleDependency`, so `dep.rowBasedChecksums()` is directly available — no need to construct our own. Config flags and SQL-path scope are inherited from upstream. `WriteBufferManager.addRecord(partition, key, value)` already receives row-level key/value, matching `RowBasedChecksum.update` exactly; call `update` per partition here, and at commit time replace the `0L` via `Spark4Compat.mapStatus` with the aggregated value. Constraints / boundaries: - Cross-version: `RowBasedChecksum` only exists in Spark 4.1+. The implementation must be exposed only in the 4.1 shim; 4.0 and 3.x stay as is. - Columnar / Gluten out of scope: `WriteBufferManager.addPartitionData(byte[])` receives an already-serialized partition byte stream with no key/value visible, so row-based checksum cannot work on this path. - Consumer side is out of RSS scope; the driver handles it natively. ## References - SPARK-54663 / apache/spark#50230: Computes RowBasedChecksum in ShuffleWriters - SPARK-53575 / apache/spark#52336: Retry entire consumer stages when checksum mismatch detected -- 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]
