52 std::shared_ptr<ProductModel::Design> GetDesign(
const std::string& designName);
53 std::shared_ptr<FileModel::Design::FileArchive> GetFileArchive(
const std::string& designName);
59 void AddFileArchive(
const std::string& designName, std::shared_ptr<FileModel::Design::FileArchive> fileArchive,
bool save, std::uint64_t injectedBytes = 0);
66 void InvalidateDesign(
const std::string& designName);
68 bool SaveFileArchive(
const std::string& designName);
70 std::vector<std::string> getLoadedDesignNames(
const std::string& filter =
"")
const;
71 std::vector<std::string> getLoadedFileArchiveNames(
const std::string& filter =
"")
const;
72 std::vector<std::string> getUnloadedDesignNames(
const std::string& filter =
"")
const;
74 int loadAllFileArchives(
bool stopOnError);
75 int loadAllDesigns(
bool stopOnError);
79 void setDirectory(
const std::string& directory);
80 std::string getDirectory()
const;
84 void ensureDirectoryExists()
const;
88 LoadState GetLoadState(
const std::string& designName)
const;
112 bool LoadDesignAsync(
const std::string& designName);
118 bool ContainsDesign(
const std::string& designName)
const;
121 void setCacheMaxBytes(std::uint64_t maxBytes);
122 std::uint64_t getCacheMaxBytes()
const;
127 void setMaxBackgroundLoads(std::size_t maxLoads);
128 std::size_t getMaxBackgroundLoads()
const;
162 bool TryGetDesignBytes(
const std::string& designName, std::string& outBytes)
const;
165 bool TryGetJsonPayload(
const std::string& designName, JsonPayloadKind kind, std::string& outJson)
const;
168 bool HasCachedResponse(
const std::string& designName)
const;
174 void SerializeResponsePayloads(
const std::string& designName);
179 std::uint64_t designSerializationCount()
const;
185 std::uint64_t GetResponseGeneration(
const std::string& designName)
const;
190 std::uint64_t cachedBytes()
const;
194 std::uint64_t cachedResponseBytes()
const;
205 void setMonolithicWireBudget(std::uint64_t maxBytes);
206 std::uint64_t getMonolithicWireBudget()
const;
209 std::string m_directory;
216 mutable std::shared_mutex m_cacheMutex;
229 using DesignFuture = std::shared_future<std::shared_ptr<ProductModel::Design>>;
230 using FileArchiveFuture = std::shared_future<std::shared_ptr<FileModel::Design::FileArchive>>;
231 using LoadStateMap = std::unordered_map<std::string, LoadState>;
232 using LoadObserverList = std::vector<LoadEventCallback>;
237 std::unordered_map<std::string, DesignFuture> m_designsInFlight;
238 std::unordered_map<std::string, FileArchiveFuture> m_fileArchivesInFlight;
240 LoadStateMap m_loadStatesByName;
241 LoadObserverList m_loadObservers;
248 std::uint64_t m_epoch = 0;
251 mutable std::mutex m_loadStateMutex;
258 std::unordered_set<std::string> m_pendingAsyncLoads;
266 mutable std::mutex m_backgroundMutex;
267 std::condition_variable m_backgroundSlotCv;
268 std::condition_variable m_backgroundDoneCv;
269 std::size_t m_maxBackgroundLoads = DEFAULT_MAX_BACKGROUND_LOADS;
270 std::size_t m_activeBackgroundLoads = 0;
271 std::size_t m_outstandingBackgroundLoads = 0;
280 static const std::string*& loadingDesignName()
282 thread_local const std::string* s_loadingDesignName =
nullptr;
283 return s_loadingDesignName;
290 std::uint64_t estimatedBytes = 0;
295 std::uint64_t serializedBytes = 0;
296 std::chrono::steady_clock::time_point lastServed;
299 using LruEntryMap = std::unordered_map<std::string, LruEntry>;
306 LruEntryMap m_lruEntries;
307 std::uint64_t m_cachedBytes = 0;
308 std::uint64_t m_maxBytes = DEFAULT_CACHE_MAX_BYTES;
311 mutable std::mutex m_lruMutex;
316 std::uint64_t m_monolithicWireBudget = DEFAULT_MONOLITHIC_WIRE_BUDGET;
321 struct ResponsePayload
323 std::string designPbBytes;
324 std::string designJson;
325 std::string fileModelJson;
327 std::uint64_t totalBytes()
const
329 return static_cast<std::uint64_t
>(designPbBytes.size()) +
330 static_cast<std::uint64_t
>(designJson.size()) +
331 static_cast<std::uint64_t
>(fileModelJson.size());
348 std::uint64_t storedBytes = 0;
351 bool TryGetDesignBytes(
const std::string& designName, std::string& outBytes)
const;
352 bool TryGetJsonPayload(
const std::string& designName,
JsonPayloadKind kind, std::string& outJson)
const;
353 bool HasEntry(
const std::string& designName)
const;
354 std::uint64_t StoredBytes(
const std::string& designName)
const;
355 std::uint64_t TotalStoredBytes()
const;
356 std::uint64_t CurrentGeneration(
const std::string& designName)
const;
357 StoreResult TryStore(
const std::string& designName, std::uint64_t generation, ResponsePayload&& payload);
360 void BumpGenerations(
const std::vector<std::string>& designNames);
362 std::uint64_t Invalidate(
const std::string& designName);
364 std::uint64_t designSerializations()
const;
369 ResponsePayload payload;
372 mutable std::mutex m_mutex;
373 std::unordered_map<std::string, Entry> m_entriesByName;
378 std::unordered_map<std::string, std::uint64_t> m_generationsByName;
379 std::uint64_t m_designSerializations = 0;
382 ResponseStore m_responseStore;
395 void CommitResponsePayloads(
const std::string& designName,
396 std::uint64_t epochAtStart,
397 const std::shared_ptr<ProductModel::Design>& pDesign,
398 ResponsePayload&& payload,
399 std::uint64_t generation);
404 void ScheduleResponsePreSerialization(
const std::string& designName,
405 std::shared_ptr<ProductModel::Design> pDesign,
406 std::uint64_t epochAtStart);
410 void PreSerializeWorker(std::string designName,
411 std::shared_ptr<ProductModel::Design> pDesign,
412 std::uint64_t epochAtStart,
413 std::uint64_t generation);
417 void InvalidateResponsePayloads(
const std::string& designName);
421 void AdjustLruSerializedBytes(
const std::string& designName);
425 void LogFailedStateRecordError(
const std::string& designName);
428 std::uint64_t CurrentEpoch()
const;
430 std::shared_ptr<ProductModel::Design> LoadDesign(
const std::string& designName);
431 std::shared_ptr<FileModel::Design::FileArchive> LoadFileArchive(
const std::string& designName);
432 std::shared_ptr<FileModel::Design::FileArchive> GetFileArchiveInternal(
const std::string& designName,
bool updateState);
436 std::filesystem::path FindArchivePath(
const std::string& designName)
const;
441 void BackgroundLoad(
const std::string& designName);
442 void AcquireBackgroundSlot();
443 void ReleaseBackgroundSlot();
450 template <
typename T,
typename LoadFn,
typename CacheMap,
typename InFlightMap>
451 std::shared_ptr<T> GetOrLoadSingleFlight(
452 const std::string& designName,
454 InFlightMap& inFlightMap,
460 std::shared_ptr<T> pCached;
462 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
463 auto findIt = cacheMap.find(designName);
464 if (findIt != cacheMap.end())
466 pCached = findIt->second;
472 TouchLru(designName);
484 const auto generationAtStart = m_responseStore.CurrentGeneration(designName);
485 std::promise<std::shared_ptr<T>> promise;
486 std::shared_future<std::shared_ptr<T>> future = promise.get_future().share();
487 bool isLoader =
false;
488 std::uint64_t epochAtStart = 0;
490 std::lock_guard<std::mutex> lock(m_loadStateMutex);
491 auto findIt = inFlightMap.find(designName);
492 if (findIt != inFlightMap.end())
494 future = findIt->second;
498 inFlightMap.emplace(designName, future);
503 epochAtStart = m_epoch;
513 const std::string* pLoadingName = loadingDesignName();
514 if (pLoadingName !=
nullptr && *pLoadingName == designName)
516 throw std::runtime_error(
517 "DesignCache observer re-entry: load observers must not call back into the DesignCache for the design being loaded");
522 auto pValue = future.get();
523 TouchLru(designName);
536 bool promiseSatisfied =
false;
537 struct LoaderExitGuard
539 DesignCache* m_pCache;
540 InFlightMap& m_rInFlightMap;
541 const std::string& m_rDesignName;
542 std::promise<std::shared_ptr<T>>& m_rPromise;
551 m_rPromise.set_exception(std::current_exception()
552 ? std::current_exception()
553 : std::make_exception_ptr(std::runtime_error(
554 "DesignCache: loader exited without completing the load for \"" + m_rDesignName +
"\"")));
555 m_pCache->eraseInFlight(m_rInFlightMap, m_rDesignName);
557 } loaderExitGuard{
this, inFlightMap, designName, promise, promiseSatisfied };
568 explicit LoadingMarker(
const std::string& name) :
569 m_previous(loadingDesignName())
571 loadingDesignName() = &name;
575 loadingDesignName() = m_previous;
577 const std::string* m_previous;
578 } loadingMarker(designName);
580 TransitionLoadState(designName, LoadState::Loading);
583 std::shared_ptr<T> pValue;
596 promise.set_exception(std::current_exception());
597 promiseSatisfied =
true;
598 if (updateState && !WasClearedSince(epochAtStart))
609 TransitionLoadState(designName, LoadState::Failed);
613 LogFailedStateRecordError(designName);
616 eraseInFlight(inFlightMap, designName);
620 if (pValue ==
nullptr)
633 promise.set_value(
nullptr);
634 promiseSatisfied =
true;
637 EraseLoadState(designName);
639 eraseInFlight(inFlightMap, designName);
643 const auto invalidatedSinceRegistration =
644 m_responseStore.CurrentGeneration(designName) != generationAtStart;
645 if (WasClearedSince(epochAtStart) || invalidatedSinceRegistration)
667 promise.set_value(pValue);
668 promiseSatisfied =
true;
669 eraseInFlight(inFlightMap, designName);
676 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
677 auto findIt = cacheMap.find(designName);
678 if (findIt != cacheMap.end())
680 pValue = findIt->second;
684 cacheMap[designName] = pValue;
689 InsertLruAndEvict(designName);
697 if constexpr (std::is_same_v<T, ProductModel::Design>)
701 ScheduleResponsePreSerialization(designName, pValue, epochAtStart);
711 TransitionLoadState(designName, LoadState::Loaded);
715 promise.set_value(pValue);
716 promiseSatisfied =
true;
717 eraseInFlight(inFlightMap, designName);
721 template <
typename T>
722 void eraseInFlight(std::unordered_map<std::string, std::shared_future<std::shared_ptr<T>>>& inFlightMap,
723 const std::string& designName)
727 std::lock_guard<std::mutex> lock(m_loadStateMutex);
728 inFlightMap.erase(designName);
731 void TouchLru(
const std::string& designName);
732 void InsertLruAndEvict(
const std::string& designName, std::uint64_t injectedBytes = 0);
733 std::vector<std::string> EvictOverBudgetLocked(
const std::string& protectedName);
734 void EvictCacheEntries(
const std::vector<std::string>& names);
735 std::uint64_t EstimateDesignBytes(
const std::string& designName, std::uint64_t injectedBytes = 0)
const;
740 void ResyncLruEstimate(
const std::string& designName);
742 void TransitionLoadState(
const std::string& designName, LoadState to);
749 void EraseLoadState(
const std::string& designName);
751 void NotifyLoadObservers(
const std::string& designName, LoadState from, LoadState to,
const LoadObserverList& observers);
755 bool WasClearedSince(std::uint64_t epoch)
const;
757 constexpr inline static std::uint64_t DEFAULT_CACHE_MAX_BYTES = 4096ull * 1024ull * 1024ull;
758 constexpr inline static std::size_t DEFAULT_MAX_BACKGROUND_LOADS = 2;
764 constexpr inline static std::uint64_t DEFAULT_MONOLITHIC_WIRE_BUDGET = 256ull * 1024ull * 1024ull;
767 constexpr inline static std::uint64_t DESIGN_BYTES_OVERHEAD = 4096;
769 constexpr inline static const char* DESIGN_EXTENSIONS[] = {
"zip",
"tgz",
"tar.gz",
"tar",
"gzip" ,
"gz" };