diff options
| -rw-r--r-- | zencore/include/zencore/iobuffer.h | 29 | ||||
| -rw-r--r-- | zencore/uid.cpp | 7 | ||||
| -rw-r--r-- | zenserver/cache/structuredcache.cpp | 80 |
3 files changed, 82 insertions, 34 deletions
diff --git a/zencore/include/zencore/iobuffer.h b/zencore/include/zencore/iobuffer.h index 6f3609d19..54801f9ac 100644 --- a/zencore/include/zencore/iobuffer.h +++ b/zencore/include/zencore/iobuffer.h @@ -28,6 +28,35 @@ enum class ZenContentType : uint8_t kCOUNT }; +inline std::string_view +ToString(ZenContentType ContentType) +{ + using namespace std::literals; + + switch (ContentType) + { + default: + case ZenContentType::kUnknownContentType: + return "unknown"sv; + case ZenContentType::kBinary: + return "binary"sv; + case ZenContentType::kText: + return "text"sv; + case ZenContentType::kJSON: + return "json"sv; + case ZenContentType::kCbObject: + return "cb-object"sv; + case ZenContentType::kCbPackage: + return "cb-package"sv; + case ZenContentType::kCbPackageOffer: + return "cb-package-offer"sv; + case ZenContentType::kCompressedBinary: + return "compressed-binary"sv; + case ZenContentType::kYAML: + return "yaml"sv; + } +} + struct IoBufferFileReference { void* FileHandle; diff --git a/zencore/uid.cpp b/zencore/uid.cpp index d4b708288..86cdfae3a 100644 --- a/zencore/uid.cpp +++ b/zencore/uid.cpp @@ -99,11 +99,10 @@ Oid::ToString(char OutString[StringLength]) StringBuilderBase& Oid::ToString(StringBuilderBase& OutString) const { - char str[StringLength + 1]; - ToHexBytes(reinterpret_cast<const uint8_t*>(OidBits), sizeof(Oid::OidBits), str); - str[2 * sizeof(Oid)] = '\0'; + String_t Str; + ToHexBytes(reinterpret_cast<const uint8_t*>(OidBits), sizeof(Oid::OidBits), Str); - OutString.AppendRange(str, &str[25]); + OutString.AppendRange(Str, &Str[StringLength]); return OutString; } diff --git a/zenserver/cache/structuredcache.cpp b/zenserver/cache/structuredcache.cpp index e4af2a5b2..74cee6614 100644 --- a/zenserver/cache/structuredcache.cpp +++ b/zenserver/cache/structuredcache.cpp @@ -290,9 +290,7 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request if (QueryUpstream) { - const ZenContentType CacheRecordType = Ref.BucketSegment == "legacy"sv ? ZenContentType::kBinary - : AcceptType == ZenContentType::kCbPackage ? ZenContentType::kCbPackage - : ZenContentType::kCbObject; + const ZenContentType CacheRecordType = AcceptType; if (auto UpstreamResult = m_UpstreamCache->GetCacheRecord({Ref.BucketSegment, Ref.HashKey}, CacheRecordType); UpstreamResult.Success) @@ -321,9 +319,10 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request else { Success = false; - ZEN_WARN("Get - cache record '{}/{}' FAILED, invalid compact binary object from upstream", + ZEN_WARN("Get - '{}/{}' '{}' FAILED, invalid compact binary object from upstream", Ref.BucketSegment, - Ref.HashKey); + Ref.HashKey, + ToString(AcceptType)); } } @@ -353,9 +352,10 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request } else { - ZEN_WARN("GET - cache record '{}/{}' FAILED, upstream attachment not compressed", + ZEN_WARN("Get - '{}/{}' '{}' FAILED, upstream attachment not compressed", Ref.BucketSegment, - Ref.HashKey); + Ref.HashKey, + ToString(ZenContentType::kCbPackage)); } } AttachmentCount++; @@ -379,15 +379,16 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request else { Success = false; - ZEN_WARN("GET - cache record '{}/{}' FAILED, attachments missing in upstream package", + ZEN_WARN("Get - '{}/{}' '{}' FAILED, attachments missing in upstream package", Ref.BucketSegment, - Ref.HashKey); + Ref.HashKey, + ToString(AcceptType)); } } else { Success = false; - ZEN_WARN("GET - cache record '{}/{}' FAILED, invalid upstream package", Ref.BucketSegment, Ref.HashKey); + ZEN_WARN("Get - '{}/{}' '{}' FAILED, invalid upstream package", Ref.BucketSegment, Ref.HashKey, ToString(AcceptType)); } } } @@ -395,8 +396,7 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request if (!Success) { - ZEN_DEBUG("MISS - '{}/{}'", Ref.BucketSegment, Ref.HashKey); - + ZEN_DEBUG("MISS - '{}/{}' '{}'", Ref.BucketSegment, Ref.HashKey, ToString(AcceptType)); return Request.WriteResponse(HttpResponseCode::NotFound); } @@ -408,7 +408,7 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request if (ValidationResult != CbValidateError::None) { - ZEN_WARN("GET - cache record '{}/{}' FAILED, invalid compact binary object", Ref.BucketSegment, Ref.HashKey); + ZEN_WARN("GET - '{}/{}' '{}' FAILED, invalid compact binary object", Ref.BucketSegment, Ref.HashKey, ToString(AcceptType)); return Request.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, "Invalid cache record"sv); } @@ -434,9 +434,10 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request if (ValidCount != AttachmentCount) { - ZEN_WARN("GET - cache record '{}/{}' FAILED, found '{}' of '{}' attachments", + ZEN_WARN("GET - '{}/{}' '{}' FAILED, found '{}' of '{}' attachments", Ref.BucketSegment, Ref.HashKey, + ToString(AcceptType), ValidCount, AttachmentCount); @@ -446,10 +447,11 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request Package.SetObject(LoadCompactBinaryObject(Value.Value)); - ZEN_DEBUG("HIT - '{}/{}' {}, {} attachments (LOCAL)", + ZEN_DEBUG("HIT - '{}/{}' {} '{}', {} attachments (LOCAL)", Ref.BucketSegment, Ref.HashKey, NiceBytes(AttachmentBytes + Value.Value.Size()), + ToString(HttpContentType::kCbPackage), AttachmentCount); BinaryWriter MemStream; @@ -461,10 +463,11 @@ HttpStructuredCacheService::HandleGetCacheRecord(zen::HttpServerRequest& Request } else { - ZEN_DEBUG("HIT - '{}/{}' {} ({})", + ZEN_DEBUG("HIT - '{}/{}' {} '{}' ({})", Ref.BucketSegment, Ref.HashKey, NiceBytes(Value.Value.Size()), + ToString(Value.Value.GetContentType()), InUpstreamCache ? "UPSTREAM" : "LOCAL"); Request.WriteResponse(HttpResponseCode::OK, Value.Value.GetContentType(), Value.Value); @@ -484,10 +487,12 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request const HttpContentType ContentType = Request.RequestContentType(); const bool StoreUpstream = m_UpstreamCache && (CachePolicy::StoreRemote == (Policy & CachePolicy::StoreRemote)); - if (ContentType == HttpContentType::kBinary || ContentType == HttpContentType::kUnknownContentType) + Body.SetContentType(ContentType); + + if (ContentType == HttpContentType::kBinary) { + ZEN_DEBUG("PUT - '{}/{}' {} '{}'", Ref.BucketSegment, Ref.HashKey, NiceBytes(Body.Size()), ToString(ContentType)); m_CacheStore.Put(Ref.BucketSegment, Ref.HashKey, {.Value = Body}); - ZEN_DEBUG("PUT - binary '{}/{}' {}", Ref.BucketSegment, Ref.HashKey, NiceBytes(Body.Size())); if (StoreUpstream) { @@ -503,7 +508,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request if (ValidationResult != CbValidateError::None) { - ZEN_WARN("PUT - cache record '{}/{}' ({} bytes) FAILED, invalid compact binary", Ref.BucketSegment, Ref.HashKey, Body.Size()); + ZEN_WARN("PUT - '{}/{}' '{}' FAILED, invalid compact binary", Ref.BucketSegment, Ref.HashKey, ToString(ContentType)); return Request.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Compact binary validation failed"sv); } @@ -525,7 +530,13 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request if (ValidCacheRecord) { - ZEN_DEBUG("PUT - cache record '{}/{}' {}, {} attachments", Ref.BucketSegment, Ref.HashKey, NiceBytes(Body.Size()), ValidCount); + ZEN_DEBUG("PUT - '{}/{}' {} '{}', {} attachments", + Ref.BucketSegment, + Ref.HashKey, + NiceBytes(Body.Size()), + ToString(ContentType), + ValidCount); + m_CacheStore.Put(Ref.BucketSegment, Ref.HashKey, {.Value = Body}); if (StoreUpstream) @@ -540,9 +551,10 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request } else { - ZEN_WARN("PUT - cache record '{}/{}' FAILED, found {}/{} attachments", + ZEN_WARN("PUT - '{}/{}' '{}' FAILED, found {}/{} attachments", Ref.BucketSegment, Ref.HashKey, + ToString(ContentType), ValidCount, AttachmentCount); @@ -555,7 +567,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request if (!Package.TryLoad(Body)) { - ZEN_WARN("PUT - cache record '{}/{}' FAILED, invalid package", Ref.BucketSegment, Ref.HashKey); + ZEN_WARN("PUT - '{}/{}' '{}' FAILED, invalid package", Ref.BucketSegment, Ref.HashKey, ToString(ContentType)); return Request.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Invalid package"sv); } @@ -584,17 +596,19 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request } else { - ZEN_WARN("PUT - cache record '{}/{}' FAILED, attachment '{}' is not compressed", + ZEN_WARN("PUT - '{}/{}' '{}' FAILED, attachment '{}' is not compressed", Ref.BucketSegment, Ref.HashKey, + ToString(HttpContentType::kCbPackage), AttachmentHash.AsHash()); } } else { - ZEN_WARN("PUT - cache record '{}/{}' FAILED, missing attachment '{}'", + ZEN_WARN("PUT - '{}/{}' '{}' FAILED, missing attachment '{}'", Ref.BucketSegment, Ref.HashKey, + ToString(HttpContentType::kCbPackage), AttachmentHash.AsHash()); } }); @@ -606,13 +620,17 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request return Request.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Invalid attachments"sv); } - ZEN_DEBUG("PUT - cache record '{}/{}' {}, {}/{} new attachments", + ZEN_DEBUG("PUT - '{}/{}' {} '{}', {}/{} new attachments", Ref.BucketSegment, Ref.HashKey, NiceBytes(Body.GetSize()), + ToString(ContentType), NewAttachmentCount, Attachments.size()); + IoBuffer CacheRecordValue = CacheRecord.GetBuffer().AsIoBuffer(); + CacheRecordValue.SetContentType(ZenContentType::kCbObject); + m_CacheStore.Put(Ref.BucketSegment, Ref.HashKey, {.Value = CacheRecord.GetBuffer().AsIoBuffer()}); if (StoreUpstream) @@ -627,7 +645,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request } else { - Request.WriteResponse(HttpResponseCode::BadRequest); + return Request.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Content-Type invalid"sv); } } @@ -690,12 +708,12 @@ HttpStructuredCacheService::HandleGetCachePayload(zen::HttpServerRequest& Reques return Request.WriteResponse(HttpResponseCode::NotFound); } - ZEN_DEBUG("HIT - '{}/{}/{}' {} (type: {}) ({})", + ZEN_DEBUG("HIT - '{}/{}/{}' {} '{}' ({})", Ref.BucketSegment, Ref.HashKey, Ref.PayloadId, NiceBytes(Payload.Size()), - Payload.GetContentType(), + ToString(Payload.GetContentType()), InUpstreamCache ? "UPSTREAM" : "LOCAL"); Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Payload); @@ -714,6 +732,8 @@ HttpStructuredCacheService::HandlePutCachePayload(zen::HttpServerRequest& Reques return Request.WriteResponse(HttpResponseCode::BadRequest); } + Body.SetContentType(Request.RequestContentType()); + IoHash ChunkHash = IoHash::HashBuffer(Body); CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(Body)); @@ -731,12 +751,12 @@ HttpStructuredCacheService::HandlePutCachePayload(zen::HttpServerRequest& Reques m_CidStore.AddCompressedCid(Ref.PayloadId, ChunkHash); - ZEN_DEBUG("PUT - payload '{}/{}/{}' {} (type: {}) {}", + ZEN_DEBUG("PUT - '{}/{}/{}' {} '{}' ({})", Ref.BucketSegment, Ref.HashKey, Ref.PayloadId, NiceBytes(Body.Size()), - Body.GetContentType(), + ToString(Body.GetContentType()), Result.New ? "NEW" : "OLD"); const HttpResponseCode ResponseCode = Result.New ? HttpResponseCode::Created : HttpResponseCode::OK; |