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
|
//===-- LLVMCASCacheProvider.cpp - LLVMCAS Remote Cache Service -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// A RemoteCacheProvider implementation using on-disk \p cas::ObjectStore and
// \p cas::ActionCache. While this is intended for testing purposes, it also
// functions as a reference implementation for a \p RemoteCacheProvider.
//
//===----------------------------------------------------------------------===//
#include "llvm/RemoteCachingService/LLVMCASCacheProvider.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/CAS/ActionCache.h"
#include "llvm/CAS/ObjectStore.h"
#include "llvm/RemoteCachingService/RemoteCacheProvider.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::cas;
using namespace llvm::cas::remote;
static cas::CacheKey cacheKeyFromString(StringRef Data) {
// FIXME: Enhance \p cas::ActionCache to be able to pass data for key, without
// needing to store the key into a \p cas::ObjectStore.
static ExitOnError ExitOnErr("LLVMCASCacheProvider: ");
auto CAS = cas::createInMemoryCAS();
return CAS->getID(ExitOnErr(CAS->storeFromString({}, Data)));
}
namespace {
class LLVMCASCacheProvider final : public RemoteCacheProvider {
ThreadPool Pool{hardware_concurrency()};
std::string TempDir;
std::unique_ptr<cas::ObjectStore> CAS;
std::unique_ptr<cas::ActionCache> ActCache;
public:
void initialize(StringRef TempPath, std::unique_ptr<cas::ObjectStore> CAS,
std::unique_ptr<cas::ActionCache> Cache);
void GetValueAsync(std::string Key,
std::function<void(Expected<std::optional<std::string>>)>
Receiver) override;
void PutValueAsync(std::string Key, std::string Value,
std::function<void(Error)> Receiver) override;
void
CASLoadAsync(std::string CASID, bool WriteToDisk,
std::function<void(Expected<LoadResponse>)> Receiver) override;
void
CASSaveAsync(BlobContents Blob,
std::function<void(Expected<std::string>)> Receiver) override;
void
CASGetAsync(std::string CASID, bool WriteToDisk,
std::function<void(Expected<GetResponse>)> Receiver) override;
void
CASPutAsync(BlobContents Blob, SmallVector<std::string> Refs,
std::function<void(Expected<std::string>)> Receiver) override;
Expected<std::optional<std::string>> GetValue(StringRef Key);
Error PutValue(StringRef Key, StringRef Value);
Expected<LoadResponse> CASLoad(StringRef CASID, bool WriteToDisk);
Expected<std::string> CASSave(const BlobContents &Blob);
Expected<GetResponse> CASGet(StringRef CASID, bool WriteToDisk);
Expected<std::string> CASPut(const BlobContents &Blob,
ArrayRef<std::string> Refs);
};
} // namespace
void LLVMCASCacheProvider::initialize(StringRef TempPath,
std::unique_ptr<cas::ObjectStore> CAS,
std::unique_ptr<cas::ActionCache> Cache) {
TempDir = TempPath.str();
this->CAS = std::move(CAS);
this->ActCache = std::move(Cache);
}
void LLVMCASCacheProvider::GetValueAsync(
std::string Key,
std::function<void(Expected<std::optional<std::string>>)> Receiver) {
Pool.async([this, Key = std::move(Key), Receiver = std::move(Receiver)]() {
Receiver(GetValue(Key));
});
}
void LLVMCASCacheProvider::PutValueAsync(std::string Key, std::string Value,
std::function<void(Error)> Receiver) {
Pool.async(
[this, Key = std::move(Key), Value = std::move(Value),
Receiver = std::move(Receiver)]() { Receiver(PutValue(Key, Value)); });
}
void LLVMCASCacheProvider::CASLoadAsync(
std::string CASID, bool WriteToDisk,
std::function<void(Expected<LoadResponse>)> Receiver) {
Pool.async([this, CASID = std::move(CASID), WriteToDisk,
Receiver = std::move(Receiver)]() {
Receiver(CASLoad(CASID, WriteToDisk));
});
}
void LLVMCASCacheProvider::CASSaveAsync(
BlobContents Blob, std::function<void(Expected<std::string>)> Receiver) {
Pool.async([this, Blob = std::move(Blob), Receiver = std::move(Receiver)]() {
Receiver(CASSave(Blob));
});
}
void LLVMCASCacheProvider::CASGetAsync(
std::string CASID, bool WriteToDisk,
std::function<void(Expected<GetResponse>)> Receiver) {
Pool.async([this, CASID = std::move(CASID), WriteToDisk,
Receiver = std::move(Receiver)]() {
Receiver(CASGet(CASID, WriteToDisk));
});
}
void LLVMCASCacheProvider::CASPutAsync(
BlobContents Blob, SmallVector<std::string> Refs,
std::function<void(Expected<std::string>)> Receiver) {
Pool.async(
[this, Blob = std::move(Blob), Refs = std::move(Refs),
Receiver = std::move(Receiver)]() { Receiver(CASPut(Blob, Refs)); });
}
Expected<std::optional<std::string>>
LLVMCASCacheProvider::GetValue(StringRef RawKey) {
cas::CacheKey Key = cacheKeyFromString(RawKey);
Expected<std::optional<cas::CASID>> ID = ActCache->get(Key);
if (!ID)
return ID.takeError();
if (*ID) {
Expected<cas::ObjectProxy> Obj = CAS->getProxy(**ID);
if (!Obj)
return Obj.takeError();
return Obj->getData().str();
} else {
return std::nullopt;
}
}
Error LLVMCASCacheProvider::PutValue(StringRef RawKey, StringRef Value) {
Expected<cas::ObjectRef> Obj = CAS->storeFromString({}, Value);
if (!Obj)
return Obj.takeError();
cas::CacheKey Key = cacheKeyFromString(RawKey);
return ActCache->put(Key, CAS->getID(*Obj));
}
Expected<RemoteCacheProvider::LoadResponse>
LLVMCASCacheProvider::CASLoad(StringRef CASID, bool WriteToDisk) {
Expected<cas::CASID> ID = CAS->parseID(CASID);
if (!ID)
return ID.takeError();
Expected<cas::ObjectProxy> Obj = CAS->getProxy(*ID);
if (!Obj)
return Obj.takeError();
StringRef BlobData = Obj->getData();
if (WriteToDisk) {
SmallString<128> ModelPath{TempDir};
sys::path::append(ModelPath, "%%%%%%%.blob");
int FD;
SmallString<256> TempPath;
std::error_code EC = sys::fs::createUniqueFile(ModelPath, FD, TempPath);
if (EC)
return createStringError(EC, "failed creating '" + ModelPath +
"': " + EC.message());
raw_fd_ostream OS(FD, /*shouldClose=*/true);
OS << BlobData;
OS.close();
return LoadResponse{
/*KeyNotFound*/ false,
BlobContents{/*IsFilePath*/ true, TempPath.str().str()}};
} else {
return LoadResponse{/*KeyNotFound*/ false,
BlobContents{/*IsFilePath*/ false, BlobData.str()}};
}
}
Expected<std::string> LLVMCASCacheProvider::CASSave(const BlobContents &Blob) {
std::optional<cas::ObjectRef> Ref;
if (Blob.IsFilePath) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileBuf =
MemoryBuffer::getFile(Blob.DataOrPath);
if (!FileBuf)
return createStringError(FileBuf.getError(),
"failed reading '" + Blob.DataOrPath +
"': " + FileBuf.getError().message());
if (Error E =
CAS->storeFromString({}, (*FileBuf)->getBuffer()).moveInto(Ref))
return std::move(E);
} else {
if (Error E = CAS->storeFromString({}, Blob.DataOrPath).moveInto(Ref))
return std::move(E);
}
return CAS->getID(*Ref).toString();
}
Expected<RemoteCacheProvider::GetResponse>
LLVMCASCacheProvider::CASGet(StringRef CASID, bool WriteToDisk) {
Expected<cas::CASID> ID = CAS->parseID(CASID);
if (!ID)
return ID.takeError();
Expected<cas::ObjectProxy> Obj = CAS->getProxy(*ID);
if (!Obj)
return Obj.takeError();
StringRef BlobData = Obj->getData();
SmallVector<std::string> Refs;
Refs.reserve(Obj->getNumReferences());
auto Err = Obj->forEachReference([&](ObjectRef Ref) {
Refs.push_back(toStringRef(CAS->getID(Ref).getHash()).str());
return Error::success();
});
if (Err)
return std::move(Err);
if (WriteToDisk) {
SmallString<128> ModelPath{TempDir};
sys::path::append(ModelPath, "%%%%%%%.blob");
int FD;
SmallString<256> TempPath;
std::error_code EC = sys::fs::createUniqueFile(ModelPath, FD, TempPath);
if (EC)
return createStringError(EC, "failed creating '" + ModelPath +
"': " + EC.message());
raw_fd_ostream OS(FD, /*shouldClose=*/true);
OS << BlobData;
OS.close();
return GetResponse{/*KeyNotFound*/ false,
BlobContents{/*IsFilePath*/ true, TempPath.str().str()},
std::move(Refs)};
} else {
return GetResponse{/*KeyNotFound*/ false,
BlobContents{/*IsFilePath*/ false, BlobData.str()},
std::move(Refs)};
}
}
Expected<std::string>
LLVMCASCacheProvider::CASPut(const BlobContents &Blob,
ArrayRef<std::string> RawRefs) {
SmallVector<ObjectRef> Refs;
for (const auto &Ref : RawRefs) {
auto ID = CAS->parseID(Ref);
if (!ID)
return ID.takeError();
auto CASRef = CAS->getReference(*ID);
if (!CASRef)
return createStringError(inconvertibleErrorCode(),
"cannot get ObjectRef from CASID ");
Refs.push_back(*CASRef);
}
std::optional<cas::ObjectRef> Ref;
if (Blob.IsFilePath) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileBuf =
MemoryBuffer::getFile(Blob.DataOrPath);
if (!FileBuf)
return createStringError(FileBuf.getError(),
"failed reading '" + Blob.DataOrPath +
"': " + FileBuf.getError().message());
if (Error E =
CAS->storeFromString(Refs, (*FileBuf)->getBuffer()).moveInto(Ref))
return std::move(E);
} else {
if (Error E = CAS->storeFromString(Refs, Blob.DataOrPath).moveInto(Ref))
return std::move(E);
}
return CAS->getID(*Ref).toString();
}
std::unique_ptr<RemoteCacheProvider>
cas::remote::createLLVMCASCacheProvider(StringRef TempPath,
std::unique_ptr<ObjectStore> CAS,
std::unique_ptr<ActionCache> Cache) {
auto Provider = std::make_unique<LLVMCASCacheProvider>();
Provider->initialize(TempPath, std::move(CAS), std::move(Cache));
return Provider;
}
|