diff options
| author | Martin Ridgers <[email protected]> | 2021-11-15 14:15:21 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-11-15 14:15:21 +0100 |
| commit | da95225ad2d24163da386705b58a990e0792e57b (patch) | |
| tree | a25d25b936b8d620cc35494f95e539f69a02bb1e /zenserver/cache/structuredcache.cpp | |
| parent | Merged main (diff) | |
| parent | Handle 'partial on error' cache policy. (diff) | |
| download | zen-da95225ad2d24163da386705b58a990e0792e57b.tar.xz zen-da95225ad2d24163da386705b58a990e0792e57b.zip | |
Merged main
Diffstat (limited to 'zenserver/cache/structuredcache.cpp')
| -rw-r--r-- | zenserver/cache/structuredcache.cpp | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/zenserver/cache/structuredcache.cpp b/zenserver/cache/structuredcache.cpp index e1edfd161..6fba7648f 100644 --- a/zenserver/cache/structuredcache.cpp +++ b/zenserver/cache/structuredcache.cpp @@ -476,7 +476,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request TotalCount++; }); - ZEN_DEBUG("PUT - '{}/{}' {} '{}' attachments '{}/{}' (Valid/Total)", + ZEN_DEBUG("PUT - '{}/{}' {} '{}' attachments '{}/{}' (valid/total)", Ref.BucketSegment, Ref.HashKey, NiceBytes(Body.Size()), @@ -555,7 +555,7 @@ HttpStructuredCacheService::HandlePutCacheRecord(zen::HttpServerRequest& Request return Request.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Invalid attachment(s)"sv); } - ZEN_DEBUG("PUT - '{}/{}' {} '{}', attachments '{}/{}/{}' (New/Valid/Total)", + ZEN_DEBUG("PUT - '{}/{}' {} '{}', attachments '{}/{}/{}' (new/valid/total)", Ref.BucketSegment, Ref.HashKey, NiceBytes(Body.GetSize()), @@ -834,8 +834,9 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req CacheRecordPolicy::Load(Params["Policy"sv].AsObjectView(), Policy); - const bool SkipAttachments = (Policy.GetRecordPolicy() & CachePolicy::SkipAttachments) == CachePolicy::SkipAttachments; - const bool QueryRemote = m_UpstreamCache && ((Policy.GetRecordPolicy() & CachePolicy::QueryRemote) == CachePolicy::QueryRemote); + const bool PartialOnError = Policy.HasRecordPolicy(CachePolicy::PartialOnError); + const bool SkipAttachments = Policy.HasRecordPolicy(CachePolicy::SkipAttachments); + const bool QueryRemote = Policy.HasRecordPolicy(CachePolicy::QueryRemote) && m_UpstreamCache; for (CbFieldView KeyView : Params["CacheKeys"sv]) { @@ -853,27 +854,36 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req for (size_t KeyIndex = 0; const CacheKey& Key : CacheKeys) { ZenCacheValue CacheValue; + uint32_t MissingCount = 0; + if (m_CacheStore.Get(Key.Bucket, Key.Hash, CacheValue)) { CbObjectView CacheRecord(CacheValue.Value.Data()); if (!SkipAttachments) { - CacheRecord.IterateAttachments([this, &RpcResponse](CbFieldView AttachmentHash) { + CacheRecord.IterateAttachments([this, &MissingCount, &RpcResponse](CbFieldView AttachmentHash) { if (IoBuffer Chunk = m_CidStore.FindChunkByCid(AttachmentHash.AsHash())) { RpcResponse.AddAttachment(CbAttachment(CompressedBuffer::FromCompressed(SharedBuffer(Chunk)))); } + else + { + MissingCount++; + } }); } + } + if (CacheValue.Value && (MissingCount == 0 || PartialOnError)) + { ZEN_DEBUG("HIT - '{}/{}' {} '{}' (LOCAL)", Key.Bucket, Key.Hash, NiceBytes(CacheValue.Value.Size()), ToString(CacheValue.Value.GetContentType())); - CacheValues[KeyIndex] = CacheValue.Value; + CacheValues[KeyIndex] = std::move(CacheValue.Value); m_CacheStats.HitCount++; } else if (QueryRemote) @@ -882,7 +892,7 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req } else { - ZEN_DEBUG("MISS - '{}/{}'", Key.Bucket, Key.Hash); + ZEN_DEBUG("MISS - '{}/{}' {}", Key.Bucket, Key.Hash, MissingCount ? "(partial)"sv : ""sv); m_CacheStats.MissCount++; } @@ -892,10 +902,14 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req if (!UpstreamRequests.empty() && m_UpstreamCache) { const auto OnCacheRecordGetComplete = - [this, &CacheKeys, &CacheValues, &RpcResponse, SkipAttachments](CacheRecordGetCompleteParams&& Params) { + [this, &CacheKeys, &CacheValues, &RpcResponse, PartialOnError, SkipAttachments](CacheRecordGetCompleteParams&& Params) { + ZEN_ASSERT(Params.KeyIndex < CacheValues.size()); + + IoBuffer CacheValue; + AttachmentCount Count; + if (Params.Record) { - AttachmentCount Count; Params.Record.IterateAttachments([this, &RpcResponse, SkipAttachments, &Params, &Count](CbFieldView HashView) { if (const CbAttachment* Attachment = Params.Package.FindAttachment(HashView.AsHash())) { @@ -929,18 +943,23 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req Count.Total++; }); - ZEN_DEBUG("HIT - '{}/{}' {} '{}' attachments '{}/{}/{}' (New/Valid/Total) (UPSTREAM)", + if ((Count.Valid == Count.Total) || PartialOnError) + { + CacheValue = CbObject::Clone(Params.Record).GetBuffer().AsIoBuffer(); + } + } + + if (CacheValue) + { + ZEN_DEBUG("HIT - '{}/{}' {} '{}' attachments '{}/{}/{}' (new/valid/total) (UPSTREAM)", Params.CacheKey.Bucket, Params.CacheKey.Hash, - NiceBytes(Params.Record.GetView().GetSize()), + NiceBytes(CacheValue.GetSize()), ToString(HttpContentType::kCbPackage), Count.New, Count.Valid, Count.Total); - ZEN_ASSERT(Params.KeyIndex < CacheValues.size()); - - IoBuffer CacheValue = CbObject::Clone(Params.Record).GetBuffer().AsIoBuffer(); CacheValue.SetContentType(ZenContentType::kCbObject); CacheValues[Params.KeyIndex] = CacheValue; @@ -951,7 +970,8 @@ HttpStructuredCacheService::HandleRpcGetCacheRecords(zen::HttpServerRequest& Req } else { - ZEN_DEBUG("MISS - '{}/{}'", Params.CacheKey.Bucket, Params.CacheKey.Hash); + const bool IsPartial = Count.Valid != Count.Total; + ZEN_DEBUG("MISS - '{}/{}' {}", Params.CacheKey.Bucket, Params.CacheKey.Hash, IsPartial ? "(partial)"sv : ""sv); m_CacheStats.MissCount++; } }; |