Skip to content

Commit 5f2866f

Browse files
coeuvrecopybara-github
authored andcommitted
Do the AC integrity check for disk part of the combined cache.
This will decrease the cache-hit rate for disk cache when building without the bytes. See bazelbuild#17080 for reasoning. With lease service, the decrement will be fixed. Fixes bazelbuild#17080. Closes bazelbuild#17201. PiperOrigin-RevId: 502818641 Change-Id: I1f73dd38d7e52e2f39b367e6114aab714de22d3c
1 parent 928e0fe commit 5f2866f

File tree

4 files changed

+141
-30
lines changed

4 files changed

+141
-30
lines changed

src/main/java/com/google/devtools/build/lib/remote/disk/DiskAndRemoteCacheClient.java

+41-19
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,53 @@ public ListenableFuture<Void> downloadBlob(
183183
}
184184
}
185185

186+
private ListenableFuture<CachedActionResult> downloadActionResultFromRemote(
187+
RemoteActionExecutionContext context, ActionKey actionKey, boolean inlineOutErr) {
188+
return Futures.transformAsync(
189+
remoteCache.downloadActionResult(context, actionKey, inlineOutErr),
190+
(cachedActionResult) -> {
191+
if (cachedActionResult == null) {
192+
return immediateFuture(null);
193+
} else {
194+
return Futures.transform(
195+
diskCache.uploadActionResult(context, actionKey, cachedActionResult.actionResult()),
196+
v -> cachedActionResult,
197+
directExecutor());
198+
}
199+
},
200+
directExecutor());
201+
}
202+
186203
@Override
187204
public ListenableFuture<CachedActionResult> downloadActionResult(
188205
RemoteActionExecutionContext context, ActionKey actionKey, boolean inlineOutErr) {
189-
if (context.getReadCachePolicy().allowDiskCache()
190-
&& diskCache.containsActionResult(actionKey)) {
191-
return diskCache.downloadActionResult(context, actionKey, inlineOutErr);
206+
ListenableFuture<CachedActionResult> future = immediateFuture(null);
207+
208+
if (context.getReadCachePolicy().allowDiskCache()) {
209+
// If Build without the Bytes is enabled, the future will likely return null
210+
// and fallback to remote cache because AC integrity check is enabled and referenced blobs are
211+
// probably missing from disk cache due to BwoB.
212+
//
213+
// TODO(chiwang): With lease service, instead of doing the integrity check against local
214+
// filesystem, we can check whether referenced blobs are alive in the lease service to
215+
// increase the cache-hit rate for disk cache.
216+
future = diskCache.downloadActionResult(context, actionKey, inlineOutErr);
192217
}
193218

194219
if (context.getReadCachePolicy().allowRemoteCache()) {
195-
return Futures.transformAsync(
196-
remoteCache.downloadActionResult(context, actionKey, inlineOutErr),
197-
(cachedActionResult) -> {
198-
if (cachedActionResult == null) {
199-
return immediateFuture(null);
200-
} else {
201-
return Futures.transform(
202-
diskCache.uploadActionResult(
203-
context, actionKey, cachedActionResult.actionResult()),
204-
v -> cachedActionResult,
205-
directExecutor());
206-
}
207-
},
208-
directExecutor());
209-
} else {
210-
return immediateFuture(null);
220+
future =
221+
Futures.transformAsync(
222+
future,
223+
(result) -> {
224+
if (result == null) {
225+
return downloadActionResultFromRemote(context, actionKey, inlineOutErr);
226+
} else {
227+
return immediateFuture(result);
228+
}
229+
},
230+
directExecutor());
211231
}
232+
233+
return future;
212234
}
213235
}

src/main/java/com/google/devtools/build/lib/remote/disk/DiskCacheClient.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ public boolean contains(Digest digest) {
6868
return toPath(digest.getHash(), /* actionResult= */ false).exists();
6969
}
7070

