diff options
| author | Stefan Boberg <[email protected]> | 2021-10-03 21:40:20 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-10-03 21:40:20 +0200 |
| commit | 132c476c82e82ed7978e82067c9159aaeebbd36f (patch) | |
| tree | ded8d61005361a7d9feb7e5a1aa1153e79d98839 | |
| parent | Merge branch 'main' of https://github.com/EpicGames/zen (diff) | |
| parent | http: Moved logic for body suppression to a more central location (diff) | |
| download | zen-132c476c82e82ed7978e82067c9159aaeebbd36f.tar.xz zen-132c476c82e82ed7978e82067c9159aaeebbd36f.zip | |
Merge branch 'main' of https://github.com/EpicGames/zen
| -rw-r--r-- | zencore/include/zencore/stats.h | 2 | ||||
| -rw-r--r-- | zencore/stats.cpp | 11 | ||||
| -rw-r--r-- | zenhttp/httpserver.cpp | 40 | ||||
| -rw-r--r-- | zenhttp/httpsys.cpp | 42 | ||||
| -rw-r--r-- | zenserver-test/zenserver-test.cpp | 106 | ||||
| -rw-r--r-- | zenserver/cache/structuredcache.cpp | 41 | ||||
| -rw-r--r-- | zenserver/projectstore.cpp | 5 |
7 files changed, 205 insertions, 42 deletions
diff --git a/zencore/include/zencore/stats.h b/zencore/include/zencore/stats.h index dfa8dac34..2e567d614 100644 --- a/zencore/include/zencore/stats.h +++ b/zencore/include/zencore/stats.h @@ -209,6 +209,8 @@ public: Scope(OperationTiming& Outer); ~Scope(); + void Cancel(); + private: OperationTiming& m_Outer; uint64_t m_StartTick; diff --git a/zencore/stats.cpp b/zencore/stats.cpp index 34dc2828f..47efba76b 100644 --- a/zencore/stats.cpp +++ b/zencore/stats.cpp @@ -398,7 +398,16 @@ OperationTiming::Scope::Scope(OperationTiming& Outer) : m_Outer(Outer), m_StartT OperationTiming::Scope::~Scope() { - m_Outer.Update(GetHifreqTimerValue() - m_StartTick); + if (m_StartTick != 0) + { + m_Outer.Update(GetHifreqTimerValue() - m_StartTick); + } +} + +void +OperationTiming::Scope::Cancel() +{ + m_StartTick = 0; } ////////////////////////////////////////////////////////////////////////// diff --git a/zenhttp/httpserver.cpp b/zenhttp/httpserver.cpp index cfd1463ba..193426ed2 100644 --- a/zenhttp/httpserver.cpp +++ b/zenhttp/httpserver.cpp @@ -67,13 +67,16 @@ MapContentTypeToString(HttpContentType ContentType) ////////////////////////////////////////////////////////////////////////// static constinit uint32_t HashBinary = HashStringDjb2("application/octet-stream"sv); -static constinit uint32_t HashJson = HashStringDjb2("application/json"sv); -static constinit uint32_t HashYaml = HashStringDjb2("text/yaml"sv); +static constinit uint32_t HashApplicationJson = HashStringDjb2("application/json"sv); +static constinit uint32_t HashApplicationYaml = HashStringDjb2("text/yaml"sv); static constinit uint32_t HashText = HashStringDjb2("text/plain"sv); static constinit uint32_t HashCompactBinary = HashStringDjb2("application/x-ue-cb"sv); static constinit uint32_t HashCompactBinaryPackage = HashStringDjb2("application/x-ue-cbpkg"sv); static constinit uint32_t HashCompactBinaryPackageOffer = HashStringDjb2("application/x-ue-offer"sv); static constinit uint32_t HashCompressedBinary = HashStringDjb2("application/x-ue-comp"sv); +static constinit uint32_t HashJson = HashStringDjb2("json"sv); +static constinit uint32_t HashYaml = HashStringDjb2("yaml"sv); +static constinit uint32_t HashHtml = HashStringDjb2("text/html"sv); std::once_flag InitContentTypeLookup; @@ -81,14 +84,21 @@ struct HashedTypeEntry { uint32_t Hash; HttpContentType Type; -} TypeHashTable[] = {{HashBinary, HttpContentType::kBinary}, - {HashCompactBinary, HttpContentType::kCbObject}, - {HashCompactBinaryPackage, HttpContentType::kCbPackage}, - {HashCompactBinaryPackageOffer, HttpContentType::kCbPackageOffer}, - {HashJson, HttpContentType::kJSON}, - {HashYaml, HttpContentType::kYAML}, - {HashText, HttpContentType::kText}, - {HashCompressedBinary, HttpContentType::kCompressedBinary}}; +} TypeHashTable[] = { + // clang-format off + {HashBinary, HttpContentType::kBinary}, + {HashCompactBinary, HttpContentType::kCbObject}, + {HashCompactBinaryPackage, HttpContentType::kCbPackage}, + {HashCompactBinaryPackageOffer, HttpContentType::kCbPackageOffer}, + {HashJson, HttpContentType::kJSON}, + {HashApplicationJson, HttpContentType::kJSON}, + {HashYaml, HttpContentType::kYAML}, + {HashApplicationYaml, HttpContentType::kYAML}, + {HashText, HttpContentType::kText}, + {HashCompressedBinary, HttpContentType::kCompressedBinary}, + {HashHtml, HttpContentType::kHTML}, + // clang-format on +}; HttpContentType ParseContentTypeImpl(const std::string_view& ContentTypeString) @@ -120,6 +130,16 @@ ParseContentTypeInit(const std::string_view& ContentTypeString) std::sort(std::begin(TypeHashTable), std::end(TypeHashTable), [](const HashedTypeEntry& Lhs, const HashedTypeEntry& Rhs) { return Lhs.Hash < Rhs.Hash; }); + + // validate that there are no hash collisions + + uint32_t LastHash = 0; + + for (const auto& Item : TypeHashTable) + { + ZEN_ASSERT(LastHash != Item.Hash); + LastHash = Item.Hash; + } }); ParseContentType = ParseContentTypeImpl; diff --git a/zenhttp/httpsys.cpp b/zenhttp/httpsys.cpp index 9b2e7f832..fedaf282e 100644 --- a/zenhttp/httpsys.cpp +++ b/zenhttp/httpsys.cpp @@ -1086,6 +1086,8 @@ HttpSysServerRequest::HttpSysServerRequest(HttpSysTransaction& Tx, HttpService& const int PrefixLength = Service.UriPrefixLength(); const int AbsPathLength = HttpRequestPtr->CookedUrl.AbsPathLength / sizeof(char16_t); + HttpContentType AcceptContentType = HttpContentType::kUnknownContentType; + if (AbsPathLength >= PrefixLength) { // We convert the URI immediately because most of the code involved prefers to deal @@ -1094,15 +1096,33 @@ HttpSysServerRequest::HttpSysServerRequest(HttpSysTransaction& Tx, HttpService& WideToUtf8({(char16_t*)HttpRequestPtr->CookedUrl.pAbsPath + PrefixLength, gsl::narrow<size_t>(AbsPathLength - PrefixLength)}, m_UriUtf8); + + std::string_view Uri8{m_UriUtf8}; + + const size_t LastComponentIndex = Uri8.find_last_of('/'); + + if (LastComponentIndex != std::string_view::npos) + { + Uri8.remove_prefix(LastComponentIndex); + } + + const size_t LastDotIndex = Uri8.find_last_of('.'); + + if (LastDotIndex != std::string_view::npos) + { + Uri8.remove_prefix(LastDotIndex + 1); + } + + AcceptContentType = ParseContentType(Uri8); } else { m_UriUtf8.Reset(); } - if (auto QueryStringLength = HttpRequestPtr->CookedUrl.QueryStringLength) + if (uint16_t QueryStringLength = HttpRequestPtr->CookedUrl.QueryStringLength) { - --QueryStringLength; + --QueryStringLength; // We skip the leading question mark WideToUtf8({(char16_t*)(HttpRequestPtr->CookedUrl.pQueryString) + 1, QueryStringLength / sizeof(char16_t)}, m_QueryStringUtf8); } @@ -1114,7 +1134,23 @@ HttpSysServerRequest::HttpSysServerRequest(HttpSysTransaction& Tx, HttpService& m_Verb = TranslateHttpVerb(HttpRequestPtr->Verb); m_ContentLength = GetContentLength(HttpRequestPtr); m_ContentType = GetContentType(HttpRequestPtr); - m_AcceptType = GetAcceptType(HttpRequestPtr); + + // It an explicit content type extension was specified then we'll use that over any + // Accept: header value that may be present + + if (AcceptContentType != HttpContentType::kUnknownContentType) + { + m_AcceptType = AcceptContentType; + } + else + { + m_AcceptType = GetAcceptType(HttpRequestPtr); + } + + if (m_Verb == HttpVerb::kHead) + { + SetSuppressResponseBody(); + } } Oid diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index 0e5e73ffc..fe21aa834 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -1448,16 +1448,21 @@ TEST_CASE("zcache.policy") return Buf; }; - auto GeneratePackage = [](zen::IoHash& OutAttachmentKey) -> zen::CbPackage { + auto GeneratePackage = [](zen::IoHash& OutRecordKey, zen::IoHash& OutAttachmentKey) -> zen::CbPackage { auto Data = zen::SharedBuffer::Clone(zen::MakeMemoryView<uint8_t>({1, 2, 3, 4, 5, 6, 7, 8, 9})); auto CompressedData = zen::CompressedBuffer::Compress(Data); OutAttachmentKey = zen::IoHash::FromBLAKE3(CompressedData.GetRawHash()); - zen::CbWriter Obj; - Obj.BeginObject("obj"sv); - Obj.AddBinaryAttachment("data", OutAttachmentKey); - Obj.EndObject(); + + zen::CbWriter Writer; + Writer.BeginObject("obj"sv); + Writer.AddBinaryAttachment("data", OutAttachmentKey); + Writer.EndObject(); + CbObject CacheRecord = Writer.Save().AsObject(); + + OutRecordKey = IoHash::HashBuffer(CacheRecord.GetBuffer().GetView()); + zen::CbPackage Package; - Package.SetObject(Obj.Save().AsObject()); + Package.SetObject(CacheRecord); Package.AddAttachment(zen::CbAttachment(CompressedData)); return Package; @@ -1587,7 +1592,8 @@ TEST_CASE("zcache.policy") LocalCfg.Spawn(LocalInst); zen::IoHash Key; - zen::CbPackage Package = GeneratePackage(Key); + zen::IoHash PayloadId; + zen::CbPackage Package = GeneratePackage(Key, PayloadId); auto Buf = ToBuffer(Package); // Store package upstream @@ -1623,7 +1629,8 @@ TEST_CASE("zcache.policy") LocalCfg.Spawn(LocalInst); zen::IoHash Key; - zen::CbPackage Package = GeneratePackage(Key); + zen::IoHash PayloadId; + zen::CbPackage Package = GeneratePackage(Key, PayloadId); auto Buf = ToBuffer(Package); // Store packge locally @@ -1659,7 +1666,8 @@ TEST_CASE("zcache.policy") LocalCfg.Spawn(LocalInst); zen::IoHash Key; - zen::CbPackage Package = GeneratePackage(Key); + zen::IoHash PayloadId; + zen::CbPackage Package = GeneratePackage(Key, PayloadId); auto Buf = ToBuffer(Package); // Store package locally and upstream @@ -1692,7 +1700,8 @@ TEST_CASE("zcache.policy") LocalCfg.Spawn(LocalInst); zen::IoHash Key; - zen::CbPackage Package = GeneratePackage(Key); + zen::IoHash PayloadId; + zen::CbPackage Package = GeneratePackage(Key, PayloadId); auto Buf = ToBuffer(Package); // Store package locally @@ -1748,7 +1757,8 @@ TEST_CASE("zcache.policy") LocalCfg.Spawn(LocalInst); zen::IoHash Key; - zen::CbPackage Package = GeneratePackage(Key); + zen::IoHash PayloadId; + zen::CbPackage Package = GeneratePackage(Key, PayloadId); auto Buf = ToBuffer(Package); // Store package upstream @@ -1791,6 +1801,80 @@ TEST_CASE("zcache.policy") CHECK(Package.GetAttachments().size() != 0); } } + + SUBCASE("skip - 'data' returns empty cache record/payload") + { + ZenConfig Cfg = ZenConfig::New(); + ZenServerInstance Instance(TestEnv); + const auto Bucket = "test"sv; + + Cfg.Spawn(Instance); + + zen::IoHash Key; + zen::IoHash PayloadId; + zen::CbPackage Package = GeneratePackage(Key, PayloadId); + auto Buf = ToBuffer(Package); + + // Store package + { + cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(Cfg.BaseUri, Bucket, Key)}, + cpr::Body{(const char*)Buf.GetData(), Buf.GetSize()}, + cpr::Header{{"Content-Type", "application/x-ue-cbpkg"}}); + CHECK(Result.status_code == 201); + } + + // Get package + { + cpr::Response Result = cpr::Get(cpr::Url{"{}/{}/{}?skip=data"_format(Cfg.BaseUri, Bucket, Key)}, + cpr::Header{{"Accept", "application/x-ue-cbpkg"}}); + CHECK(Result.status_code == 200); + CHECK(Result.text.size() == 0); + } + + // Get record + { + cpr::Response Result = cpr::Get(cpr::Url{"{}/{}/{}?skip=data"_format(Cfg.BaseUri, Bucket, Key)}, + cpr::Header{{"Accept", "application/x-ue-cbobject"}}); + CHECK(Result.status_code == 200); + CHECK(Result.text.size() == 0); + } + + // Get payload + { + cpr::Response Result = cpr::Get(cpr::Url{"{}/{}/{}/{}?skip=data"_format(Cfg.BaseUri, Bucket, Key, PayloadId)}, + cpr::Header{{"Accept", "application/x-ue-comp"}}); + CHECK(Result.status_code == 200); + CHECK(Result.text.size() == 0); + } + } + + SUBCASE("skip - 'data' returns empty binary value") + { + ZenConfig Cfg = ZenConfig::New(); + ZenServerInstance Instance(TestEnv); + const auto Bucket = "test"sv; + + Cfg.Spawn(Instance); + + zen::IoHash Key; + auto BinaryValue = GenerateData(1024, Key); + + // Store binary cache value + { + cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(Cfg.BaseUri, Bucket, Key)}, + cpr::Body{(const char*)BinaryValue.GetData(), BinaryValue.GetSize()}, + cpr::Header{{"Content-Type", "application/octet-stream"}}); + CHECK(Result.status_code == 201); + } + + // Get package + { + cpr::Response Result = cpr::Get(cpr::Url{"{}/{}/{}?skip=data"_format(Cfg.BaseUri, Bucket, Key)}, + cpr::Header{{"Accept", "application/octet-stream"}}); + CHECK(Result.status_code == 200); + CHECK(Result.text.size() == 0); + } + } } struct RemoteExecutionRequest diff --git a/zenserver/cache/structuredcache.cpp b/zenserver/cache/structuredcache.cpp index 8ab0276c5..d6174caf6 100644 --- a/zenserver/cache/structuredcache.cpp +++ b/zenserver/cache/structuredcache.cpp @@ -200,8 +200,10 @@ HttpStructuredCacheService::HandleRequest(HttpServerRequest& Request) { std::string_view Key = Request.RelativeUri(); - if (Key.empty()) + if (Key.empty() || Key == "stats.json") { + $.Cancel(); + return HandleStatusRequest(Request); } @@ -270,10 +272,6 @@ HttpStructuredCacheService::HandleCacheRecordRequest(HttpServerRequest& Request, case kHead: case kGet: { - if (Verb == kHead) - { - Request.SetSuppressResponseBody(); - } HandleGetCacheRecord(Request, Ref, Policy); } break; @@ -418,10 +416,17 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request if (ValidationResult != CbValidateError::None) { ZEN_WARN("GET - '{}/{}' '{}' FAILED, invalid compact binary object", Ref.BucketSegment, Ref.HashKey, ToString(AcceptType)); + m_CacheStats.MissCount++; return Request.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, "Invalid cache record"sv); } - const bool SkipAttachments = zen::CachePolicy::SkipAttachments == (Policy & zen::CachePolicy::SkipAttachments); + if ((Policy & CachePolicy::SkipData) == CachePolicy::SkipData) + { + m_CacheStats.HitCount++; + return Request.WriteResponse(HttpResponseCode::OK); + } + + const bool SkipAttachments = (Policy & CachePolicy::SkipAttachments) == CachePolicy::SkipAttachments; uint32_t AttachmentCount = 0; uint32_t ValidCount = 0; uint64_t AttachmentBytes = 0ull; @@ -487,7 +492,14 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request m_CacheStats.UpstreamHitCount++; } - Request.WriteResponse(HttpResponseCode::OK, Value.Value.GetContentType(), Value.Value); + if ((Policy & CachePolicy::SkipData) == CachePolicy::SkipData) + { + Request.WriteResponse(HttpResponseCode::OK); + } + else + { + Request.WriteResponse(HttpResponseCode::OK, Value.Value.GetContentType(), Value.Value); + } } } @@ -676,10 +688,6 @@ HttpStructuredCacheService::HandleCachePayloadRequest(HttpServerRequest& Request case kHead: case kGet: { - if (Verb == kHead) - { - Request.SetSuppressResponseBody(); - } HandleGetCachePayload(Request, Ref, Policy); } break; @@ -740,7 +748,14 @@ HttpStructuredCacheService::HandleGetCachePayload(zen::HttpServerRequest& Reques m_CacheStats.UpstreamHitCount++; } - Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Payload); + if ((Policy & CachePolicy::SkipData) == CachePolicy::SkipData) + { + Request.WriteResponse(HttpResponseCode::OK); + } + else + { + Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Payload); + } } void @@ -868,7 +883,9 @@ HttpStructuredCacheService::HandleStatusRequest(zen::HttpServerRequest& Request) const uint64_t TotalCount = HitCount + MissCount; Cbo.BeginObject("cache"); + Cbo << "hits" << HitCount << "misses" << MissCount; Cbo << "hit_ratio" << (TotalCount > 0 ? (double(HitCount) / double(TotalCount) * 100.0) : 0.0); + Cbo << "upstream_hits" << m_CacheStats.UpstreamHitCount; Cbo << "upstream_ratio" << (HitCount > 0 ? (double(UpstreamHitCount) / double(HitCount)) * 100.0 : 0.0); Cbo.EndObject(); diff --git a/zenserver/projectstore.cpp b/zenserver/projectstore.cpp index 6b24692e1..5c4983472 100644 --- a/zenserver/projectstore.cpp +++ b/zenserver/projectstore.cpp @@ -1200,11 +1200,6 @@ HttpProjectService::HttpProjectService(CasStore& Store, ProjectStore* Projects) return HttpReq.WriteResponse(HttpResponseCode::NotFound); } - if (Verb == HttpVerb::kHead) - { - HttpReq.SetSuppressResponseBody(); - } - if (IsOffset) { if (Offset > Value.Size()) |