JPS: Fix updating cache format version for rebuilt targets by removing i/o optimization

Problem: previously, format-version.txt file was deleted while cleaning
target output on rebuild. Since directory was deleted, cache
attributesDiff stored in memory was not updated. This causes that on
saveExpectedStateIfNeeded does nothing.

The right way to fix it is move cache version format diff into
AbstractIncrementalCache, and override `clean()` method. But this is
hard to do for lookup storage, since used compilers set (jvm/js) will
known only after context init. This can be done by moving `expected`
attributes out of manager, but this is huge change for small benefit.

So, the optimal way for now is to write version for each build, even if
it is not changed.

#KT-27044 Fixed
This commit is contained in:
Sergey Rostov
2018-10-15 12:26:21 +03:00
parent cf31e4d58c
commit 5232f080d6
7 changed files with 11 additions and 33 deletions
@@ -27,10 +27,6 @@ data class CacheAttributesDiff<Attrs: Any>(
else CacheStatus.CLEARED
}
fun saveExpectedIfNeeded() {
if (expected != actual) manager.writeActualVersion(expected)
}
override fun toString(): String {
return "$status: actual=$actual -> expected=$expected"
}
@@ -32,11 +32,8 @@ interface CacheAttributesManager<Attrs : Any> {
/**
* Write [values] as cache attributes for next build execution.
*
* This is internal operation that should be implemented by particular implementation of CacheAttributesManager.
* Consider using `loadDiff().saveExpectedIfNeeded()` for saving attributes values for next build.
*/
fun writeActualVersion(values: Attrs?)
fun writeVersion(values: Attrs? = expected)
/**
* Check if cache with [actual] attributes values can be used when [expected] attributes are required.
@@ -52,28 +49,14 @@ fun <Attrs : Any> CacheAttributesManager<Attrs>.loadDiff(
fun <Attrs : Any> CacheAttributesManager<Attrs>.loadAndCheckStatus() =
loadDiff().status
/**
* This method is kept only for compatibility.
* Save [expected] cache attributes values if it is enabled and not equals to [actual].
*/
@Deprecated(
message = "Consider using `this.loadDiff().saveExpectedIfNeeded()` and cache `loadDiff()` result.",
replaceWith = ReplaceWith("loadDiff().saveExpectedIfNeeded()")
)
fun <Attrs : Any> CacheAttributesManager<Attrs>.saveIfNeeded(
actual: Attrs? = this.loadActual(),
expected: Attrs = this.expected
?: error("To save disabled cache status [delete] should be called (this behavior is kept for compatibility)")
) = loadDiff(actual, expected).saveExpectedIfNeeded()
/**
* This method is kept only for compatibility.
* Delete actual cache attributes values if it existed.
*/
@Deprecated(
message = "Consider using `this.loadDiff().saveExpectedIfNeeded()` and cache `loadDiff()` result.",
replaceWith = ReplaceWith("writeActualVersion(null)")
replaceWith = ReplaceWith("writeVersion(null)")
)
fun CacheAttributesManager<*>.clean() {
writeActualVersion(null)
writeVersion(null)
}
@@ -44,7 +44,7 @@ class CacheVersionManager(
null
}
override fun writeActualVersion(values: CacheVersion?) {
override fun writeVersion(values: CacheVersion?) {
if (values == null) versionFile.delete()
else {
versionFile.parentFile.mkdirs()
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.incremental.components.ExpectActualTracker
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.incremental.parsing.classesFqNames
import org.jetbrains.kotlin.incremental.storage.version.CacheVersionManager
import org.jetbrains.kotlin.incremental.storage.version.saveIfNeeded
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.progress.CompilationCanceledStatus
import java.io.File
@@ -272,7 +271,7 @@ abstract class IncrementalCompilerRunner<
processChangesAfterBuild(compilationMode, currentBuildInfo, dirtyData)
if (exitCode == ExitCode.OK) {
cachesVersionManagers.forEach { it.saveIfNeeded() }
cachesVersionManagers.forEach { it.writeVersion() }
}
return exitCode
@@ -96,7 +96,7 @@ class KotlinChunk internal constructor(val context: KotlinCompileContext, val ta
context.ensureLookupsCacheAttributesSaved()
targets.forEach {
it.initialLocalCacheAttributesDiff.saveExpectedIfNeeded()
it.initialLocalCacheAttributesDiff.manager.writeVersion()
}
val serializedMetaInfo = representativeTarget.buildMetaInfoFactory.serializeToString(compilerArguments)
@@ -149,7 +149,7 @@ class KotlinCompileContext(val jpsContext: CompileContext) {
*/
fun ensureLookupsCacheAttributesSaved() {
if (lookupAttributesSaved.compareAndSet(false, true)) {
initialLookupsCacheStateDiff.saveExpectedIfNeeded()
initialLookupsCacheStateDiff.manager.writeVersion()
}
}
@@ -207,7 +207,7 @@ class KotlinCompileContext(val jpsContext: CompileContext) {
private fun clearLookupCache() {
KotlinBuilder.LOG.info("Clearing lookup cache")
dataManager.cleanLookupStorage(KotlinBuilder.LOG)
initialLookupsCacheStateDiff.saveExpectedIfNeeded()
initialLookupsCacheStateDiff.manager.writeVersion()
}
fun cleanupCaches() {
@@ -47,12 +47,12 @@ class CompositeLookupsCacheAttributesManager(
return CompositeLookupsCacheAttributes(version.version, components)
}
override fun writeActualVersion(values: CompositeLookupsCacheAttributes?) {
override fun writeVersion(values: CompositeLookupsCacheAttributes?) {
if (values == null) {
versionManager.writeActualVersion(null)
versionManager.writeVersion(null)
actualComponentsFile.delete()
} else {
versionManager.writeActualVersion(CacheVersion(values.version))
versionManager.writeVersion(CacheVersion(values.version))
actualComponentsFile.parentFile.mkdirs()
actualComponentsFile.writeText(values.components.joinToString("\n"))