forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache compilers in zinc nailgun instance
- Loading branch information
1 parent
ab1cd47
commit 3e798c1
Showing
11 changed files
with
467 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/scala/org/pantsbuild/zinc/compiler/CompilerCache.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.pantsbuild.zinc.compiler | ||
|
||
import com.google.common.hash.{HashCode, Hashing} | ||
import java.io.File | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.{FileAlreadyExistsException, Files, StandardCopyOption} | ||
import java.util.concurrent.Callable | ||
import org.pantsbuild.zinc.cache.Cache | ||
import org.pantsbuild.zinc.compiler.CompilerUtils.newScalaCompiler | ||
import org.pantsbuild.zinc.compiler.InputUtils.ScalaJars | ||
import org.pantsbuild.zinc.scalautil.ScalaUtils | ||
import sbt.internal.inc.ZincUtil | ||
import xsbti.compile.{ClasspathOptionsUtil, Compilers} | ||
|
||
class CompilerCache(limit: Int) { | ||
val cache = Cache[HashCode, Compilers](limit) | ||
|
||
def make(scalaJars: ScalaJars, javaHome: Option[File], compiledBridgeJar: DigestedFile): Compilers = { | ||
val instance = ScalaUtils.scalaInstance(scalaJars.compiler.file, scalaJars.extra.map { _.file }, scalaJars.library.file) | ||
ZincUtil.compilers(instance, ClasspathOptionsUtil.auto, javaHome, newScalaCompiler(instance, compiledBridgeJar.file)) | ||
} | ||
|
||
def get(compilerCacheDir: File, scalaJars: ScalaJars, javaHome: Option[File], compiledBridgeJar: DigestedFile): Compilers = { | ||
val cacheKeyBuilder = Hashing.sha256().newHasher(); | ||
|
||
for (file <- Seq(scalaJars.compiler, scalaJars.library) ++ scalaJars.extra) { | ||
cacheKeyBuilder.putBytes(HashCode.fromString(file.digest.fingerprintHex).asBytes) | ||
cacheKeyBuilder.putLong(file.digest.sizeBytes) | ||
} | ||
|
||
javaHome match { | ||
case Some(file) => cacheKeyBuilder.putString(file.getCanonicalPath, StandardCharsets.UTF_8) | ||
case None => {} | ||
} | ||
|
||
val cacheKey = cacheKeyBuilder.hash() | ||
cache.get(cacheKey, new Callable[Compilers] { def call(): Compilers = { | ||
val versionedCompilerCacheDir = new File(compilerCacheDir, cacheKey.toString) | ||
|
||
val newScalaCompilerJar = CompilerCache.rename(scalaJars.compiler, versionedCompilerCacheDir, "scala-compiler") | ||
val newScalaLibraryJar = CompilerCache.rename(scalaJars.library, versionedCompilerCacheDir, "scala-library") | ||
val newScalaExtraJars = scalaJars.extra.map { CompilerCache.rename(_, versionedCompilerCacheDir, "scala-extra") } | ||
val newCompiledBridgeJar = CompilerCache.rename(compiledBridgeJar, versionedCompilerCacheDir, "compiled-bridge") | ||
if (!versionedCompilerCacheDir.exists) { | ||
val tempVersionedCompilerCacheDir = compilerCacheDir.toPath | ||
.resolve(s"${ cacheKey.toString }.tmp") | ||
Files.createDirectories(tempVersionedCompilerCacheDir) | ||
Files.copy(scalaJars.compiler.file.toPath, tempVersionedCompilerCacheDir.resolve(newScalaCompilerJar.file.getName)) | ||
Files.copy(scalaJars.library.file.toPath, tempVersionedCompilerCacheDir.resolve(newScalaLibraryJar.file.getName)) | ||
for ((oldExtra, newExtra) <- scalaJars.extra zip newScalaExtraJars) | ||
Files.copy(oldExtra.file.toPath, tempVersionedCompilerCacheDir.resolve(newExtra.file.getName)) | ||
Files.copy(compiledBridgeJar.file.toPath, tempVersionedCompilerCacheDir.resolve(newCompiledBridgeJar.file.getName)) | ||
try { | ||
Files.move(tempVersionedCompilerCacheDir, versionedCompilerCacheDir.toPath, StandardCopyOption.ATOMIC_MOVE) | ||
} catch { | ||
case _: FileAlreadyExistsException => { | ||
// Ignore - trust that someone else atomically created the directory properly | ||
} | ||
} | ||
} | ||
|
||
make(ScalaJars(newScalaCompilerJar, newScalaLibraryJar, newScalaExtraJars), javaHome, newCompiledBridgeJar) | ||
}}) | ||
} | ||
} | ||
|
||
object CompilerCache { | ||
def rename(file: DigestedFile, dir: File, namePrefix: String): DigestedFile = { | ||
DigestedFile(new File(dir, s"${namePrefix}-${file.digest.fingerprintHex}-${file.digest.sizeBytes}.jar"), Some(file.digest)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.pantsbuild.zinc.compiler | ||
|
||
import com.google.common.hash.HashCode | ||
import java.io.File | ||
import java.nio.file.{Files, Path} | ||
import java.security.MessageDigest | ||
import org.pantsbuild.zinc.util.Util | ||
|
||
case class Digest(fingerprintHex: String, sizeBytes: Long) | ||
|
||
case class DigestedFile(file: File, private val _digest: Option[Digest] = None) extends scopt.Read[DigestedFile] { | ||
|
||
lazy val digest = _digest.getOrElse { DigestedFile.digest(file.toPath) } | ||
|
||
override def arity: Int = 1 | ||
|
||
override def reads: String => DigestedFile = DigestedFile.fromString | ||
|
||
def normalise(relativeTo: File): DigestedFile = { | ||
DigestedFile(Util.normalise(Some(relativeTo))(file), _digest) | ||
} | ||
} | ||
|
||
object DigestedFile { | ||
def digest(path: Path): Digest = { | ||
val digest = MessageDigest.getInstance("SHA-256") | ||
val bytes = Files.readAllBytes(path) | ||
Digest(HashCode.fromBytes(digest.digest(bytes)).toString, bytes.size) | ||
} | ||
|
||
def fromString(s: String): DigestedFile = { | ||
s.split("=", 2) match { | ||
case arr if arr.size == 1 => { | ||
val file = new File(arr(0)) | ||
DigestedFile(file, None) | ||
} | ||
case arr if arr.size == 2 => { | ||
val file = new File(arr(0)) | ||
val digest = arr(1).split("-", 2) match { | ||
case digestParts if digestParts.size == 2 => Digest(digestParts(0), digestParts(1).toLong) | ||
case _ => throw new RuntimeException(s"Bad digest: ${arr(1)}") | ||
} | ||
DigestedFile(file, Some(digest)) | ||
} | ||
} | ||
} | ||
|
||
implicit def digestedFileRead: scopt.Read[DigestedFile] = scopt.Read.stringRead.map { DigestedFile.fromString } | ||
|
||
} |
Oops, something went wrong.