Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/include/llvm/CAS/ActionCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class ActionCache {
CanBeDistributed);
}

/// Validate the ActionCache contents.
virtual Error validate() const = 0;

virtual ~ActionCache() = default;

protected:
Expand All @@ -97,6 +100,9 @@ class ActionCache {
/// Create an action cache in memory.
std::unique_ptr<ActionCache> createInMemoryActionCache();

/// Create an action cache on disk.
Expected<std::unique_ptr<ActionCache>> createOnDiskActionCache(StringRef Path);

} // end namespace llvm::cas

#endif // LLVM_CAS_ACTIONCACHE_H
59 changes: 59 additions & 0 deletions llvm/include/llvm/CAS/BuiltinUnifiedCASDatabases.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CAS_BUILTINUNIFIEDCASDATABASES_H
#define LLVM_CAS_BUILTINUNIFIEDCASDATABASES_H

#include "llvm/Support/Error.h"

namespace llvm::cas {

class ActionCache;
class ObjectStore;

/// Create on-disk \c ObjectStore and \c ActionCache instances based on
/// \c ondisk::UnifiedOnDiskCache, with built-in hashing.
Expected<std::pair<std::unique_ptr<ObjectStore>, std::unique_ptr<ActionCache>>>
createOnDiskUnifiedCASDatabases(StringRef Path);

/// Represents the result of validating the contents using
/// \c validateOnDiskUnifiedCASDatabasesIfNeeded.
///
/// Note: invalid results are handled as an \c Error.
enum class ValidationResult {
/// The data is already valid.
Valid,
/// The data was invalid, but was recovered.
Recovered,
/// Validation was skipped, as it was not needed.
Skipped,
};

/// Validate the data in \p Path, if needed to ensure correctness.
///
/// \param Path directory for the on-disk database.
/// \param CheckHash Whether to validate hashes match the data.
/// \param AllowRecovery Whether to automatically recover from invalid data by
/// marking the files for garbage collection.
/// \param ForceValidation Whether to force validation to occur even if it
/// should not be necessary.
/// \param LLVMCasBinary If provided, validation is performed out-of-process
/// using the given \c llvm-cas executable which protects against crashes
/// during validation. Otherwise validation is performed in-process.
///
/// \returns \c Valid if the data is already valid, \c Recovered if data
/// was invalid but has been cleared, \c Skipped if validation is not needed,
/// or an \c Error if validation cannot be performed or if the data is left
/// in an invalid state because \p AllowRecovery is false.
Expected<ValidationResult> validateOnDiskUnifiedCASDatabasesIfNeeded(
StringRef Path, bool CheckHash, bool AllowRecovery, bool ForceValidation,
std::optional<StringRef> LLVMCasBinary);

} // namespace llvm::cas

#endif // LLVM_CAS_BUILTINUNIFIEDCASDATABASES_H
47 changes: 46 additions & 1 deletion llvm/include/llvm/CAS/ObjectStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the declaration of the ObjectStore class.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CAS_OBJECTSTORE_H
#define LLVM_CAS_OBJECTSTORE_H
Expand Down Expand Up @@ -111,7 +116,10 @@ class ObjectStore {
virtual Expected<bool> isMaterialized(ObjectRef Ref) const = 0;

/// Validate the underlying object referred by CASID.
virtual Error validate(const CASID &ID) = 0;
virtual Error validateObject(const CASID &ID) = 0;

/// Validate the entire ObjectStore.
virtual Error validate(bool CheckHash) const = 0;

protected:
/// Load the object referenced by \p Ref.
Expand Down Expand Up @@ -215,9 +223,39 @@ class ObjectStore {
return Data.size();
}

/// Set the size for limiting growth of on-disk storage. This has an effect
/// for when the instance is closed.
///
/// Implementations may be not have this implemented.
virtual Error setSizeLimit(std::optional<uint64_t> SizeLimit) {
return Error::success();
}

/// \returns the storage size of the on-disk CAS data.
///
/// Implementations that don't have an implementation for this should return
/// \p std::nullopt.
virtual Expected<std::optional<uint64_t>> getStorageSize() const {
return std::nullopt;
}

/// Prune local storage to reduce its size according to the desired size
/// limit. Pruning can happen concurrently with other operations.
///
/// Implementations may be not have this implemented.
virtual Error pruneStorageData() { return Error::success(); }

/// Validate the whole node tree.
Error validateTree(ObjectRef Ref);

/// Import object from another CAS. This will import the full tree from the
/// other CAS.
Expected<ObjectRef> importObject(ObjectStore &Upstream, ObjectRef Other);

/// Print the ObjectStore internals for debugging purpose.
virtual void print(raw_ostream &) const {}
void dump() const;

/// Get CASContext
const CASContext &getContext() const { return Context; }

Expand Down Expand Up @@ -290,8 +328,15 @@ class ObjectProxy {
ObjectHandle H;
};

/// Create an in memory CAS.
std::unique_ptr<ObjectStore> createInMemoryCAS();

/// \returns true if \c LLVM_ENABLE_ONDISK_CAS configuration was enabled.
bool isOnDiskCASEnabled();

/// Create a persistent on-disk path at \p Path.
Expected<std::unique_ptr<ObjectStore>> createOnDiskCAS(const Twine &Path);

} // namespace cas
} // namespace llvm

