diff --git a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/ClientUtils.kt b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/ClientUtils.kt index cd924ebfe36..5e382b1929a 100644 --- a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/ClientUtils.kt +++ b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/ClientUtils.kt @@ -21,7 +21,7 @@ import java.nio.file.Files import java.rmi.registry.LocateRegistry -internal val MAX_PORT_NUMBER = 0xffff +internal const val MAX_PORT_NUMBER = 0xffff enum class DaemonReportCategory { @@ -30,14 +30,15 @@ enum class DaemonReportCategory { fun makeRunFilenameString(timestamp: String, digest: String, port: String, escapeSequence: String = ""): String = - "$COMPILE_DAEMON_DEFAULT_FILES_PREFIX$escapeSequence.$timestamp$escapeSequence.$digest$escapeSequence.$port$escapeSequence.run" + "$COMPILE_DAEMON_DEFAULT_FILES_PREFIX$escapeSequence.$timestamp$escapeSequence.$digest$escapeSequence.$port$escapeSequence.run" fun makePortFromRunFilenameExtractor(digest: String): (String) -> Int? { val regex = makeRunFilenameString(timestamp = "[0-9TZ:\\.\\+-]+", digest = digest, port = "(\\d+)", escapeSequence = "\\").toRegex() - return { regex.find(it) - ?.groups?.get(1) - ?.value?.toInt() + return { + regex.find(it) + ?.groups?.get(1) + ?.value?.toInt() } } @@ -49,56 +50,66 @@ data class DaemonWithMetadata(val daemon: CompileService, val runFile: File, val // TODO: consider using compiler jar signature (checksum) as a CompilerID (plus java version, plus ???) instead of classpath checksum // would allow to use same compiler from taken different locations // reqs: check that plugins (or anything els) should not be part of the CP -fun walkDaemons(registryDir: File, - compilerId: CompilerId, - fileToCompareTimestamp: File, - filter: (File, Int) -> Boolean = { _, _ -> true }, - report: (DaemonReportCategory, String) -> Unit = { _, _ -> } +fun walkDaemons( + registryDir: File, + compilerId: CompilerId, + fileToCompareTimestamp: File, + filter: (File, Int) -> Boolean = { _, _ -> true }, + report: (DaemonReportCategory, String) -> Unit = { _, _ -> }, ): Sequence { val classPathDigest = compilerId.digest() val portExtractor = makePortFromRunFilenameExtractor(classPathDigest) return registryDir.walk() - .map { Pair(it, portExtractor(it.name)) } - .filter { (file, port) -> port != null && filter(file, port) } - .mapNotNull { (file, port) -> - assert(port!! in 1..(MAX_PORT_NUMBER - 1)) - val relativeAge = fileToCompareTimestamp.lastModified() - file.lastModified() - report(DaemonReportCategory.DEBUG, "found daemon on port $port ($relativeAge ms old), trying to connect") - val daemon = tryConnectToDaemon(port, report) - // cleaning orphaned file; note: daemon should shut itself down if it detects that the run file is deleted - if (daemon == null) { - if (relativeAge - ORPHANED_RUN_FILE_AGE_THRESHOLD_MS <= 0) { - report(DaemonReportCategory.DEBUG, "found fresh run file '${file.absolutePath}' ($relativeAge ms old), but no daemon, ignoring it") + .map { Pair(it, portExtractor(it.name)) } + .filter { (file, port) -> port != null && filter(file, port) } + .mapNotNull { (file, port) -> + assert(port!! in 1.. Unit): CompileService? { - try { - val daemon = LocateRegistry.getRegistry(LoopbackNetworkInterface.loopbackInetAddressName, port, LoopbackNetworkInterface.clientLoopbackSocketFactory) - ?.lookup(COMPILER_SERVICE_RMI_NAME) + val daemon = LocateRegistry.getRegistry( + LoopbackNetworkInterface.loopbackInetAddressName, + port, + LoopbackNetworkInterface.clientLoopbackSocketFactory + ) + ?.lookup(COMPILER_SERVICE_RMI_NAME) when (daemon) { null -> report(DaemonReportCategory.INFO, "daemon not found") is CompileService -> return daemon else -> report(DaemonReportCategory.INFO, "Unable to cast compiler service, actual class received: ${daemon::class.java.name}") } - } - catch (e: Throwable) { + } catch (e: Throwable) { report(DaemonReportCategory.INFO, "cannot connect to registry: " + (e.cause?.message ?: e.message ?: "unknown error")) } return null