aboutsummaryrefslogtreecommitdiff
path: root/zenserver
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2021-11-12 11:08:53 +0100
committerPer Larsson <[email protected]>2021-11-12 11:08:53 +0100
commit3faf0b57c625152a8facfca1c4995bd9edc95707 (patch)
tree0ae1ff078147091bafb7f5f4799943cebd0a7f50 /zenserver
parentChanged from batch to RPC. (diff)
downloadzen-3faf0b57c625152a8facfca1c4995bd9edc95707.tar.xz
zen-3faf0b57c625152a8facfca1c4995bd9edc95707.zip
Movec cache utility types to zenutil and fixed unit tests.
Diffstat (limited to 'zenserver')
-rw-r--r--zenserver/cache/cachekey.cpp163
-rw-r--r--zenserver/cache/cachekey.h135
-rw-r--r--zenserver/cache/structuredcache.cpp49
-rw-r--r--zenserver/upstream/upstreamcache.h3
-rw-r--r--zenserver/zenserver.vcxproj2
-rw-r--r--zenserver/zenserver.vcxproj.filters6
6 files changed, 27 insertions, 331 deletions
diff --git a/zenserver/cache/cachekey.cpp b/zenserver/cache/cachekey.cpp
deleted file mode 100644
index 2ead9ac58..000000000
--- a/zenserver/cache/cachekey.cpp
+++ /dev/null
@@ -1,163 +0,0 @@
-// Copyright Epic Games, Inc. All Rights Reserved.
-
-#include "cachekey.h"
-
-#include <zencore/compactbinary.h>
-#include <zencore/compactbinarybuilder.h>
-#include <zencore/string.h>
-
-namespace zen {
-
-using namespace std::literals;
-
-namespace detail { namespace cacheopt {
- constexpr std::string_view Local = "local"sv;
- constexpr std::string_view Remote = "remote"sv;
- constexpr std::string_view Data = "data"sv;
- constexpr std::string_view Meta = "meta"sv;
- constexpr std::string_view Value = "value"sv;
- constexpr std::string_view Attachments = "attachments"sv;
-}} // namespace detail::cacheopt
-
-CachePolicy
-ParseQueryCachePolicy(std::string_view QueryPolicy, CachePolicy Default)
-{
- if (QueryPolicy.empty())
- {
- return Default;
- }
-
- CachePolicy Result = CachePolicy::None;
-
- ForEachStrTok(QueryPolicy, ',', [&Result](const std::string_view& Token) {
- if (Token == detail::cacheopt::Local)
- {
- Result |= CachePolicy::QueryLocal;
- }
- if (Token == detail::cacheopt::Remote)
- {
- Result |= CachePolicy::QueryRemote;
- }
- return true;
- });
-
- return Result;
-}
-
-CachePolicy
-ParseStoreCachePolicy(std::string_view StorePolicy, CachePolicy Default)
-{
- if (StorePolicy.empty())
- {
- return Default;
- }
-
- CachePolicy Result = CachePolicy::None;
-
- ForEachStrTok(StorePolicy, ',', [&Result](const std::string_view& Token) {
- if (Token == detail::cacheopt::Local)
- {
- Result |= CachePolicy::StoreLocal;
- }
- if (Token == detail::cacheopt::Remote)
- {
- Result |= CachePolicy::StoreRemote;
- }
- return true;
- });
-
- return Result;
-}
-
-CachePolicy
-ParseSkipCachePolicy(std::string_view SkipPolicy, CachePolicy Default)
-{
- if (SkipPolicy.empty())
- {
- return Default;
- }
-
- CachePolicy Result = CachePolicy::None;
-
- ForEachStrTok(SkipPolicy, ',', [&Result](const std::string_view& Token) {
- if (Token == detail::cacheopt::Meta)
- {
- Result |= CachePolicy::SkipMeta;
- }
- if (Token == detail::cacheopt::Value)
- {
- Result |= CachePolicy::SkipValue;
- }
- if (Token == detail::cacheopt::Attachments)
- {
- Result |= CachePolicy::SkipAttachments;
- }
- if (Token == detail::cacheopt::Data)
- {
- Result |= CachePolicy::SkipData;
- }
- return true;
- });
-
- return Result;
-}
-
-CachePolicy
-CacheRecordPolicy::GetPayloadPolicy(const Oid& PayloadId) const
-{
- if (const auto It = m_PayloadPolicies.find(PayloadId); It != m_PayloadPolicies.end())
- {
- return It->second;
- }
-
- return m_DefaultPayloadPolicy;
-}
-
-bool
-CacheRecordPolicy::Load(CbObjectView RecordPolicyObject, CacheRecordPolicy& OutRecordPolicy)
-{
- using namespace std::literals;
-
- const uint32_t RecordPolicy = RecordPolicyObject["RecordPolicy"sv].AsUInt32(static_cast<uint32_t>(CachePolicy::Default));
- const uint32_t DefaultPayloadPolicy =
- RecordPolicyObject["DefaultPayloadPolicy"sv].AsUInt32(static_cast<uint32_t>(CachePolicy::Default));
-
- OutRecordPolicy.m_RecordPolicy = static_cast<CachePolicy>(RecordPolicy);
- OutRecordPolicy.m_DefaultPayloadPolicy = static_cast<CachePolicy>(DefaultPayloadPolicy);
-
- for (CbFieldView PayloadPolicyView : RecordPolicyObject["PayloadPolicies"sv])
- {
- CbObjectView PayloadPolicyObject = PayloadPolicyView.AsObjectView();
- const Oid PayloadId = PayloadPolicyObject["Id"sv].AsObjectId();
- const uint32_t PayloadPolicy = PayloadPolicyObject["Policy"sv].AsUInt32();
-
- if (PayloadId != Oid::Zero && PayloadPolicy != 0)
- {
- OutRecordPolicy.m_PayloadPolicies.emplace(PayloadId, static_cast<CachePolicy>(PayloadPolicy));
- }
- }
-
- return true;
-}
-
-void
-CacheRecordPolicy::Save(const CacheRecordPolicy& Policy, CbWriter& Writer)
-{
- Writer << "RecordPolicy"sv << static_cast<uint32_t>(Policy.GetRecordPolicy());
- Writer << "DefaultPayloadPolicy"sv << static_cast<uint32_t>(Policy.GetDefaultPayloadPolicy());
-
- if (!Policy.m_PayloadPolicies.empty())
- {
- Writer.BeginArray("PayloadPolicies"sv);
- for (const auto& Kv : Policy.m_PayloadPolicies)
- {
- Writer.AddObjectId("Id"sv, Kv.first);
- Writer << "Policy"sv << static_cast<uint32_t>(Kv.second);
- }
- Writer.EndArray();
- }
-}
-
-const CacheKey CacheKey::Empty = CacheKey{.Bucket = std::string(), .Hash = IoHash()};
-
-} // namespace zen
diff --git a/zenserver/cache/cachekey.h b/zenserver/cache/cachekey.h
deleted file mode 100644
index c32f7ed87..000000000
--- a/zenserver/cache/cachekey.h
+++ /dev/null
@@ -1,135 +0,0 @@
-// Copyright Epic Games, Inc. All Rights Reserved.
-
-#pragma once
-
-#include <zencore/iohash.h>
-#include <zencore/string.h>
-#include <zencore/uid.h>
-#include <gsl/gsl-lite.hpp>
-
-#include <unordered_map>
-
-namespace zen {
-
-class CbObjectView;
-class CbWriter;
-
-enum class CachePolicy : uint8_t
-{
- None = 0,
- QueryLocal = 1 << 0,
- QueryRemote = 1 << 1,
- Query = QueryLocal | QueryRemote,
- StoreLocal = 1 << 2,
- StoreRemote = 1 << 3,
- Store = StoreLocal | StoreRemote,
- SkipMeta = 1 << 4,
- SkipValue = 1 << 5,
- SkipAttachments = 1 << 6,
- SkipData = SkipMeta | SkipValue | SkipAttachments,
- SkipLocalCopy = 1 << 7,
- Local = QueryLocal | StoreLocal,
- Remote = QueryRemote | StoreRemote,
- Default = Query | Store,
- Disable = None,
-};
-
-gsl_DEFINE_ENUM_BITMASK_OPERATORS(CachePolicy);
-
-CachePolicy ParseQueryCachePolicy(std::string_view QueryPolicy, CachePolicy Default = CachePolicy::Query);
-
-CachePolicy ParseStoreCachePolicy(std::string_view StorePolicy, CachePolicy Default = CachePolicy::Store);
-
-CachePolicy ParseSkipCachePolicy(std::string_view SkipPolicy, CachePolicy Default = CachePolicy::None);
-
-class CacheRecordPolicy
-{
-public:
- CacheRecordPolicy() = default;
-
- CachePolicy GetRecordPolicy() const { return m_RecordPolicy; }
- CachePolicy GetPayloadPolicy(const Oid& PayloadId) const;
- CachePolicy GetDefaultPayloadPolicy() const { return m_DefaultPayloadPolicy; }
-
- static bool Load(CbObjectView RecordPolicyObject, CacheRecordPolicy& OutRecordPolicy);
- static void Save(const CacheRecordPolicy& Policy, CbWriter& Writer);
-
-private:
- using PayloadPolicyMap = std::unordered_map<Oid, CachePolicy, Oid::Hasher>;
-
- CachePolicy m_RecordPolicy = CachePolicy::Default;
- CachePolicy m_DefaultPayloadPolicy = CachePolicy::Default;
- PayloadPolicyMap m_PayloadPolicies;
-};
-
-struct CacheKey
-{
- std::string Bucket;
- IoHash Hash;
-
- static CacheKey Create(std::string_view Bucket, const IoHash& Hash) { return {.Bucket = ToLower(Bucket), .Hash = Hash}; }
-
- static const CacheKey Empty;
-};
-
-inline bool
-operator==(const CacheKey& A, const CacheKey& B)
-{
- return A.Bucket == B.Bucket && A.Hash == B.Hash;
-}
-
-inline bool
-operator!=(const CacheKey& A, const CacheKey& B)
-{
- return A.Bucket != B.Bucket || A.Hash != B.Hash;
-}
-
-inline bool
-operator<(const CacheKey& A, const CacheKey& B)
-{
- const std::string& BucketA = A.Bucket;
- const std::string& BucketB = B.Bucket;
- return BucketA == BucketB ? A.Hash < B.Hash : BucketA < BucketB;
-}
-
-struct CacheChunkRequest
-{
- CacheKey Key;
- IoHash ChunkId;
- Oid PayloadId;
- uint64_t RawOffset = 0ull;
- uint64_t RawSize = ~uint64_t(0);
- CachePolicy Policy = CachePolicy::Default;
-};
-
-inline bool
-operator<(const CacheChunkRequest& A, const CacheChunkRequest& B)
-{
- if (A.Key < B.Key)
- {
- return true;
- }
- if (B.Key < A.Key)
- {
- return false;
- }
- if (A.ChunkId < B.ChunkId)
- {
- return true;
- }
- if (B.ChunkId < A.ChunkId)
- {
- return false;
- }
- if (A.PayloadId < B.PayloadId)
- {
- return true;
- }
- if (B.PayloadId < A.PayloadId)
- {
- return false;
- }
- return A.RawOffset < B.RawOffset;
-}
-
-} // namespace zen
diff --git a/zenserver/cache/structuredcache.cpp b/zenserver/cache/structuredcache.cpp
index e78e583aa..1b6406562 100644
--- a/zenserver/cache/structuredcache.cpp
+++ b/zenserver/cache/structuredcache.cpp
@@ -11,8 +11,9 @@
#include <zencore/timer.h>
#include <zenhttp/httpserver.h>
#include <zenstore/CAS.h>
+#include <zenutil/cache/cache.h>
-#include "cachekey.h"
+//#include "cachekey.h"
#include "monitoring/httpstats.h"
#include "structuredcache.h"
#include "structuredcachestore.h"
@@ -796,15 +797,15 @@ HttpStructuredCacheService::HandleRpcRequest(zen::HttpServerRequest& Request)
}
Request.WriteResponseAsync(
- [this, BatchRequest = zen::LoadCompactBinaryObject(Request.ReadPayload())](HttpServerRequest& AsyncRequest) {
- const std::string_view Method = BatchRequest["Method"sv].AsString();
+ [this, RpcRequest = zen::LoadCompactBinaryObject(Request.ReadPayload())](HttpServerRequest& AsyncRequest) {
+ const std::string_view Method = RpcRequest["Method"sv].AsString();
if (Method == "GetCacheRecords"sv)
{
- HandleRpcGetCacheRecords(AsyncRequest, BatchRequest);
+ HandleRpcGetCacheRecords(AsyncRequest, RpcRequest);
}
else if (Method == "GetCachePayloads"sv)
{
- HandleRpcGetCachePayloads(AsyncRequest, BatchRequest);
+ HandleRpcGetCachePayloads(AsyncRequest, RpcRequest);
}
else
{
@@ -820,18 +821,18 @@ HttpStructuredCacheService::HandleRpcRequest(zen::HttpServerRequest& Request)
}
void
-HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Request, CbObjectView BatchRequest)
+HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Request, CbObjectView RpcRequest)
{
using namespace fmt::literals;
- CbPackage BatchResponse;
+ CbPackage RpcResponse;
CacheRecordPolicy Policy;
- CbObjectView Params = BatchRequest["Params"sv].AsObjectView();
+ CbObjectView Params = RpcRequest["Params"sv].AsObjectView();
std::vector<CacheKey> CacheKeys;
std::vector<IoBuffer> CacheValues;
std::vector<size_t> UpstreamRequests;
- ZEN_ASSERT(BatchRequest["Method"sv].AsString() == "GetCacheRecords"sv);
+ ZEN_ASSERT(RpcRequest["Method"sv].AsString() == "GetCacheRecords"sv);
CacheRecordPolicy::Load(Params["Policy"sv].AsObjectView(), Policy);
@@ -860,10 +861,10 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
if (!SkipAttachments)
{
- CacheRecord.IterateAttachments([this, &BatchResponse](CbFieldView AttachmentHash) {
+ CacheRecord.IterateAttachments([this, &RpcResponse](CbFieldView AttachmentHash) {
if (IoBuffer Chunk = m_CidStore.FindChunkByCid(AttachmentHash.AsHash()))
{
- BatchResponse.AddAttachment(CbAttachment(CompressedBuffer::FromCompressed(SharedBuffer(Chunk))));
+ RpcResponse.AddAttachment(CbAttachment(CompressedBuffer::FromCompressed(SharedBuffer(Chunk))));
}
});
}
@@ -893,7 +894,7 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
if (!UpstreamRequests.empty() && m_UpstreamCache)
{
const auto OnCacheRecordGetComplete =
- [this, &CacheKeys, &CacheValues, &BatchResponse, SkipAttachments](CacheRecordGetCompleteParams&& Params) {
+ [this, &CacheKeys, &CacheValues, &RpcResponse, SkipAttachments](CacheRecordGetCompleteParams&& Params) {
if (Params.Record)
{
Params.Record.IterateAttachments([&](CbFieldView AttachmentHash) {
@@ -905,7 +906,7 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
if (!SkipAttachments)
{
- BatchResponse.AddAttachment(CbAttachment(Compressed));
+ RpcResponse.AddAttachment(CbAttachment(Compressed));
}
}
}
@@ -917,6 +918,7 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
NiceBytes(Params.Record.GetView().GetSize()),
ToString(HttpContentType::kCbObject));
+ ZEN_ASSERT(Params.KeyIndex < CacheValues.size());
CacheValues[Params.KeyIndex] = IoBufferBuilder::MakeCloneFromMemory(Params.Record.GetView());
m_CacheStats.HitCount++;
m_CacheStats.UpstreamHitCount++;
@@ -948,10 +950,10 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
}
ResponseObject.EndArray();
- BatchResponse.SetObject(ResponseObject.Save());
+ RpcResponse.SetObject(ResponseObject.Save());
BinaryWriter MemStream;
- BatchResponse.Save(MemStream);
+ RpcResponse.Save(MemStream);
Request.WriteResponse(HttpResponseCode::OK,
HttpContentType::kCbPackage,
@@ -959,16 +961,16 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req
}
void
-HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Request, CbObjectView BatchRequest)
+HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Request, CbObjectView RpcRequest)
{
using namespace fmt::literals;
- ZEN_ASSERT(BatchRequest["Method"sv].AsString() == "GetCachePayloads"sv);
+ ZEN_ASSERT(RpcRequest["Method"sv].AsString() == "GetCachePayloads"sv);
std::vector<CacheChunkRequest> ChunkRequests;
std::vector<size_t> UpstreamRequests;
std::vector<IoBuffer> Chunks;
- CbObjectView Params = BatchRequest["Params"sv].AsObjectView();
+ CbObjectView Params = RpcRequest["Params"sv].AsObjectView();
for (CbFieldView RequestView : Params["ChunkRequests"sv])
{
@@ -1029,7 +1031,7 @@ HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Re
CurrentKey = ChunkRequest.Key;
ZenCacheValue CacheValue;
- if (m_CacheStore.Get(ChunkRequest.Key.Bucket, ChunkRequest.Key.Hash, CacheValue))
+ if (m_CacheStore.Get(CurrentKey.Bucket, CurrentKey.Hash, CacheValue))
{
CurrentRecordBuffer = CacheValue.Value;
}
@@ -1094,6 +1096,7 @@ HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Re
NiceBytes(Params.Payload.GetSize()),
"UPSTREAM");
+ ZEN_ASSERT(Params.RequestIndex < Chunks.size());
Chunks[Params.RequestIndex] = std::move(Params.Payload);
m_CacheStats.HitCount++;
@@ -1109,7 +1112,7 @@ HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Re
m_UpstreamCache->GetCachePayloads(ChunkRequests, UpstreamRequests, std::move(OnCachePayloadGetComplete));
}
- CbPackage BatchResponse;
+ CbPackage RpcResponse;
CbObjectWriter ResponseObject;
ResponseObject.BeginArray("Result"sv);
@@ -1119,7 +1122,7 @@ HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Re
if (Chunks[ChunkIndex])
{
ResponseObject << ChunkRequests[ChunkIndex].ChunkId;
- BatchResponse.AddAttachment(CbAttachment(CompressedBuffer::FromCompressed(SharedBuffer(std::move(Chunks[ChunkIndex])))));
+ RpcResponse.AddAttachment(CbAttachment(CompressedBuffer::FromCompressed(SharedBuffer(std::move(Chunks[ChunkIndex])))));
}
else
{
@@ -1128,10 +1131,10 @@ HttpStructuredCacheService::HandleRpcGetCachePayloads(zen::HttpServerRequest& Re
}
ResponseObject.EndArray();
- BatchResponse.SetObject(ResponseObject.Save());
+ RpcResponse.SetObject(ResponseObject.Save());
BinaryWriter MemStream;
- BatchResponse.Save(MemStream);
+ RpcResponse.Save(MemStream);
Request.WriteResponse(HttpResponseCode::OK,
HttpContentType::kCbPackage,
diff --git a/zenserver/upstream/upstreamcache.h b/zenserver/upstream/upstreamcache.h
index 681d8e96f..e5c3521b9 100644
--- a/zenserver/upstream/upstreamcache.h
+++ b/zenserver/upstream/upstreamcache.h
@@ -2,11 +2,10 @@
#pragma once
-#include "cache/cachekey.h"
-
#include <zencore/iobuffer.h>
#include <zencore/iohash.h>
#include <zencore/zencore.h>
+#include <zenutil/cache/cache.h>
#include <atomic>
#include <chrono>
diff --git a/zenserver/zenserver.vcxproj b/zenserver/zenserver.vcxproj
index d90dd009a..d954d3f8d 100644
--- a/zenserver/zenserver.vcxproj
+++ b/zenserver/zenserver.vcxproj
@@ -105,7 +105,6 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="admin\admin.h" />
- <ClInclude Include="cache\cachekey.h" />
<ClInclude Include="cache\structuredcache.h" />
<ClInclude Include="cache\structuredcachestore.h" />
<ClInclude Include="compute\apply.h" />
@@ -132,7 +131,6 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="admin\admin.cpp" />
- <ClCompile Include="cache\cachekey.cpp" />
<ClCompile Include="cache\structuredcache.cpp" />
<ClCompile Include="cache\structuredcachestore.cpp" />
<ClCompile Include="compute\apply.cpp" />
diff --git a/zenserver/zenserver.vcxproj.filters b/zenserver/zenserver.vcxproj.filters
index ae5411afb..04c6267ba 100644
--- a/zenserver/zenserver.vcxproj.filters
+++ b/zenserver/zenserver.vcxproj.filters
@@ -41,9 +41,6 @@
<ClInclude Include="experimental\vfs.h" />
<ClInclude Include="monitoring\httpstats.h" />
<ClInclude Include="monitoring\httpstatus.h" />
- <ClInclude Include="cache\cachekey.h">
- <Filter>cache</Filter>
- </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="zenserver.cpp" />
@@ -79,9 +76,6 @@
<ClCompile Include="experimental\vfs.cpp" />
<ClCompile Include="monitoring\httpstats.cpp" />
<ClCompile Include="monitoring\httpstatus.cpp" />
- <ClCompile Include="cache\cachekey.cpp">
- <Filter>cache</Filter>
- </ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="cache">