-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ConfigurationCacheReport.kt
330 lines (288 loc) · 11.1 KB
/
ConfigurationCacheReport.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.configurationcache.problems
import org.apache.groovy.json.internal.CharBuf
import org.gradle.api.internal.file.temp.TemporaryFileProvider
import org.gradle.configurationcache.logger
import org.gradle.internal.buildoption.InternalFlag
import org.gradle.internal.buildoption.InternalOptions
import org.gradle.internal.concurrent.ExecutorFactory
import org.gradle.internal.concurrent.ManagedExecutor
import org.gradle.internal.hash.HashCode
import org.gradle.internal.hash.Hashing
import org.gradle.internal.hash.HashingOutputStream
import org.gradle.internal.problems.failure.Failure
import org.gradle.internal.service.scopes.Scope
import org.gradle.internal.service.scopes.ServiceScope
import java.io.Closeable
import java.io.File
import java.nio.file.Files
import java.util.concurrent.Callable
import java.util.concurrent.TimeUnit
import kotlin.contracts.contract
@ServiceScope(Scope.BuildTree::class)
class ConfigurationCacheReport(
executorFactory: ExecutorFactory,
temporaryFileProvider: TemporaryFileProvider,
internalOptions: InternalOptions
) : Closeable {
companion object {
private
val stacktraceHashes = InternalFlag("org.gradle.configuration-cache.internal.report.stacktrace-hashes", false)
}
private
sealed class State {
open fun onDiagnostic(kind: DiagnosticKind, problem: PropertyProblem): State =
illegalState()
/**
* Writes the report file to the given [outputDirectory] if and only if
* there are diagnostics to report.
*
* @return a pair with the new [State] and the written [File], which will be `null` when there are no diagnostics.
*/
open fun commitReportTo(
outputDirectory: File,
buildDisplayName: String?,
cacheAction: String,
requestedTasks: String?,
totalProblemCount: Int
): Pair<State, File?> =
illegalState()
open fun close(): State =
illegalState()
private
fun illegalState(): Nothing =
throw IllegalStateException("Operation is not valid in ${javaClass.simpleName} state.")
class Idle(
private val onFirstDiagnostic: (kind: DiagnosticKind, problem: PropertyProblem) -> State
) : State() {
/**
* There's nothing to write, return null.
*/
override fun commitReportTo(
outputDirectory: File,
buildDisplayName: String?,
cacheAction: String,
requestedTasks: String?,
totalProblemCount: Int
): Pair<State, File?> =
this to null
override fun onDiagnostic(kind: DiagnosticKind, problem: PropertyProblem): State =
onFirstDiagnostic(kind, problem)
override fun close(): State =
this
}
class Spooling(
val spoolFile: File,
val executor: ManagedExecutor,
/**
* [JsonModelWriter] uses Groovy's [CharBuf] for fast json encoding.
*/
val groovyJsonClassLoader: ClassLoader,
val decorate: (PropertyProblem, ProblemSeverity) -> DecoratedPropertyProblem
) : State() {
private
val hashingStream = HashingOutputStream(Hashing.md5(), spoolFile.outputStream().buffered())
private
val writer = HtmlReportWriter(hashingStream.writer())
init {
executor.submit {
Thread.currentThread().contextClassLoader = groovyJsonClassLoader
writer.beginHtmlReport()
}
}
override fun onDiagnostic(kind: DiagnosticKind, problem: PropertyProblem): State {
executor.submit {
val severity = if (kind == DiagnosticKind.PROBLEM) ProblemSeverity.Failure else ProblemSeverity.Info
writer.writeDiagnostic(kind, decorate(problem, severity))
}
return this
}
override fun commitReportTo(
outputDirectory: File,
buildDisplayName: String?,
cacheAction: String,
requestedTasks: String?,
totalProblemCount: Int
): Pair<State, File?> {
val reportFile = try {
executor
.submit(Callable {
closeHtmlReport(buildDisplayName, cacheAction, requestedTasks, totalProblemCount)
moveSpoolFileTo(outputDirectory)
})
.get(30, TimeUnit.SECONDS)
} finally {
executor.shutdownAndAwaitTermination()
}
return Closed to reportFile
}
override fun close(): State {
if (executor.isShutdown) {
writer.close()
} else {
executor.submit {
writer.close()
}
executor.shutdown()
}
return Closed
}
private
fun closeHtmlReport(buildDisplayName: String?, cacheAction: String, requestedTasks: String?, totalProblemCount: Int) {
writer.endHtmlReport(buildDisplayName, cacheAction, requestedTasks, totalProblemCount)
writer.close()
}
private
fun ManagedExecutor.shutdownAndAwaitTermination() {
shutdown()
if (!awaitTermination(1, TimeUnit.SECONDS)) {
val unfinishedTasks = shutdownNow()
logger.warn(
"Configuration cache report is taking too long to write... "
+ "The build might finish before the report has been completely written."
)
logger.info("Unfinished tasks: {}", unfinishedTasks)
}
}
private
fun moveSpoolFileTo(outputDirectory: File): File {
val reportDir = outputDirectory.resolve(reportHash())
val reportFile = reportDir.resolve(HtmlReportTemplate.reportHtmlFileName)
if (!reportFile.exists()) {
require(reportDir.mkdirs()) {
"Could not create configuration cache report directory '$reportDir'"
}
Files.move(spoolFile.toPath(), reportFile.toPath())
}
return reportFile
}
private
fun reportHash() =
hashingStream.hash().toCompactString()
}
object Closed : State() {
override fun close(): State = this
}
}
private
val isStacktraceHashes = internalOptions.getOption(stacktraceHashes).get()
private
var state: State = State.Idle { kind, problem ->
State.Spooling(
temporaryFileProvider.createTemporaryFile("configuration-cache-report", "html"),
executorFactory.create("Configuration cache report writer", 1),
CharBuf::class.java.classLoader,
::decorateProblem
).onDiagnostic(kind, problem)
}
private
val stateLock = Object()
private
val failureDecorator = FailureDecorator()
private
fun decorateProblem(problem: PropertyProblem, severity: ProblemSeverity): DecoratedPropertyProblem {
val failure = problem.stackTracingFailure
return DecoratedPropertyProblem(
problem.trace,
decorateMessage(problem, failure),
decoratedFailureFor(failure, severity),
problem.documentationSection
)
}
private
fun decoratedFailureFor(failure: Failure?, severity: ProblemSeverity): DecoratedFailure? {
return when {
failure != null -> failureDecorator.decorate(failure)
severity == ProblemSeverity.Failure -> DecoratedFailure.MARKER
else -> null
}
}
private
fun decorateMessage(problem: PropertyProblem, failure: Failure?): StructuredMessage {
if (!isStacktraceHashes || failure == null) {
return problem.message
}
val failureHash = failure.hashWithoutMessages()
return StructuredMessage.build {
reference("[${failureHash.toCompactString()}]")
text(" ")
message(problem.message)
}
}
override fun close() {
modifyState {
close()
}
}
fun onProblem(problem: PropertyProblem) {
modifyState {
onDiagnostic(DiagnosticKind.PROBLEM, problem)
}
}
fun onInput(input: PropertyProblem) {
modifyState {
onDiagnostic(DiagnosticKind.INPUT, input)
}
}
/**
* Writes the report file to [outputDirectory].
*
* The file is laid out in such a way as to allow extracting the pure JSON model,
* see [HtmlReportWriter].
*/
internal
fun writeReportFileTo(outputDirectory: File, buildDisplayName: String?, cacheAction: String, requestedTasks: String?, totalProblemCount: Int): File? {
var reportFile: File?
modifyState {
val (newState, outputFile) = commitReportTo(outputDirectory, buildDisplayName, cacheAction, requestedTasks, totalProblemCount)
reportFile = outputFile
newState
}
return reportFile
}
private
inline fun modifyState(f: State.() -> State) {
contract {
callsInPlace(f, kotlin.contracts.InvocationKind.EXACTLY_ONCE)
}
synchronized(stateLock) {
state = state.f()
}
}
/**
* A heuristic to get the same hash for different instances of an exception
* occurring at the same location.
*/
private
fun Failure.hashWithoutMessages(): HashCode {
val root = this@hashWithoutMessages
val hasher = Hashing.newHasher()
for (failure in sequence { visitFailures(root) }) {
hasher.putString(failure.exceptionType.name)
for (element in failure.stackTrace) {
hasher.putString(element.toString())
}
}
return hasher.hash()
}
private
suspend fun SequenceScope<Failure>.visitFailures(failure: Failure) {
yield(failure)
failure.suppressed.forEach { visitFailures(it) }
failure.causes.forEach { visitFailures(it) }
}
}