diff options
| author | Per Larsson <[email protected]> | 2021-11-01 14:36:39 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-11-01 14:36:39 +0100 |
| commit | ff676a3e570563b2de32b61563b46a828437e4aa (patch) | |
| tree | 2c7799b8f525c26f18b1cccfaeefa8355f0f9798 | |
| parent | Upload cache record before blobs and call finalize when processing upstream t... (diff) | |
| parent | Moved declaration of ZenDiskCacheLayer::CacheBucket in the .h (diff) | |
| download | zen-ff676a3e570563b2de32b61563b46a828437e4aa.tar.xz zen-ff676a3e570563b2de32b61563b46a828437e4aa.zip | |
Merge branch 'main' of https://github.com/EpicGames/zen
| -rw-r--r-- | zencore/filesystem.cpp | 2 | ||||
| -rw-r--r-- | zencore/include/zencore/sharedbuffer.h | 6 | ||||
| -rw-r--r-- | zencore/memory.cpp | 4 | ||||
| -rw-r--r-- | zencore/xmake.lua | 2 | ||||
| -rw-r--r-- | zenserver-test/zenserver-test.cpp | 27 | ||||
| -rw-r--r-- | zenserver/cache/cacheagent.cpp | 5 | ||||
| -rw-r--r-- | zenserver/cache/cacheagent.h | 9 | ||||
| -rw-r--r-- | zenserver/cache/structuredcachestore.cpp | 215 | ||||
| -rw-r--r-- | zenserver/cache/structuredcachestore.h | 93 | ||||
| -rw-r--r-- | zenserver/zenserver.cpp | 28 | ||||
| -rw-r--r-- | zenserver/zenserver.vcxproj | 2 | ||||
| -rw-r--r-- | zenserver/zenserver.vcxproj.filters | 6 |
12 files changed, 255 insertions, 144 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp index b8e8eff55..a642e2cf6 100644 --- a/zencore/filesystem.cpp +++ b/zencore/filesystem.cpp @@ -840,7 +840,7 @@ TEST_CASE("filesystem") // GetExePath -- this is not a great test as it's so dependent on where the this code gets linked in path BinPath = GetRunningExecutablePath(); - const bool ExpectedExe = BinPath.stem() == "zencore-test" || BinPath.stem() == "zenserver-test" || BinPath.stem() == "zenstore-test"; + const bool ExpectedExe = ToUtf8(BinPath.stem().native()).ends_with("-test"sv) || BinPath.stem() == "zenserver"; CHECK(ExpectedExe); CHECK(is_regular_file(BinPath)); diff --git a/zencore/include/zencore/sharedbuffer.h b/zencore/include/zencore/sharedbuffer.h index 640c3fe74..1f87dc639 100644 --- a/zencore/include/zencore/sharedbuffer.h +++ b/zencore/include/zencore/sharedbuffer.h @@ -143,6 +143,12 @@ public: /** Make a non-owned view of the input */ [[nodiscard]] inline static SharedBuffer MakeView(MemoryView View) { return MakeView(View.GetData(), View.GetSize()); } + /** Make a non-owning view of the memory of the contiguous container. */ + [[nodiscard]] inline static SharedBuffer MakeView(const std::ranges::contiguous_range auto& Container) + { + std::span Span = Container; + return MakeView(Span.data(), Span.size() * sizeof(typename decltype(Span)::element_type)); + } /** Make a non-owned view of the input */ [[nodiscard]] ZENCORE_API static SharedBuffer MakeView(const void* Data, uint64_t Size); /** Make a non-owned view of the input */ diff --git a/zencore/memory.cpp b/zencore/memory.cpp index 62c81076d..14ea7ca1d 100644 --- a/zencore/memory.cpp +++ b/zencore/memory.cpp @@ -185,13 +185,13 @@ TEST_CASE("ChunkingLinearAllocator") TEST_CASE("MemoryView") { { - uint8_t Array1[16]; + uint8_t Array1[16] = {}; MemoryView View1 = MakeMemoryView(Array1); CHECK(View1.GetSize() == 16); } { - uint32_t Array2[16]; + uint32_t Array2[16] = {}; MemoryView View2 = MakeMemoryView(Array2); CHECK(View2.GetSize() == 64); } diff --git a/zencore/xmake.lua b/zencore/xmake.lua index 5de7f476d..0b0b51758 100644 --- a/zencore/xmake.lua +++ b/zencore/xmake.lua @@ -8,7 +8,9 @@ target('zencore') "vcpkg::spdlog", "vcpkg::fmt", "vcpkg::doctest", + "vcpkg::json11", "vcpkg::lz4", + "vcpkg::mimalloc", "vcpkg::cpr", "vcpkg::curl", -- required by cpr "vcpkg::zlib", -- required by curl diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index 23be9f729..450468bb4 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -66,6 +66,7 @@ ZEN_THIRD_PARTY_INCLUDES_END #endif using namespace fmt::literals; +using namespace std::literals; /* @@ -126,10 +127,10 @@ public: m_Headers.reserve(16); zen::ExtendableStringBuilder<256> RequestBody; - RequestBody << "GET " << Path << " HTTP/1.1\r\n"; - RequestBody << "Host: " << Server << "\r\n"; - RequestBody << "Accept: */*\r\n"; - RequestBody << "Connection: " << (m_KeepAlive ? "keep-alive" : "close") << "\r\n\r\n"; // TODO: support keep-alive + RequestBody << "GET "sv << Path << " HTTP/1.1\r\n"sv; + RequestBody << "Host: "sv << Server << "\r\n"sv; + RequestBody << "Accept: */*\r\n"sv; + RequestBody << "Connection: "sv << (m_KeepAlive ? "keep-alive"sv : "close"sv) << "\r\n\r\n"sv; // TODO: support keep-alive m_RequestBody = RequestBody; @@ -460,12 +461,12 @@ using namespace spdlog; using namespace spdlog::details; using namespace std::literals; -class full_formatter final : public spdlog::formatter +class full_test_formatter final : public spdlog::formatter { public: - full_formatter(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch) : m_Epoch(Epoch), m_LogId(LogId) {} + full_test_formatter(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch) : m_Epoch(Epoch), m_LogId(LogId) {} - virtual std::unique_ptr<formatter> clone() const override { return std::make_unique<full_formatter>(m_LogId, m_Epoch); } + virtual std::unique_ptr<formatter> clone() const override { return std::make_unique<full_test_formatter>(m_LogId, m_Epoch); } static constexpr bool UseDate = false; @@ -680,7 +681,7 @@ main(int argc, char** argv) zen::logging::InitializeLogging(); spdlog::set_level(spdlog::level::debug); - spdlog::set_formatter(std::make_unique<::logging::full_formatter>("test", std::chrono::system_clock::now())); + spdlog::set_formatter(std::make_unique<::logging::full_test_formatter>("test", std::chrono::system_clock::now())); std::filesystem::path ProgramBaseDir = std::filesystem::path(argv[0]).parent_path(); std::filesystem::path TestBaseDir = ProgramBaseDir.parent_path().parent_path() / ".test"; @@ -1704,11 +1705,12 @@ TEST_CASE("zcache.policy") zen::IoHash Key; zen::IoHash PayloadId; - zen::CbPackage Package = GeneratePackage(Key, PayloadId); - auto Buf = ToBuffer(Package); // Store package locally { + zen::CbPackage Package = GeneratePackage(Key, PayloadId); + auto Buf = ToBuffer(Package); + CHECK(Package.GetAttachments().size() != 0); cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(LocalCfg.BaseUri, Bucket, Key)}, cpr::Body{(const char*)Buf.GetData(), Buf.GetSize()}, @@ -1761,11 +1763,12 @@ TEST_CASE("zcache.policy") zen::IoHash Key; zen::IoHash PayloadId; - zen::CbPackage Package = GeneratePackage(Key, PayloadId); - auto Buf = ToBuffer(Package); // Store package upstream { + zen::CbPackage Package = GeneratePackage(Key, PayloadId); + auto Buf = ToBuffer(Package); + CHECK(Package.GetAttachments().size() != 0); cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(UpstreamCfg.BaseUri, Bucket, Key)}, cpr::Body{(const char*)Buf.GetData(), Buf.GetSize()}, diff --git a/zenserver/cache/cacheagent.cpp b/zenserver/cache/cacheagent.cpp deleted file mode 100644 index f4d1cabe6..000000000 --- a/zenserver/cache/cacheagent.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#include "cacheagent.h" - -#include <gsl/gsl-lite.hpp> diff --git a/zenserver/cache/cacheagent.h b/zenserver/cache/cacheagent.h deleted file mode 100644 index 145d0f79f..000000000 --- a/zenserver/cache/cacheagent.h +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -class CacheAgent -{ -public: -private: -}; diff --git a/zenserver/cache/structuredcachestore.cpp b/zenserver/cache/structuredcachestore.cpp index e7b840ed8..119062833 100644 --- a/zenserver/cache/structuredcachestore.cpp +++ b/zenserver/cache/structuredcachestore.cpp @@ -6,18 +6,26 @@ #include <zencore/windows.h> #include <zencore/compactbinary.h> +#include <zencore/compactbinarybuilder.h> +#include <zencore/compactbinarypackage.h> +#include <zencore/compactbinaryvalidation.h> +#include <zencore/compress.h> #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/iobuffer.h> #include <zencore/logging.h> #include <zencore/string.h> +#include <zencore/testing.h> +#include <zencore/testutils.h> #include <zencore/thread.h> -#include <zenstore/basicfile.h> #include <zenstore/cas.h> #include <zenstore/caslog.h> +#include <zenstore/cidstore.h> +#include <zenstore/gc.h> #include <concepts> #include <filesystem> +#include <ranges> #include <unordered_map> ZEN_THIRD_PARTY_INCLUDES_START @@ -31,7 +39,7 @@ namespace zen { using namespace fmt::literals; -ZenCacheStore::ZenCacheStore(CasStore& Cas, const std::filesystem::path& RootDir) : m_DiskLayer{Cas, RootDir} +ZenCacheStore::ZenCacheStore(const std::filesystem::path& RootDir) : m_DiskLayer{RootDir} { ZEN_INFO("initializing structured cache at '{}'", RootDir); CreateDirectories(RootDir); @@ -125,7 +133,8 @@ ZenCacheStore::Scrub(ScrubContext& Ctx) void ZenCacheStore::GarbageCollect(GcContext& GcCtx) { - ZEN_UNUSED(GcCtx); + m_DiskLayer.GarbageCollect(GcCtx); + m_MemLayer.GarbageCollect(GcCtx); } ////////////////////////////////////////////////////////////////////////// @@ -150,7 +159,7 @@ ZenCacheMemoryLayer::Get(std::string_view InBucket, const IoHash& HashKey, ZenCa return false; } - CacheBucket* Bucket = Bucket = &it->second; + CacheBucket* Bucket = &it->second; _.ReleaseNow(); @@ -213,7 +222,12 @@ ZenCacheMemoryLayer::Scrub(ScrubContext& Ctx) void ZenCacheMemoryLayer::GarbageCollect(GcContext& GcCtx) { - ZEN_UNUSED(GcCtx); + RwLock::SharedLockScope _(m_Lock); + + for (auto& Kv : m_Buckets) + { + Kv.second.GarbageCollect(GcCtx); + } } void @@ -302,110 +316,50 @@ ZenCacheMemoryLayer::CacheBucket::Put(const IoHash& HashKey, const ZenCacheValue ////////////////////////////////////////////////////////////////////////// -#pragma pack(push) -#pragma pack(1) +inline DiskLocation::DiskLocation() = default; -struct DiskLocation +inline DiskLocation::DiskLocation(uint64_t Offset, uint64_t ValueSize, uint32_t IndexSize, uint64_t Flags) +: OffsetAndFlags(CombineOffsetAndFlags(Offset, Flags)) +, LowerSize(ValueSize & 0xFFFFffff) +, IndexDataSize(IndexSize) { - inline DiskLocation() = default; +} - inline DiskLocation(uint64_t Offset, uint64_t ValueSize, uint32_t IndexSize, uint64_t Flags) - : OffsetAndFlags(CombineOffsetAndFlags(Offset, Flags)) - , LowerSize(ValueSize & 0xFFFFffff) - , IndexDataSize(IndexSize) - { - } +inline uint64_t DiskLocation::CombineOffsetAndFlags(uint64_t Offset, uint64_t Flags) +{ + return Offset | Flags; +} - static const uint64_t kOffsetMask = 0x0000'ffFF'ffFF'ffFFull; - static const uint64_t kSizeMask = 0x00FF'0000'0000'0000ull; - static const uint64_t kFlagsMask = 0xff00'0000'0000'0000ull; - static const uint64_t kStandaloneFile = 0x8000'0000'0000'0000ull; - static const uint64_t kStructured = 0x4000'0000'0000'0000ull; - static const uint64_t kTombStone = 0x2000'0000'0000'0000ull; +inline uint64_t DiskLocation::Offset() const +{ + return OffsetAndFlags & kOffsetMask; +} - static uint64_t CombineOffsetAndFlags(uint64_t Offset, uint64_t Flags) { return Offset | Flags; } +inline uint64_t DiskLocation::Size() const +{ + return LowerSize; +} - inline uint64_t Offset() const { return OffsetAndFlags & kOffsetMask; } - inline uint64_t Size() const { return LowerSize; } - inline uint64_t IsFlagSet(uint64_t Flag) const { return OffsetAndFlags & Flag; } - inline ZenContentType GetContentType() const - { - ZenContentType ContentType = ZenContentType::kBinary; +inline uint64_t DiskLocation::IsFlagSet(uint64_t Flag) const +{ + return OffsetAndFlags & Flag; +} - if (IsFlagSet(DiskLocation::kStructured)) - { - ContentType = ZenContentType::kCbObject; - } +inline ZenContentType DiskLocation::GetContentType() const +{ + ZenContentType ContentType = ZenContentType::kBinary; - return ContentType; + if (IsFlagSet(DiskLocation::kStructured)) + { + ContentType = ZenContentType::kCbObject; } -private: - uint64_t OffsetAndFlags = 0; - uint32_t LowerSize = 0; - uint32_t IndexDataSize = 0; -}; - -struct DiskIndexEntry -{ - IoHash Key; - DiskLocation Location; -}; - -#pragma pack(pop) + return ContentType; +} -static_assert(sizeof(DiskIndexEntry) == 36); +////////////////////////////////////////////////////////////////////////// -struct ZenCacheDiskLayer::CacheBucket -{ - CacheBucket(CasStore& Cas); - ~CacheBucket(); - - void OpenOrCreate(std::filesystem::path BucketDir, bool AllowCreate = true); - static bool Delete(std::filesystem::path BucketDir); - - bool Get(const IoHash& HashKey, ZenCacheValue& OutValue); - void Put(const IoHash& HashKey, const ZenCacheValue& Value); - void Drop(); - void Flush(); - void Scrub(ScrubContext& Ctx); - void GarbageCollect(GcContext& GcCtx); - - inline bool IsOk() const { return m_IsOk; } - -private: - CasStore& m_CasStore; - std::filesystem::path m_BucketDir; - Oid m_BucketId; - bool m_IsOk = false; - uint64_t m_LargeObjectThreshold = 64 * 1024; - - // These files are used to manage storage of small objects for this bucket - - BasicFile m_SobsFile; - TCasLogFile<DiskIndexEntry> m_SlogFile; - - RwLock m_IndexLock; - tsl::robin_map<IoHash, DiskLocation, IoHash::Hasher> m_Index; - uint64_t m_WriteCursor = 0; - - void BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey); - void PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value); - bool GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue); - bool GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue); - - // These locks are here to avoid contention on file creation, therefore it's sufficient - // that we take the same lock for the same hash - // - // These locks are small and should really be spaced out so they don't share cache lines, - // but we don't currently access them at particularly high frequency so it should not be - // an issue in practice - - RwLock m_ShardedLocks[256]; - inline RwLock& LockForHash(const IoHash& Hash) { return m_ShardedLocks[Hash.Hash[19]]; } -}; - -ZenCacheDiskLayer::CacheBucket::CacheBucket(CasStore& Cas) : m_CasStore(Cas) +ZenCacheDiskLayer::CacheBucket::CacheBucket() { } @@ -839,7 +793,7 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c ////////////////////////////////////////////////////////////////////////// -ZenCacheDiskLayer::ZenCacheDiskLayer(CasStore& Cas, const std::filesystem::path& RootDir) : m_RootDir(RootDir), m_CasStore(Cas) +ZenCacheDiskLayer::ZenCacheDiskLayer(const std::filesystem::path& RootDir) : m_RootDir(RootDir) { } @@ -873,7 +827,7 @@ ZenCacheDiskLayer::Get(std::string_view InBucket, const IoHash& HashKey, ZenCach } else { - auto It = m_Buckets.try_emplace(std::string(InBucket), m_CasStore); + auto It = m_Buckets.try_emplace(std::string(InBucket)); Bucket = &It.first->second; std::filesystem::path BucketPath = m_RootDir; @@ -916,7 +870,7 @@ ZenCacheDiskLayer::Put(std::string_view InBucket, const IoHash& HashKey, const Z } else { - auto It = m_Buckets.try_emplace(std::string(InBucket), m_CasStore); + auto It = m_Buckets.try_emplace(std::string(InBucket)); Bucket = &It.first->second; std::filesystem::path bucketPath = m_RootDir; @@ -972,7 +926,7 @@ ZenCacheDiskLayer::DiscoverBuckets() } else { - auto InsertResult = m_Buckets.try_emplace(BucketName8, m_CasStore); + auto InsertResult = m_Buckets.try_emplace(BucketName8); std::filesystem::path BucketPath = m_RootDir; BucketPath /= BucketName8; @@ -1050,7 +1004,64 @@ ZenCacheDiskLayer::Scrub(ScrubContext& Ctx) void ZenCacheDiskLayer::GarbageCollect(GcContext& GcCtx) { - ZEN_UNUSED(GcCtx); + RwLock::SharedLockScope _(m_Lock); + + for (auto& Kv : m_Buckets) + { + Kv.second.GarbageCollect(GcCtx); + } +} + +////////////////////////////////////////////////////////////////////////// + +#if ZEN_WITH_TESTS + +TEST_CASE("z$.store") +{ + using namespace fmt::literals; + using namespace std::literals; + + ScopedTemporaryDirectory TempDir; + + ZenCacheStore Zcs(TempDir.Path() / "cache"); + + const int kIterationCount = 100; + + for (int i = 0; i < kIterationCount; ++i) + { + const IoHash Key = IoHash::HashBuffer(&i, sizeof i); + + CbObjectWriter Cbo; + Cbo << "hey" << i; + CbObject Obj = Cbo.Save(); + + ZenCacheValue Value; + Value.Value = Obj.GetBuffer().AsIoBuffer(); + Value.Value.SetContentType(ZenContentType::kCbObject); + + Zcs.Put("test_bucket"sv, Key, Value); + } + + for (int i = 0; i < kIterationCount; ++i) + { + const IoHash Key = IoHash::HashBuffer(&i, sizeof i); + + ZenCacheValue Value; + Zcs.Get("test_bucket"sv, Key, /* out */ Value); + + REQUIRE(Value.Value); + CHECK(Value.Value.GetContentType() == ZenContentType::kCbObject); + CHECK_EQ(ValidateCompactBinary(Value.Value, CbValidateMode::All), CbValidateError::None); + CbObject Obj = LoadCompactBinaryObject(Value.Value); + CHECK_EQ(Obj["hey"].AsInt32(), i); + } +} + +#endif + +void +z$_forcelink() +{ } } // namespace zen diff --git a/zenserver/cache/structuredcachestore.h b/zenserver/cache/structuredcachestore.h index 4753af627..6521d8393 100644 --- a/zenserver/cache/structuredcachestore.h +++ b/zenserver/cache/structuredcachestore.h @@ -7,7 +7,9 @@ #include <zencore/iohash.h> #include <zencore/thread.h> #include <zencore/uid.h> +#include <zenstore/basicfile.h> #include <zenstore/cas.h> +#include <zenstore/caslog.h> #pragma warning(push) #pragma warning(disable : 4127) @@ -98,10 +100,46 @@ private: Configuration m_Configuration; }; +#pragma pack(push) +#pragma pack(1) + +struct DiskLocation +{ + static const uint64_t kOffsetMask = 0x0000'ffFF'ffFF'ffFFull; + static const uint64_t kSizeMask = 0x00FF'0000'0000'0000ull; + static const uint64_t kFlagsMask = 0xff00'0000'0000'0000ull; + static const uint64_t kStandaloneFile = 0x8000'0000'0000'0000ull; + static const uint64_t kStructured = 0x4000'0000'0000'0000ull; + static const uint64_t kTombStone = 0x2000'0000'0000'0000ull; + + DiskLocation(); + DiskLocation(uint64_t Offset, uint64_t ValueSize, uint32_t IndexSize, uint64_t Flags); + static uint64_t CombineOffsetAndFlags(uint64_t Offset, uint64_t Flags); + uint64_t Offset() const; + uint64_t Size() const; + uint64_t IsFlagSet(uint64_t Flag) const; + ZenContentType GetContentType() const; + +private: + uint64_t OffsetAndFlags = 0; + uint32_t LowerSize = 0; + uint32_t IndexDataSize = 0; +}; + +struct DiskIndexEntry +{ + IoHash Key; + DiskLocation Location; +}; + +#pragma pack(pop) + +static_assert(sizeof(DiskIndexEntry) == 36); + class ZenCacheDiskLayer { public: - ZenCacheDiskLayer(CasStore& Cas, const std::filesystem::path& RootDir); + explicit ZenCacheDiskLayer(const std::filesystem::path& RootDir); ~ZenCacheDiskLayer(); bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue); @@ -117,9 +155,54 @@ private: /** A cache bucket manages a single directory containing metadata and data for that bucket */ - struct CacheBucket; + struct CacheBucket + { + CacheBucket(); + ~CacheBucket(); + + void OpenOrCreate(std::filesystem::path BucketDir, bool AllowCreate = true); + static bool Delete(std::filesystem::path BucketDir); + + bool Get(const IoHash& HashKey, ZenCacheValue& OutValue); + void Put(const IoHash& HashKey, const ZenCacheValue& Value); + void Drop(); + void Flush(); + void Scrub(ScrubContext& Ctx); + void GarbageCollect(GcContext& GcCtx); + + inline bool IsOk() const { return m_IsOk; } + + private: + std::filesystem::path m_BucketDir; + Oid m_BucketId; + bool m_IsOk = false; + uint64_t m_LargeObjectThreshold = 64 * 1024; + + // These files are used to manage storage of small objects for this bucket + + BasicFile m_SobsFile; + TCasLogFile<DiskIndexEntry> m_SlogFile; + + RwLock m_IndexLock; + tsl::robin_map<IoHash, DiskLocation, IoHash::Hasher> m_Index; + uint64_t m_WriteCursor = 0; + + void BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey); + void PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value); + bool GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue); + bool GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue); + + // These locks are here to avoid contention on file creation, therefore it's sufficient + // that we take the same lock for the same hash + // + // These locks are small and should really be spaced out so they don't share cache lines, + // but we don't currently access them at particularly high frequency so it should not be + // an issue in practice + + RwLock m_ShardedLocks[256]; + inline RwLock& LockForHash(const IoHash& Hash) { return m_ShardedLocks[Hash.Hash[19]]; } + }; - CasStore& m_CasStore; std::filesystem::path m_RootDir; RwLock m_Lock; std::unordered_map<std::string, CacheBucket> m_Buckets; // TODO: make this case insensitive @@ -128,7 +211,7 @@ private: class ZenCacheStore { public: - ZenCacheStore(CasStore& Cas, const std::filesystem::path& RootDir); + explicit ZenCacheStore(const std::filesystem::path& RootDir); ~ZenCacheStore(); bool Get(std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue); @@ -146,4 +229,6 @@ private: uint64_t m_LastScrubTime = 0; }; +void z$_forcelink(); + } // namespace zen diff --git a/zenserver/zenserver.cpp b/zenserver/zenserver.cpp index 80bc6a6d4..9a8090fc0 100644 --- a/zenserver/zenserver.cpp +++ b/zenserver/zenserver.cpp @@ -627,7 +627,7 @@ ZenServer::InitializeStructuredCache(ZenServiceConfig& ServiceConfig) auto ValueOrDefault = [](std::string_view Value, std::string_view Default) { return Value.empty() ? Default : Value; }; ZEN_INFO("instantiating structured cache service"); - m_CacheStore = std::make_unique<ZenCacheStore>(*m_CasStore, m_DataRoot / "cache"); + m_CacheStore = std::make_unique<ZenCacheStore>(m_DataRoot / "cache"); std::unique_ptr<zen::UpstreamCache> UpstreamCache; if (ServiceConfig.UpstreamCacheConfig.CachePolicy != UpstreamCachePolicy::Disabled) @@ -890,6 +890,22 @@ ZenWindowsService::Run() return 0; } +#if ZEN_WITH_TESTS +int +test_main(int argc, char** argv) +{ + zen::zencore_forcelinktests(); + zen::zenhttp_forcelinktests(); + zen::zenstore_forcelinktests(); + + zen::logging::InitializeLogging(); + + spdlog::set_level(spdlog::level::debug); + + return doctest::Context(argc, argv).run(); +} +#endif + int main(int argc, char* argv[]) { @@ -899,6 +915,16 @@ main(int argc, char* argv[]) mi_version(); #endif +#if ZEN_WITH_TESTS + if (argc >= 2) + { + if (argv[1] == "test"sv) + { + return test_main(argc, argv); + } + } +#endif + try { ZenServerOptions GlobalOptions; diff --git a/zenserver/zenserver.vcxproj b/zenserver/zenserver.vcxproj index b670582e7..d954d3f8d 100644 --- a/zenserver/zenserver.vcxproj +++ b/zenserver/zenserver.vcxproj @@ -120,7 +120,6 @@ <ClInclude Include="testing\httptest.h" /> <ClInclude Include="upstream\jupiter.h" /> <ClInclude Include="projectstore.h" /> - <ClInclude Include="cache\cacheagent.h" /> <ClInclude Include="testing\launch.h" /> <ClInclude Include="casstore.h" /> <ClInclude Include="diag\diagsvcs.h" /> @@ -142,7 +141,6 @@ <ClCompile Include="monitoring\httpstats.cpp" /> <ClCompile Include="monitoring\httpstatus.cpp" /> <ClCompile Include="projectstore.cpp" /> - <ClCompile Include="cache\cacheagent.cpp" /> <ClCompile Include="sos\sos.cpp" /> <ClCompile Include="testing\httptest.cpp" /> <ClCompile Include="upstream\jupiter.cpp" /> diff --git a/zenserver/zenserver.vcxproj.filters b/zenserver/zenserver.vcxproj.filters index b87fa0016..04c6267ba 100644 --- a/zenserver/zenserver.vcxproj.filters +++ b/zenserver/zenserver.vcxproj.filters @@ -5,9 +5,6 @@ <ClInclude Include="projectstore.h" /> <ClInclude Include="casstore.h" /> <ClInclude Include="testing\launch.h" /> - <ClInclude Include="cache\cacheagent.h"> - <Filter>cache</Filter> - </ClInclude> <ClInclude Include="diag\diagsvcs.h"> <Filter>diag</Filter> </ClInclude> @@ -49,9 +46,6 @@ <ClCompile Include="zenserver.cpp" /> <ClCompile Include="projectstore.cpp" /> <ClCompile Include="casstore.cpp" /> - <ClCompile Include="cache\cacheagent.cpp"> - <Filter>cache</Filter> - </ClCompile> <ClCompile Include="experimental\usnjournal.cpp"> <Filter>experimental</Filter> </ClCompile> |