Use connect-and-lease when using daemon in JPS & Gradle

Relates to KT-15562 "Service is dying".

This commit includes multiple changes:
 1. JPS & Gradle daemon clients are refactored to use `connectAndLease` from `KotlinCompilerClient`.
 `connectAndLease` was introduced in previous commits
 2. `withKotlin` was removed because `connectAndLease` already covers retrying on connection error
 3. Gradle flag files creation is changed:
   * client-alive flag file lives as long as Gradle instance lives,
   * session-alive flag file lives until the end of a build.

Original commit: 9d95c841a6
This commit is contained in:
Alexey Tsvetkov
2017-02-22 07:57:47 +03:00
parent a4cffe9b9e
commit bf01fa4729
@@ -25,6 +25,8 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.config.CompilerSettings import org.jetbrains.kotlin.config.CompilerSettings
import org.jetbrains.kotlin.config.additionalArgumentsAsList import org.jetbrains.kotlin.config.additionalArgumentsAsList
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.common.* import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.jps.build.KotlinBuilder import org.jetbrains.kotlin.jps.build.KotlinBuilder
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@@ -48,7 +50,17 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
} }
companion object { companion object {
private @Volatile var jpsDaemonConnection: DaemonConnection? = null @Volatile
private var _jpsCompileServiceSession: CompileServiceSession? = null
@Synchronized
private fun getOrCreateDaemonConnection(newConnection: ()-> CompileServiceSession?): CompileServiceSession? {
if (_jpsCompileServiceSession == null) {
_jpsCompileServiceSession = newConnection()
}
return _jpsCompileServiceSession
}
} }
fun runK2JvmCompiler( fun runK2JvmCompiler(
@@ -116,14 +128,19 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName") else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName")
} }
val res = withDaemon(environment, retryOnConnectionError = true) { daemon, sessionId -> log.debug("Try to connect to daemon")
val compilerMode = CompilerMode.JPS_COMPILER val connection = getDaemonConnection(environment)
val verbose = compilerArgs.verbose if (connection == null) {
val options = CompilationOptions(compilerMode, targetPlatform, reportCategories(verbose), reportSeverity(verbose), requestedCompilationResults = emptyArray()) log.info("Could not connect to daemon")
daemon.compile(sessionId, serializeWithAdditionalCompilerArgs(compilerArgs), options, JpsCompilerServicesFacadeImpl(environment), null) return null
} }
return res?.get()?.let { exitCodeFromProcessExitCode(it) } val (daemon, sessionId) = connection
val compilerMode = CompilerMode.JPS_COMPILER
val verbose = compilerArgs.verbose
val options = CompilationOptions(compilerMode, targetPlatform, reportCategories(verbose), reportSeverity(verbose), requestedCompilationResults = emptyArray())
val res = daemon.compile(sessionId, serializeWithAdditionalCompilerArgs(compilerArgs), options, JpsCompilerServicesFacadeImpl(environment), null)
return exitCodeFromProcessExitCode(res.get())
} }
private fun withAdditionalArguments(compilerArgs: CommonCompilerArguments): CommonCompilerArguments { private fun withAdditionalArguments(compilerArgs: CommonCompilerArguments): CommonCompilerArguments {
@@ -213,17 +230,15 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
} }
} }
@Synchronized override fun getDaemonConnection(environment: JpsCompilerEnvironment): CompileServiceSession? =
override fun getDaemonConnection(environment: JpsCompilerEnvironment): DaemonConnection { getOrCreateDaemonConnection {
if (jpsDaemonConnection == null) { val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector) val compilerPath = File(libPath, "kotlin-compiler.jar")
val compilerPath = File(libPath, "kotlin-compiler.jar") val compilerId = CompilerId.makeCompilerId(compilerPath)
val compilerId = CompilerId.makeCompilerId(compilerPath) val daemonOptions = configureDaemonOptions()
// TODO: pass daemon options to newDaemonConnection
val daemonOptions = configureDaemonOptions() val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions)
val flagFile = makeAutodeletingFlagFile("compiler-jps-session-", File(daemonOptions.runFilesPathOrDefault)) val sessionFlagFile = makeAutodeletingFlagFile("compiler-jps-session-", File(daemonOptions.runFilesPathOrDefault))
jpsDaemonConnection = newDaemonConnection(compilerId, flagFile, environment, daemonOptions) newDaemonConnection(compilerId, clientFlagFile, sessionFlagFile, environment, daemonOptions)
} }
return jpsDaemonConnection!!
}
} }