akshaytayal commented on code in PR #13519: URL: https://github.com/apache/gluten/pull/13519#discussion_r4648664647
########## shims/spark43/src/main/scala/org/apache/spark/storage/GlutenPushBasedFetchHelper.scala: ########## @@ -0,0 +1,399 @@ +/* + * 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.storage + +import org.apache.spark.MapOutputTracker +import org.apache.spark.MapOutputTracker.SHUFFLE_PUSH_MAP_ID +import org.apache.spark.internal.Logging +import org.apache.spark.network.shuffle.{BlockStoreClient, MergedBlockMeta, MergedBlocksMetaListener} +import org.apache.spark.shuffle.ShuffleReadMetricsReporter +import org.apache.spark.storage.BlockManagerId.SHUFFLE_MERGER_IDENTIFIER +import org.apache.spark.storage.ShuffleBlockFetcherIterator._ + +import org.roaringbitmap.RoaringBitmap + +import java.util.concurrent.TimeUnit + +import scala.collection.mutable +import scala.collection.mutable.ArrayBuffer +import scala.util.{Failure, Success} + +/** + * Helper class for [[ShuffleBlockFetcherIterator]] that encapsulates all the push-based + * functionality to fetch push-merged block meta and shuffle chunks. A push-merged block contains + * multiple shuffle chunks where each shuffle chunk contains multiple shuffle blocks that belong to + * the common reduce partition and were merged by the external shuffle service to that chunk. + */ +private class GlutenPushBasedFetchHelper( + private val iterator: GlutenShuffleBlockFetcherIterator, + private val shuffleClient: BlockStoreClient, + private val blockManager: BlockManager, + private val mapOutputTracker: MapOutputTracker, + private val shuffleMetrics: ShuffleReadMetricsReporter) + extends Logging { + + private[this] val startTimeNs = System.nanoTime() + + private[storage] val localShuffleMergerBlockMgrId = BlockManagerId( + SHUFFLE_MERGER_IDENTIFIER, + blockManager.blockManagerId.host, + blockManager.blockManagerId.port, + blockManager.blockManagerId.topologyInfo) + + /** A map for storing shuffle chunk bitmap. */ + private[this] val chunksMetaMap = new mutable.HashMap[ShuffleBlockChunkId, RoaringBitmap]() + + /** Returns true if the address is for a push-merged block. */ + def isPushMergedShuffleBlockAddress(address: BlockManagerId): Boolean = { + SHUFFLE_MERGER_IDENTIFIER == address.executorId + } + + /** Returns true if the address is of a remote push-merged block. false otherwise. */ + def isRemotePushMergedBlockAddress(address: BlockManagerId): Boolean = { + isPushMergedShuffleBlockAddress(address) && address.host != blockManager.blockManagerId.host + } + + /** Returns true if the address is of a push-merged-local block. false otherwise. */ + def isLocalPushMergedBlockAddress(address: BlockManagerId): Boolean = { + isPushMergedShuffleBlockAddress(address) && address.host == blockManager.blockManagerId.host + } + + /** + * This is executed by the task thread when the `iterator.next()` is invoked and the iterator + * processes a response of type [[ShuffleBlockFetcherIterator.SuccessFetchResult]]. + * + * @param blockId + * shuffle chunk id. + */ + def removeChunk(blockId: ShuffleBlockChunkId): Unit = { + chunksMetaMap.remove(blockId) + } + + /** + * This is executed by the task thread when the `iterator.next()` is invoked and the iterator + * processes a response of type [[ShuffleBlockFetcherIterator.PushMergedLocalMetaFetchResult]]. + * + * @param blockId + * shuffle chunk id. + */ + def addChunk(blockId: ShuffleBlockChunkId, chunkMeta: RoaringBitmap): Unit = { + chunksMetaMap(blockId) = chunkMeta + } + + /** + * Get the RoaringBitMap for a specific ShuffleBlockChunkId + * + * @param blockId + * shuffle chunk id. + */ + def getRoaringBitMap(blockId: ShuffleBlockChunkId): Option[RoaringBitmap] = { + chunksMetaMap.get(blockId) + } + + /** + * Get the number of map blocks in a ShuffleBlockChunk + * @param blockId + * @return + */ + def getShuffleChunkCardinality(blockId: ShuffleBlockChunkId): Int = { + getRoaringBitMap(blockId).map(_.getCardinality).getOrElse(0) + } + + /** + * This is executed by the task thread when the `iterator.next()` is invoked and the iterator + * processes a response of type [[ShuffleBlockFetcherIterator.PushMergedRemoteMetaFetchResult]]. + * + * @param shuffleId + * shuffle id. + * @param reduceId + * reduce id. + * @param blockSize + * size of the push-merged block. + * @param bitmaps + * chunk bitmaps, where each bitmap contains all the mapIds that were merged to that chunk. + * @return + * shuffle chunks to fetch. + */ + def createChunkBlockInfosFromMetaResponse( + shuffleId: Int, + shuffleMergeId: Int, + reduceId: Int, + blockSize: Long, + bitmaps: Array[RoaringBitmap]): ArrayBuffer[(BlockId, Long, Int)] = { + val approxChunkSize = blockSize / bitmaps.length + val blocksToFetch = new ArrayBuffer[(BlockId, Long, Int)]() + for (i <- bitmaps.indices) { + val blockChunkId = ShuffleBlockChunkId(shuffleId, shuffleMergeId, reduceId, i) + chunksMetaMap.put(blockChunkId, bitmaps(i)) + logDebug(s"adding block chunk $blockChunkId of size $approxChunkSize") + blocksToFetch += ((blockChunkId, approxChunkSize, SHUFFLE_PUSH_MAP_ID)) + } + blocksToFetch + } + + /** + * This is executed by the task thread when the iterator is initialized and only if it has + * push-merged blocks for which it needs to fetch the metadata. + * + * @param req + * [[ShuffleBlockFetcherIterator.FetchRequest]] that only contains requests to fetch metadata of + * push-merged blocks. + */ + def sendFetchMergedStatusRequest(req: FetchRequest): Unit = { + val sizeMap = req.blocks.map { + case FetchBlockInfo(blockId, size, _) => + val shuffleBlockId = blockId.asInstanceOf[ShuffleMergedBlockId] + ((shuffleBlockId.shuffleId, shuffleBlockId.reduceId), size) + }.toMap + val address = req.address + val mergedBlocksMetaListener = new MergedBlocksMetaListener { + override def onSuccess( + shuffleId: Int, + shuffleMergeId: Int, + reduceId: Int, + meta: MergedBlockMeta): Unit = { + logDebug( + s"Received the meta of push-merged block for ($shuffleId, $shuffleMergeId," + + s" $reduceId) from ${req.address.host}:${req.address.port}") + try { + iterator.addToResultsQueue( Review Comment: Fixed in 7ff7c67. The Spark 4.3 `GlutenPushBasedFetchHelper` now ports Spark's `checkStaleMapIdInMergedBlock` and applies it on both the remote and the push-merged-local metadata paths. When a merged chunk bitmap contains a stale map index, it falls back to the original blocks. The Spark 4.2 copy is unchanged, since 4.2 has no stale map indexes. The tests are in #13520 (ef3d8f0): `GlutenShuffleBlockFetcherIteratorSuite`, adapted from Spark's SPARK-57491 test, covers both paths with and without a stale index. -- 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]
