Ngone51 commented on a change in pull request #31495: URL: https://github.com/apache/spark/pull/31495#discussion_r571434693
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/HDFSMetadataLog.scala ########## @@ -239,18 +239,35 @@ class HDFSMetadataLog[T <: AnyRef : ClassTag](sparkSession: SparkSession, path: .reverse } + private var lastPurgedBatchId: Long = -1L + /** * Removes all the log entry earlier than thresholdBatchId (exclusive). */ override def purge(thresholdBatchId: Long): Unit = { - val batchIds = fileManager.list(metadataPath, batchFilesFilter) - .map(f => pathToBatchId(f.getPath)) - - for (batchId <- batchIds if batchId < thresholdBatchId) { - val path = batchIdToPath(batchId) - fileManager.delete(path) - logTrace(s"Removed metadata log file: $path") + val possibleTargetBatchIds = (lastPurgedBatchId + 1 until thresholdBatchId) + if (possibleTargetBatchIds.length <= 3) { Review comment: Usually, `possibleTargetBatchIds.length` should be 1? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/HDFSMetadataLog.scala ########## @@ -239,18 +239,35 @@ class HDFSMetadataLog[T <: AnyRef : ClassTag](sparkSession: SparkSession, path: .reverse } + private var lastPurgedBatchId: Long = -1L + /** * Removes all the log entry earlier than thresholdBatchId (exclusive). */ override def purge(thresholdBatchId: Long): Unit = { - val batchIds = fileManager.list(metadataPath, batchFilesFilter) - .map(f => pathToBatchId(f.getPath)) - - for (batchId <- batchIds if batchId < thresholdBatchId) { - val path = batchIdToPath(batchId) - fileManager.delete(path) - logTrace(s"Removed metadata log file: $path") + val possibleTargetBatchIds = (lastPurgedBatchId + 1 until thresholdBatchId) + if (possibleTargetBatchIds.length <= 3) { + // avoid using list if we only need to purge at most 3 elements + possibleTargetBatchIds.foreach { batchId => + val path = batchIdToPath(batchId) + if (fileManager.exists(path)) { Review comment: This confused me. Why not blindly delete the path as the original way does? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/OffsetSeqLog.scala ########## @@ -46,6 +47,23 @@ import org.apache.spark.sql.connector.read.streaming.{Offset => OffsetV2} class OffsetSeqLog(sparkSession: SparkSession, path: String) extends HDFSMetadataLog[OffsetSeq](sparkSession, path) { + private val cachedMetadata = new ju.TreeMap[Long, OffsetSeq]() + + override def add(batchId: Long, metadata: OffsetSeq): Boolean = { + val added = super.add(batchId, metadata) + if (added) { + // cache metadata as it will be read again + cachedMetadata.put(batchId, metadata) + // we don't access metadata for (batchId - 2) batches; evict them Review comment: `TreeMap` seems overkill to me if we only cache at most 2 batches. How about using a 2-size array? We can put/get the elements by the index of (batchId % 2). For example: ```scala private val cachedMetadata = Array[OffsetSeq](null, null) // put cachedMetadata(batchId % 2) = metadata // get Option(cachedMetadata.get(batchId % 2)).orElse(super.get(batchId)) ``` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org