From bf01fa472985a92190ee6f116a9ffd4df0018d0b Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Wed, 22 Feb 2017 07:57:47 +0300 Subject: [PATCH] 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: 9d95c841a6e182d28887407ee11476591157bf5a --- .../compilerRunner/JpsKotlinCompilerRunner.kt | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt index 7326e908566..c5dda295025 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt @@ -25,6 +25,8 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.config.CompilerSettings 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.jps.build.KotlinBuilder import java.io.ByteArrayOutputStream @@ -48,7 +50,17 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { } 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( @@ -116,14 +128,19 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName") } - val res = withDaemon(environment, retryOnConnectionError = true) { daemon, sessionId -> - val compilerMode = CompilerMode.JPS_COMPILER - val verbose = compilerArgs.verbose - val options = CompilationOptions(compilerMode, targetPlatform, reportCategories(verbose), reportSeverity(verbose), requestedCompilationResults = emptyArray()) - daemon.compile(sessionId, serializeWithAdditionalCompilerArgs(compilerArgs), options, JpsCompilerServicesFacadeImpl(environment), null) + log.debug("Try to connect to daemon") + val connection = getDaemonConnection(environment) + if (connection == null) { + log.info("Could not connect to daemon") + 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 { @@ -213,17 +230,15 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { } } - @Synchronized - override fun getDaemonConnection(environment: JpsCompilerEnvironment): DaemonConnection { - if (jpsDaemonConnection == null) { - val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector) - val compilerPath = File(libPath, "kotlin-compiler.jar") - val compilerId = CompilerId.makeCompilerId(compilerPath) - // TODO: pass daemon options to newDaemonConnection - val daemonOptions = configureDaemonOptions() - val flagFile = makeAutodeletingFlagFile("compiler-jps-session-", File(daemonOptions.runFilesPathOrDefault)) - jpsDaemonConnection = newDaemonConnection(compilerId, flagFile, environment, daemonOptions) - } - return jpsDaemonConnection!! - } + override fun getDaemonConnection(environment: JpsCompilerEnvironment): CompileServiceSession? = + getOrCreateDaemonConnection { + val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector) + val compilerPath = File(libPath, "kotlin-compiler.jar") + val compilerId = CompilerId.makeCompilerId(compilerPath) + val daemonOptions = configureDaemonOptions() + + val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions) + val sessionFlagFile = makeAutodeletingFlagFile("compiler-jps-session-", File(daemonOptions.runFilesPathOrDefault)) + newDaemonConnection(compilerId, clientFlagFile, sessionFlagFile, environment, daemonOptions) + } } \ No newline at end of file