Fixed locking statistics files when multiple gradle daemons started

This commit is contained in:
Andrey Uskov
2020-01-27 17:59:46 +03:00
parent a68a6b77df
commit 9ae3b2cf43
3 changed files with 7 additions and 5 deletions
@@ -175,9 +175,10 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
try { try {
for (gradleUserHome in gradleUserDirs) { for (gradleUserHome in gradleUserDirs) {
BuildSessionLogger.listProfileFiles(File(gradleUserHome, STATISTICS_FOLDER_NAME))?.forEach { statisticFile -> BuildSessionLogger.listProfileFiles(File(gradleUserHome, STATISTICS_FOLDER_NAME))?.forEach { statisticFile ->
var fileWasRead = true
try { try {
var previousEvent: MetricsContainer? = null var previousEvent: MetricsContainer? = null
MetricsContainer.readFromFile(statisticFile) { metricContainer -> fileWasRead = MetricsContainer.readFromFile(statisticFile) { metricContainer ->
processMetricsContainer(metricContainer, previousEvent) processMetricsContainer(metricContainer, previousEvent)
previousEvent = metricContainer previousEvent = metricContainer
} }
@@ -185,7 +186,7 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
Logger.getInstance(KotlinFUSLogger::class.java) Logger.getInstance(KotlinFUSLogger::class.java)
.info("Failed to process file ${statisticFile.absolutePath}: ${e.message}", e) .info("Failed to process file ${statisticFile.absolutePath}: ${e.message}", e)
} finally { } finally {
if (!statisticFile.delete()) { if (fileWasRead && !statisticFile.delete()) {
Logger.getInstance(KotlinFUSLogger::class.java) Logger.getInstance(KotlinFUSLogger::class.java)
.warn("[FUS] Failed to delete file ${statisticFile.absolutePath}") .warn("[FUS] Failed to delete file ${statisticFile.absolutePath}")
} }
@@ -24,7 +24,7 @@ class FileRecordLogger(file: File) : IRecordLogger {
init { init {
lock = try { lock = try {
channel.lock() channel.tryLock() ?: throw IOException("Could not acquire an exclusive lock of file ${file.name}")
} catch (e: Exception) { } catch (e: Exception) {
channel.close() channel.close()
// wrap in order to unify with FileOverlappingException // wrap in order to unify with FileOverlappingException
@@ -43,9 +43,9 @@ class MetricsContainer : IStatisticsValuesConsumer {
private val numericalMetricsMap = NumericalMetrics.values().associateBy(NumericalMetrics::name) private val numericalMetricsMap = NumericalMetrics.values().associateBy(NumericalMetrics::name)
fun readFromFile(file: File, consumer: (MetricsContainer) -> Unit) { fun readFromFile(file: File, consumer: (MetricsContainer) -> Unit): Boolean {
val channel = FileChannel.open(Paths.get(file.toURI()), StandardOpenOption.WRITE, StandardOpenOption.READ) val channel = FileChannel.open(Paths.get(file.toURI()), StandardOpenOption.WRITE, StandardOpenOption.READ)
channel.lock() channel.tryLock() ?: return false
val inputStream = Channels.newInputStream(channel) val inputStream = Channels.newInputStream(channel)
try { try {
@@ -86,6 +86,7 @@ class MetricsContainer : IStatisticsValuesConsumer {
} finally { } finally {
channel.close() channel.close()
} }
return true
} }
} }