一、前言
1.相关版本:Spark Master branch(2018.11, compiled-version spark-2.4.1)
2. SparkSQL 的CacheManager 独立于SparkCore 的 CacheManager(SparkCore的缓存管理机制详见:https://blog.youkuaiyun.com/don_chiang709/article/details/84065510文章的 Spark 子模块 Storage 的Cache Manager段落)
二、SparkSQL 的CacheManager
1. 关键的函数(cacheQuery/buildBuffers)及类(InMemoryRelation/CachedRDDBuilder/InMemoryTableScanExec)列表如下:
step1) 当用户显式/隐式调用cacheTable接口时,会先创建LinkedList里的InMemoryRelation,而InMemoryRelation由入参CachedRDDBuilder构建。
step2)当SparkPlan真正执行的时候,会调用cachedColumnBuffers检测是否缓存了,如果没有,就调用buildBuffers缓存起来。注意默认的Storage.Level是Memory_And_disk.
总结:
SparkSQL 的缓存管理最终依靠RDD的Storage.Level来缓存数据。
参考:
Spark Sql 之 cacheTable: https://www.jianshu.com/p/a71084447b1e
Spark SQL 源码分析之 In-Memory Columnar Storage 之 cache table: https://blog.youkuaiyun.com/oopsoom/article/details/39525483