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



##########
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:
       > e.g. In file stream source we delete source files based on the commit. 
   
   I don't understand why this could bring a correctness issue (I'm still very 
new to streaming so I may miss some use cases). The only problem I can imagine 
now is `get(batchId)` may lead to the correctness issue as `batchId` like 2 or 
4 results in the same index. But this can also be addressed by saving the 
`batchId` at the same time, e.g., `Array[(batchId, OffsetSeq)]`, which I think 
also addresses your concern for batchId coupling.
   
   BTW, I did a minor performance test between these two ways, and seems it 
does have a performance impact:
   
   ```scala
   
   // ~270ms
   timeTakeMs {
     treeMap.put(1, "1")
     treeMap.put(2, "3")
     treeMap.put(3, "3")
     treeMap.headMap(2, true).clear()
   }
   
   // ~0ms
   timeTakeMs {
     array(0) = "0"
     array(1) = "1"
     array(0) = "2"
   }
   ```
   
   Thoughts?




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to