Deriving callback server classes from UnicastRemoteObject instead of manual registration, magically solves the problems with parallel builds; modifying and refactoring usage places accordingly

This commit is contained in:
Ilya Chernikov
2015-10-13 18:26:33 +02:00
parent 5b5ec2cb04
commit 21558fe1c6
3 changed files with 20 additions and 48 deletions
@@ -25,23 +25,15 @@ import org.jetbrains.kotlin.progress.CompilationCanceledStatus
import org.jetbrains.kotlin.rmi.CompilerCallbackServicesFacade import org.jetbrains.kotlin.rmi.CompilerCallbackServicesFacade
import org.jetbrains.kotlin.rmi.LoopbackNetworkInterface import org.jetbrains.kotlin.rmi.LoopbackNetworkInterface
import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT
import java.rmi.server.UnicastRemoteObject
public class CompilerCallbackServicesFacadeServer( public class CompilerCallbackServicesFacadeServer(
val incrementalCompilationComponents: IncrementalCompilationComponents? = null, val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
val compilationCancelledStatus: CompilationCanceledStatus? = null, val compilationCancelledStatus: CompilationCanceledStatus? = null,
port: Int = SOCKET_ANY_FREE_PORT port: Int = SOCKET_ANY_FREE_PORT
) : CompilerCallbackServicesFacade { ) : CompilerCallbackServicesFacade,
java.rmi.server.UnicastRemoteObject(port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory)
init { {
UnicastRemoteObject.exportObject(this, port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory)
}
public fun disconnect() {
UnicastRemoteObject.unexportObject(this, true)
}
override fun hasIncrementalCaches(): Boolean = incrementalCompilationComponents != null override fun hasIncrementalCaches(): Boolean = incrementalCompilationComponents != null
override fun hasLookupTracker(): Boolean = incrementalCompilationComponents != null override fun hasLookupTracker(): Boolean = incrementalCompilationComponents != null
@@ -114,14 +114,7 @@ public object KotlinCompilerClient {
public fun compile(compilerService: CompileService, targetPlatform: CompileService.TargetPlatform, args: Array<out String>, out: OutputStream): Int { public fun compile(compilerService: CompileService, targetPlatform: CompileService.TargetPlatform, args: Array<out String>, out: OutputStream): Int {
val outStrm = RemoteOutputStreamServer(out) val outStrm = RemoteOutputStreamServer(out)
val servicesFacade = CompilerCallbackServicesFacadeServer() return compilerService.remoteCompile(targetPlatform, args, CompilerCallbackServicesFacadeServer(), outStrm, CompileService.OutputFormat.PLAIN, outStrm)
try {
return compilerService.remoteCompile(targetPlatform, args, servicesFacade, outStrm, CompileService.OutputFormat.PLAIN, outStrm)
}
finally {
servicesFacade.disconnect()
outStrm.disconnect()
}
} }
@@ -132,22 +125,15 @@ public object KotlinCompilerClient {
compilerOut: OutputStream, compilerOut: OutputStream,
daemonOut: OutputStream, daemonOut: OutputStream,
profiler: Profiler = DummyProfiler() profiler: Profiler = DummyProfiler()
): Int { ): Int = profiler.withMeasure(this) {
compileService.remoteIncrementalCompile(
val compilerOutStreamServer = RemoteOutputStreamServer(compilerOut) targetPlatform,
val daemonOutStreamServer = RemoteOutputStreamServer(daemonOut) args,
val servicesFacade = CompilerCallbackServicesFacadeServer(incrementalCompilationComponents = callbackServices.incrementalCompilationComponents, CompilerCallbackServicesFacadeServer(incrementalCompilationComponents = callbackServices.incrementalCompilationComponents,
compilationCancelledStatus = callbackServices.compilationCanceledStatus) compilationCancelledStatus = callbackServices.compilationCanceledStatus),
try { RemoteOutputStreamServer(compilerOut),
return profiler.withMeasure(this) { CompileService.OutputFormat.XML,
compileService.remoteIncrementalCompile(targetPlatform, args, servicesFacade, compilerOutStreamServer, CompileService.OutputFormat.XML, daemonOutStreamServer) RemoteOutputStreamServer(daemonOut))
}
}
finally {
servicesFacade.disconnect()
compilerOutStreamServer.disconnect()
daemonOutStreamServer.disconnect()
}
} }
public val COMPILE_DAEMON_CLIENT_OPTIONS_PROPERTY: String = "kotlin.daemon.client.options" public val COMPILE_DAEMON_CLIENT_OPTIONS_PROPERTY: String = "kotlin.daemon.client.options"
@@ -230,8 +216,9 @@ public object KotlinCompilerClient {
println("Used memory $memAfter (${"%+d".format(memAfter - memBefore)} kb)") println("Used memory $memAfter (${"%+d".format(memAfter - memBefore)} kb)")
} }
finally { finally {
servicesFacade.disconnect() // forcing RMI to unregister all objects and stop
outStrm.disconnect() java.rmi.server.UnicastRemoteObject.unexportObject(servicesFacade, true)
java.rmi.server.UnicastRemoteObject.unexportObject(outStrm, true)
} }
} }
} }
@@ -20,19 +20,12 @@ import org.jetbrains.kotlin.rmi.LoopbackNetworkInterface
import org.jetbrains.kotlin.rmi.RemoteOutputStream import org.jetbrains.kotlin.rmi.RemoteOutputStream
import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT
import java.io.OutputStream import java.io.OutputStream
import java.rmi.server.UnicastRemoteObject
class RemoteOutputStreamServer(val out: OutputStream, port: Int = SOCKET_ANY_FREE_PORT) : RemoteOutputStream { class RemoteOutputStreamServer(val out: OutputStream, port: Int = SOCKET_ANY_FREE_PORT)
: RemoteOutputStream,
init { java.rmi.server.UnicastRemoteObject(port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory)
UnicastRemoteObject.exportObject(this, port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory) {
}
public fun disconnect() {
UnicastRemoteObject.unexportObject(this, true)
}
override fun close() { override fun close() {
out.close() out.close()
} }