HeartSaVioR commented on a change in pull request #31495:
URL: https://github.com/apache/spark/pull/31495#discussion_r572473687



##########
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:
       Looks like adding the check doesn't contribute noticeable latency;
   
   before the change (exist -> delete)
   
   ```
   grep "walCommit took" driver_stderr.log | awk '{print $7}' | head -n 1100 | 
tail -n 1000 | datamash max 1 min 1 mean 1 median 1 perc:90 1 perc:95 1 perc:99 
1
   1874 152     242.131 220.5   306     342     654.84
   ```
   
   after the change (delete)
   
   ```
   grep "walCommit took" driver_stderr.log | awk '{print $7}' | head -n 1100 | 
tail -n 1000 | datamash max 1 min 1 mean 1 median 1 perc:90 1 perc:95 1 perc:99 
1
   1882 156     252.162 231.5   311     357.2   559.16
   ```
   
   but I agree the check is unnecessary. Will remove it.




----------------------------------------------------------------
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

Reply via email to