Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,16 @@ class CacheManager extends Logging {
(fs, path.makeQualified(fs.getUri, fs.getWorkingDirectory))
}

cachedData.foreach {
case data if data.plan.find(lookupAndRefresh(_, fs, qualifiedPath)).isDefined =>
val dataIndex = cachedData.indexWhere(cd => data.plan.sameResult(cd.plan))
if (dataIndex >= 0) {
data.cachedRepresentation.cachedColumnBuffers.unpersist(blocking = true)
cachedData.remove(dataIndex)
}
sparkSession.sharedState.cacheManager.cacheQuery(Dataset.ofRows(sparkSession, data.plan))
case _ => // Do Nothing
cachedData.filter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the previous one doesn't work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of collection can't be modified during iterating. Some elements are not iterated over if we delete/add elements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we are still modifying it during iteration, after the filter. can you be more specific about what the problem is?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a java collection so that we can remove elements while iterating?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After filter, we iterate on a different collection than cachedData, so it is no problem to add/delete elements to cachedData.

Copy link
Member Author

@viirya viirya Feb 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem can be shown clearly with an example code snippet:

val t = new scala.collection.mutable.ArrayBuffer[Int]

t += 1
t += 2

t.foreach { 
  case i if i > 0 =>
    println(s"i = $i")
    val index = t.indexWhere(_ == i)
    if (index >= 0) {
      t.remove(index)
    }
    println(s"t: $t")
    t += (i + 2)
    println(s"t: $t")
}

Output:

i = 1    // The first iteration, we get the first element "1"
t: ArrayBuffer(2)   // "1" has been removed from the array
t: ArrayBuffer(2, 3) // New element "3" has been inserted
i = 3   // In next iteration, element "2" is wrongly skipped
t: ArrayBuffer(2)     // "3" has been removed from the array
t: ArrayBuffer(2, 5) 

The element "2" is never iterated over.

case data if data.plan.find(lookupAndRefresh(_, fs, qualifiedPath)).isDefined => true
case _ => false
}.foreach { data =>
val dataIndex = cachedData.indexWhere(cd => data.plan.sameResult(cd.plan))
if (dataIndex >= 0) {
data.cachedRepresentation.cachedColumnBuffers.unpersist(blocking = true)
cachedData.remove(dataIndex)
}
sparkSession.sharedState.cacheManager.cacheQuery(Dataset.ofRows(sparkSession, data.plan))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,20 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with SharedSQLContext
assert(getNumInMemoryRelations(cachedPlan2) == 4)
}
}

test("refreshByPath should refresh all cached plans with the specified path") {
withTempDir { dir =>
val path = dir.getCanonicalPath()

spark.range(10).write.mode("overwrite").parquet(path)
spark.read.parquet(path).cache()
spark.read.parquet(path).filter($"id" > 4).cache()
assert(spark.read.parquet(path).filter($"id" > 4).count() == 5)

spark.range(20).write.mode("overwrite").parquet(path)
spark.catalog.refreshByPath(path)
assert(spark.read.parquet(path).count() == 20)
assert(spark.read.parquet(path).filter($"id" > 4).count() == 15)
}
}
}