-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathBuildGraphHasher.kt
More file actions
187 lines (174 loc) · 6.81 KB
/
BuildGraphHasher.kt
File metadata and controls
187 lines (174 loc) · 6.81 KB
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
package com.bazel_diff.hash
import com.bazel_diff.bazel.BazelClient
import com.bazel_diff.bazel.BazelModService
import com.bazel_diff.bazel.BazelRule
import com.bazel_diff.bazel.BazelSourceFileTarget
import com.bazel_diff.bazel.BazelTarget
import com.bazel_diff.extensions.toHexString
import com.bazel_diff.log.Logger
import com.google.common.collect.Sets
import java.nio.file.Path
import java.util.Calendar
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.atomic.AtomicReference
import java.util.stream.Collectors
import kotlin.io.path.readBytes
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class BuildGraphHasher(private val bazelClient: BazelClient) : KoinComponent {
private val targetHasher: TargetHasher by inject()
private val sourceFileHasher: SourceFileHasher by inject()
private val bazelModService: BazelModService by inject()
private val logger: Logger by inject()
fun hashAllBazelTargetsAndSourcefiles(
seedFilepaths: Set<Path> = emptySet(),
ignoredAttrs: Set<String> = emptySet(),
modifiedFilepaths: Set<Path> = emptySet()
): Map<String, TargetHash> {
val (sourceDigests, allTargets) =
runBlocking {
val targetsTask = async(Dispatchers.IO) { bazelClient.queryAllTargets() }
val allTargets = targetsTask.await()
val sourceTargets =
allTargets
.filter { it is BazelTarget.SourceFile }
.map { it as BazelTarget.SourceFile }
val sourceDigestsFuture =
async(Dispatchers.IO) {
val sourceHashDurationEpoch = Calendar.getInstance().getTimeInMillis()
val sourceFileTargets = hashSourcefiles(sourceTargets, modifiedFilepaths)
val sourceHashDuration =
Calendar.getInstance().getTimeInMillis() - sourceHashDurationEpoch
logger.i { "Source file hashes calculated in $sourceHashDuration" }
sourceFileTargets
}
Pair(sourceDigestsFuture.await(), allTargets)
}
val seedForFilepaths =
runBlocking(Dispatchers.IO) { createSeedForFilepaths(seedFilepaths) }
return hashAllTargets(
seedForFilepaths, sourceDigests, allTargets, ignoredAttrs, modifiedFilepaths)
}
private fun hashSourcefiles(
targets: List<BazelTarget.SourceFile>,
modifiedFilepaths: Set<Path>
): ConcurrentMap<String, ByteArray> {
val exception = AtomicReference<Exception?>(null)
val result: ConcurrentMap<String, ByteArray> =
targets
.parallelStream()
.map { sourceFile: BazelTarget.SourceFile ->
val seed = sha256 {
safePutBytes(sourceFile.name.toByteArray())
for (subinclude in sourceFile.subincludeList) {
safePutBytes(subinclude.toByteArray())
}
}
try {
val sourceFileTarget = BazelSourceFileTarget(sourceFile.name, seed)
Pair(
sourceFileTarget.name,
sourceFileHasher.digest(sourceFileTarget, modifiedFilepaths))
} catch (e: Exception) {
exception.set(e)
null
}
}
.filter { pair -> pair != null }
.collect(
Collectors.toConcurrentMap(
{ pair -> pair!!.first },
{ pair -> pair!!.second },
))
exception.get()?.let { throw it }
return result
}
private fun hashAllTargets(
seedHash: ByteArray,
sourceDigests: ConcurrentMap<String, ByteArray>,
allTargets: List<BazelTarget>,
ignoredAttrs: Set<String>,
modifiedFilepaths: Set<Path>
): Map<String, TargetHash> {
val ruleHashes: ConcurrentMap<String, TargetDigest> = ConcurrentHashMap()
val targetToRule: MutableMap<String, BazelRule> = HashMap()
traverseGraph(allTargets, targetToRule)
return allTargets
.parallelStream()
.map { target: BazelTarget ->
val targetDigest =
targetHasher.digest(
target,
targetToRule,
sourceDigests,
ruleHashes,
seedHash,
ignoredAttrs,
modifiedFilepaths)
Pair(
target.name,
TargetHash(
target.javaClass.name.substringAfterLast('$'),
targetDigest.overallDigest.toHexString(),
targetDigest.directDigest.toHexString(),
targetDigest.deps,
))
}
.filter { targetEntry: Pair<String, TargetHash>? -> targetEntry != null }
.collect(
Collectors.toMap(
{ obj: Pair<String, TargetHash> -> obj.first },
{ obj: Pair<String, TargetHash> -> obj.second },
))
}
/** Traverses the list of targets and revisits the targets with yet-unknown generating rule */
private fun traverseGraph(
allTargets: List<BazelTarget>,
targetToRule: MutableMap<String, BazelRule>
) {
var targetsToAnalyse: Set<BazelTarget> = Sets.newHashSet(allTargets)
while (!targetsToAnalyse.isEmpty()) {
val initialSize = targetsToAnalyse.size
val nextTargets: MutableSet<BazelTarget> = Sets.newHashSet()
for (target in targetsToAnalyse) {
val targetName = target.name
when (target) {
is BazelTarget.GeneratedFile -> {
targetToRule[target.generatingRuleName]?.let { targetToRule[targetName] = it }
?: nextTargets.add(target)
}
is BazelTarget.Rule -> targetToRule[targetName] = target.rule
is BazelTarget.SourceFile -> continue
}
}
val newSize = nextTargets.size
if (newSize >= initialSize) {
throw RuntimeException("Not possible to traverse the build graph")
}
targetsToAnalyse = nextTargets
}
}
private suspend fun createSeedForFilepaths(seedFilepaths: Set<Path>): ByteArray {
// Include MODULE.bazel dependency graph in hash
// This ensures that module version changes (e.g., abseil-cpp 20240116.2 -> 20240722.0)
// are detected and cascade to all dependent targets
val moduleGraph = bazelModService.getModuleGraph()
if (moduleGraph != null) {
logger.i { "Including module graph in seed hash (${moduleGraph.length} bytes)" }
}
return sha256 {
// Include seed filepaths in hash
for (path in seedFilepaths) {
putBytes(path.readBytes())
}
// Include module graph if available
if (moduleGraph != null) {
putBytes(moduleGraph.toByteArray())
}
}
}
}