[IC] Make CompilationTransaction to be able to close CachesManager

This commit is contained in:
Alexander.Likhachev
2023-01-26 20:25:35 +01:00
committed by Space Team
parent 3ed651a7a6
commit 25a3dcf3d1
3 changed files with 84 additions and 43 deletions
@@ -0,0 +1,8 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.incremental
class CachesManagerCloseException(cause: Throwable) : Exception("Failed to close caches", cause)
@@ -35,6 +35,16 @@ interface CompilationTransaction : Closeable {
* Marks the transaction as successful, so it should not revert changes if it is able to perform revert.
*/
fun markAsSuccessful()
/**
* A closeable object (caches manager) that is had to be closed on the transaction close
*/
var cachesManager: Closeable?
/**
* A throwable that was thrown during the transaction execution
*/
var executionThrowable: Throwable?
}
fun CompilationTransaction.write(file: Path, writeAction: () -> Unit) {
@@ -55,6 +65,22 @@ fun CompilationTransaction.writeBytes(file: Path, array: ByteArray) {
}
}
inline fun <R> CompilationTransaction.runWithin(
exceptionTransformer: (Throwable) -> R = { throw it },
body: (CompilationTransaction) -> R
): R {
return runCatching {
use {
try {
body(this)
} catch (t: Throwable) {
executionThrowable = t
throw t
}
}
}.recover { exceptionTransformer(it) }.getOrThrow() // some exceptions may be transformed into results
}
/**
* A dummy implementation of compilation transaction
*/
@@ -73,10 +99,21 @@ class DummyCompilationTransaction : CompilationTransaction {
// do nothing
}
override fun close() {
// do nothing
}
override var cachesManager: Closeable? = null
override var executionThrowable: Throwable? = null
override fun close() {
try {
cachesManager?.close()
} catch (t: Throwable) {
if (executionThrowable != null) {
executionThrowable?.addSuppressed(CachesManagerCloseException(t))
} else {
throw CachesManagerCloseException(t)
}
}
}
}
/**
@@ -173,12 +210,30 @@ class RecoverableCompilationTransaction(
successful = true
}
override var cachesManager: Closeable? = null
override var executionThrowable: Throwable? = null
override fun close() {
if (successful) {
cleanupStash()
} else {
revertChanges()
cleanupStash()
val mainException = runCatching {
cachesManager?.close()
}.exceptionOrNull()?.run {
successful = false
val exception = CachesManagerCloseException(this)
executionThrowable?.addSuppressed(exception)
executionThrowable ?: exception
}
try {
if (successful) {
cleanupStash()
} else {
revertChanges()
cleanupStash()
}
} catch (t: Throwable) {
mainException?.addSuppressed(t)
throw mainException ?: t
}
}
}