Expand Down
12 changes: 7 additions & 5 deletions llvm/include/llvm/CAS/OnDiskGraphDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,16 @@ class OnDiskGraphDB {
/// \param HashByteSize Size for the object digest hash bytes.
/// \param UpstreamDB Optional on-disk store to be used for faulting-in nodes
/// if they don't exist in the primary store. The upstream store is only used
/// for reading nodes, new nodes are only written to the primary store.
/// for reading nodes, new nodes are only written to the primary store. User
/// need to make sure \p UpstreamDB outlives current instance of
/// OnDiskGraphDB and the common usage is to have an \p UnifiedOnDiskCache to
/// manage both.
/// \param Policy If \p UpstreamDB is provided, controls how nodes are copied
/// to primary store. This is recorded at creation time and subsequent opens
/// need to pass the same policy otherwise the \p open will fail.
static Expected<std::unique_ptr<OnDiskGraphDB>>
open(StringRef Path, StringRef HashName, unsigned HashByteSize,
std::unique_ptr<OnDiskGraphDB> UpstreamDB = nullptr,
OnDiskGraphDB *UpstreamDB = nullptr,
FaultInPolicy Policy = FaultInPolicy::FullTree);

~OnDiskGraphDB();
Expand Down Expand Up @@ -438,8 +441,7 @@ class OnDiskGraphDB {

// Private constructor.
OnDiskGraphDB(StringRef RootPath, OnDiskTrieRawHashMap Index,
OnDiskDataAllocator DataPool,
std::unique_ptr<OnDiskGraphDB> UpstreamDB,
OnDiskDataAllocator DataPool, OnDiskGraphDB *UpstreamDB,
FaultInPolicy Policy);

/// Mapping from hash to object reference.
Expand All @@ -459,7 +461,7 @@ class OnDiskGraphDB {
std::string RootPath;

/// Optional on-disk store to be used for faulting-in nodes.
std::unique_ptr<OnDiskGraphDB> UpstreamDB;
OnDiskGraphDB *UpstreamDB = nullptr;

/// The policy used to fault in data from upstream.
FaultInPolicy FIPolicy;
Expand Down
15 changes: 12 additions & 3 deletions llvm/include/llvm/CAS/OnDiskKeyValueDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace llvm::cas::ondisk {

class UnifiedOnDiskCache;

/// An on-disk key-value data store with the following properties:
/// * Keys are fixed length binary hashes with expected normal distribution.
/// * Values are buffers of the same size, specified at creation time.
Expand Down Expand Up @@ -59,9 +61,13 @@ class OnDiskKeyValueDB {
/// \param KeySize Size for the key hash bytes.
/// \param ValueName Identifier name for the values.
/// \param ValueSize Size for the value bytes.
/// \param UnifiedCache An optional UnifiedOnDiskCache that manages the size
/// and lifetime of the CAS instance and it must owns current initializing
/// KeyValueDB after initialized.
static Expected<std::unique_ptr<OnDiskKeyValueDB>>
open(StringRef Path, StringRef HashName, unsigned KeySize,
StringRef ValueName, size_t ValueSize);
StringRef ValueName, size_t ValueSize,
UnifiedOnDiskCache *UnifiedCache = nullptr);

using CheckValueT =
function_ref<Error(FileOffset Offset, ArrayRef<char> Data)>;
Expand All @@ -70,11 +76,14 @@ class OnDiskKeyValueDB {
Error validate(CheckValueT CheckValue) const;

private:
OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache)
: ValueSize(ValueSize), Cache(std::move(Cache)) {}
OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache,
UnifiedOnDiskCache *UnifiedCache)
: ValueSize(ValueSize), Cache(std::move(Cache)),
UnifiedCache(UnifiedCache) {}

const size_t ValueSize;
OnDiskTrieRawHashMap Cache;
UnifiedOnDiskCache *UnifiedCache = nullptr;
};

} // namespace llvm::cas::ondisk
Expand Down
Loading