-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ConfigurationCacheProblems.kt
294 lines (244 loc) · 11.1 KB
/
ConfigurationCacheProblems.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
/*
* 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 com.google.common.collect.Sets.newConcurrentHashSet
import org.gradle.api.initialization.Settings
import org.gradle.api.internal.SettingsInternal
import org.gradle.api.logging.Logging
import org.gradle.configurationcache.ConfigurationCacheAction
import org.gradle.configurationcache.ConfigurationCacheAction.LOAD
import org.gradle.configurationcache.ConfigurationCacheAction.STORE
import org.gradle.configurationcache.ConfigurationCacheAction.UPDATE
import org.gradle.configurationcache.ConfigurationCacheKey
import org.gradle.configurationcache.ConfigurationCacheProblemsException
import org.gradle.configurationcache.TooManyConfigurationCacheProblemsException
import org.gradle.configurationcache.initialization.ConfigurationCacheStartParameter
import org.gradle.initialization.RootBuildLifecycleListener
import org.gradle.internal.InternalBuildAdapter
import org.gradle.internal.event.ListenerManager
import org.gradle.internal.problems.failure.FailureFactory
import org.gradle.internal.service.scopes.Scope
import org.gradle.internal.service.scopes.ServiceScope
import org.gradle.problems.buildtree.ProblemReporter
import org.gradle.problems.buildtree.ProblemReporter.ProblemConsumer
import java.io.File
@ServiceScope(Scope.BuildTree::class)
internal
class ConfigurationCacheProblems(
private
val startParameter: ConfigurationCacheStartParameter,
private
val report: ConfigurationCacheReport,
private
val cacheKey: ConfigurationCacheKey,
private
val listenerManager: ListenerManager,
private
val failureFactory: FailureFactory
) : ProblemsListener, ProblemReporter, AutoCloseable {
private
val summarizer = ConfigurationCacheProblemsSummary()
private
val buildNameHandler = BuildNameHandler()
private
val postBuildHandler = PostBuildProblemsHandler()
private
var buildName: String? = null
private
var isFailOnProblems = startParameter.failOnProblems
private
var isFailingBuildDueToSerializationError = false
private
var reusedProjects = 0
private
var updatedProjects = 0
private
var incompatibleTasks = newConcurrentHashSet<String>()
private
lateinit var cacheAction: ConfigurationCacheAction
val shouldDiscardEntry: Boolean
get() {
if (cacheAction == LOAD) {
return false
}
if (isFailingBuildDueToSerializationError) {
return true
}
val summary = summarizer.get()
return discardStateDueToProblems(summary) || hasTooManyProblems(summary)
}
init {
listenerManager.addListener(buildNameHandler)
listenerManager.addListener(postBuildHandler)
}
override fun close() {
listenerManager.removeListener(buildNameHandler)
listenerManager.removeListener(postBuildHandler)
}
fun action(action: ConfigurationCacheAction) {
cacheAction = action
}
fun failingBuildDueToSerializationError() {
isFailingBuildDueToSerializationError = true
isFailOnProblems = false
}
fun projectStateStats(reusedProjects: Int, updatedProjects: Int) {
this.reusedProjects = reusedProjects
this.updatedProjects = updatedProjects
}
override fun forIncompatibleTask(path: String): ProblemsListener {
incompatibleTasks.add(path)
return object : ProblemsListener {
override fun onProblem(problem: PropertyProblem) {
onProblem(problem, ProblemSeverity.Suppressed)
}
override fun onError(trace: PropertyTrace, error: Exception, message: StructuredMessageBuilder) {
val failure = failureFactory.create(error)
onProblem(PropertyProblem(trace, StructuredMessage.build(message), error, failure))
}
}
}
override fun onProblem(problem: PropertyProblem) {
onProblem(problem, ProblemSeverity.Failure)
}
private
fun onProblem(problem: PropertyProblem, severity: ProblemSeverity) {
if (summarizer.onProblem(problem, severity)) {
report.onProblem(problem)
}
}
override fun getId(): String {
return "configuration-cache"
}
fun queryFailure(summary: Summary = summarizer.get(), htmlReportFile: File? = null): Throwable? {
val failDueToProblems = summary.failureCount > 0 && isFailOnProblems
val hasTooManyProblems = hasTooManyProblems(summary)
val summaryText = { summary.textForConsole(cacheAction.summaryText(), htmlReportFile) }
return when {
// TODO - always include this as a build failure;
// currently it is disabled when a serialization problem happens
failDueToProblems -> {
ConfigurationCacheProblemsException(summary.causes, summaryText)
}
hasTooManyProblems -> {
TooManyConfigurationCacheProblemsException(summary.causes, summaryText)
}
else -> null
}
}
/**
* Writes the report to the given [reportDir] if any [diagnostics][DiagnosticKind] have
* been reported in which case a warning is also logged with the location of the report.
*/
override fun report(reportDir: File, validationFailures: ProblemConsumer) {
val summary = summarizer.get()
val hasNoProblems = summary.problemCount == 0
val outputDirectory = outputDirectoryFor(reportDir)
val cacheActionText = cacheAction.summaryText()
val requestedTasks = startParameter.requestedTasksOrDefault()
val buildDisplayName = buildName
val htmlReportFile = report.writeReportFileTo(outputDirectory, buildDisplayName, cacheActionText, requestedTasks, summary.problemCount)
if (htmlReportFile == null) {
// there was nothing to report (no problems, no build configuration inputs)
require(hasNoProblems)
return
}
when (val failure = queryFailure(summary, htmlReportFile)) {
null -> {
val logReportAsInfo = hasNoProblems && !startParameter.alwaysLogReportLinkAsWarning
val log: (String) -> Unit = if (logReportAsInfo) logger::info else logger::warn
log(summary.textForConsole(cacheActionText, htmlReportFile))
}
else -> validationFailures.accept(failure)
}
}
private
fun ConfigurationCacheAction.summaryText() =
when (this) {
LOAD -> "reusing"
STORE -> "storing"
UPDATE -> "updating"
}
private
fun ConfigurationCacheStartParameter.requestedTasksOrDefault() =
requestedTaskNames.takeIf { it.isNotEmpty() }?.joinToString(" ")
private
fun outputDirectoryFor(buildDir: File): File =
buildDir.resolve("reports/configuration-cache/$cacheKey")
private
inner class BuildNameHandler : InternalBuildAdapter() {
override fun settingsEvaluated(settings: Settings) {
if ((settings as SettingsInternal).gradle.isRootBuild) {
buildName = settings.rootProject.name
}
}
}
private
inner class PostBuildProblemsHandler : RootBuildLifecycleListener {
override fun afterStart() = Unit
override fun beforeComplete() {
val summary = summarizer.get()
val problemCount = summary.problemCount
val hasProblems = problemCount > 0
val discardStateDueToProblems = discardStateDueToProblems(summary)
val hasTooManyProblems = hasTooManyProblems(summary)
val problemCountString = problemCount.counter("problem")
val reusedProjectsString = reusedProjects.counter("project")
val updatedProjectsString = updatedProjects.counter("project")
when {
isFailingBuildDueToSerializationError && !hasProblems -> log("Configuration cache entry discarded due to serialization error.")
isFailingBuildDueToSerializationError -> log("Configuration cache entry discarded with {}.", problemCountString)
cacheAction == STORE && discardStateDueToProblems && !hasProblems -> log("Configuration cache entry discarded${incompatibleTasksSummary()}")
cacheAction == STORE && discardStateDueToProblems -> log("Configuration cache entry discarded with {}.", problemCountString)
cacheAction == STORE && hasTooManyProblems -> log("Configuration cache entry discarded with too many problems ({}).", problemCountString)
cacheAction == STORE && !hasProblems -> log("Configuration cache entry stored.")
cacheAction == STORE -> log("Configuration cache entry stored with {}.", problemCountString)
cacheAction == UPDATE && !hasProblems -> log("Configuration cache entry updated for {}, {} up-to-date.", updatedProjectsString, reusedProjectsString)
cacheAction == UPDATE -> log("Configuration cache entry updated for {} with {}, {} up-to-date.", updatedProjectsString, problemCountString, reusedProjectsString)
cacheAction == LOAD && !hasProblems -> log("Configuration cache entry reused.")
cacheAction == LOAD -> log("Configuration cache entry reused with {}.", problemCountString)
hasTooManyProblems -> log("Too many configuration cache problems found ({}).", problemCountString)
hasProblems -> log("Configuration cache problems found ({}).", problemCountString)
// else not storing or loading and no problems to report
}
}
}
private
fun incompatibleTasksSummary() = when {
incompatibleTasks.isNotEmpty() -> " because incompatible ${if (incompatibleTasks.size > 1) "tasks were" else "task was"} found: ${incompatibleTasks.joinToString(", ") { "'$it'" }}."
else -> "."
}
private
fun discardStateDueToProblems(summary: Summary) =
(summary.problemCount > 0 || incompatibleTasks.isNotEmpty()) && isFailOnProblems
private
fun hasTooManyProblems(summary: Summary) =
summary.nonSuppressedProblemCount > startParameter.maxProblems
private
fun log(msg: String, vararg args: Any = emptyArray()) {
logger.warn(msg, *args)
}
private
val logger = Logging.getLogger(ConfigurationCacheProblems::class.java)
private
fun Int.counter(singular: String, plural: String = "${singular}s"): String {
return when (this) {
0 -> "no $plural"
1 -> "1 $singular"
else -> "$this $plural"
}
}
}