71-
/** Returns {@code true} if the provided {@code key} is stored in the Action Cache. */
72-
public boolean containsActionResult(ActionKey actionKey) {
73-
return toPath(actionKey.getDigest().getHash(), /* actionResult= */ true).exists();
74-
}
75-
7671
public void captureFile(Path src, Digest digest, boolean isActionCache) throws IOException {
7772
Path target = toPath(digest.getHash(), isActionCache);
7873
target.getParentDirectory().createDirectoryAndParents();
@@ -161,7 +156,11 @@ public ListenableFuture<CachedActionResult> downloadActionResult(
161156
}
162157

163158
if (checkActionResult) {
164-
checkActionResult(actionResult);
159+
try {
160+
checkActionResult(actionResult);
161+
} catch (CacheNotFoundException e) {
162+
return Futures.immediateFuture(null);
163+
}
165164
}
166165

167166
return Futures.immediateFuture(CachedActionResult.disk(actionResult));

src/test/java/com/google/devtools/build/lib/remote/BUILD

+5
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,20 @@ java_test(
171171
java_test(
172172
name = "DiskCacheIntegrationTest",
173173
srcs = ["DiskCacheIntegrationTest.java"],
174+
runtime_deps = [
175+
"//third_party/grpc-java:grpc-jar",
176+
],
174177
deps = [
175178
"//src/main/java/com/google/devtools/build/lib:runtime",
176179
"//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper:credential_module",
177180
"//src/main/java/com/google/devtools/build/lib/remote",
178181
"//src/main/java/com/google/devtools/build/lib/standalone",
179182
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
180183
"//src/test/java/com/google/devtools/build/lib/buildtool/util",
184+
"//src/test/java/com/google/devtools/build/lib/remote/util:integration_test_utils",
181185
"//src/test/java/com/google/devtools/build/lib/testutil:TestUtils",
182186
"//third_party:guava",
183187
"//third_party:junit4",
188+
"//third_party:truth",
184189
],
185190
)

src/test/java/com/google/devtools/build/lib/remote/DiskCacheIntegrationTest.java

+90-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.remote;
1515

16+
import static com.google.common.truth.Truth.assertThat;
1617
import static com.google.devtools.build.lib.testutil.TestUtils.tmpDirFile;
1718

1819
import com.google.common.collect.ImmutableList;
1920
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialModule;
2021
import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase;
22+
import com.google.devtools.build.lib.remote.util.IntegrationTestUtils;
23+
import com.google.devtools.build.lib.remote.util.IntegrationTestUtils.WorkerInstance;
2124
import com.google.devtools.build.lib.runtime.BlazeModule;
2225
import com.google.devtools.build.lib.runtime.BlazeRuntime;
2326
import com.google.devtools.build.lib.runtime.BlockWaitingModule;
@@ -32,6 +35,25 @@
3235

3336
@RunWith(JUnit4.class)
3437
public class DiskCacheIntegrationTest extends BuildIntegrationTestCase {
38+
private WorkerInstance worker;
39+
40+
private void startWorker() throws Exception {
41+
if (worker == null) {
42+
worker = IntegrationTestUtils.startWorker();
43+
}
44+
}
45+
46+
private void enableRemoteExec(String... additionalOptions) {
47+
assertThat(worker).isNotNull();
48+
addOptions("--remote_executor=grpc://localhost:" + worker.getPort());
49+
addOptions(additionalOptions);
50+
}
51+
52+
private void enableRemoteCache(String... additionalOptions) {
53+
assertThat(worker).isNotNull();
54+
addOptions("--remote_cache=grpc://localhost:" + worker.getPort());
55+
addOptions(additionalOptions);
56+
}
3557

3658
private static PathFragment getDiskCacheDir() {
3759
PathFragment testTmpDir = PathFragment.create(tmpDirFile().getAbsolutePath());
@@ -48,6 +70,10 @@ protected void setupOptions() throws Exception {
4870
@After
4971
public void tearDown() throws IOException {
5072
getWorkspace().getFileSystem().getPath(getDiskCacheDir()).deleteTree();
73+
74+
if (worker != null) {
75+
worker.stop();
76+
}
5177
}
5278

5379
@Override
@@ -93,7 +119,7 @@ public void hitDiskCache() throws Exception {
93119
events.assertContainsInfo("2 disk cache hit");
94120
}
95121

96-
private void blobsReferencedInAAreMissingCasIgnoresAc(String... additionalOptions)
122+
private void doBlobsReferencedInAcAreMissingFromCasIgnoresAc(String... additionalOptions)
97123
throws Exception {
98124
// Arrange: Prepare the workspace and populate disk cache
99125
write(
@@ -126,13 +152,72 @@ private void blobsReferencedInAAreMissingCasIgnoresAc(String... additionalOption
126152
}
127153

128154
@Test
129-
public void blobsReferencedInAcAreMissingCas_ignoresAc() throws Exception {
130-
blobsReferencedInAAreMissingCasIgnoresAc();
155+
public void blobsReferencedInAcAreMissingFromCas_ignoresAc() throws Exception {
156+
doBlobsReferencedInAcAreMissingFromCasIgnoresAc();
157+
}
158+
159+
@Test
160+
public void bwob_blobsReferencedInAcAreMissingFromCas_ignoresAc() throws Exception {
161+
doBlobsReferencedInAcAreMissingFromCasIgnoresAc("--remote_download_minimal");
131162
}
132163

133164
@Test
134-
public void bwob_blobsReferencedInAcAreMissingCas_ignoresAc() throws Exception {
135-
blobsReferencedInAAreMissingCasIgnoresAc("--remote_download_minimal");
165+
public void bwobAndRemoteExec_blobsReferencedInAcAreMissingFromCas_ignoresAc() throws Exception {
166+
startWorker();
167+
enableRemoteExec("--remote_download_minimal");
168+
doBlobsReferencedInAcAreMissingFromCasIgnoresAc();
169+
}
170+
171+
@Test
172+
public void bwobAndRemoteCache_blobsReferencedInAcAreMissingFromCas_ignoresAc() throws Exception {
173+
startWorker();
174+
enableRemoteCache("--remote_download_minimal");
175+
doBlobsReferencedInAcAreMissingFromCasIgnoresAc();
176+
}
177+
178+
private void doRemoteExecWithDiskCache(String... additionalOptions) throws Exception {
179+
// Arrange: Prepare the workspace and populate disk cache
180+
startWorker();
181+
enableRemoteExec(additionalOptions);
182+
write(
183+
"BUILD",
184+
"genrule(",
185+
" name = 'foo',",
186+
" srcs = ['foo.in'],",
187+
" outs = ['foo.out'],",
188+
" cmd = 'cat $(SRCS) > $@',",
189+
")",
190+
"genrule(",
191+
" name = 'foobar',",
192+
" srcs = [':foo.out', 'bar.in'],",
193+
" outs = ['foobar.out'],",
194+
" cmd = 'cat $(SRCS) > $@',",
195+
")");
196+
write("foo.in", "foo");
197+
write("bar.in", "bar");
198+
buildTarget("//:foobar");
199+
cleanAndRestartServer();
200+
201+
// Act: Do a clean build
202+
enableRemoteExec("--remote_download_minimal");
203+
buildTarget("//:foobar");
204+
}
205+
206+
@Test
207+
public void remoteExecWithDiskCache_hitDiskCache() throws Exception {
208+
doRemoteExecWithDiskCache();
209+
210+
// Assert: Should hit the disk cache
211+
events.assertContainsInfo("2 disk cache hit");
212+
}
213+
214+
@Test
215+
public void bwob_remoteExecWithDiskCache_hitRemoteCache() throws Exception {
216+
doRemoteExecWithDiskCache("--remote_download_minimal");
217+
218+
// Assert: Should hit the remote cache because blobs referenced by the AC are missing from disk
219+
// cache due to BwoB.
220+
events.assertContainsInfo("2 remote cache hit");
136221
}
137222

138223
private void cleanAndRestartServer() throws Exception {

0 commit comments

Comments
 (0)