[Daemon] Improve some daemon log messages

A few messages were improved:
  * If the daemon process died at the startup, add its last 10 lines of the output to the log and report to the EXCEPTION (ERROR in the case of Gradle) level
  * If some exception occurs during connection attempt, add its stacktrace and report to the EXCEPTION (ERROR in the case of Gradle) level
  * Added more DEBUG level messages
  * Some important messages are moved to the INFO level
  * Added a suggestion to report issue to kotl.in/issue
^KT-55322 Fixed
This commit is contained in:
Alexander.Likhachev
2023-11-21 12:58:05 +01:00
committed by Space Team
parent 95bc49054f
commit b000b66ef7
8 changed files with 120 additions and 27 deletions
@@ -103,6 +103,7 @@ object KotlinCompilerClient {
sessionAliveFlagFile: File? = null,
): CompileServiceSession? {
val ignoredDaemonSessionFiles = mutableSetOf<File>()
var daemonStartupAttemptsCount = 0
return connectLoop(reportingTargets, autostart) { isLastAttempt ->
fun CompileService.tryToLeaseSession(): CompileServiceSession? {
@@ -112,8 +113,23 @@ object KotlinCompilerClient {
if (!leaseSession) return CompileServiceSession(this, CompileService.NO_SESSION)
return leaseCompileSession(sessionAliveFlagFile?.absolutePath).takeUnless { it is CompileService.CallResult.Dying }?.let {
CompileServiceSession(this, it.get())
return when (val leaseSessionResult = leaseCompileSession(sessionAliveFlagFile?.absolutePath)) {
is CompileService.CallResult.Dying -> {
reportingTargets.report(DaemonReportCategory.DEBUG, "the daemon is already dying, skipping it")
null
}
is CompileService.CallResult.Good -> {
val sessionId = leaseSessionResult.get()
reportingTargets.report(DaemonReportCategory.DEBUG, "successfully leased a compile session (id = $sessionId)")
CompileServiceSession(this, sessionId)
}
else -> {
reportingTargets.report(DaemonReportCategory.DEBUG, "got an expected result on attempt to lease a compile session")
// the call to get() below shall lead to an exception throwing
// if it does happen, it indicates real problems,
// so no special handling is required, and it's ok to fail-fast
CompileServiceSession(this, leaseSessionResult.get())
}
}
}
@@ -133,8 +149,9 @@ object KotlinCompilerClient {
}
is DaemonSearchResult.NotFound -> {
if (!isLastAttempt && autostart) {
if (startDaemon(compilerId, result.requiredJvmOptions, daemonOptions, reportingTargets)) {
reportingTargets.report(DaemonReportCategory.DEBUG, "new daemon started, trying to find it")
reportingTargets.report(DaemonReportCategory.DEBUG, "trying to start a new compiler daemon")
if (startDaemon(compilerId, result.requiredJvmOptions, daemonOptions, reportingTargets, daemonStartupAttemptsCount++)) {
reportingTargets.report(DaemonReportCategory.DEBUG, "new compiler daemon started, trying to find it")
}
}
null
@@ -375,15 +392,17 @@ object KotlinCompilerClient {
if (res != null) return res
if (err != null) {
val errorDetails = err?.let { ", error: ${it.stackTraceToString()}" } ?: ""
if (attempts >= DAEMON_CONNECT_CYCLE_ATTEMPTS || !autostart) {
reportingTargets.report(
DaemonReportCategory.INFO,
(if (attempts >= DAEMON_CONNECT_CYCLE_ATTEMPTS || !autostart) "no more retries on: " else "retrying($attempts) on: ")
+ err.toString()
DaemonReportCategory.EXCEPTION,
"Failed connecting to the daemon in $attempts retries$errorDetails"
)
} else {
reportingTargets.report(DaemonReportCategory.INFO, "#$attempts retrying connecting to the daemon $errorDetails")
}
if (attempts++ > DAEMON_CONNECT_CYCLE_ATTEMPTS || !autostart) {
if (++attempts > DAEMON_CONNECT_CYCLE_ATTEMPTS || !autostart) {
return null
}
}
@@ -436,6 +455,7 @@ object KotlinCompilerClient {
daemonJVMOptions: DaemonJVMOptions,
daemonOptions: DaemonOptions,
reportingTargets: DaemonReportingTargets,
startupAttempt: Int,
): Boolean {
val javaExecutable = File(File(CompilerSystemProperties.JAVA_HOME.safeValue, "bin"), "java")
val serverHostname = CompilerSystemProperties.JAVA_RMI_SERVER_HOSTNAME.value
@@ -459,7 +479,7 @@ object KotlinCompilerClient {
COMPILER_DAEMON_CLASS_FQN +
daemonOptions.mappers.flatMap { it.toArgs(COMPILE_DAEMON_CMDLINE_OPTIONS_PREFIX) } +
compilerId.mappers.flatMap { it.toArgs(COMPILE_DAEMON_CMDLINE_OPTIONS_PREFIX) }
reportingTargets.report(DaemonReportCategory.DEBUG, "starting the daemon as: " + args.joinToString(" "))
reportingTargets.report(DaemonReportCategory.INFO, "starting the daemon as: " + args.joinToString(" "))
val processBuilder = ProcessBuilder(args)
processBuilder.redirectErrorStream(true)
val workingDir = File(daemonOptions.runFilesPath).apply { mkdirs() }
@@ -470,6 +490,8 @@ object KotlinCompilerClient {
val isEchoRead = Semaphore(1)
isEchoRead.acquire()
val lastDaemonCliOutputs = LastDaemonCliOutputs()
val stdoutThread =
thread {
try {
@@ -477,6 +499,7 @@ object KotlinCompilerClient {
.reader()
.forEachLine {
if (Thread.currentThread().isInterrupted) return@forEachLine
lastDaemonCliOutputs.add(it)
if (it == COMPILE_DAEMON_IS_READY_MESSAGE) {
reportingTargets.report(
DaemonReportCategory.DEBUG,
@@ -515,8 +538,8 @@ object KotlinCompilerClient {
return when {
!isProcessAlive(daemon) -> {
reportingTargets.report(
DaemonReportCategory.INFO,
"Daemon terminated unexpectedly with error code: ${daemon.exitValue()}"
DaemonReportCategory.EXCEPTION,
"The daemon has terminated unexpectedly on startup attempt #${startupAttempt + 1} with error code: ${daemon.exitValue()}. ${lastDaemonCliOutputs.getAsSingleString()}"
)
false
}
@@ -0,0 +1,36 @@
/*
* 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.daemon.client
private const val limit = 10
private const val outputIndent = 4
/**
* Holds the last [limit] lines of the daemon process standard output to be able to print them in the case of problems with the daemon.
*/
internal class LastDaemonCliOutputs {
private val lastPrintedLines: MutableList<String> = LimitedLinkedList(limit)
private var totalCount = 0
fun add(line: String) {
lastPrintedLines.add(line)
totalCount++
}
private fun calculateAbsoluteIndex(relativeIndex: Int) = totalCount - lastPrintedLines.size + relativeIndex
private val ellipsisIfRequired
get() = if (totalCount > limit) " ".repeat(outputIndent) + "... (${totalCount - limit} more lines)${System.lineSeparator()}" else ""
fun getAsSingleString() = lastPrintedLines
.takeIf { it.isNotEmpty() }
?.withIndex()
?.joinToString(
System.lineSeparator(),
prefix = "The daemon process output:${System.lineSeparator()}$ellipsisIfRequired",
) { (index, line) -> " ".repeat(outputIndent) + "${calculateAbsoluteIndex(index) + 1}. $line" }
?: (" ".repeat(outputIndent) + "The daemon process produced no output")
}
@@ -0,0 +1,19 @@
/*
* 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.daemon.client
import java.util.*
internal class LimitedLinkedList<E>(private val limit: Int) : LinkedList<E>() {
override fun add(element: E): Boolean {
val added = super.add(element)
while (added && size > limit) {
super.remove()
}
return added
}
}