Skip to content

Commit

Permalink
Merge pull request #2552 from tgodzik/parallel-compile
Browse files Browse the repository at this point in the history
bugfix: Synchronize on target instead of origin when copying
  • Loading branch information
tgodzik authored Jan 3, 2025
2 parents c1ff013 + 15b40a8 commit 9058c4e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/src/main/scala/bloop/io/ParallelOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ object ParallelOps {
}
}
} finally {
takenByOtherCopyProcess.remove(originFile)
takenByOtherCopyProcess.remove(targetFile)
// Complete successfully to unblock other tasks
p.success(())
}
Expand All @@ -223,7 +223,7 @@ object ParallelOps {

def acquireFile: MonixTask[Unit] = {
val currentPromise = Promise[Unit]()
val promiseInMap = takenByOtherCopyProcess.putIfAbsent(originFile, currentPromise)
val promiseInMap = takenByOtherCopyProcess.putIfAbsent(targetFile, currentPromise)
if (promiseInMap == null) {
triggerCopy(currentPromise)
} else {
Expand Down
73 changes: 73 additions & 0 deletions backend/src/test/scala/bloop/io/ParallelOpsSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package bloop.io

import java.nio.file.Files
import bloop.io.ParallelOps.CopyMode
import org.junit.Test
import bloop.logging.RecordingLogger
import bloop.task.Task
import monix.execution.Scheduler
import java.nio.file.StandardOpenOption
import scala.concurrent.duration._
import scala.concurrent.Await

class ParallelOpsSuite {

private def createRandomDirectory() = {

val from = Files.createTempDirectory("parallel")
val inputFile = from.resolve("text.scala")
val text = "random\n" * 100
Files.write(inputFile, text.getBytes, StandardOpenOption.CREATE, StandardOpenOption.APPEND)
}
@Test
def runMultipleCopies() = {

val from = createRandomDirectory()
val to = Files.createTempDirectory("parallel")

val config =
ParallelOps.CopyConfiguration(5, CopyMode.ReplaceExisting, Set.empty, Set.empty)
val logger = new RecordingLogger()

val tasks =
for (_ <- 0 to 100)
yield ParallelOps
.copyDirectories(config)(
from,
to,
Scheduler.Implicits.global,
enableCancellation = false,
logger
)

val result = Task.gatherUnordered(tasks).runAsync(Scheduler.Implicits.global)
val res = Await.result(result, 15.seconds)
assert(res.size == 101)
}

@Test
def runMultipleCopiesFromDifferentSource() = {

val to = Files.createTempDirectory("parallel")

val config =
ParallelOps.CopyConfiguration(100, CopyMode.ReplaceExisting, Set.empty, Set.empty)
val logger = new RecordingLogger()

val tasks =
for (_ <- 0 to 100)
yield ParallelOps
.copyDirectories(config)(
createRandomDirectory(),
to,
Scheduler.Implicits.global,
enableCancellation = false,
logger
)

val result = Task.gatherUnordered(tasks).runAsync(Scheduler.Implicits.global)
val res = Await.result(result, 15.seconds)
assert(res.size == 101)
}

}

0 comments on commit 9058c4e

Please sign in to comment.