-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.4.0] Use disk cache for HTTP cache server. (#23556)
So that we can have proper cache eviction integration tests for HTTP cache later. PiperOrigin-RevId: 672501366 Change-Id: I52048a4498601cfa98cc9695d5ddddc5b93f56b9
- Loading branch information
Showing
11 changed files
with
200 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...e/src/main/java/com/google/devtools/build/remote/worker/OnDiskHttpCacheServerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2024 The Bazel Authors. All rights reserved. | ||
// | ||
// 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 com.google.devtools.build.remote.worker; | ||
|
||
import build.bazel.remote.execution.v2.Digest; | ||
import com.google.common.io.ByteStreams; | ||
import com.google.devtools.build.lib.remote.Store; | ||
import com.google.devtools.build.lib.remote.util.DigestUtil; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.devtools.build.remote.worker.http.AbstractHttpCacheServerHandler; | ||
import io.netty.channel.ChannelHandler.Sharable; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import javax.annotation.Nullable; | ||
|
||
/** A simple HTTP REST disk cache used during test. */ | ||
@Sharable | ||
public class OnDiskHttpCacheServerHandler extends AbstractHttpCacheServerHandler { | ||
private final OnDiskBlobStoreCache cache; | ||
|
||
public OnDiskHttpCacheServerHandler(OnDiskBlobStoreCache cache) { | ||
this.cache = cache; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected byte[] readFromCache(String uri) throws IOException { | ||
var diskCache = cache.getDiskCacheClient(); | ||
Path path; | ||
if (uri.startsWith("/ac/")) { | ||
path = diskCache.toPath(uri.substring("/ac/".length()), Store.AC); | ||
} else if (uri.startsWith("/cas/")) { | ||
path = diskCache.toPath(uri.substring("/cas/".length()), Store.CAS); | ||
} else { | ||
throw new IOException("Invalid uri: " + uri); | ||
} | ||
|
||
try (var out = new ByteArrayOutputStream(); | ||
var in = path.getInputStream()) { | ||
ByteStreams.copy(in, out); | ||
return out.toByteArray(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void writeToCache(String uri, byte[] content) throws IOException { | ||
var diskCache = cache.getDiskCacheClient(); | ||
Digest digest; | ||
Store store; | ||
if (uri.startsWith("/ac/")) { | ||
digest = DigestUtil.buildDigest(uri.substring(4), content.length); | ||
store = Store.AC; | ||
} else if (uri.startsWith("/cas/")) { | ||
digest = DigestUtil.buildDigest(uri.substring(5), content.length); | ||
store = Store.CAS; | ||
} else { | ||
throw new IOException("Invalid uri: " + uri); | ||
} | ||
|
||
diskCache.saveFile(digest, store, new ByteArrayInputStream(content)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ain/java/com/google/devtools/build/remote/worker/http/InMemoryHttpCacheServerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// 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 com.google.devtools.build.remote.worker.http; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import io.netty.channel.ChannelHandler.Sharable; | ||
import java.util.concurrent.ConcurrentMap; | ||
import javax.annotation.Nullable; | ||
|
||
/** A simple HTTP REST in-memory cache used during testing the LRE. */ | ||
@Sharable | ||
public class InMemoryHttpCacheServerHandler extends AbstractHttpCacheServerHandler { | ||
|
||
private final ConcurrentMap<String, byte[]> cache; | ||
|
||
@VisibleForTesting | ||
public InMemoryHttpCacheServerHandler(ConcurrentMap<String, byte[]> cache) { | ||
this.cache = Preconditions.checkNotNull(cache); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected byte[] readFromCache(String uri) { | ||
return cache.get(uri); | ||
} | ||
|
||
@Override | ||
protected void writeToCache(String uri, byte[] content) { | ||
cache.putIfAbsent(uri, content); | ||
} | ||
} |
Oops, something went wrong.