aboutsummaryrefslogtreecommitdiff
path: root/zenserver/cache
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2021-12-14 12:34:47 +0100
committerPer Larsson <[email protected]>2021-12-14 12:34:47 +0100
commitb6c6568e1618f10d2160d836b65e35586e3c740f (patch)
treef6a929cf918850bbba87d0ee67cd3482b2d50e24 /zenserver/cache
parentFixed bug in z$ service returning partial cache records and enable small obje... (diff)
parentPartial revert b363c5b (diff)
downloadzen-b6c6568e1618f10d2160d836b65e35586e3c740f.tar.xz
zen-b6c6568e1618f10d2160d836b65e35586e3c740f.zip
Merged main.
Diffstat (limited to 'zenserver/cache')
-rw-r--r--zenserver/cache/cachetracking.cpp40
-rw-r--r--zenserver/cache/cachetracking.h5
-rw-r--r--zenserver/cache/structuredcache.cpp39
-rw-r--r--zenserver/cache/structuredcachestore.cpp50
-rw-r--r--zenserver/cache/structuredcachestore.h2
5 files changed, 76 insertions, 60 deletions
diff --git a/zenserver/cache/cachetracking.cpp b/zenserver/cache/cachetracking.cpp
index d1c99a597..ae132f37f 100644
--- a/zenserver/cache/cachetracking.cpp
+++ b/zenserver/cache/cachetracking.cpp
@@ -2,24 +2,26 @@
#include "cachetracking.h"
-#include <zencore/compactbinarybuilder.h>
-#include <zencore/compactbinaryvalue.h>
-#include <zencore/endian.h>
-#include <zencore/filesystem.h>
-#include <zencore/logging.h>
-#include <zencore/scopeguard.h>
-#include <zencore/string.h>
+#if ZEN_USE_CACHE_TRACKER
-#include <zencore/testing.h>
-#include <zencore/testutils.h>
+# include <zencore/compactbinarybuilder.h>
+# include <zencore/compactbinaryvalue.h>
+# include <zencore/endian.h>
+# include <zencore/filesystem.h>
+# include <zencore/logging.h>
+# include <zencore/scopeguard.h>
+# include <zencore/string.h>
+
+# include <zencore/testing.h>
+# include <zencore/testutils.h>
ZEN_THIRD_PARTY_INCLUDES_START
-#pragma comment(lib, "Rpcrt4.lib") // RocksDB made me do this
-#include <fmt/format.h>
-#include <rocksdb/db.h>
-#include <tsl/robin_map.h>
-#include <tsl/robin_set.h>
-#include <gsl/gsl-lite.hpp>
+# pragma comment(lib, "Rpcrt4.lib") // RocksDB made me do this
+# include <fmt/format.h>
+# include <rocksdb/db.h>
+# include <tsl/robin_map.h>
+# include <tsl/robin_set.h>
+# include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
@@ -150,7 +152,7 @@ struct ZenCacheTracker::Impl
{
std::filesystem::path StatsDbPath{StateDirectory / ".zdb"};
- std::string RocksdbPath = ToUtf8(StatsDbPath);
+ std::string RocksdbPath = StatsDbPath.string();
ZEN_DEBUG("opening tracker db at '{}'", RocksdbPath);
@@ -291,7 +293,7 @@ ZenCacheTracker::IterateSnapshots(std::function<void(uint64_t TimeStamp, CbObjec
m_Impl->IterateSnapshots(std::move(Callback));
}
-#if ZEN_WITH_TESTS
+# if ZEN_WITH_TESTS
TEST_CASE("z$.tracker")
{
@@ -365,7 +367,7 @@ TEST_CASE("z$.tracker")
CHECK_EQ(SnapshotCount, 11);
}
-#endif
+# endif
void
cachetracker_forcelink()
@@ -373,3 +375,5 @@ cachetracker_forcelink()
}
} // namespace zen
+
+#endif // ZEN_USE_CACHE_TRACKER
diff --git a/zenserver/cache/cachetracking.h b/zenserver/cache/cachetracking.h
index 06109ebb0..fdfe1a4c7 100644
--- a/zenserver/cache/cachetracking.h
+++ b/zenserver/cache/cachetracking.h
@@ -10,6 +10,9 @@
namespace zen {
+#define ZEN_USE_CACHE_TRACKER 0
+#if ZEN_USE_CACHE_TRACKER
+
class CbObject;
/**
@@ -33,4 +36,6 @@ private:
void cachetracker_forcelink();
+#endif // ZEN_USE_CACHE_TRACKER
+
} // namespace zen
diff --git a/zenserver/cache/structuredcache.cpp b/zenserver/cache/structuredcache.cpp
index 22c8c4afb..b322a5d1b 100644
--- a/zenserver/cache/structuredcache.cpp
+++ b/zenserver/cache/structuredcache.cpp
@@ -13,7 +13,7 @@
#include <zencore/stream.h>
#include <zencore/timer.h>
#include <zenhttp/httpserver.h>
-#include <zenstore/CAS.h>
+#include <zenstore/cas.h>
#include <zenutil/cache/cache.h>
//#include "cachekey.h"
@@ -152,7 +152,7 @@ void
HttpStructuredCacheService::HandleCacheBucketRequest(HttpServerRequest& Request, std::string_view Bucket)
{
ZEN_UNUSED(Request, Bucket);
- switch (auto Verb = Request.RequestVerb())
+ switch (Request.RequestVerb())
{
using enum HttpVerb;
@@ -175,13 +175,16 @@ HttpStructuredCacheService::HandleCacheBucketRequest(HttpServerRequest& Request,
return Request.WriteResponse(HttpResponseCode::NotFound);
}
break;
+
+ default:
+ break;
}
}
void
HttpStructuredCacheService::HandleCacheRecordRequest(HttpServerRequest& Request, const CacheRef& Ref, CachePolicy Policy)
{
- switch (auto Verb = Request.RequestVerb())
+ switch (Request.RequestVerb())
{
using enum HttpVerb;
@@ -442,7 +445,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request
if (StoreUpstream)
{
ZEN_ASSERT(m_UpstreamCache);
- auto Result = m_UpstreamCache->EnqueueUpstream({.Type = ZenContentType::kBinary, .CacheKey = {Ref.BucketSegment, Ref.HashKey}});
+ m_UpstreamCache->EnqueueUpstream({.Type = ZenContentType::kBinary, .Key = {Ref.BucketSegment, Ref.HashKey}});
}
Request.WriteResponse(HttpResponseCode::Created);
@@ -486,9 +489,8 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request
if (StoreUpstream && !IsPartialRecord)
{
ZEN_ASSERT(m_UpstreamCache);
- auto Result = m_UpstreamCache->EnqueueUpstream({.Type = ZenContentType::kCbObject,
- .CacheKey = {Ref.BucketSegment, Ref.HashKey},
- .PayloadIds = std::move(ValidAttachments)});
+ m_UpstreamCache->EnqueueUpstream(
+ {.Type = ZenContentType::kCbObject, .Key = {Ref.BucketSegment, Ref.HashKey}, .PayloadIds = std::move(ValidAttachments)});
}
Request.WriteResponse(HttpResponseCode::Created);
@@ -568,9 +570,8 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request
if (StoreUpstream && !IsPartialRecord)
{
ZEN_ASSERT(m_UpstreamCache);
- auto Result = m_UpstreamCache->EnqueueUpstream({.Type = ZenContentType::kCbPackage,
- .CacheKey = {Ref.BucketSegment, Ref.HashKey},
- .PayloadIds = std::move(ValidAttachments)});
+ m_UpstreamCache->EnqueueUpstream(
+ {.Type = ZenContentType::kCbPackage, .Key = {Ref.BucketSegment, Ref.HashKey}, .PayloadIds = std::move(ValidAttachments)});
}
Request.WriteResponse(HttpResponseCode::Created);
@@ -584,7 +585,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request
void
HttpStructuredCacheService::HandleCachePayloadRequest(HttpServerRequest& Request, const CacheRef& Ref, CachePolicy Policy)
{
- switch (auto Verb = Request.RequestVerb())
+ switch (Request.RequestVerb())
{
using enum HttpVerb;
@@ -768,7 +769,7 @@ HttpStructuredCacheService::ValidateKeyUri(HttpServerRequest& Request, CacheRef&
void
HttpStructuredCacheService::HandleRpcRequest(zen::HttpServerRequest& Request)
{
- switch (auto Verb = Request.RequestVerb())
+ switch (Request.RequestVerb())
{
using enum HttpVerb;
@@ -927,8 +928,8 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
{
ZEN_DEBUG("Uncompressed payload '{}' from upstream cache record '{}/{}'",
HashView.AsHash(),
- Params.CacheKey.Bucket,
- Params.CacheKey.Hash);
+ Params.Key.Bucket,
+ Params.Key.Hash);
Count.Invalid++;
}
}
@@ -948,8 +949,8 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
if (CacheValue)
{
ZEN_DEBUG("HIT - '{}/{}' {} '{}' attachments '{}/{}/{}' (new/valid/total) (UPSTREAM)",
- Params.CacheKey.Bucket,
- Params.CacheKey.Hash,
+ Params.Key.Bucket,
+ Params.Key.Hash,
NiceBytes(CacheValue.GetSize()),
ToString(HttpContentType::kCbPackage),
Count.New,
@@ -959,7 +960,7 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
CacheValue.SetContentType(ZenContentType::kCbObject);
CacheValues[Params.KeyIndex] = CacheValue;
- m_CacheStore.Put(Params.CacheKey.Bucket, Params.CacheKey.Hash, {.Value = CacheValue});
+ m_CacheStore.Put(Params.Key.Bucket, Params.Key.Hash, {.Value = CacheValue});
m_CacheStats.HitCount++;
m_CacheStats.UpstreamHitCount++;
@@ -967,7 +968,7 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
else
{
const bool IsPartial = Count.Valid != Count.Total;
- ZEN_DEBUG("MISS - '{}/{}' {}", Params.CacheKey.Bucket, Params.CacheKey.Hash, IsPartial ? "(partial)"sv : ""sv);
+ ZEN_DEBUG("MISS - '{}/{}' {}", Params.Key.Bucket, Params.Key.Hash, IsPartial ? "(partial)"sv : ""sv);
m_CacheStats.MissCount++;
}
};
@@ -1135,7 +1136,7 @@ HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Re
const auto OnCachePayloadGetComplete = [this, &ChunkRequests, &Chunks](CachePayloadGetCompleteParams&& Params) {
if (CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(Params.Payload)))
{
- auto InsertResult = m_CidStore.AddChunk(Compressed);
+ m_CidStore.AddChunk(Compressed);
ZEN_DEBUG("HIT - '{}/{}/{}' {} ({})",
Params.Request.Key.Bucket,
diff --git a/zenserver/cache/structuredcachestore.cpp b/zenserver/cache/structuredcachestore.cpp
index 44c7b728b..718a8db51 100644
--- a/zenserver/cache/structuredcachestore.cpp
+++ b/zenserver/cache/structuredcachestore.cpp
@@ -2,7 +2,7 @@
#include "structuredcachestore.h"
-#include "cachetracking.h"
+#include <zencore/except.h>
#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinarypackage.h>
@@ -23,7 +23,10 @@
#include <zenstore/caslog.h>
#include <zenstore/cidstore.h>
-#include <chrono>
+#if ZEN_PLATFORM_WINDOWS
+# include <zencore/windows.h>
+#endif
+
#include <concepts>
#include <memory_resource>
#include <ranges>
@@ -38,8 +41,7 @@ ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
using namespace fmt::literals;
-using PathBuilder = WideStringBuilder<256>;
-namespace fs = std::filesystem;
+namespace fs = std::filesystem;
static CbObject
LoadCompactBinaryObject(const fs::path& Path)
@@ -487,7 +489,7 @@ private:
uint64_t m_SobsCursor = 0;
std::atomic_uint64_t m_TotalSize{};
- void BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey);
+ void BuildPath(PathBuilderBase& Path, const IoHash& HashKey);
void PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value);
bool GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue);
void DeleteStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, const fs::path& Path, std::error_code& Ec);
@@ -619,17 +621,17 @@ ZenCacheDiskLayer::CacheBucket::OpenLog(const fs::path& BucketDir, const bool Is
}
void
-ZenCacheDiskLayer::CacheBucket::BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey)
+ZenCacheDiskLayer::CacheBucket::BuildPath(PathBuilderBase& Path, const IoHash& HashKey)
{
char HexString[sizeof(HashKey.Hash) * 2];
ToHexBytes(HashKey.Hash, sizeof HashKey.Hash, HexString);
- Path.Append(m_BucketDir.c_str());
+ Path.Append(m_BucketDir);
Path.Append(L"/blob/");
Path.AppendAsciiRange(HexString, HexString + 3);
- Path.Append(L"/");
+ Path.AppendSeparator();
Path.AppendAsciiRange(HexString + 3, HexString + 5);
- Path.Append(L"/");
+ Path.AppendSeparator();
Path.AppendAsciiRange(HexString + 5, HexString + sizeof(HexString));
}
@@ -650,12 +652,12 @@ ZenCacheDiskLayer::CacheBucket::GetInlineCacheValue(const DiskLocation& Loc, Zen
bool
ZenCacheDiskLayer::CacheBucket::GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue)
{
- PathBuilder DataFilePath;
+ PathBuilder<256> DataFilePath;
BuildPath(DataFilePath, HashKey);
RwLock::SharedLockScope ValueLock(LockForHash(HashKey));
- if (IoBuffer Data = IoBufferBuilder::MakeFromFile(DataFilePath.c_str()))
+ if (IoBuffer Data = IoBufferBuilder::MakeFromFile(DataFilePath.ToPath()))
{
OutValue.Value = Data;
OutValue.Value.SetContentType(Loc.GetContentType());
@@ -970,8 +972,8 @@ ZenCacheDiskLayer::CacheBucket::CollectGarbage(GcContext& GcCtx)
// Remove all standalone file(s)
// NOTE: This can probably be made asynchronously
{
- std::error_code Ec;
- PathBuilder Path;
+ std::error_code Ec;
+ PathBuilder<256> Path;
for (const auto& Entry : ExpiredEntries)
{
@@ -988,7 +990,7 @@ ZenCacheDiskLayer::CacheBucket::CollectGarbage(GcContext& GcCtx)
if (Ec)
{
- ZEN_WARN("delete expired z$ standalone file '{}' FAILED, reason '{}'", WideToUtf8(Path.ToString()), Ec.message());
+ ZEN_WARN("delete expired z$ standalone file '{}' FAILED, reason '{}'", Path.ToUtf8(), Ec.message());
Ec.clear();
}
}
@@ -1168,7 +1170,7 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c
{
RwLock::ExclusiveLockScope ValueLock(LockForHash(HashKey));
- PathBuilder DataFilePath;
+ PathBuilder<256> DataFilePath;
BuildPath(DataFilePath, HashKey);
TemporaryFile DataFile;
@@ -1190,7 +1192,7 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c
// Move file into place (atomically)
- std::filesystem::path FsPath{DataFilePath.c_str()};
+ std::filesystem::path FsPath{DataFilePath.ToPath()};
DataFile.MoveTemporaryIntoPlace(FsPath, Ec);
@@ -1200,7 +1202,7 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c
do
{
- std::filesystem::path ParentPath = std::filesystem::path(DataFilePath.c_str()).parent_path();
+ std::filesystem::path ParentPath = FsPath.parent_path();
CreateDirectories(ParentPath);
DataFile.MoveTemporaryIntoPlace(FsPath, Ec);
@@ -1225,7 +1227,7 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c
if (Ec)
{
- throw std::system_error(Ec, "Failed to finalize file '{}'"_format(WideToUtf8(DataFilePath)));
+ throw std::system_error(Ec, "Failed to finalize file '{}'"_format(DataFilePath.ToUtf8()));
}
}
@@ -1371,11 +1373,11 @@ ZenCacheDiskLayer::DiscoverBuckets()
virtual bool VisitDirectory([[maybe_unused]] const std::filesystem::path& Parent, const path_view& DirectoryName) override
{
- Dirs.push_back(std::wstring(DirectoryName));
+ Dirs.push_back((decltype(Dirs)::value_type)(DirectoryName));
return false;
}
- std::vector<std::wstring> Dirs;
+ std::vector<std::filesystem::path::string_type> Dirs;
} Visit;
Traversal.TraverseFileSystem(m_RootDir, Visit);
@@ -1384,11 +1386,15 @@ ZenCacheDiskLayer::DiscoverBuckets()
RwLock::ExclusiveLockScope _(m_Lock);
- for (const std::wstring& BucketName : Visit.Dirs)
+ for (const auto& BucketName : Visit.Dirs)
{
// New bucket needs to be created
- const std::string BucketName8 = ToUtf8(BucketName);
+#if ZEN_PLATFORM_WINDOWS
+ std::string BucketName8 = WideToUtf8(BucketName);
+#else
+ const auto& BucketName8 = BucketName;
+#endif
if (auto It = m_Buckets.find(BucketName8); It != m_Buckets.end())
{
diff --git a/zenserver/cache/structuredcachestore.h b/zenserver/cache/structuredcachestore.h
index b64b1353e..41b47b409 100644
--- a/zenserver/cache/structuredcachestore.h
+++ b/zenserver/cache/structuredcachestore.h
@@ -23,9 +23,9 @@ ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
+class PathBuilderBase;
class CasStore;
class CasGc;
-class WideStringBuilderBase;
class ZenCacheTracker;
/******************************************************************************