// Copyright Epic Games, Inc. All Rights Reserved. #include "projectstore.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "buildsremoteprojectstore.h" #include "fileremoteprojectstore.h" #include "jupiterremoteprojectstore.h" #include "remoteprojectstore.h" #include "zenremoteprojectstore.h" ZEN_THIRD_PARTY_INCLUDES_START #include #include #include ZEN_THIRD_PARTY_INCLUDES_END #if ZEN_WITH_TESTS # include # include #endif // ZEN_WITH_TESTS namespace zen { const FLLMTag& GetProjectstoreTag() { static FLLMTag _("store", FLLMTag("project")); return _; } namespace { bool PrepareDirectoryDelete(const std::filesystem::path& Dir, std::filesystem::path& OutDeleteDir) { std::filesystem::path DroppedBucketPath; do { if (!IsDir(Dir)) { return true; } StringBuilder<64> MovedId; Oid::NewOid().ToString(MovedId); std::string DroppedName = fmt::format("[dropped]{}({})", Dir.filename().string(), MovedId); DroppedBucketPath = Dir.parent_path() / DroppedName; if (IsDir(DroppedBucketPath)) { if (!DeleteDirectories(DroppedBucketPath)) { ZEN_INFO("Drop directory '{}' for '{}' already exists but could not be removed, attempting different name.", DroppedBucketPath, Dir); continue; } if (IsDir(DroppedBucketPath)) { ZEN_INFO("Drop directory '{}' for '{}' still exists after remove, attempting different name.", DroppedBucketPath, Dir); continue; } } int RenameAttempt = 0; do { std::error_code Ec; RenameDirectory(Dir, DroppedBucketPath, Ec); if (!Ec) { OutDeleteDir = DroppedBucketPath; return true; } if (IsDir(DroppedBucketPath)) { ZEN_INFO("Can't rename '{}' to still existing drop directory '{}'. Reason: '{}'. Attempting different name.", Dir, DroppedBucketPath, Ec.message()); break; } if (++RenameAttempt == 10) { ZEN_INFO("Can't rename '{}' to drop directory '{}' after {} attempts. Reason: {}.", Dir, DroppedBucketPath, RenameAttempt, Ec.message()); return false; } ZEN_INFO("Can't rename '{}' to drop directory '{}', pausing and retrying. Reason: {}.", Dir, DroppedBucketPath, Ec.message()); Sleep(100); } while (true); } while (true); return false; } bool IsFileOlderThan(const std::filesystem::path& CheckPath, const std::filesystem::path& ReferencePath) { std::error_code Ec; std::filesystem::file_time_type CheckWriteTime = std::filesystem::last_write_time(CheckPath, Ec); if (Ec) { return true; } std::filesystem::file_time_type ReferenceWriteTime = std::filesystem::last_write_time(ReferencePath, Ec); if (Ec) { return true; } return CheckWriteTime < ReferenceWriteTime; } struct CreateRemoteStoreResult { std::shared_ptr Store; std::string Description; }; CreateRemoteStoreResult CreateRemoteStore(CbObjectView Params, AuthMgr& AuthManager, size_t MaxBlockSize, size_t MaxChunkEmbedSize, const std::filesystem::path& TempFilePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; std::shared_ptr RemoteStore; if (CbObjectView File = Params["file"sv].AsObjectView(); File) { std::filesystem::path FolderPath(File["path"sv].AsString()); if (FolderPath.empty()) { return {nullptr, "Missing file path"}; } std::string_view Name(File["name"sv].AsString()); if (Name.empty()) { return {nullptr, "Missing file name"}; } std::string_view OptionalBaseName(File["basename"sv].AsString()); bool ForceDisableBlocks = File["disableblocks"sv].AsBool(false); bool ForceEnableTempBlocks = File["enabletempblocks"sv].AsBool(false); FileRemoteStoreOptions Options = {RemoteStoreOptions{.MaxBlockSize = MaxBlockSize, .MaxChunkEmbedSize = MaxChunkEmbedSize}, FolderPath, std::string(Name), std::string(OptionalBaseName), ForceDisableBlocks, ForceEnableTempBlocks}; RemoteStore = CreateFileRemoteStore(Options); } if (CbObjectView Cloud = Params["cloud"sv].AsObjectView(); Cloud) { std::string_view CloudServiceUrl = Cloud["url"sv].AsString(); if (CloudServiceUrl.empty()) { return {nullptr, "Missing service url"}; } std::string Url = cpr::util::urlDecode(std::string(CloudServiceUrl)); std::string_view Namespace = Cloud["namespace"sv].AsString(); if (Namespace.empty()) { return {nullptr, "Missing namespace"}; } std::string_view Bucket = Cloud["bucket"sv].AsString(); if (Bucket.empty()) { return {nullptr, "Missing bucket"}; } std::string_view OpenIdProvider = Cloud["openid-provider"sv].AsString(); std::string AccessToken = std::string(Cloud["access-token"sv].AsString()); if (AccessToken.empty()) { std::string_view AccessTokenEnvVariable = Cloud["access-token-env"].AsString(); if (!AccessTokenEnvVariable.empty()) { AccessToken = GetEnvVariable(AccessTokenEnvVariable); } } std::filesystem::path OidcExePath; if (std::string_view OidcExePathString = Cloud["oidc-exe-path"].AsString(); !OidcExePathString.empty()) { std::filesystem::path OidcExePathMaybe(OidcExePathString); if (!IsFile(OidcExePathMaybe)) { ZEN_WARN("Path to OidcToken executable '{}' can not be reached by server", OidcExePathString); OidcExePath = std::move(OidcExePathMaybe); } } std::string_view KeyParam = Cloud["key"sv].AsString(); if (KeyParam.empty()) { return {nullptr, "Missing key"}; } if (KeyParam.length() != IoHash::StringLength) { return {nullptr, "Invalid key"}; } IoHash Key = IoHash::FromHexString(KeyParam); if (Key == IoHash::Zero) { return {nullptr, "Invalid key string"}; } IoHash BaseKey = IoHash::Zero; std::string_view BaseKeyParam = Cloud["basekey"sv].AsString(); if (!BaseKeyParam.empty()) { if (BaseKeyParam.length() != IoHash::StringLength) { return {nullptr, "Invalid base key"}; } BaseKey = IoHash::FromHexString(BaseKeyParam); if (BaseKey == IoHash::Zero) { return {nullptr, "Invalid base key string"}; } } bool ForceDisableBlocks = Cloud["disableblocks"sv].AsBool(false); bool ForceDisableTempBlocks = Cloud["disabletempblocks"sv].AsBool(false); bool AssumeHttp2 = Cloud["assumehttp2"sv].AsBool(false); JupiterRemoteStoreOptions Options = {RemoteStoreOptions{.MaxBlockSize = MaxBlockSize, .MaxChunkEmbedSize = MaxChunkEmbedSize}, Url, std::string(Namespace), std::string(Bucket), Key, BaseKey, std::string(OpenIdProvider), AccessToken, AuthManager, OidcExePath, ForceDisableBlocks, ForceDisableTempBlocks, AssumeHttp2}; RemoteStore = CreateJupiterRemoteStore(Options, TempFilePath, /*Quiet*/ false); } if (CbObjectView Zen = Params["zen"sv].AsObjectView(); Zen) { std::string_view Url = Zen["url"sv].AsString(); std::string_view Project = Zen["project"sv].AsString(); if (Project.empty()) { return {nullptr, "Missing project"}; } std::string_view Oplog = Zen["oplog"sv].AsString(); if (Oplog.empty()) { return {nullptr, "Missing oplog"}; } ZenRemoteStoreOptions Options = {RemoteStoreOptions{.MaxBlockSize = MaxBlockSize, .MaxChunkEmbedSize = MaxChunkEmbedSize}, std::string(Url), std::string(Project), std::string(Oplog)}; RemoteStore = CreateZenRemoteStore(Options, TempFilePath); } if (CbObjectView Builds = Params["builds"sv].AsObjectView(); Builds) { std::string_view BuildsServiceUrl = Builds["url"sv].AsString(); if (BuildsServiceUrl.empty()) { return {nullptr, "Missing service url"}; } std::string Url = cpr::util::urlDecode(std::string(BuildsServiceUrl)); std::string_view Namespace = Builds["namespace"sv].AsString(); if (Namespace.empty()) { return {nullptr, "Missing namespace"}; } std::string_view Bucket = Builds["bucket"sv].AsString(); if (Bucket.empty()) { return {nullptr, "Missing bucket"}; } std::string_view OpenIdProvider = Builds["openid-provider"sv].AsString(); std::string AccessToken = std::string(Builds["access-token"sv].AsString()); if (AccessToken.empty()) { std::string_view AccessTokenEnvVariable = Builds["access-token-env"].AsString(); if (!AccessTokenEnvVariable.empty()) { AccessToken = GetEnvVariable(AccessTokenEnvVariable); } } std::filesystem::path OidcExePath; if (std::string_view OidcExePathString = Builds["oidc-exe-path"].AsString(); !OidcExePathString.empty()) { std::filesystem::path OidcExePathMaybe(OidcExePathString); if (!IsFile(OidcExePathMaybe)) { ZEN_WARN("Path to OidcToken executable '{}' can not be reached by server", OidcExePathString); OidcExePath = std::move(OidcExePathMaybe); } } std::string_view BuildIdParam = Builds["buildsid"sv].AsString(); if (BuildIdParam.empty()) { return {nullptr, "Missing build id"}; } if (BuildIdParam.length() != Oid::StringLength) { return {nullptr, "Invalid build id"}; } Oid BuildId = Oid::FromHexString(BuildIdParam); if (BuildId == Oid::Zero) { return {nullptr, "Invalid build id string"}; } bool ForceDisableBlocks = Builds["disableblocks"sv].AsBool(false); bool ForceDisableTempBlocks = Builds["disabletempblocks"sv].AsBool(false); bool AssumeHttp2 = Builds["assumehttp2"sv].AsBool(false); MemoryView MetaDataSection = Builds["metadata"sv].AsBinaryView(); IoBuffer MetaData(IoBuffer::Wrap, MetaDataSection.GetData(), MetaDataSection.GetSize()); BuildsRemoteStoreOptions Options = {RemoteStoreOptions{.MaxBlockSize = MaxBlockSize, .MaxChunkEmbedSize = MaxChunkEmbedSize}, Url, std::string(Namespace), std::string(Bucket), BuildId, std::string(OpenIdProvider), AccessToken, AuthManager, OidcExePath, ForceDisableBlocks, ForceDisableTempBlocks, AssumeHttp2, MetaData}; RemoteStore = CreateJupiterBuildsRemoteStore(Options, TempFilePath, /*Quiet*/ false); } if (!RemoteStore) { return {nullptr, "Unknown remote store type"}; } return {std::move(RemoteStore), ""}; } std::pair ConvertResult(const RemoteProjectStore::Result& Result) { if (Result.ErrorCode == 0) { return {HttpResponseCode::OK, Result.Text}; } return {static_cast(Result.ErrorCode), Result.Reason.empty() ? Result.Text : Result.Text.empty() ? Result.Reason : fmt::format("{}: {}", Result.Reason, Result.Text)}; } #pragma pack(push) #pragma pack(1) struct OplogIndexHeader { static constexpr uint32_t ExpectedMagic = 0x7569647a; // 'zidx'; static constexpr uint32_t CurrentVersion = 1; static constexpr uint64_t DataAlignment = 8; uint32_t Magic = ExpectedMagic; uint32_t Version = CurrentVersion; uint64_t LogPosition = 0; uint32_t LSNCount = 0; uint64_t KeyCount = 0; uint32_t OpAddressMapCount = 0; uint32_t LatestOpMapCount = 0; uint64_t ChunkMapCount = 0; uint64_t MetaMapCount = 0; uint64_t FileMapCount = 0; uint32_t Checksum = 0; static uint32_t ComputeChecksum(const OplogIndexHeader& Header) { return XXH32(&Header.Magic, sizeof(OplogIndexHeader) - sizeof(uint32_t), 0xC0C0'BABA); } }; #pragma pack(pop) static_assert(sizeof(OplogIndexHeader) == 64); static std::uint64_t GetModificationTagFromRawHash(const IoHash& Hash) { IoHash::Hasher H; return H(Hash); } static std::uint64_t GetModificationTagFromModificationTime(IoBuffer FileBuffer) { IoBufferFileReference FileRef; if (FileBuffer.GetFileReference(FileRef)) { std::error_code Ec; uint64_t ModificationTick = GetModificationTickFromHandle(FileRef.FileHandle, Ec); if (!Ec) { return ModificationTick; } } return {}; } } // namespace ////////////////////////////////////////////////////////////////////////// Oid ComputeOpKey(const CbObjectView& Op) { using namespace std::literals; eastl::fixed_vector KeyData; Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { auto Begin = reinterpret_cast(Data); auto End = Begin + Size; KeyData.insert(KeyData.end(), Begin, End); }); XXH3_128 KeyHash128; // This logic currently exists to work around a problem caused by misusing the xxhash // functions in the past. Short keys are evaluated using the old and buggy // path but longer paths are evaluated properly. In the future all key lengths // should be evaluated using the proper path, this is a temporary workaround to // maintain compatibility with existing disk state. if (KeyData.size() < 240) { XXH3_128Stream_deprecated KeyHasher; KeyHasher.Append(KeyData.data(), KeyData.size()); KeyHash128 = KeyHasher.GetHash(); } else { KeyHash128 = XXH3_128::HashMemory(KeyData.data(), KeyData.size()); } Oid KeyHash; memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash); return KeyHash; } struct ProjectStore::OplogStorage : public RefCounted { OplogStorage(ProjectStore::Oplog* OwnerOplog, std::filesystem::path BasePath) : m_OwnerOplog(OwnerOplog), m_OplogStoragePath(BasePath) { } ~OplogStorage() { ZEN_INFO("oplog '{}/{}': closing oplog storage at {}", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), m_OplogStoragePath); try { Flush(); m_Oplog.Close(); m_OpBlobs.Close(); } catch (const std::exception& Ex) { ZEN_WARN("oplog '{}/{}': flushing oplog at '{}' failed. Reason: '{}'", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), m_OplogStoragePath, Ex.what()); } } [[nodiscard]] bool Exists() const { return Exists(m_OplogStoragePath); } [[nodiscard]] static bool Exists(const std::filesystem::path& BasePath) { return IsFile(GetLogPath(BasePath)) && IsFile(GetBlobsPath(BasePath)); } [[nodiscard]] bool IsValid() const { return IsValid(m_OplogStoragePath); } [[nodiscard]] static bool IsValid(const std::filesystem::path& BasePath) { return TCasLogFile::IsValid(GetLogPath(BasePath)); } void WipeState() const { std::error_code Ec; RemoveFile(GetLogPath(), Ec); RemoveFile(GetBlobsPath(), Ec); } static bool Delete(const std::filesystem::path& BasePath) { return DeleteDirectories(BasePath); } uint64_t OpBlobsSize() const { return FileSizeFromPath(GetBlobsPath()); } uint64_t OpsSize() const { return OpsSize(m_OplogStoragePath); } static uint64_t OpsSize(const std::filesystem::path& BasePath) { if (Exists(BasePath)) { std::error_code DummyEc; return FileSizeFromPath(GetLogPath(BasePath)) + FileSizeFromPath(GetBlobsPath(BasePath)); } return 0; } uint32_t MaxLSN() const { return m_MaxLsn; } void SetMaxLSNAndNextWriteAddress(uint32_t MaxLSN, const OplogEntryAddress& NextOpFileOffset) { m_MaxLsn.store(MaxLSN); m_NextOpsOffset = RoundUp((NextOpFileOffset.Offset * m_OpsAlign) + NextOpFileOffset.Size, m_OpsAlign); } void Open(bool IsCreate) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::Open"); if (IsCreate) { ZEN_INFO("oplog '{}/{}': initializing storage at '{}'", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), m_OplogStoragePath); DeleteDirectories(m_OplogStoragePath); CreateDirectories(m_OplogStoragePath); } else { ZEN_INFO("oplog '{}/{}': opening storage at '{}'", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), m_OplogStoragePath); } m_Oplog.Open(GetLogPath(m_OplogStoragePath), IsCreate ? CasLogFile::Mode::kTruncate : CasLogFile::Mode::kWrite); m_Oplog.Initialize(); m_OpBlobs.Open(GetBlobsPath(m_OplogStoragePath), IsCreate ? BasicFile::Mode::kTruncate : BasicFile::Mode::kWrite); ZEN_ASSERT(IsPow2(m_OpsAlign)); ZEN_ASSERT(!(m_NextOpsOffset & (m_OpsAlign - 1))); } IoBuffer GetOpBuffer(BasicFileBuffer& OpBlobsBuffer, const OplogEntry& LogEntry) const { ZEN_MEMSCOPE(GetProjectstoreTag()); const uint64_t OpFileOffset = LogEntry.OpCoreOffset * m_OpsAlign; const MemoryView OpBufferView = OpBlobsBuffer.MakeView(LogEntry.OpCoreSize, OpFileOffset); if (OpBufferView.GetSize() == LogEntry.OpCoreSize) { return IoBuffer(IoBuffer::Wrap, OpBufferView.GetData(), OpBufferView.GetSize()); } else { IoBuffer OpBuffer(LogEntry.OpCoreSize); OpBlobsBuffer.Read((void*)OpBuffer.Data(), LogEntry.OpCoreSize, OpFileOffset); return OpBuffer; } } uint64_t GetEffectiveBlobsSize(std::span Addresses) const { uint64_t EffectiveSize = 0; for (const OplogEntryAddress& Address : Addresses) { EffectiveSize += RoundUp(Address.Size, m_OpsAlign); } return EffectiveSize; } void Compact( std::span LSNs, std::function&& Callback, bool RetainLSNs, bool DryRun) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::Compact"); ZEN_INFO("oplog '{}/{}': compacting at '{}'", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), m_OplogStoragePath); Stopwatch Timer; StringBuilder<64> OplogName; Oid::NewOid().ToString(OplogName); std::filesystem::path OplogPath = m_OplogStoragePath / OplogName.c_str(); std::error_code Ec; TCasLogFile Oplog; Oplog.Open(OplogPath, CasLogFile::Mode::kTruncate); (void)Oplog.Initialize(); TemporaryFile OpBlobs; OpBlobs.CreateTemporary(m_OplogStoragePath, Ec); if (Ec) { throw std::system_error(Ec, fmt::format("Failed to create temp file for op blob at '{}'", m_OplogStoragePath)); } try { std::vector Ops; Ops.reserve(LSNs.size()); tsl::robin_map LSNToIndex; LSNToIndex.reserve(LSNs.size()); for (uint32_t LSN : LSNs) { LSNToIndex[LSN] = (size_t)-1; } RwLock::ExclusiveLockScope Lock(m_RwLock); const uint64_t SkipEntryCount = 0; m_Oplog.Replay( [&](const OplogEntry& LogEntry) { if (auto It = LSNToIndex.find(LogEntry.OpLsn); It != LSNToIndex.end()) { if (It->second != (size_t)-1) { Ops[It->second] = LogEntry; } else { LSNToIndex[LogEntry.OpLsn] = Ops.size(); Ops.push_back(LogEntry); } } }, SkipEntryCount); std::sort(Ops.begin(), Ops.end(), [&](const OplogEntry& Lhs, const OplogEntry& Rhs) { return Lhs.OpCoreOffset < Rhs.OpCoreOffset; }); std::vector OldLSNs; OldLSNs.reserve(Ops.size()); uint64_t OpWriteOffset = 0; uint32_t MaxLSN = 0; { BasicFileBuffer OldBlobsBuffer(m_OpBlobs, 65536); BasicFileWriter NewOpBlobsBuffer(OpBlobs, 65536); for (OplogEntry& LogEntry : Ops) { OldLSNs.push_back(LogEntry.OpLsn); IoBuffer OpBuffer = GetOpBuffer(OldBlobsBuffer, LogEntry); if (RetainLSNs) { MaxLSN = Max(MaxLSN, LogEntry.OpLsn); } else { LogEntry.OpLsn = ++MaxLSN; } LogEntry.OpCoreOffset = gsl::narrow(OpWriteOffset / m_OpsAlign); NewOpBlobsBuffer.Write(OpBuffer.GetData(), LogEntry.OpCoreSize, OpWriteOffset); OpWriteOffset = RoundUp((LogEntry.OpCoreOffset * m_OpsAlign) + LogEntry.OpCoreSize, m_OpsAlign); } Oplog.Append(Ops); } uint64_t OldOpLogSize = m_Oplog.GetLogSize(); uint64_t OldOpBlobsSize = m_OpBlobs.FileSize(); if (!DryRun) { m_Oplog.Close(); m_OpBlobs.Close(); Oplog.Close(); RenameFile(OplogPath, GetLogPath(), Ec); if (Ec) { throw std::system_error( Ec, fmt::format("Oplog::Compact failed to rename temporary oplog blob storage file from '{}' to '{}'", OplogPath, GetLogPath())); } OpBlobs.MoveTemporaryIntoPlace(GetBlobsPath(), Ec); if (Ec) { // We failed late - clean everything up as best we can RemoveFile(OpBlobs.GetPath(), Ec); RemoveFile(GetLogPath(), Ec); RemoveFile(GetBlobsPath(), Ec); throw std::system_error(Ec, fmt::format("Oplog::Compact failed to rename temporary oplog file from '{}' to '{}'", OpBlobs.GetPath(), GetBlobsPath())); } m_Oplog.Open(GetLogPath(), CasLogFile::Mode::kWrite); m_Oplog.Initialize(); m_OpBlobs.Open(GetBlobsPath(), BasicFile::Mode::kWrite); m_MaxLsn.store(MaxLSN); m_NextOpsOffset.store(OpWriteOffset); } for (size_t Index = 0; Index < Ops.size(); Index++) { const OplogEntry& LogEntry = Ops[Index]; Callback(LogEntry.OpKeyHash, OldLSNs[Index], LogEntry.OpLsn, OplogEntryAddress{.Offset = LogEntry.OpCoreOffset, .Size = LogEntry.OpCoreSize}); } ZEN_INFO("oplog '{}/{}': compact completed in {} - Max LSN# {}, New size: {}, old size {}.", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), NiceTimeSpanMs(Timer.GetElapsedTimeMs()), m_MaxLsn.load(), NiceBytes(m_Oplog.GetLogSize() + m_OpBlobs.FileSize()), NiceBytes(OldOpLogSize + OldOpBlobsSize)); } catch (const std::exception& /*Ex*/) { RemoveFile(OpBlobs.GetPath(), Ec); throw; } } static std::filesystem::path GetLogPath(const std::filesystem::path& OplogStoragePath) { using namespace std::literals; return OplogStoragePath / "ops.zlog"sv; } static std::filesystem::path GetBlobsPath(const std::filesystem::path& OplogStoragePath) { using namespace std::literals; return OplogStoragePath / "ops.zops"sv; } std::filesystem::path GetLogPath() const { return GetLogPath(m_OplogStoragePath); } std::filesystem::path GetBlobsPath() const { return GetBlobsPath(m_OplogStoragePath); } void ReplayLog(std::function&& Handler, uint64_t SkipEntryCount = 0) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::ReplayLog"); if (m_Oplog.GetLogCount() == SkipEntryCount) { return; } Stopwatch Timer; uint64_t OpsBlockSize = m_OpBlobs.FileSize(); std::vector OpLogEntries; uint64_t InvalidEntries = 0; { tsl::robin_map LatestKeys; m_Oplog.Replay( [&](const OplogEntry& LogEntry) { if (LogEntry.IsTombstone()) { if (auto It = LatestKeys.find(LogEntry.OpKeyHash); It == LatestKeys.end()) { ZEN_SCOPED_WARN("found tombstone referencing unknown key {}", LogEntry.OpKeyHash); ++InvalidEntries; return; } } else if (LogEntry.OpCoreSize == 0) { ZEN_SCOPED_WARN("skipping zero size op {}", LogEntry.OpKeyHash); ++InvalidEntries; return; } else if (LogEntry.OpLsn == 0) { ZEN_SCOPED_WARN("skipping zero lsn op {}", LogEntry.OpKeyHash); ++InvalidEntries; return; } const uint64_t OpFileOffset = LogEntry.OpCoreOffset * m_OpsAlign; if ((OpFileOffset + LogEntry.OpCoreSize) > OpsBlockSize) { ZEN_SCOPED_WARN("skipping out of bounds op {}", LogEntry.OpKeyHash); ++InvalidEntries; return; } if (auto It = LatestKeys.find(LogEntry.OpKeyHash); It != LatestKeys.end()) { OplogEntry& Entry = OpLogEntries[It->second]; if (LogEntry.IsTombstone() && Entry.IsTombstone()) { ZEN_SCOPED_WARN("found double tombstone - {}", LogEntry.OpKeyHash); } Entry = LogEntry; } else { const size_t OpIndex = OpLogEntries.size(); LatestKeys[LogEntry.OpKeyHash] = OpIndex; OpLogEntries.push_back(LogEntry); } }, SkipEntryCount); } std::sort(OpLogEntries.begin(), OpLogEntries.end(), [&](const OplogEntry& Lhs, const OplogEntry& Rhs) { return Lhs.OpCoreOffset < Rhs.OpCoreOffset; }); uint64_t TombstoneEntries = 0; BasicFileBuffer OpBlobsBuffer(m_OpBlobs, 65536); uint32_t MaxOpLsn = m_MaxLsn; uint64_t NextOpFileOffset = m_NextOpsOffset; for (const OplogEntry& LogEntry : OpLogEntries) { if (LogEntry.IsTombstone()) { TombstoneEntries++; } else { IoBuffer OpBuffer = GetOpBuffer(OpBlobsBuffer, LogEntry); // Verify checksum, ignore op data if incorrect const uint32_t ExpectedOpCoreHash = LogEntry.OpCoreHash; const uint32_t OpCoreHash = uint32_t(XXH3_64bits(OpBuffer.GetData(), LogEntry.OpCoreSize) & 0xffffFFFF); if (OpCoreHash != ExpectedOpCoreHash) { ZEN_WARN("oplog '{}/{}': skipping bad checksum op - {}. Expected: {}, found: {}", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), LogEntry.OpKeyHash, ExpectedOpCoreHash, OpCoreHash); } else if (CbValidateError Err = ValidateCompactBinary(OpBuffer.GetView(), CbValidateMode::Default); Err != CbValidateError::None) { ZEN_WARN("oplog '{}/{}': skipping invalid format op - {}. Error: '{}'", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), LogEntry.OpKeyHash, ToString(Err)); } else { Handler(CbObjectView(OpBuffer.GetData()), LogEntry); MaxOpLsn = Max(MaxOpLsn, LogEntry.OpLsn); const uint64_t EntryNextOpFileOffset = RoundUp((LogEntry.OpCoreOffset * m_OpsAlign) + LogEntry.OpCoreSize, m_OpsAlign); NextOpFileOffset = Max(NextOpFileOffset, EntryNextOpFileOffset); } } } m_MaxLsn = MaxOpLsn; m_NextOpsOffset = NextOpFileOffset; ZEN_INFO("oplog '{}/{}': replay from '{}' completed in {} - Max LSN# {}, Next offset: {}, {} tombstones, {} invalid entries", m_OwnerOplog->GetOuterProject()->Identifier, m_OwnerOplog->OplogId(), m_OplogStoragePath, NiceTimeSpanMs(Timer.GetElapsedTimeMs()), m_MaxLsn.load(), m_NextOpsOffset.load(), TombstoneEntries, InvalidEntries); } void ReplayLogEntries(const std::span Entries, std::function&& Handler) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::ReplayLogEntries"); BasicFileBuffer OpBlobsBuffer(m_OpBlobs, 65536); for (const OplogEntryAddress& Entry : Entries) { const uint64_t OpFileOffset = Entry.Offset * m_OpsAlign; MemoryView OpBufferView = OpBlobsBuffer.MakeView(Entry.Size, OpFileOffset); if (OpBufferView.GetSize() == Entry.Size) { Handler(CbObjectView(OpBufferView.GetData())); continue; } IoBuffer OpBuffer(Entry.Size); OpBlobsBuffer.Read((void*)OpBuffer.Data(), Entry.Size, OpFileOffset); Handler(CbObjectView(OpBuffer.Data())); } } CbObject GetOp(const OplogEntryAddress& Entry) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::GetOp"); IoBuffer OpBuffer(Entry.Size); const uint64_t OpFileOffset = Entry.Offset * m_OpsAlign; m_OpBlobs.Read((void*)OpBuffer.Data(), Entry.Size, OpFileOffset); return CbObject(SharedBuffer(std::move(OpBuffer))); } struct AppendOpData { MemoryView Buffer; uint32_t OpCoreHash; Oid KeyHash; }; static OplogStorage::AppendOpData GetAppendOpData(const CbObjectView& Core) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; AppendOpData OpData; OpData.Buffer = Core.GetView(); const uint64_t WriteSize = OpData.Buffer.GetSize(); OpData.OpCoreHash = uint32_t(XXH3_64bits(OpData.Buffer.GetData(), WriteSize) & 0xffffFFFF); ZEN_ASSERT(WriteSize != 0); OpData.KeyHash = ComputeOpKey(Core); return OpData; } OplogEntry AppendOp(const AppendOpData& OpData) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::AppendOp"); uint64_t WriteSize = OpData.Buffer.GetSize(); RwLock::ExclusiveLockScope Lock(m_RwLock); const uint64_t WriteOffset = m_NextOpsOffset; const uint32_t OpLsn = ++m_MaxLsn; if (OpLsn == std::numeric_limits::max()) { ZEN_ERROR("Oplog count has exceeded available range for oplog {}", m_OwnerOplog->OplogId()); throw std::runtime_error(fmt::format("Oplog count has exceeded available range for oplog {}", m_OwnerOplog->OplogId())); } m_NextOpsOffset = RoundUp(WriteOffset + WriteSize, m_OpsAlign); Lock.ReleaseNow(); ZEN_ASSERT(IsMultipleOf(WriteOffset, m_OpsAlign)); OplogEntry Entry = {.OpLsn = OpLsn, .OpCoreOffset = gsl::narrow_cast(WriteOffset / m_OpsAlign), .OpCoreSize = uint32_t(WriteSize), .OpCoreHash = OpData.OpCoreHash, .OpKeyHash = OpData.KeyHash}; m_OpBlobs.Write(OpData.Buffer.GetData(), WriteSize, WriteOffset); m_Oplog.Append(Entry); return Entry; } std::vector AppendOps(std::span Ops) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OplogStorage::AppendOps"); size_t OpCount = Ops.size(); std::vector> OffsetAndSizes; std::vector OpLsns; OffsetAndSizes.resize(OpCount); OpLsns.resize(OpCount); for (size_t OpIndex = 0; OpIndex < OpCount; OpIndex++) { OffsetAndSizes[OpIndex].second = Ops[OpIndex].Buffer.GetSize(); } uint64_t WriteStart = 0; uint64_t WriteLength = 0; { RwLock::ExclusiveLockScope Lock(m_RwLock); WriteStart = m_NextOpsOffset; ZEN_ASSERT(IsMultipleOf(WriteStart, m_OpsAlign)); uint64_t WriteOffset = WriteStart; for (size_t OpIndex = 0; OpIndex < OpCount; OpIndex++) { OffsetAndSizes[OpIndex].first = WriteOffset - WriteStart; OpLsns[OpIndex] = ++m_MaxLsn; if (OpLsns[OpIndex] == std::numeric_limits::max()) { ZEN_ERROR("Oplog count has exceeded available range for oplog {}", m_OwnerOplog->OplogId()); throw std::runtime_error(fmt::format("Oplog count has exceeded available range for oplog {}", m_OwnerOplog->OplogId())); } WriteOffset = RoundUp(WriteOffset + OffsetAndSizes[OpIndex].second, m_OpsAlign); } WriteLength = WriteOffset - WriteStart; m_NextOpsOffset = RoundUp(WriteOffset, m_OpsAlign); } IoBuffer WriteBuffer(WriteLength); std::vector Entries; Entries.resize(OpCount); for (size_t OpIndex = 0; OpIndex < OpCount; OpIndex++) { MutableMemoryView WriteBufferView = WriteBuffer.GetMutableView().RightChop(OffsetAndSizes[OpIndex].first); WriteBufferView.CopyFrom(Ops[OpIndex].Buffer); Entries[OpIndex] = {.OpLsn = OpLsns[OpIndex], .OpCoreOffset = gsl::narrow_cast((WriteStart + OffsetAndSizes[OpIndex].first) / m_OpsAlign), .OpCoreSize = uint32_t(OffsetAndSizes[OpIndex].second), .OpCoreHash = Ops[OpIndex].OpCoreHash, .OpKeyHash = Ops[OpIndex].KeyHash}; } m_OpBlobs.Write(WriteBuffer.GetData(), WriteBuffer.GetSize(), WriteStart); m_Oplog.Append(Entries); return Entries; } void AppendTombstone(Oid KeyHash) { OplogEntry Entry = {.OpKeyHash = KeyHash}; Entry.MakeTombstone(); m_Oplog.Append(Entry); } void Flush() { m_Oplog.Flush(); m_OpBlobs.Flush(); } uint64_t LogCount() const { return m_Oplog.GetLogCount(); } LoggerRef Log() { return m_OwnerOplog->Log(); } private: ProjectStore::Oplog* m_OwnerOplog; std::filesystem::path m_OplogStoragePath; mutable RwLock m_RwLock; TCasLogFile m_Oplog; BasicFile m_OpBlobs; std::atomic m_NextOpsOffset{0}; uint64_t m_OpsAlign = 32; std::atomic m_MaxLsn{0}; }; ////////////////////////////////////////////////////////////////////////// ProjectStore::Oplog::Oplog(std::string_view Id, Project* Project, CidStore& Store, std::filesystem::path BasePath, const std::filesystem::path& MarkerPath) : m_OuterProject(Project) , m_OplogId(Id) , m_CidStore(Store) , m_BasePath(BasePath) , m_MarkerPath(MarkerPath) , m_MetaValid(false) , m_PendingPrepOpAttachmentsRetainEnd(GcClock::Now()) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; m_Storage = new OplogStorage(this, m_BasePath); bool StoreExists = m_Storage->Exists(); if (StoreExists) { if (!m_Storage->IsValid()) { ZEN_WARN("Invalid oplog found at '{}'. Wiping state for oplog.", m_BasePath); m_Storage->WipeState(); std::error_code DummyEc; RemoveFile(m_MetaPath, DummyEc); } } m_Storage->Open(/* IsCreate */ !StoreExists); m_TempPath = m_BasePath / "temp"sv; m_MetaPath = m_BasePath / "ops.meta"sv; m_MetaValid = !IsFileOlderThan(m_MetaPath, m_Storage->GetBlobsPath()); CleanDirectory(m_TempPath, /*ForceRemoveReadOnlyFiles*/ false); } ProjectStore::Oplog::~Oplog() { if (m_Storage) { Flush(); } } void ProjectStore::Oplog::Flush() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Oplog::Flush"); RwLock::SharedLockScope Lock(m_OplogLock); ZEN_ASSERT(m_Storage); m_Storage->Flush(); if (!m_MetaValid) { std::error_code DummyEc; RemoveFile(m_MetaPath, DummyEc); } uint64_t LogCount = m_Storage->LogCount(); if (m_LogFlushPosition != LogCount) { WriteIndexSnapshot(); } } void ProjectStore::Oplog::Scrub(ScrubContext& Ctx) { ZEN_MEMSCOPE(GetProjectstoreTag()); std::vector BadEntryKeys; using namespace std::literals; IterateOplogWithKey([&](uint32_t Lsn, const Oid& Key, CbObjectView Op) { ZEN_UNUSED(Lsn); std::vector Cids; Op.IterateAttachments([&](CbFieldView Visitor) { Cids.emplace_back(Visitor.AsAttachment()); }); { const Oid KeyHash = ComputeOpKey(Op); ZEN_ASSERT_FORMAT(KeyHash == Key, "oplog data does not match information from index (op:{} != index:{})", KeyHash, Key); } for (const IoHash& Cid : Cids) { if (!m_CidStore.ContainsChunk(Cid)) { // oplog entry references a CAS chunk which is not // present BadEntryKeys.push_back(Key); return; } if (Ctx.IsBadCid(Cid)) { // oplog entry references a CAS chunk which has been // flagged as bad BadEntryKeys.push_back(Key); return; } } }); if (!BadEntryKeys.empty()) { if (Ctx.RunRecovery()) { ZEN_WARN("oplog '{}/{}': scrubbing found {} bad ops in oplog @ '{}', these will be removed from the index", m_OuterProject->Identifier, m_OplogId, BadEntryKeys.size(), m_BasePath); // Actually perform some clean-up RwLock::ExclusiveLockScope _(m_OplogLock); for (const auto& Key : BadEntryKeys) { if (auto It = m_LatestOpMap.find(Key); It != m_LatestOpMap.end()) { m_OpAddressMap.erase(It->second); m_LatestOpMap.erase(It); } m_Storage->AppendTombstone(Key); } if (!BadEntryKeys.empty()) { m_MetaValid = false; } } else { ZEN_WARN("oplog '{}/{}': scrubbing found {} bad ops in oplog @ '{}' but no cleanup will be performed", m_OuterProject->Identifier, m_OplogId, BadEntryKeys.size(), m_BasePath); } } } uint64_t ProjectStore::Oplog::TotalSize(const std::filesystem::path& BasePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; uint64_t Size = OplogStorage::OpsSize(BasePath); std::filesystem::path StateFilePath = BasePath / "oplog.zcb"sv; if (IsFile(StateFilePath)) { Size += FileSizeFromPath(StateFilePath); } std::filesystem::path MetaFilePath = BasePath / "ops.meta"sv; if (IsFile(MetaFilePath)) { Size += FileSizeFromPath(MetaFilePath); } std::filesystem::path IndexFilePath = BasePath / "ops.zidx"sv; if (IsFile(IndexFilePath)) { Size += FileSizeFromPath(IndexFilePath); } return Size; } uint64_t ProjectStore::Oplog::TotalSize() const { return TotalSize(m_BasePath); } void ProjectStore::Oplog::ResetState() { RwLock::ExclusiveLockScope _(m_OplogLock); m_ChunkMap.clear(); m_MetaMap.clear(); m_FileMap.clear(); m_OpAddressMap.clear(); m_LatestOpMap.clear(); m_Storage = {}; } bool ProjectStore::Oplog::PrepareForDelete(std::filesystem::path& OutRemoveDirectory) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::ExclusiveLockScope _(m_OplogLock); m_UpdateCaptureRefCounter = 0; m_CapturedLSNs.reset(); m_CapturedAttachments.reset(); m_PendingPrepOpAttachments.clear(); m_ChunkMap.clear(); m_MetaMap.clear(); m_FileMap.clear(); m_OpAddressMap.clear(); m_LatestOpMap.clear(); m_Storage = {}; if (PrepareDirectoryDelete(m_BasePath, OutRemoveDirectory)) { return true; } return false; } bool ProjectStore::Oplog::ExistsAt(const std::filesystem::path& BasePath) { using namespace std::literals; std::filesystem::path StateFilePath = BasePath / "oplog.zcb"sv; return IsFile(StateFilePath); } bool ProjectStore::Oplog::Exists() const { return ExistsAt(m_BasePath); } void ProjectStore::Oplog::Read() { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; ZEN_TRACE_CPU("Oplog::Read"); ZEN_LOG_SCOPE("Oplog::Read '{}'", m_OplogId); ZEN_DEBUG("oplog '{}': reading config from '{}'", m_OuterProject->Identifier, m_OplogId, m_BasePath); std::optional Config = ReadStateFile(m_BasePath, [this]() { return Log(); }); if (Config.has_value()) { if (Config.value().GetSize() == 0) { // Invalid config file return; } m_MarkerPath = Config.value()["gcpath"sv].AsU8String(); } if (!m_MetaValid) { std::error_code DummyEc; RemoveFile(m_MetaPath, DummyEc); } ReadIndexSnapshot(); m_Storage->ReplayLog( [&](CbObjectView Op, const OplogEntry& OpEntry) { // MaxLSN = Max(OpEntry.OpLsn, MaxLSN); const OplogEntryMapping OpMapping = GetMapping(Op); // Update chunk id maps for (const ChunkMapping& Chunk : OpMapping.Chunks) { m_ChunkMap.insert_or_assign(Chunk.Id, Chunk.Hash); } for (const FileMapping& File : OpMapping.Files) { if (File.Hash != IoHash::Zero) { m_ChunkMap.insert_or_assign(File.Id, File.Hash); } m_FileMap.insert_or_assign( File.Id, FileMapEntry{.ServerPath = File.Hash == IoHash::Zero ? File.ServerPath : std::string(), .ClientPath = File.ClientPath}); } for (const ChunkMapping& Meta : OpMapping.Meta) { m_MetaMap.insert_or_assign(Meta.Id, Meta.Hash); } m_OpAddressMap.emplace(OpEntry.OpLsn, OplogEntryAddress{.Offset = OpEntry.OpCoreOffset, .Size = OpEntry.OpCoreSize}); m_LatestOpMap[OpEntry.OpKeyHash] = OpEntry.OpLsn; }, m_LogFlushPosition); if (m_Storage->LogCount() != m_LogFlushPosition) { WriteIndexSnapshot(); } } void ProjectStore::Oplog::Write() { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; BinaryWriter Mem; CbObjectWriter Cfg; Cfg << "gcpath"sv << PathToUtf8(m_MarkerPath); Cfg.Save(Mem); std::filesystem::path StateFilePath = m_BasePath / "oplog.zcb"sv; ZEN_INFO("oplog '{}/{}': persisting config to '{}'", m_OuterProject->Identifier, m_OplogId, StateFilePath); TemporaryFile::SafeWriteFile(StateFilePath, Mem.GetView()); } void ProjectStore::Oplog::Update(const std::filesystem::path& MarkerPath) { if (m_MarkerPath == MarkerPath) { return; } Write(); } bool ProjectStore::Oplog::Reset() { ZEN_MEMSCOPE(GetProjectstoreTag()); std::filesystem::path MovedDir; { RwLock::ExclusiveLockScope OplogLock(m_OplogLock); m_Storage = {}; if (!PrepareDirectoryDelete(m_BasePath, MovedDir)) { m_Storage = new OplogStorage(this, m_BasePath); const bool StoreExists = m_Storage->Exists(); m_Storage->Open(/* IsCreate */ !StoreExists); m_MetaValid = !IsFileOlderThan(m_MetaPath, m_Storage->GetBlobsPath()); return false; } m_ChunkMap.clear(); m_MetaMap.clear(); m_FileMap.clear(); m_OpAddressMap.clear(); m_LatestOpMap.clear(); m_Storage = new OplogStorage(this, m_BasePath); m_Storage->Open(true); m_MetaValid = false; CleanDirectory(m_TempPath, /*ForceRemoveReadOnlyFiles*/ false); Write(); } // Erase content on disk if (!MovedDir.empty()) { OplogStorage::Delete(MovedDir); } return true; } std::optional ProjectStore::Oplog::ReadStateFile(const std::filesystem::path& BasePath, std::function&& Log) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Oplog::ReadStateFile"); using namespace std::literals; std::filesystem::path StateFilePath = BasePath / "oplog.zcb"sv; if (IsFile(StateFilePath)) { // ZEN_INFO("oplog '{}/{}': config read from '{}'", m_OuterProject->Identifier, m_OplogId, StateFilePath); BasicFile Blob; Blob.Open(StateFilePath, BasicFile::Mode::kRead); IoBuffer Obj = Blob.ReadAll(); CbValidateError ValidationError = ValidateCompactBinary(MemoryView(Obj.Data(), Obj.Size()), CbValidateMode::All); if (ValidationError != CbValidateError::None) { ZEN_ERROR("validation error {} hit for oplog config at '{}'", ToString(ValidationError), StateFilePath); return CbObject(); } return LoadCompactBinaryObject(Obj); } ZEN_INFO("config for oplog not found at '{}'. Assuming legacy store", StateFilePath); return {}; } ProjectStore::Oplog::ValidationResult ProjectStore::Oplog::Validate(std::atomic_bool& IsCancelledFlag, WorkerThreadPool* OptionalWorkerPool) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; ValidationResult Result; const size_t OpCount = OplogCount(); std::vector KeyHashes; std::vector Keys; std::vector> Attachments; std::vector Mappings; KeyHashes.reserve(OpCount); Keys.reserve(OpCount); Mappings.reserve(OpCount); IterateOplogWithKey([&](uint32_t LSN, const Oid& Key, CbObjectView OpView) { Result.LSNLow = Min(Result.LSNLow, LSN); Result.LSNHigh = Max(Result.LSNHigh, LSN); KeyHashes.push_back(Key); Keys.emplace_back(std::string(OpView["key"sv].AsString())); std::vector OpAttachments; OpView.IterateAttachments([&OpAttachments](CbFieldView Attachment) { OpAttachments.push_back(Attachment.AsAttachment()); }); Attachments.emplace_back(std::move(OpAttachments)); Mappings.push_back(GetMapping(OpView)); }); Result.OpCount = gsl::narrow(Keys.size()); RwLock ResultLock; auto ValidateOne = [&](uint32_t OpIndex) { const Oid& KeyHash = KeyHashes[OpIndex]; const std::string& Key = Keys[OpIndex]; const OplogEntryMapping& Mapping(Mappings[OpIndex]); bool HasMissingEntries = false; for (const ChunkMapping& Chunk : Mapping.Chunks) { if (!m_CidStore.ContainsChunk(Chunk.Hash)) { ResultLock.WithExclusiveLock([&]() { Result.MissingChunks.push_back({KeyHash, Chunk}); }); HasMissingEntries = true; } } for (const ChunkMapping& Meta : Mapping.Meta) { if (!m_CidStore.ContainsChunk(Meta.Hash)) { ResultLock.WithExclusiveLock([&]() { Result.MissingMetas.push_back({KeyHash, Meta}); }); HasMissingEntries = true; } } for (const FileMapping& File : Mapping.Files) { if (File.Hash == IoHash::Zero) { std::filesystem::path FilePath = m_OuterProject->RootDir / File.ServerPath; if (!IsFile(FilePath)) { ResultLock.WithExclusiveLock([&]() { Result.MissingFiles.push_back({KeyHash, File}); }); HasMissingEntries = true; } } else if (!m_CidStore.ContainsChunk(File.Hash)) { ResultLock.WithExclusiveLock([&]() { Result.MissingFiles.push_back({KeyHash, File}); }); HasMissingEntries = true; } } const std::vector& OpAttachments = Attachments[OpIndex]; for (const IoHash& Attachment : OpAttachments) { if (!m_CidStore.ContainsChunk(Attachment)) { ResultLock.WithExclusiveLock([&]() { Result.MissingAttachments.push_back({KeyHash, Attachment}); }); HasMissingEntries = true; } } if (HasMissingEntries) { ResultLock.WithExclusiveLock([&]() { Result.OpKeys.push_back({KeyHash, Key}); }); } }; std::atomic AbortFlag; std::atomic PauseFlag; ParallelWork Work(AbortFlag, PauseFlag); try { for (uint32_t OpIndex = 0; !IsCancelledFlag && OpIndex < Result.OpCount; OpIndex++) { if (AbortFlag) { break; } if (OptionalWorkerPool) { Work.ScheduleWork(*OptionalWorkerPool, [&ValidateOne, Index = OpIndex](std::atomic& AbortFlag) { ZEN_MEMSCOPE(GetProjectstoreTag()); if (AbortFlag) { return; } ValidateOne(Index); }); } else { ValidateOne(OpIndex); } } } catch (const std::exception& Ex) { AbortFlag.store(true); ZEN_WARN("Failed validating oplogs in {}. Reason: '{}'", m_BasePath, Ex.what()); } Work.Wait(); { // Check if we were deleted while we were checking the references without a lock... RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { Result = {}; } } return Result; } void ProjectStore::Oplog::WriteIndexSnapshot() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Oplog::WriteIndexSnapshot"); ZEN_DEBUG("oplog '{}/{}': write store snapshot at '{}'", m_OuterProject->Identifier, m_OplogId, m_BasePath); uint64_t EntryCount = 0; Stopwatch Timer; const auto _ = MakeGuard([&] { ZEN_INFO("oplog '{}/{}': wrote store snapshot for '{}' containing {} entries in {}", m_OuterProject->Identifier, m_OplogId, m_BasePath, EntryCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); namespace fs = std::filesystem; const fs::path IndexPath = m_BasePath / "ops.zidx"; try { // Write the current state of the location map to a new index state std::vector LSNEntries; std::vector Keys; std::vector AddressMapEntries; std::vector LatestOpMapEntries; std::vector ChunkMapEntries; std::vector MetaMapEntries; std::vector FilePathLengths; std::vector FilePaths; uint64_t IndexLogPosition = 0; { IndexLogPosition = m_Storage->LogCount(); Keys.reserve(m_LatestOpMap.size() + m_ChunkMap.size() + m_MetaMap.size() + m_FileMap.size()); AddressMapEntries.reserve(m_OpAddressMap.size()); LSNEntries.reserve(m_OpAddressMap.size()); for (const auto& It : m_OpAddressMap) { LSNEntries.push_back(It.first); AddressMapEntries.push_back(It.second); } LatestOpMapEntries.reserve(m_LatestOpMap.size()); for (const auto& It : m_LatestOpMap) { Keys.push_back(It.first); LatestOpMapEntries.push_back(It.second); } ChunkMapEntries.reserve(m_ChunkMap.size()); for (const auto& It : m_ChunkMap) { Keys.push_back(It.first); ChunkMapEntries.push_back(It.second); } MetaMapEntries.reserve(m_MetaMap.size()); for (const auto& It : m_MetaMap) { Keys.push_back(It.first); MetaMapEntries.push_back(It.second); } FilePathLengths.reserve(m_FileMap.size() * 2); FilePaths.reserve(m_FileMap.size() * 2); for (const auto& It : m_FileMap) { Keys.push_back(It.first); FilePathLengths.push_back(gsl::narrow(It.second.ServerPath.length())); FilePathLengths.push_back(gsl::narrow(It.second.ClientPath.length())); FilePaths.push_back(It.second.ServerPath); FilePaths.push_back(It.second.ClientPath); } } TemporaryFile ObjectIndexFile; std::error_code Ec; ObjectIndexFile.CreateTemporary(IndexPath.parent_path(), Ec); if (Ec) { throw std::system_error(Ec, fmt::format("Failed to create temp file for index snapshot at '{}'", IndexPath)); } { BasicFileWriter IndexFile(ObjectIndexFile, 65536u); OplogIndexHeader Header = {.LogPosition = IndexLogPosition, .LSNCount = gsl::narrow(LSNEntries.size()), .KeyCount = gsl::narrow(Keys.size()), .OpAddressMapCount = gsl::narrow(AddressMapEntries.size()), .LatestOpMapCount = gsl::narrow(LatestOpMapEntries.size()), .ChunkMapCount = gsl::narrow(ChunkMapEntries.size()), .MetaMapCount = gsl::narrow(MetaMapEntries.size()), .FileMapCount = gsl::narrow(FilePathLengths.size() / 2)}; Header.Checksum = OplogIndexHeader::ComputeChecksum(Header); uint64_t Offset = 0; IndexFile.Write(&Header, sizeof(OplogIndexHeader), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(LSNEntries.data(), LSNEntries.size() * sizeof(uint32_t), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(Keys.data(), Keys.size() * sizeof(Oid), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(AddressMapEntries.data(), AddressMapEntries.size() * sizeof(OplogEntryAddress), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(LatestOpMapEntries.data(), LatestOpMapEntries.size() * sizeof(uint32_t), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(ChunkMapEntries.data(), ChunkMapEntries.size() * sizeof(IoHash), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(MetaMapEntries.data(), MetaMapEntries.size() * sizeof(IoHash), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); IndexFile.Write(FilePathLengths.data(), FilePathLengths.size() * sizeof(uint32_t), Offset); Offset = IndexFile.AlignTo(OplogIndexHeader::DataAlignment); for (const auto& FilePath : FilePaths) { IndexFile.Write(FilePath.c_str(), FilePath.length(), Offset); Offset += FilePath.length(); } } ObjectIndexFile.Flush(); ObjectIndexFile.MoveTemporaryIntoPlace(IndexPath, Ec); if (Ec) { throw std::system_error(Ec, fmt::format("Snapshot failed to rename new snapshot '{}' to '{}', reason: '{}'", ObjectIndexFile.GetPath(), IndexPath, Ec.message())); } EntryCount = LSNEntries.size(); m_LogFlushPosition = IndexLogPosition; } catch (const std::exception& Err) { ZEN_WARN("oplog '{}/{}': snapshot FAILED, reason: '{}'", m_OuterProject->Identifier, m_OplogId, Err.what()); } } void ProjectStore::Oplog::ReadIndexSnapshot() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Oplog::ReadIndexSnapshot"); const std::filesystem::path IndexPath = m_BasePath / "ops.zidx"; if (IsFile(IndexPath)) { uint64_t EntryCount = 0; Stopwatch Timer; const auto _ = MakeGuard([&] { ZEN_INFO("oplog '{}/{}': index read from '{}' containing {} entries in {}", m_OuterProject->Identifier, m_OplogId, IndexPath, EntryCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); try { BasicFile ObjectIndexFile; ObjectIndexFile.Open(IndexPath, BasicFile::Mode::kRead); uint64_t Size = ObjectIndexFile.FileSize(); if (Size >= sizeof(OplogIndexHeader)) { OplogIndexHeader Header; uint64_t Offset = 0; ObjectIndexFile.Read(&Header, sizeof(Header), 0); Offset += sizeof(OplogIndexHeader); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); if ((Header.Magic == OplogIndexHeader::ExpectedMagic) && (Header.Version == OplogIndexHeader::CurrentVersion) && (Header.Checksum == OplogIndexHeader::ComputeChecksum(Header))) { uint32_t MaxLSN = 0; OplogEntryAddress LastOpAddress{.Offset = 0, .Size = 0}; uint32_t Checksum = OplogIndexHeader::ComputeChecksum(Header); if (Header.Checksum != Checksum) { ZEN_WARN("oplog '{}/{}': skipping invalid index file '{}'. Checksum mismatch. Expected: {}, Found: {}", m_OuterProject->Identifier, m_OplogId, IndexPath, Header.Checksum, Checksum); return; } if (Header.LatestOpMapCount + Header.ChunkMapCount + Header.MetaMapCount + Header.FileMapCount != Header.KeyCount) { ZEN_WARN("oplog '{}/{}': skipping invalid index file '{}'. Key count mismatch. Expected: {}, Found: {}", m_OuterProject->Identifier, m_OplogId, IndexPath, Header.LatestOpMapCount + Header.ChunkMapCount + Header.MetaMapCount + Header.FileMapCount, Header.KeyCount); return; } std::vector LSNEntries(Header.LSNCount); ObjectIndexFile.Read(LSNEntries.data(), LSNEntries.size() * sizeof(uint32_t), Offset); Offset += LSNEntries.size() * sizeof(uint32_t); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); size_t LSNOffset = 0; std::vector Keys(Header.KeyCount); ObjectIndexFile.Read(Keys.data(), Keys.size() * sizeof(Oid), Offset); Offset += Keys.size() * sizeof(Oid); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); size_t KeyOffset = 0; { std::vector AddressMapEntries(Header.OpAddressMapCount); ObjectIndexFile.Read(AddressMapEntries.data(), AddressMapEntries.size() * sizeof(OplogEntryAddress), Offset); Offset += AddressMapEntries.size() * sizeof(OplogEntryAddress); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); m_OpAddressMap.reserve(AddressMapEntries.size()); for (const OplogEntryAddress& Address : AddressMapEntries) { m_OpAddressMap.insert_or_assign(LSNEntries[LSNOffset++], Address); if (Address.Offset > LastOpAddress.Offset) { LastOpAddress = Address; } } } { std::vector LatestOpMapEntries(Header.LatestOpMapCount); ObjectIndexFile.Read(LatestOpMapEntries.data(), LatestOpMapEntries.size() * sizeof(uint32_t), Offset); Offset += LatestOpMapEntries.size() * sizeof(uint32_t); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); m_LatestOpMap.reserve(LatestOpMapEntries.size()); for (uint32_t LSN : LatestOpMapEntries) { m_LatestOpMap.insert_or_assign(Keys[KeyOffset++], LSN); MaxLSN = Max(MaxLSN, LSN); } } { std::vector ChunkMapEntries(Header.ChunkMapCount); ObjectIndexFile.Read(ChunkMapEntries.data(), ChunkMapEntries.size() * sizeof(IoHash), Offset); Offset += ChunkMapEntries.size() * sizeof(IoHash); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); m_ChunkMap.reserve(ChunkMapEntries.size()); for (const IoHash& ChunkId : ChunkMapEntries) { m_ChunkMap.insert_or_assign(Keys[KeyOffset++], ChunkId); } } { std::vector MetaMapEntries(Header.MetaMapCount); ObjectIndexFile.Read(MetaMapEntries.data(), MetaMapEntries.size() * sizeof(IoHash), Offset); Offset += MetaMapEntries.size() * sizeof(IoHash); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); m_MetaMap.reserve(MetaMapEntries.size()); for (const IoHash& ChunkId : MetaMapEntries) { m_MetaMap.insert_or_assign(Keys[KeyOffset++], ChunkId); } } { m_FileMap.reserve(Header.FileMapCount); std::vector FilePathLengths(Header.FileMapCount * 2); ObjectIndexFile.Read(FilePathLengths.data(), FilePathLengths.size() * sizeof(uint32_t), Offset); Offset += FilePathLengths.size() * sizeof(uint32_t); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); BasicFileBuffer IndexFile(ObjectIndexFile, 65536); auto ReadString([&IndexFile, &Offset](uint32_t Length) -> std::string { MemoryView StringData = IndexFile.MakeView(Length, Offset); if (StringData.GetSize() != Length) { throw std::runtime_error(fmt::format("Invalid format. Expected to read %u bytes but got %u", Length, uint32_t(StringData.GetSize()))); } Offset += Length; return std::string((const char*)StringData.GetData(), Length); }); for (uint64_t FileLengthOffset = 0; FileLengthOffset < FilePathLengths.size();) { std::string ServerPath = ReadString(FilePathLengths[FileLengthOffset++]); std::string ClientPath = ReadString(FilePathLengths[FileLengthOffset++]); m_FileMap.insert_or_assign( Keys[KeyOffset++], FileMapEntry{.ServerPath = std::move(ServerPath), .ClientPath = std::move(ClientPath)}); } } m_LogFlushPosition = Header.LogPosition; m_Storage->SetMaxLSNAndNextWriteAddress(MaxLSN, LastOpAddress); EntryCount = Header.LSNCount; } else { ZEN_WARN("oplog '{}/{}': skipping invalid index file '{}'", m_OuterProject->Identifier, m_OplogId, IndexPath); } } } catch (const std::exception& Ex) { m_OpAddressMap.clear(); m_LatestOpMap.clear(); m_ChunkMap.clear(); m_MetaMap.clear(); m_FileMap.clear(); m_LogFlushPosition = 0; ZEN_ERROR("oplog '{}/{}': failed reading index snapshot from '{}'. Reason: '{}'", m_OuterProject->Identifier, m_OplogId, IndexPath, Ex.what()); } } } uint32_t ProjectStore::Oplog::GetUnusedSpacePercent() const { RwLock::SharedLockScope OplogLock(m_OplogLock); return GetUnusedSpacePercentLocked(); } uint32_t ProjectStore::Oplog::GetUnusedSpacePercentLocked() const { ZEN_MEMSCOPE(GetProjectstoreTag()); const uint64_t ActualBlobsSize = m_Storage->OpBlobsSize(); if (ActualBlobsSize == 0) { return 0; } std::vector Addresses; { Addresses.reserve(m_LatestOpMap.size()); for (auto It : m_LatestOpMap) { if (auto AddressIt = m_OpAddressMap.find(It.second); AddressIt != m_OpAddressMap.end()) { Addresses.push_back(AddressIt->second); } } } const uint64_t EffectiveBlobsSize = m_Storage->GetEffectiveBlobsSize(std::move(Addresses)); if (EffectiveBlobsSize < ActualBlobsSize) { return gsl::narrow((100 * (ActualBlobsSize - EffectiveBlobsSize)) / ActualBlobsSize); } return 0; } void ProjectStore::Oplog::Compact(bool DryRun, bool RetainLSNs, std::string_view LogPrefix) { RwLock::ExclusiveLockScope Lock(m_OplogLock); Compact(Lock, DryRun, RetainLSNs, LogPrefix); } void ProjectStore::Oplog::Compact(RwLock::ExclusiveLockScope&, bool DryRun, bool RetainLSNs, std::string_view LogPrefix) { ZEN_MEMSCOPE(GetProjectstoreTag()); Stopwatch Timer; std::vector LSNs; LSNs.reserve(m_LatestOpMap.size()); for (auto It : m_LatestOpMap) { LSNs.push_back(It.second); } tsl::robin_map OpAddressMap; // Index LSN -> op data in ops blob file OidMap LatestOpMap; // op key -> latest op LSN for key uint64_t PreSize = TotalSize(); m_Storage->Compact( LSNs, [&](const Oid& OpKeyHash, uint32_t, uint32_t NewLSN, const OplogEntryAddress& NewAddress) { LatestOpMap.insert_or_assign(OpKeyHash, NewLSN); OpAddressMap.insert_or_assign(NewLSN, NewAddress); }, RetainLSNs, /*DryRun*/ DryRun); if (!DryRun) { m_OpAddressMap.swap(OpAddressMap); m_LatestOpMap.swap(LatestOpMap); WriteIndexSnapshot(); } uint64_t PostSize = TotalSize(); uint64_t FreedSize = (PreSize > PostSize) ? (PreSize - PostSize) : 0; ZEN_INFO("{} oplog '{}/{}': Compacted in {}. New size: {}, freeing: {}", LogPrefix, m_OuterProject->Identifier, m_OplogId, NiceTimeSpanMs(Timer.GetElapsedTimeMs()), NiceBytes(PostSize), NiceBytes(FreedSize)); } IoBuffer ProjectStore::Oplog::GetChunkByRawHash(const IoHash& RawHash) { return m_CidStore.FindChunkByCid(RawHash); } bool ProjectStore::Oplog::IterateChunks(std::span RawHashes, bool IncludeModTag, const std::function& AsyncCallback, WorkerThreadPool* OptionalWorkerPool, uint64_t LargeSizeLimit) { return m_CidStore.IterateChunks( RawHashes, [&](size_t Index, const IoBuffer& Payload) { return AsyncCallback(Index, Payload, IncludeModTag ? GetModificationTagFromRawHash(RawHashes[Index]) : 0); }, OptionalWorkerPool, LargeSizeLimit); } bool ProjectStore::Oplog::IterateChunks(std::span ChunkIds, bool IncludeModTag, const std::function& AsyncCallback, WorkerThreadPool* OptionalWorkerPool, uint64_t LargeSizeLimit) { ZEN_MEMSCOPE(GetProjectstoreTag()); std::vector CidChunkIndexes; std::vector CidChunkHashes; std::vector FileChunkIndexes; std::vector FileChunkPaths; { RwLock::SharedLockScope OplogLock(m_OplogLock); for (size_t ChunkIndex = 0; ChunkIndex < ChunkIds.size(); ChunkIndex++) { const Oid& ChunkId = ChunkIds[ChunkIndex]; if (auto ChunkIt = m_ChunkMap.find(ChunkId); ChunkIt != m_ChunkMap.end()) { CidChunkIndexes.push_back(ChunkIndex); CidChunkHashes.push_back(ChunkIt->second); } else if (auto MetaIt = m_MetaMap.find(ChunkId); MetaIt != m_MetaMap.end()) { CidChunkIndexes.push_back(ChunkIndex); CidChunkHashes.push_back(ChunkIt->second); } else if (auto FileIt = m_FileMap.find(ChunkId); FileIt != m_FileMap.end()) { FileChunkIndexes.push_back(ChunkIndex); FileChunkPaths.emplace_back(m_OuterProject->RootDir / FileIt->second.ServerPath); } } } if (OptionalWorkerPool) { std::atomic AbortFlag; std::atomic PauseFlag; ParallelWork Work(AbortFlag, PauseFlag); try { for (size_t ChunkIndex = 0; ChunkIndex < FileChunkIndexes.size(); ChunkIndex++) { if (AbortFlag) { break; } Work.ScheduleWork( *OptionalWorkerPool, [this, &ChunkIds, IncludeModTag, ChunkIndex, &FileChunkIndexes, &FileChunkPaths, &AsyncCallback]( std::atomic& AbortFlag) { if (AbortFlag) { return; } size_t FileChunkIndex = FileChunkIndexes[ChunkIndex]; const std::filesystem::path& FilePath = FileChunkPaths[ChunkIndex]; try { IoBuffer Payload = IoBufferBuilder::MakeFromFile(FilePath); if (!Payload) { ZEN_WARN("Trying to fetch chunk {} using file path {} failed", ChunkIds[FileChunkIndex], FilePath); } if (!AsyncCallback(FileChunkIndex, Payload, IncludeModTag ? GetModificationTagFromModificationTime(Payload) : 0)) { AbortFlag.store(true); } } catch (const std::exception& Ex) { ZEN_WARN("oplog '{}/{}': exception caught when iterating file chunk {}, path '{}'. Reason: '{}'", m_OuterProject->Identifier, m_OplogId, FileChunkIndex, FilePath, Ex.what()); } }); } if (!CidChunkHashes.empty() && !AbortFlag) { m_CidStore.IterateChunks( CidChunkHashes, [&](size_t Index, const IoBuffer& Payload) { size_t CidChunkIndex = CidChunkIndexes[Index]; if (AbortFlag) { return false; } return AsyncCallback(CidChunkIndex, Payload, IncludeModTag ? GetModificationTagFromRawHash(CidChunkHashes[Index]) : 0); }, OptionalWorkerPool, LargeSizeLimit); } } catch (const std::exception& Ex) { AbortFlag.store(true); ZEN_WARN("Failed iterating oplog chunks in {}. Reason: '{}'", m_BasePath, Ex.what()); } Work.Wait(); return !AbortFlag; } else { if (!CidChunkHashes.empty()) { m_CidStore.IterateChunks( CidChunkHashes, [&](size_t Index, const IoBuffer& Payload) { size_t CidChunkIndex = CidChunkIndexes[Index]; return AsyncCallback(CidChunkIndex, Payload, IncludeModTag ? GetModificationTagFromRawHash(CidChunkHashes[Index]) : 0); }, OptionalWorkerPool, LargeSizeLimit); } for (size_t ChunkIndex = 0; ChunkIndex < FileChunkIndexes.size(); ChunkIndex++) { size_t FileChunkIndex = FileChunkIndexes[ChunkIndex]; const std::filesystem::path& FilePath = FileChunkPaths[ChunkIndex]; IoBuffer Payload = IoBufferBuilder::MakeFromFile(FilePath); if (Payload) { bool Result = AsyncCallback(FileChunkIndex, Payload, IncludeModTag ? GetModificationTagFromModificationTime(Payload) : 0); if (!Result) { return false; } } } } return true; } IoBuffer ProjectStore::Oplog::FindChunk(const Oid& ChunkId, uint64_t* OptOutModificationTag) { RwLock::SharedLockScope OplogLock(m_OplogLock); if (!m_Storage) { return IoBuffer{}; } if (auto ChunkIt = m_ChunkMap.find(ChunkId); ChunkIt != m_ChunkMap.end()) { IoHash ChunkHash = ChunkIt->second; OplogLock.ReleaseNow(); IoBuffer Result = m_CidStore.FindChunkByCid(ChunkHash); if (Result && OptOutModificationTag != nullptr) { *OptOutModificationTag = GetModificationTagFromRawHash(ChunkHash); } return Result; } if (auto FileIt = m_FileMap.find(ChunkId); FileIt != m_FileMap.end()) { std::filesystem::path FilePath = m_OuterProject->RootDir / FileIt->second.ServerPath; OplogLock.ReleaseNow(); IoBuffer Result = IoBufferBuilder::MakeFromFile(FilePath); if (!Result) { ZEN_WARN("Trying to fetch chunk {} using file path {} failed", ChunkId, FilePath); } else if (OptOutModificationTag != nullptr) { *OptOutModificationTag = GetModificationTagFromModificationTime(Result); } return Result; } if (auto MetaIt = m_MetaMap.find(ChunkId); MetaIt != m_MetaMap.end()) { IoHash ChunkHash = MetaIt->second; OplogLock.ReleaseNow(); IoBuffer Result = m_CidStore.FindChunkByCid(ChunkHash); if (Result && OptOutModificationTag != nullptr) { *OptOutModificationTag = GetModificationTagFromRawHash(ChunkHash); } return Result; } return {}; } std::vector ProjectStore::Oplog::GetAllChunksInfo() { ZEN_MEMSCOPE(GetProjectstoreTag()); // First just capture all the chunk ids std::vector InfoArray; { RwLock::SharedLockScope _(m_OplogLock); if (m_Storage) { const size_t NumEntries = m_FileMap.size() + m_ChunkMap.size(); InfoArray.reserve(NumEntries); for (const auto& Kv : m_FileMap) { InfoArray.push_back({.ChunkId = Kv.first}); } for (const auto& Kv : m_ChunkMap) { InfoArray.push_back({.ChunkId = Kv.first}); } } } for (ChunkInfo& Info : InfoArray) { if (IoBuffer Chunk = FindChunk(Info.ChunkId, nullptr)) { Info.ChunkSize = Chunk.GetSize(); } } return InfoArray; } void ProjectStore::Oplog::IterateChunkMap(std::function&& Fn) { RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return; } for (const auto& Kv : m_ChunkMap) { Fn(Kv.first, Kv.second); } } void ProjectStore::Oplog::IterateFileMap( std::function&& Fn) { RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return; } for (const auto& Kv : m_FileMap) { Fn(Kv.first, Kv.second.ServerPath, Kv.second.ClientPath); } } void ProjectStore::Oplog::IterateOplog(std::function&& Handler, const Paging& EntryPaging) { RwLock::SharedLockScope _(m_OplogLock); IterateOplogLocked(std::move(Handler), EntryPaging); } template std::span CreateSpanFromPaging(std::vector& Container, const ProjectStore::Oplog::Paging& Paging) { std::span Span(Container); int32_t Size = int32_t(Container.size()); int32_t Start = std::clamp(Paging.Start, 0, Size); int32_t End = Paging.Count < 0 ? Size : (Start + std::min(Paging.Count, Size - Start)); return Span.subspan(Start, End - Start); } void ProjectStore::Oplog::IterateOplogLocked(std::function&& Handler, const Paging& EntryPaging) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Oplog::IterateOplogLocked"); if (!m_Storage) { return; } std::vector Entries; Entries.reserve(m_LatestOpMap.size()); for (const auto& Kv : m_LatestOpMap) { const auto AddressEntry = m_OpAddressMap.find(Kv.second); ZEN_ASSERT(AddressEntry != m_OpAddressMap.end()); Entries.push_back(AddressEntry->second); } std::sort(Entries.begin(), Entries.end(), [](const OplogEntryAddress& Lhs, const OplogEntryAddress& Rhs) { return Lhs.Offset < Rhs.Offset; }); std::span EntrySpan = CreateSpanFromPaging(Entries, EntryPaging); m_Storage->ReplayLogEntries(EntrySpan, [&](CbObjectView Op) { Handler(Op); }); } static constexpr uint32_t OplogMetaDataExpectedMagic = 0x6f'74'6d'62; // 'omta'; void ProjectStore::Oplog::GetAttachmentsLocked(std::vector& OutAttachments, bool StoreMetaDataOnDisk) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Oplog::GetAttachmentsLocked"); if (!m_Storage) { return; } if (StoreMetaDataOnDisk && m_MetaValid) { IoBuffer MetadataPayload = IoBufferBuilder::MakeFromFile(m_MetaPath); if (MetadataPayload) { ZEN_TRACE_CPU("Store::Oplog::GetAttachmentsFromMetaData"); if (GetAttachmentsFromMetaData( MetadataPayload, OplogMetaDataExpectedMagic, [&](std::span Keys, std::span AttachmentCounts, std::span Attachments) { ZEN_UNUSED(Keys); ZEN_UNUSED(AttachmentCounts); OutAttachments.insert(OutAttachments.end(), Attachments.begin(), Attachments.end()); })) { return; } } } std::vector Keys; std::vector AttachmentCounts; size_t AttachmentOffset = OutAttachments.size(); IterateOplogLocked( [&](CbObjectView Op) { using namespace std::literals; size_t CurrentAttachmentCount = OutAttachments.size(); Op.IterateAttachments([&](CbFieldView Visitor) { OutAttachments.emplace_back(Visitor.AsAttachment()); }); if (StoreMetaDataOnDisk) { const Oid KeyHash = ComputeOpKey(Op); Keys.push_back(KeyHash); AttachmentCounts.push_back(gsl::narrow(OutAttachments.size() - CurrentAttachmentCount)); } }, Paging{}); if (StoreMetaDataOnDisk) { const IoHash* FirstAttachment = OutAttachments.data() + AttachmentOffset; size_t AttachmentCount = OutAttachments.size() - AttachmentOffset; IoBuffer MetaPayload = BuildReferenceMetaData(OplogMetaDataExpectedMagic, Keys, AttachmentCounts, std::span(FirstAttachment, AttachmentCount)) .Flatten() .AsIoBuffer(); const std::filesystem::path MetaPath = m_MetaPath; std::error_code Ec; TemporaryFile::SafeWriteFile(MetaPath, MetaPayload.GetView(), Ec); if (Ec) { m_MetaValid = false; ZEN_WARN("oplog '{}/{}': unable to set meta data meta path: '{}'. Reason: '{}'", m_OuterProject->Identifier, m_OplogId, MetaPath, Ec.message()); } else { m_MetaValid = true; } } } size_t ProjectStore::Oplog::GetOplogEntryCount() const { RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return 0; } return m_LatestOpMap.size(); } void ProjectStore::Oplog::IterateOplogWithKey(std::function&& Handler) { IterateOplogWithKey(std::move(Handler), Paging{}); } void ProjectStore::Oplog::IterateOplogWithKey(std::function&& Handler, const Paging& EntryPaging) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return; } std::vector SortedEntries; std::vector SortedKeys; std::vector SortedLSNs; { const auto TargetEntryCount = m_LatestOpMap.size(); std::vector EntryIndexes; std::vector Entries; std::vector Keys; std::vector LSNs; Entries.reserve(TargetEntryCount); EntryIndexes.reserve(TargetEntryCount); Keys.reserve(TargetEntryCount); LSNs.reserve(TargetEntryCount); for (const auto& Kv : m_LatestOpMap) { const auto AddressEntry = m_OpAddressMap.find(Kv.second); ZEN_ASSERT(AddressEntry != m_OpAddressMap.end()); Entries.push_back(AddressEntry->second); Keys.push_back(Kv.first); LSNs.push_back(Kv.second); EntryIndexes.push_back(EntryIndexes.size()); } std::sort(EntryIndexes.begin(), EntryIndexes.end(), [&Entries](const size_t& Lhs, const size_t& Rhs) { const OplogEntryAddress& LhsEntry = Entries[Lhs]; const OplogEntryAddress& RhsEntry = Entries[Rhs]; return LhsEntry.Offset < RhsEntry.Offset; }); SortedEntries.reserve(EntryIndexes.size()); SortedKeys.reserve(EntryIndexes.size()); SortedLSNs.reserve(EntryIndexes.size()); for (size_t Index : EntryIndexes) { SortedEntries.push_back(Entries[Index]); SortedKeys.push_back(Keys[Index]); SortedLSNs.push_back(LSNs[Index]); } } std::span EntrySpan = CreateSpanFromPaging(SortedEntries, EntryPaging); size_t EntryIndex = EntrySpan.empty() ? 0 : static_cast(&EntrySpan.front() - &SortedEntries.front()); m_Storage->ReplayLogEntries(EntrySpan, [&](CbObjectView Op) { Handler(SortedLSNs[EntryIndex], SortedKeys[EntryIndex], Op); EntryIndex++; }); } std::optional ProjectStore::Oplog::GetOpIndexByKey(const Oid& Key) { RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return {}; } if (const auto LatestOp = m_LatestOpMap.find(Key); LatestOp != m_LatestOpMap.end()) { return LatestOp->second; } return {}; } std::optional ProjectStore::Oplog::GetOpByKey(const Oid& Key) { RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return {}; } if (const auto LatestOp = m_LatestOpMap.find(Key); LatestOp != m_LatestOpMap.end()) { const auto AddressEntry = m_OpAddressMap.find(LatestOp->second); ZEN_ASSERT(AddressEntry != m_OpAddressMap.end()); return m_Storage->GetOp(AddressEntry->second); } return {}; } std::optional ProjectStore::Oplog::GetOpByIndex(uint32_t Index) { RwLock::SharedLockScope _(m_OplogLock); if (!m_Storage) { return {}; } if (const auto AddressEntryIt = m_OpAddressMap.find(Index); AddressEntryIt != m_OpAddressMap.end()) { return m_Storage->GetOp(AddressEntryIt->second); } return {}; } void ProjectStore::Oplog::AddChunkMappings(const std::unordered_map& ChunkMappings) { RwLock::ExclusiveLockScope OplogLock(m_OplogLock); for (const auto& It : ChunkMappings) { AddChunkMapping(OplogLock, It.first, It.second); } } void ProjectStore::Oplog::CaptureAddedAttachments(std::span AttachmentHashes) { ZEN_MEMSCOPE(GetProjectstoreTag()); m_OplogLock.WithExclusiveLock([this, AttachmentHashes]() { if (m_CapturedAttachments) { m_CapturedAttachments->reserve(m_CapturedAttachments->size() + AttachmentHashes.size()); m_CapturedAttachments->insert(m_CapturedAttachments->end(), AttachmentHashes.begin(), AttachmentHashes.end()); } }); } void ProjectStore::Oplog::EnableUpdateCapture() { ZEN_MEMSCOPE(GetProjectstoreTag()); m_OplogLock.WithExclusiveLock([&]() { if (m_UpdateCaptureRefCounter == 0) { ZEN_ASSERT(!m_CapturedLSNs); ZEN_ASSERT(!m_CapturedAttachments); m_CapturedLSNs = std::make_unique>(); m_CapturedAttachments = std::make_unique>(); } else { ZEN_ASSERT(m_CapturedLSNs); ZEN_ASSERT(m_CapturedAttachments); } m_UpdateCaptureRefCounter++; }); } void ProjectStore::Oplog::DisableUpdateCapture() { ZEN_MEMSCOPE(GetProjectstoreTag()); m_OplogLock.WithExclusiveLock([&]() { ZEN_ASSERT(m_CapturedLSNs); ZEN_ASSERT(m_CapturedAttachments); ZEN_ASSERT(m_UpdateCaptureRefCounter > 0); m_UpdateCaptureRefCounter--; if (m_UpdateCaptureRefCounter == 0) { m_CapturedLSNs.reset(); m_CapturedAttachments.reset(); } }); } void ProjectStore::Oplog::IterateCapturedLSNsLocked(std::function&& Callback) { ZEN_MEMSCOPE(GetProjectstoreTag()); if (m_CapturedLSNs) { if (!m_Storage) { return; } for (uint32_t UpdatedLSN : *m_CapturedLSNs) { if (const auto AddressEntryIt = m_OpAddressMap.find(UpdatedLSN); AddressEntryIt != m_OpAddressMap.end()) { Callback(m_Storage->GetOp(AddressEntryIt->second)); } } } } std::vector ProjectStore::Oplog::GetCapturedAttachmentsLocked() { ZEN_MEMSCOPE(GetProjectstoreTag()); if (m_CapturedAttachments) { return *m_CapturedAttachments; } return {}; } std::vector ProjectStore::Oplog::CheckPendingChunkReferences(std::span ChunkHashes, const GcClock::Duration& RetainTime) { ZEN_MEMSCOPE(GetProjectstoreTag()); m_OplogLock.WithExclusiveLock([&]() { GcClock::TimePoint Now = GcClock::Now(); if (m_PendingPrepOpAttachmentsRetainEnd < Now) { m_PendingPrepOpAttachments.clear(); } m_PendingPrepOpAttachments.insert(ChunkHashes.begin(), ChunkHashes.end()); GcClock::TimePoint NewEndPoint = Now + RetainTime; if (m_PendingPrepOpAttachmentsRetainEnd < NewEndPoint) { m_PendingPrepOpAttachmentsRetainEnd = NewEndPoint; } }); std::vector MissingChunks; MissingChunks.reserve(ChunkHashes.size()); for (const IoHash& FileHash : ChunkHashes) { if (!m_CidStore.ContainsChunk(FileHash)) { MissingChunks.push_back(FileHash); } } return MissingChunks; } void ProjectStore::Oplog::RemovePendingChunkReferences(std::span ChunkHashes) { ZEN_MEMSCOPE(GetProjectstoreTag()); m_OplogLock.WithExclusiveLock([&]() { GcClock::TimePoint Now = GcClock::Now(); if (m_PendingPrepOpAttachmentsRetainEnd < Now) { m_PendingPrepOpAttachments.clear(); } else { for (const IoHash& Chunk : ChunkHashes) { m_PendingPrepOpAttachments.erase(Chunk); } } }); } std::vector ProjectStore::Oplog::GetPendingChunkReferencesLocked() { ZEN_MEMSCOPE(GetProjectstoreTag()); std::vector Result; Result.reserve(m_PendingPrepOpAttachments.size()); Result.insert(Result.end(), m_PendingPrepOpAttachments.begin(), m_PendingPrepOpAttachments.end()); GcClock::TimePoint Now = GcClock::Now(); if (m_PendingPrepOpAttachmentsRetainEnd < Now) { m_PendingPrepOpAttachments.clear(); } return Result; } void ProjectStore::Oplog::AddFileMapping(const RwLock::ExclusiveLockScope&, const Oid& FileId, const IoHash& Hash, std::string_view ServerPath, std::string_view ClientPath) { FileMapEntry Entry; if (Hash != IoHash::Zero) { m_ChunkMap.insert_or_assign(FileId, Hash); } else { Entry.ServerPath = ServerPath; } Entry.ClientPath = ClientPath; m_FileMap[FileId] = std::move(Entry); } void ProjectStore::Oplog::AddChunkMapping(const RwLock::ExclusiveLockScope&, const Oid& ChunkId, const IoHash& Hash) { m_ChunkMap.insert_or_assign(ChunkId, Hash); } void ProjectStore::Oplog::AddMetaMapping(const RwLock::ExclusiveLockScope&, const Oid& ChunkId, const IoHash& Hash) { m_MetaMap.insert_or_assign(ChunkId, Hash); } ProjectStore::Oplog::OplogEntryMapping ProjectStore::Oplog::GetMapping(CbObjectView Core) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; OplogEntryMapping Result; // Update chunk id maps for (CbFieldView Field : Core) { std::string_view FieldName = Field.GetName(); if (FieldName == "package"sv) { CbObjectView PackageObj = Field.AsObjectView(); Oid Id = PackageObj["id"sv].AsObjectId(); IoHash Hash = PackageObj["data"sv].AsBinaryAttachment(); Result.Chunks.emplace_back(ChunkMapping{Id, Hash}); ZEN_DEBUG("oplog {}/{}: package data {} -> {}", m_OuterProject->Identifier, m_OplogId, Id, Hash); continue; } if (FieldName == "bulkdata"sv) { CbArrayView BulkDataArray = Field.AsArrayView(); for (CbFieldView& Entry : BulkDataArray) { CbObjectView BulkObj = Entry.AsObjectView(); Oid Id = BulkObj["id"sv].AsObjectId(); IoHash Hash = BulkObj["data"sv].AsBinaryAttachment(); Result.Chunks.emplace_back(ChunkMapping{Id, Hash}); ZEN_DEBUG("oplog {}/{}: bulkdata {} -> {}", m_OuterProject->Identifier, m_OplogId, Id, Hash); } continue; } if (FieldName == "packagedata"sv) { CbArrayView PackageDataArray = Field.AsArrayView(); for (CbFieldView& Entry : PackageDataArray) { CbObjectView PackageDataObj = Entry.AsObjectView(); Oid Id = PackageDataObj["id"sv].AsObjectId(); IoHash Hash = PackageDataObj["data"sv].AsBinaryAttachment(); Result.Chunks.emplace_back(ChunkMapping{Id, Hash}); ZEN_DEBUG("oplog {}/{}: package {} -> {}", m_OuterProject->Identifier, m_OplogId, Id, Hash); } continue; } if (FieldName == "files"sv) { CbArrayView FilesArray = Field.AsArrayView(); Result.Files.reserve(FilesArray.Num()); for (CbFieldView& Entry : FilesArray) { CbObjectView FileObj = Entry.AsObjectView(); std::string_view ServerPath = FileObj["serverpath"sv].AsString(); std::string_view ClientPath = FileObj["clientpath"sv].AsString(); Oid Id = FileObj["id"sv].AsObjectId(); IoHash Hash = FileObj["data"sv].AsBinaryAttachment(); if (ServerPath.empty() && Hash == IoHash::Zero) { ZEN_WARN("oplog {}/{}: invalid file for entry '{}', missing both 'serverpath' and 'data' fields", m_OuterProject->Identifier, m_OplogId, Id); continue; } if (ClientPath.empty()) { ZEN_WARN("oplog {}/{}: invalid file for entry '{}', missing 'clientpath' field", m_OuterProject->Identifier, m_OplogId, Id); continue; } Result.Files.emplace_back(FileMapping{Id, Hash, std::string(ServerPath), std::string(ClientPath)}); ZEN_DEBUG("oplog {}/{}: file {} -> {}, ServerPath: {}, ClientPath: {}", m_OuterProject->Identifier, m_OplogId, Id, Hash, ServerPath, ClientPath); } continue; } if (FieldName == "meta"sv) { CbArrayView MetaArray = Field.AsArrayView(); Result.Meta.reserve(MetaArray.Num()); for (CbFieldView& Entry : MetaArray) { CbObjectView MetaObj = Entry.AsObjectView(); Oid Id = MetaObj["id"sv].AsObjectId(); IoHash Hash = MetaObj["data"sv].AsBinaryAttachment(); Result.Meta.emplace_back(ChunkMapping{Id, Hash}); auto NameString = MetaObj["name"sv].AsString(); ZEN_DEBUG("oplog {}/{}: meta data ({}) {} -> {}", m_OuterProject->Identifier, m_OplogId, NameString, Id, Hash); } continue; } } return Result; } uint32_t ProjectStore::Oplog::RegisterOplogEntry(RwLock::ExclusiveLockScope& OplogLock, const OplogEntryMapping& OpMapping, const OplogEntry& OpEntry) { ZEN_MEMSCOPE(GetProjectstoreTag()); // For now we're assuming the update is all in-memory so we can hold an exclusive lock without causing // too many problems. Longer term we'll probably want to ensure we can do concurrent updates however using namespace std::literals; // Update chunk id maps for (const ChunkMapping& Chunk : OpMapping.Chunks) { AddChunkMapping(OplogLock, Chunk.Id, Chunk.Hash); } for (const FileMapping& File : OpMapping.Files) { AddFileMapping(OplogLock, File.Id, File.Hash, File.ServerPath, File.ClientPath); } for (const ChunkMapping& Meta : OpMapping.Meta) { AddMetaMapping(OplogLock, Meta.Id, Meta.Hash); } m_OpAddressMap.emplace(OpEntry.OpLsn, OplogEntryAddress{.Offset = OpEntry.OpCoreOffset, .Size = OpEntry.OpCoreSize}); m_LatestOpMap[OpEntry.OpKeyHash] = OpEntry.OpLsn; return OpEntry.OpLsn; } uint32_t ProjectStore::Oplog::AppendNewOplogEntry(CbPackage OpPackage) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Oplog::AppendNewOplogEntry"); const CbObject& Core = OpPackage.GetObject(); const uint32_t EntryId = AppendNewOplogEntry(Core); if (EntryId == 0xffffffffu) { // The oplog has been deleted so just drop this return EntryId; } // Persist attachments after oplog entry so GC won't find attachments without references uint64_t AttachmentBytes = 0; uint64_t NewAttachmentBytes = 0; auto Attachments = OpPackage.GetAttachments(); if (!Attachments.empty()) { std::vector WriteAttachmentBuffers; std::vector WriteRawHashes; std::vector WriteRawSizes; WriteAttachmentBuffers.reserve(Attachments.size()); WriteRawHashes.reserve(Attachments.size()); WriteRawSizes.reserve(Attachments.size()); for (const auto& Attach : Attachments) { ZEN_ASSERT(Attach.IsCompressedBinary()); const CompressedBuffer& AttachmentData = Attach.AsCompressedBinary(); const uint64_t AttachmentSize = AttachmentData.DecodeRawSize(); WriteAttachmentBuffers.push_back(AttachmentData.GetCompressed().Flatten().AsIoBuffer()); WriteRawHashes.push_back(Attach.GetHash()); WriteRawSizes.push_back(AttachmentSize); AttachmentBytes += AttachmentSize; } std::vector InsertResults = m_CidStore.AddChunks(WriteAttachmentBuffers, WriteRawHashes); for (size_t Index = 0; Index < InsertResults.size(); Index++) { if (InsertResults[Index].New) { NewAttachmentBytes += WriteRawSizes[Index]; } } } ZEN_DEBUG("oplog entry #{} attachments: {} new, {} total", EntryId, NiceBytes(NewAttachmentBytes), NiceBytes(AttachmentBytes)); return EntryId; } RefPtr ProjectStore::Oplog::GetStorage() { ZEN_MEMSCOPE(GetProjectstoreTag()); RefPtr Storage; { RwLock::SharedLockScope _(m_OplogLock); Storage = m_Storage; } return Storage; } uint32_t ProjectStore::Oplog::AppendNewOplogEntry(CbObjectView Core) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Oplog::AppendNewOplogEntry"); using namespace std::literals; RefPtr Storage = GetStorage(); if (!Storage) { return 0xffffffffu; } OplogEntryMapping Mapping = GetMapping(Core); OplogStorage::AppendOpData OpData = OplogStorage::GetAppendOpData(Core); const OplogEntry OpEntry = Storage->AppendOp(OpData); RwLock::ExclusiveLockScope OplogLock(m_OplogLock); const uint32_t EntryId = RegisterOplogEntry(OplogLock, Mapping, OpEntry); if (m_CapturedLSNs) { m_CapturedLSNs->push_back(EntryId); } m_MetaValid = false; return EntryId; } std::vector ProjectStore::Oplog::AppendNewOplogEntries(std::span Cores) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Oplog::AppendNewOplogEntries"); using namespace std::literals; RefPtr Storage = GetStorage(); if (!Storage) { return std::vector(Cores.size(), 0xffffffffu); } size_t OpCount = Cores.size(); std::vector Mappings; std::vector OpDatas; Mappings.resize(OpCount); OpDatas.resize(OpCount); for (size_t OpIndex = 0; OpIndex < OpCount; OpIndex++) { const CbObjectView& Core = Cores[OpIndex]; OpDatas[OpIndex] = OplogStorage::GetAppendOpData(Core); Mappings[OpIndex] = GetMapping(Core); } std::vector OpEntries = Storage->AppendOps(OpDatas); std::vector EntryIds; EntryIds.resize(OpCount); { { RwLock::ExclusiveLockScope OplogLock(m_OplogLock); if (m_CapturedLSNs) { m_CapturedLSNs->reserve(m_CapturedLSNs->size() + OpCount); } for (size_t OpIndex = 0; OpIndex < OpCount; OpIndex++) { EntryIds[OpIndex] = RegisterOplogEntry(OplogLock, Mappings[OpIndex], OpEntries[OpIndex]); if (m_CapturedLSNs) { m_CapturedLSNs->push_back(EntryIds[OpIndex]); } } } m_MetaValid = false; } return EntryIds; } ////////////////////////////////////////////////////////////////////////// ProjectStore::Project::Project(ProjectStore* PrjStore, CidStore& Store, std::filesystem::path BasePath) : m_ProjectStore(PrjStore) , m_CidStore(Store) , m_OplogStoragePath(BasePath) , m_LastAccessTimes({std::make_pair(std::string(), GcClock::TickCount())}) { } ProjectStore::Project::~Project() { // Only write access times if we have not been explicitly deleted if (!m_OplogStoragePath.empty()) { WriteAccessTimes(); } } bool ProjectStore::Project::Exists(const std::filesystem::path& BasePath) { return IsFile(BasePath / "Project.zcb"); } void ProjectStore::Project::Read() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Project::Read"); using namespace std::literals; std::filesystem::path ProjectStateFilePath = m_OplogStoragePath / "Project.zcb"sv; ZEN_DEBUG("project '{}': reading config from '{}'", Identifier, ProjectStateFilePath); BasicFile Blob; Blob.Open(ProjectStateFilePath, BasicFile::Mode::kRead); IoBuffer Obj = Blob.ReadAll(); CbValidateError ValidationError = ValidateCompactBinary(MemoryView(Obj.Data(), Obj.Size()), CbValidateMode::All); if (ValidationError == CbValidateError::None) { CbObject Cfg = LoadCompactBinaryObject(Obj); Identifier = std::filesystem::path(Cfg["id"sv].AsU8String()).string(); RootDir = std::filesystem::path(Cfg["root"sv].AsU8String()).string(); ProjectRootDir = std::filesystem::path(Cfg["project"sv].AsU8String()).string(); EngineRootDir = std::filesystem::path(Cfg["engine"sv].AsU8String()).string(); ProjectFilePath = std::filesystem::path(Cfg["projectfile"sv].AsU8String()).string(); } else { ZEN_ERROR("validation error {} hit for '{}'", ToString(ValidationError), ProjectStateFilePath); } ReadAccessTimes(); } void ProjectStore::Project::Write() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Project::Write"); using namespace std::literals; BinaryWriter Mem; CbObjectWriter Cfg; Cfg << "id"sv << Identifier; Cfg << "root"sv << PathToUtf8(RootDir); Cfg << "project"sv << PathToUtf8(ProjectRootDir); Cfg << "engine"sv << PathToUtf8(EngineRootDir); Cfg << "projectfile"sv << PathToUtf8(ProjectFilePath); Cfg.Save(Mem); CreateDirectories(m_OplogStoragePath); std::filesystem::path ProjectStateFilePath = m_OplogStoragePath / "Project.zcb"sv; ZEN_INFO("project '{}': persisting config to '{}'", Identifier, ProjectStateFilePath); TemporaryFile::SafeWriteFile(ProjectStateFilePath, Mem.GetView()); } void ProjectStore::Project::ReadAccessTimes() { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; std::filesystem::path ProjectAccessTimesFilePath = m_OplogStoragePath / "AccessTimes.zcb"sv; if (!IsFile(ProjectAccessTimesFilePath)) { return; } ZEN_DEBUG("project '{}': reading access times '{}'", Identifier, ProjectAccessTimesFilePath); BasicFile Blob; Blob.Open(ProjectAccessTimesFilePath, BasicFile::Mode::kRead); IoBuffer Obj = Blob.ReadAll(); CbValidateError ValidationError = ValidateCompactBinary(MemoryView(Obj.Data(), Obj.Size()), CbValidateMode::All); if (ValidationError == CbValidateError::None) { CbObject Reader = LoadCompactBinaryObject(Obj); uint64_t Count = Reader["count"sv].AsUInt64(0); if (Count > 0) { std::vector Ticks; Ticks.reserve(Count); CbArrayView TicksArray = Reader["ticks"sv].AsArrayView(); for (CbFieldView& TickView : TicksArray) { Ticks.emplace_back(TickView.AsUInt64()); } CbArrayView IdArray = Reader["ids"sv].AsArrayView(); uint64_t Index = 0; for (CbFieldView& IdView : IdArray) { std::string_view Id = IdView.AsString(); m_LastAccessTimes.insert_or_assign(std::string(Id), Ticks[Index++]); } } ////// Legacy format read { CbArrayView LastAccessTimes = Reader["lastaccess"sv].AsArrayView(); for (CbFieldView& Entry : LastAccessTimes) { CbObjectView AccessTime = Entry.AsObjectView(); std::string_view Id = AccessTime["id"sv].AsString(); GcClock::Tick AccessTick = AccessTime["tick"sv].AsUInt64(); m_LastAccessTimes.insert_or_assign(std::string(Id), AccessTick); } } } else { ZEN_WARN("project '{}': validation error {} hit for '{}'", Identifier, ToString(ValidationError), ProjectAccessTimesFilePath); } } void ProjectStore::Project::WriteAccessTimes() { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; CbObjectWriter Writer(32 + (m_LastAccessTimes.size() * 16)); { RwLock::SharedLockScope _(m_LastAccessTimesLock); Writer.AddInteger("count", gsl::narrow(m_LastAccessTimes.size())); Writer.BeginArray("ids"); for (const auto& It : m_LastAccessTimes) { Writer << It.first; } Writer.EndArray(); Writer.BeginArray("ticks"); for (const auto& It : m_LastAccessTimes) { Writer << gsl::narrow(It.second); } Writer.EndArray(); } CbObject Data = Writer.Save(); try { CreateDirectories(m_OplogStoragePath); std::filesystem::path ProjectAccessTimesFilePath = m_OplogStoragePath / "AccessTimes.zcb"sv; ZEN_DEBUG("project '{}': persisting access times for '{}'", Identifier, ProjectAccessTimesFilePath); TemporaryFile::SafeWriteFile(ProjectAccessTimesFilePath, Data.GetView()); } catch (const std::exception& Err) { ZEN_WARN("project '{}': writing access times FAILED, reason: '{}'", Identifier, Err.what()); } } LoggerRef ProjectStore::Project::Log() const { return m_ProjectStore->Log(); } std::filesystem::path ProjectStore::Project::BasePathForOplog(std::string_view OplogId) const { return m_OplogStoragePath / OplogId; } ProjectStore::Oplog* ProjectStore::Project::NewOplog(std::string_view OplogId, const std::filesystem::path& MarkerPath) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::ExclusiveLockScope _(m_ProjectLock); std::filesystem::path OplogBasePath = BasePathForOplog(OplogId); try { ZEN_INFO("oplog '{}/{}': creating oplog at '{}'", Identifier, OplogId, OplogBasePath); Oplog* Log = m_Oplogs .try_emplace(std::string{OplogId}, std::make_unique(OplogId, this, m_CidStore, OplogBasePath, MarkerPath)) .first->second.get(); Log->Write(); if (m_CapturedOplogs) { m_CapturedOplogs->push_back(std::string(OplogId)); } return Log; } catch (const std::exception&) { // In case of failure we need to ensure there's no half constructed entry around // // (This is probably already ensured by the try_emplace implementation?) m_Oplogs.erase(std::string{OplogId}); return nullptr; } } ProjectStore::Oplog* ProjectStore::Project::OpenOplog(std::string_view OplogId, bool AllowCompact, bool VerifyPathOnDisk) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OpenOplog"); { RwLock::SharedLockScope ProjectLock(m_ProjectLock); auto OplogIt = m_Oplogs.find(std::string(OplogId)); if (OplogIt != m_Oplogs.end()) { bool ReOpen = false; if (VerifyPathOnDisk) { std::filesystem::path OplogBasePath = BasePathForOplog(OplogId); if (!Oplog::ExistsAt(OplogBasePath)) { // Somebody deleted the oplog on disk behind our back ProjectLock.ReleaseNow(); std::filesystem::path DeletePath; if (!RemoveOplog(OplogId, DeletePath)) { ZEN_WARN("Failed to clean up deleted oplog {}/{}", Identifier, OplogId, OplogBasePath); } ReOpen = true; } } if (!ReOpen) { return OplogIt->second.get(); } } } std::filesystem::path OplogBasePath = BasePathForOplog(OplogId); RwLock::ExclusiveLockScope Lock(m_ProjectLock); if (auto It = m_Oplogs.find(std::string{OplogId}); It != m_Oplogs.end()) { return It->second.get(); } if (Oplog::ExistsAt(OplogBasePath)) { try { ZEN_INFO("oplog '{}/{}': opening oplog at '{}'", Identifier, OplogId, OplogBasePath); Oplog* Log = m_Oplogs .try_emplace(std::string{OplogId}, std::make_unique(OplogId, this, m_CidStore, OplogBasePath, std::filesystem::path{})) .first->second.get(); Log->Read(); Lock.ReleaseNow(); if (AllowCompact) { const uint32_t CompactUnusedThreshold = 50; Log->CompactIfUnusedExceeds(/*DryRun*/ false, CompactUnusedThreshold, fmt::format("Compact on initial open of oplog {}/{}: ", Identifier, OplogId)); } return Log; } catch (const std::exception& Ex) { ZEN_WARN("oplog '{}/{}': failed to open oplog at '{}': {}", Identifier, OplogId, OplogBasePath, Ex.what()); m_Oplogs.erase(std::string{OplogId}); } } return nullptr; } void ProjectStore::Oplog::CompactIfUnusedExceeds(bool DryRun, uint32_t CompactUnusedThreshold, std::string_view LogPrefix) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::ExclusiveLockScope OplogLock(m_OplogLock); if (!m_Storage) { return; } uint32_t UnusedPercent = GetUnusedSpacePercentLocked(); if (UnusedPercent >= CompactUnusedThreshold) { Compact(OplogLock, DryRun, /*RetainLSNs*/ m_Storage->MaxLSN() <= 0xff000000ul, // If we have less than 16 miln entries left of our LSN range, allow renumbering of LSNs LogPrefix); } } bool ProjectStore::Project::RemoveOplog(std::string_view OplogId, std::filesystem::path& OutDeletePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); { RwLock::ExclusiveLockScope _(m_ProjectLock); if (auto OplogIt = m_Oplogs.find(std::string(OplogId)); OplogIt == m_Oplogs.end()) { std::filesystem::path OplogBasePath = BasePathForOplog(OplogId); if (Oplog::ExistsAt(OplogBasePath)) { if (!PrepareDirectoryDelete(OplogBasePath, OutDeletePath)) { return false; } } } else { std::unique_ptr& Oplog = OplogIt->second; if (!Oplog->PrepareForDelete(OutDeletePath)) { return false; } m_DeletedOplogs.emplace_back(std::move(Oplog)); m_Oplogs.erase(OplogIt); } } m_LastAccessTimesLock.WithExclusiveLock([&]() { m_LastAccessTimes.erase(std::string(OplogId)); }); return true; } bool ProjectStore::Project::DeleteOplog(std::string_view OplogId) { ZEN_MEMSCOPE(GetProjectstoreTag()); std::filesystem::path DeletePath; if (!RemoveOplog(OplogId, DeletePath)) { return false; } // Erase content on disk if (!DeletePath.empty()) { if (!OplogStorage::Delete(DeletePath)) { ZEN_WARN("oplog '{}/{}': failed to remove old oplog path '{}'", Identifier, OplogId, DeletePath); return false; } } return true; } std::vector ProjectStore::Project::ScanForOplogs() const { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::SharedLockScope _(m_ProjectLock); std::vector Oplogs; if (Project::Exists(m_OplogStoragePath)) { DirectoryContent DirContent; GetDirectoryContent(m_OplogStoragePath, DirectoryContentFlags::IncludeDirs, DirContent); Oplogs.reserve(DirContent.Directories.size()); for (const std::filesystem::path& DirPath : DirContent.Directories) { if (Oplog::ExistsAt(DirPath)) { Oplogs.push_back(DirPath.filename().string()); } } } return Oplogs; } void ProjectStore::Project::IterateOplogs(std::function&& Fn) const { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::SharedLockScope Lock(m_ProjectLock); for (auto& Kv : m_Oplogs) { Fn(Lock, *Kv.second); } } void ProjectStore::Project::IterateOplogs(std::function&& Fn) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::SharedLockScope Lock(m_ProjectLock); for (auto& Kv : m_Oplogs) { Fn(Lock, *Kv.second); } } void ProjectStore::Project::Flush() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Project::Flush"); // We only need to flush oplogs that we have already loaded IterateOplogs([&](const RwLock::SharedLockScope&, Oplog& Ops) { Ops.Flush(); }); WriteAccessTimes(); } void ProjectStore::Project::Scrub(ScrubContext& Ctx) { ZEN_MEMSCOPE(GetProjectstoreTag()); // Scrubbing needs to check all existing oplogs std::vector OpLogs = ScanForOplogs(); for (const std::string& OpLogId : OpLogs) { OpenOplog(OpLogId, /*AllowCompact*/ false, /*VerifyPathOnDisk*/ true); } IterateOplogs([&](const RwLock::SharedLockScope&, Oplog& Ops) { if (!IsExpired(GcClock::TimePoint::min(), Ops)) { Ops.Scrub(Ctx); } }); } uint64_t ProjectStore::Project::TotalSize(const std::filesystem::path& BasePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; uint64_t Size = 0; std::filesystem::path AccessTimesFilePath = BasePath / "AccessTimes.zcb"sv; if (IsFile(AccessTimesFilePath)) { Size += FileSizeFromPath(AccessTimesFilePath); } std::filesystem::path ProjectFilePath = BasePath / "Project.zcb"sv; if (IsFile(ProjectFilePath)) { Size += FileSizeFromPath(ProjectFilePath); } return Size; } uint64_t ProjectStore::Project::TotalSize() const { ZEN_MEMSCOPE(GetProjectstoreTag()); uint64_t Result = TotalSize(m_OplogStoragePath); { std::vector OpLogs = ScanForOplogs(); for (const std::string& OpLogId : OpLogs) { std::filesystem::path OplogBasePath = BasePathForOplog(OpLogId); Result += Oplog::TotalSize(OplogBasePath); } } return Result; } bool ProjectStore::Project::PrepareForDelete(std::filesystem::path& OutDeletePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::ExclusiveLockScope _(m_ProjectLock); for (auto& It : m_Oplogs) { It.second->ResetState(); m_DeletedOplogs.emplace_back(std::move(It.second)); } m_Oplogs.clear(); bool Success = PrepareDirectoryDelete(m_OplogStoragePath, OutDeletePath); if (!Success) { return false; } m_OplogStoragePath.clear(); return true; } void ProjectStore::Project::EnableUpdateCapture() { ZEN_MEMSCOPE(GetProjectstoreTag()); m_ProjectLock.WithExclusiveLock([&]() { if (m_UpdateCaptureRefCounter == 0) { ZEN_ASSERT(!m_CapturedOplogs); m_CapturedOplogs = std::make_unique>(); } else { ZEN_ASSERT(m_CapturedOplogs); } m_UpdateCaptureRefCounter++; }); } void ProjectStore::Project::DisableUpdateCapture() { ZEN_MEMSCOPE(GetProjectstoreTag()); m_ProjectLock.WithExclusiveLock([&]() { ZEN_ASSERT(m_CapturedOplogs); ZEN_ASSERT(m_UpdateCaptureRefCounter > 0); m_UpdateCaptureRefCounter--; if (m_UpdateCaptureRefCounter == 0) { m_CapturedOplogs.reset(); } }); } std::vector ProjectStore::Project::GetCapturedOplogsLocked() { ZEN_MEMSCOPE(GetProjectstoreTag()); if (m_CapturedOplogs) { return *m_CapturedOplogs; } return {}; } std::vector ProjectStore::Project::GetGcReferencerLocks() { ZEN_MEMSCOPE(GetProjectstoreTag()); std::vector Locks; Locks.emplace_back(RwLock::SharedLockScope(m_ProjectLock)); Locks.reserve(1 + m_Oplogs.size()); for (auto& Kv : m_Oplogs) { Locks.emplace_back(Kv.second->GetGcReferencerLock()); } return Locks; } bool ProjectStore::Project::IsExpired(const std::string& EntryName, const std::filesystem::path& MarkerPath, const GcClock::TimePoint ExpireTime) const { ZEN_MEMSCOPE(GetProjectstoreTag()); if (!MarkerPath.empty()) { std::error_code Ec; if (IsFile(MarkerPath, Ec)) { if (Ec) { ZEN_WARN("{} '{}{}{}', Failed to check expiry via marker file '{}', assuming not expired", EntryName.empty() ? "project"sv : "oplog"sv, Identifier, EntryName.empty() ? ""sv : "/"sv, EntryName, MarkerPath.string()); return false; } return false; } } const GcClock::Tick ExpireTicks = ExpireTime.time_since_epoch().count(); RwLock::SharedLockScope _(m_LastAccessTimesLock); if (auto It = m_LastAccessTimes.find(EntryName); It != m_LastAccessTimes.end()) { if (It->second <= ExpireTicks) { return true; } } return false; } bool ProjectStore::Project::IsExpired(const GcClock::TimePoint ExpireTime) const { return IsExpired(std::string(), ProjectFilePath, ExpireTime); } bool ProjectStore::Project::IsExpired(const GcClock::TimePoint ExpireTime, const ProjectStore::Oplog& Oplog) const { return IsExpired(Oplog.OplogId(), Oplog.MarkerPath(), ExpireTime); } bool ProjectStore::Project::IsOplogTouchedSince(const GcClock::TimePoint TouchTime, std::string_view Oplog) const { const GcClock::Tick TouchTicks = TouchTime.time_since_epoch().count(); RwLock::ExclusiveLockScope _(m_LastAccessTimesLock); if (auto It = m_LastAccessTimes.find(std::string(Oplog)); It != m_LastAccessTimes.end()) { if (It->second > TouchTicks) { return true; } } return false; } bool ProjectStore::Project::IsExpired(const GcClock::TimePoint ExpireTime, std::string_view OplogId) const { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; { RwLock::SharedLockScope Lock(m_ProjectLock); auto OplogIt = m_Oplogs.find(std::string(OplogId)); if (OplogIt != m_Oplogs.end()) { Lock.ReleaseNow(); return IsExpired(ExpireTime, *OplogIt->second.get()); } } std::filesystem::path OplogBasePath = BasePathForOplog(OplogId); std::optional OplogConfig = Oplog::ReadStateFile(OplogBasePath, [this]() { return Log(); }); std::filesystem::path MarkerPath = OplogConfig.has_value() ? OplogConfig.value()["gcpath"sv].AsU8String() : std::u8string(); return IsExpired(std::string(OplogId), MarkerPath, ExpireTime); } void ProjectStore::Project::TouchProject() { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::ExclusiveLockScope _(m_LastAccessTimesLock); m_LastAccessTimes.insert_or_assign(std::string(), GcClock::TickCount()); } void ProjectStore::Project::TouchOplog(std::string_view Oplog) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_ASSERT(!Oplog.empty()); RwLock::ExclusiveLockScope _(m_LastAccessTimesLock); m_LastAccessTimes.insert_or_assign(std::string(Oplog), GcClock::TickCount()); } GcClock::TimePoint ProjectStore::Project::LastOplogAccessTime(std::string_view Oplog) const { RwLock::SharedLockScope _(m_LastAccessTimesLock); if (auto It = m_LastAccessTimes.find(std::string(Oplog)); It != m_LastAccessTimes.end()) { return GcClock::TimePointFromTick(It->second); } return GcClock::TimePoint::min(); } ////////////////////////////////////////////////////////////////////////// ProjectStore::ProjectStore(GetCidStoreFunc&& GetCidStore, std::filesystem::path BasePath, GcManager& Gc, JobQueue& JobQueue, OpenProcessCache& InOpenProcessCache, const Configuration& Config) : m_Log(logging::Get("project")) , m_Gc(Gc) , m_GetCidStore(std::move(GetCidStore)) , m_JobQueue(JobQueue) , m_OpenProcessCache(InOpenProcessCache) , m_ProjectBasePath(BasePath) , m_Config(Config) , m_DiskWriteBlocker(Gc.GetDiskWriteBlocker()) { ZEN_INFO("initializing project store at '{}'", m_ProjectBasePath); // m_Log.set_level(spdlog::level::debug); m_Gc.AddGcStorage(this); m_Gc.AddGcReferencer(*this); m_Gc.AddGcReferenceLocker(*this); } ProjectStore::~ProjectStore() { ZEN_INFO("closing project store at '{}'", m_ProjectBasePath); m_Gc.RemoveGcReferenceLocker(*this); m_Gc.RemoveGcReferencer(*this); m_Gc.RemoveGcStorage(this); } std::filesystem::path ProjectStore::BasePathForProject(std::string_view ProjectId) { return m_ProjectBasePath / ProjectId; } void ProjectStore::DiscoverProjects() { ZEN_MEMSCOPE(GetProjectstoreTag()); if (!IsDir(m_ProjectBasePath)) { return; } DirectoryContent DirContent; GetDirectoryContent(m_ProjectBasePath, DirectoryContentFlags::IncludeDirs, DirContent); for (const std::filesystem::path& DirPath : DirContent.Directories) { std::string DirName = PathToUtf8(DirPath.filename()); if (DirName.starts_with("[dropped]")) { continue; } OpenProject(DirName); } } void ProjectStore::IterateProjects(std::function&& Fn) { RwLock::SharedLockScope _(m_ProjectsLock); for (auto& Kv : m_Projects) { Fn(*Kv.second.Get()); } } void ProjectStore::Flush() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Flush"); ZEN_INFO("flushing project store at '{}'", m_ProjectBasePath); std::vector> Projects; { RwLock::SharedLockScope _(m_ProjectsLock); Projects.reserve(m_Projects.size()); for (auto& Kv : m_Projects) { Projects.push_back(Kv.second); } } WorkerThreadPool& WorkerPool = GetSmallWorkerPool(EWorkloadType::Burst); std::atomic AbortFlag; std::atomic PauseFlag; ParallelWork Work(AbortFlag, PauseFlag); try { for (const Ref& Project : Projects) { Work.ScheduleWork(WorkerPool, [this, Project](std::atomic&) { try { Project->Flush(); } catch (const std::exception& Ex) { ZEN_WARN("Exception while flushing project {}: {}", Project->Identifier, Ex.what()); } }); } } catch (const std::exception& Ex) { AbortFlag.store(true); ZEN_WARN("Failed projects in {}. Reason: '{}'", m_ProjectBasePath, Ex.what()); } Work.Wait(); } void ProjectStore::ScrubStorage(ScrubContext& Ctx) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_INFO("scrubbing '{}'", m_ProjectBasePath); DiscoverProjects(); std::vector> Projects; { RwLock::SharedLockScope Lock(m_ProjectsLock); Projects.reserve(m_Projects.size()); for (auto& Kv : m_Projects) { if (Kv.second->IsExpired(GcClock::TimePoint::min())) { continue; } Projects.push_back(Kv.second); } } for (const Ref& Project : Projects) { Project->Scrub(Ctx); } } GcStorageSize ProjectStore::StorageSize() const { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::StorageSize"); using namespace std::literals; GcStorageSize Result; { if (IsDir(m_ProjectBasePath)) { DirectoryContent ProjectsFolderContent; GetDirectoryContent(m_ProjectBasePath, DirectoryContentFlags::IncludeDirs, ProjectsFolderContent); for (const std::filesystem::path& ProjectBasePath : ProjectsFolderContent.Directories) { std::filesystem::path ProjectStateFilePath = ProjectBasePath / "Project.zcb"sv; if (IsFile(ProjectStateFilePath)) { Result.DiskSize += Project::TotalSize(ProjectBasePath); DirectoryContent DirContent; GetDirectoryContent(ProjectBasePath, DirectoryContentFlags::IncludeDirs, DirContent); for (const std::filesystem::path& OplogBasePath : DirContent.Directories) { Result.DiskSize += Oplog::TotalSize(OplogBasePath); } } } } } return Result; } Ref ProjectStore::OpenProject(std::string_view ProjectId) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::OpenProject"); { RwLock::SharedLockScope _(m_ProjectsLock); if (auto ProjIt = m_Projects.find(std::string{ProjectId}); ProjIt != m_Projects.end()) { return ProjIt->second; } } CidStore& ChunkStore = m_GetCidStore(ProjectId); RwLock::ExclusiveLockScope _(m_ProjectsLock); if (auto ProjIt = m_Projects.find(std::string{ProjectId}); ProjIt != m_Projects.end()) { return ProjIt->second; } std::filesystem::path BasePath = BasePathForProject(ProjectId); if (Project::Exists(BasePath)) { try { ZEN_INFO("project '{}': opening project at '{}'", ProjectId, BasePath); Ref& Prj = m_Projects .try_emplace(std::string{ProjectId}, Ref(new ProjectStore::Project(this, ChunkStore, BasePath))) .first->second; Prj->Identifier = ProjectId; Prj->Read(); return Prj; } catch (const std::exception& e) { ZEN_WARN("project '{}': failed to open at {} ({})", ProjectId, BasePath, e.what()); m_Projects.erase(std::string{ProjectId}); } } return {}; } Ref ProjectStore::NewProject(const std::filesystem::path& BasePath, std::string_view ProjectId, const std::filesystem::path& RootDir, const std::filesystem::path& EngineRootDir, const std::filesystem::path& ProjectRootDir, const std::filesystem::path& ProjectFilePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::NewProject"); CidStore& ChunkStore = m_GetCidStore(ProjectId); RwLock::ExclusiveLockScope _(m_ProjectsLock); ZEN_INFO("project '{}': creating project at '{}'", ProjectId, BasePath); Ref& Prj = m_Projects.try_emplace(std::string{ProjectId}, Ref(new ProjectStore::Project(this, ChunkStore, BasePath))) .first->second; Prj->Identifier = ProjectId; Prj->RootDir = RootDir; Prj->EngineRootDir = EngineRootDir; Prj->ProjectRootDir = ProjectRootDir; Prj->ProjectFilePath = ProjectFilePath; Prj->Write(); if (m_CapturedProjects) { m_CapturedProjects->push_back(std::string(ProjectId)); } return Prj; } bool ProjectStore::UpdateProject(std::string_view ProjectId, const std::filesystem::path& RootDir, const std::filesystem::path& EngineRootDir, const std::filesystem::path& ProjectRootDir, const std::filesystem::path& ProjectFilePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::UpdateProject"); RwLock::ExclusiveLockScope ProjectsLock(m_ProjectsLock); auto ProjIt = m_Projects.find(std::string{ProjectId}); if (ProjIt == m_Projects.end()) { return false; } Ref Prj = ProjIt->second; Prj->RootDir = RootDir; Prj->EngineRootDir = EngineRootDir; Prj->ProjectRootDir = ProjectRootDir; Prj->ProjectFilePath = ProjectFilePath; Prj->Write(); ZEN_INFO("project '{}': updated", ProjectId); return true; } bool ProjectStore::RemoveProject(std::string_view ProjectId, std::filesystem::path& OutDeletePath) { ZEN_MEMSCOPE(GetProjectstoreTag()); RwLock::ExclusiveLockScope ProjectsLock(m_ProjectsLock); auto ProjIt = m_Projects.find(std::string{ProjectId}); if (ProjIt == m_Projects.end()) { return true; } bool Success = ProjIt->second->PrepareForDelete(OutDeletePath); if (!Success) { return false; } m_Projects.erase(ProjIt); return true; } bool ProjectStore::DeleteProject(std::string_view ProjectId) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::DeleteProject"); ZEN_INFO("project '{}': deleting", ProjectId); std::filesystem::path DeletePath; if (!RemoveProject(ProjectId, DeletePath)) { return false; } if (!DeletePath.empty()) { if (!DeleteDirectories(DeletePath)) { ZEN_WARN("project '{}': failed to remove old project path '{}'", ProjectId, DeletePath); return false; } } return true; } bool ProjectStore::Exists(std::string_view ProjectId) { return Project::Exists(BasePathForProject(ProjectId)); } CbArray ProjectStore::GetProjectsList() { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::GetProjectsList"); using namespace std::literals; DiscoverProjects(); CbWriter Response; Response.BeginArray(); IterateProjects([&Response](ProjectStore::Project& Prj) { Response.BeginObject(); Response << "Id"sv << Prj.Identifier; Response << "RootDir"sv << Prj.RootDir.string(); Response << "ProjectRootDir"sv << PathToUtf8(Prj.ProjectRootDir); Response << "EngineRootDir"sv << PathToUtf8(Prj.EngineRootDir); Response << "ProjectFilePath"sv << PathToUtf8(Prj.ProjectFilePath); Response.EndObject(); }); Response.EndArray(); return Response.Save().AsArray(); } std::pair ProjectStore::GetProjectFiles(const std::string_view ProjectId, const std::string_view OplogId, const std::unordered_set& WantedFieldNames, CbObject& OutPayload) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::GetProjectFiles"); using namespace std::literals; Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Project files request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ true, /*VerifyPathOnDisk*/ true); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("Project files for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); const bool WantsAllFields = WantedFieldNames.empty(); const bool WantsIdField = WantsAllFields || WantedFieldNames.contains("id"); const bool WantsClientPathField = WantsAllFields || WantedFieldNames.contains("clientpath"); const bool WantsServerPathField = WantsAllFields || WantedFieldNames.contains("serverpath"); const bool WantsRawSizeField = WantsAllFields || WantedFieldNames.contains("rawsize"); const bool WantsSizeField = WantsAllFields || WantedFieldNames.contains("size"); std::vector Ids; std::vector ServerPaths; std::vector ClientPaths; std::vector Sizes; std::vector RawSizes; size_t Count = 0; FoundLog->IterateFileMap([&](const Oid& Id, const std::string_view& ServerPath, const std::string_view& ClientPath) { if (WantsIdField || WantsRawSizeField || WantsSizeField) { Ids.push_back(Id); } if (WantsServerPathField) { ServerPaths.push_back(std::string(ServerPath)); } if (WantsClientPathField) { ClientPaths.push_back(std::string(ClientPath)); } Count++; }); if (WantsRawSizeField || WantsSizeField) { if (WantsSizeField) { Sizes.resize(Ids.size(), (uint64_t)-1); } if (WantsRawSizeField) { RawSizes.resize(Ids.size(), (uint64_t)-1); } FoundLog->IterateChunks( Ids, false, [&](size_t Index, const IoBuffer& Payload, uint64_t /*ModTag*/) { try { if (Payload) { if (Payload.GetContentType() == ZenContentType::kCompressedBinary) { if (WantsRawSizeField) { IoHash _; if (CompressedBuffer::ValidateCompressedHeader(Payload, _, RawSizes[Index])) { if (WantsSizeField) { Sizes[Index] = Payload.GetSize(); } } else { ZEN_WARN("oplog '{}/{}': payload for project file info for id {} is not a valid compressed binary.", ProjectId, OplogId, Ids[Index]); } } else if (WantsSizeField) { Sizes[Index] = Payload.GetSize(); } } else { if (WantsSizeField) { Sizes[Index] = Payload.GetSize(); } if (WantsRawSizeField) { RawSizes[Index] = Payload.GetSize(); } } } else { ZEN_WARN("oplog '{}/{}': failed getting payload for project file info for id {}.", ProjectId, OplogId, Ids[Index]); } } catch (const std::exception& Ex) { ZEN_WARN("oplog '{}/{}': failed getting project file info for id {}. Reason: '{}'", ProjectId, OplogId, Ids[Index], Ex.what()); } return true; }, &GetSmallWorkerPool(EWorkloadType::Burst), 256u * 1024u); } CbObjectWriter Response(64u + Count * ((WantsIdField ? (5 + sizeof(Oid::OidBits)) : 0) + (WantsServerPathField ? 64 : 0) + (WantsClientPathField ? 64 : 0) + (WantsSizeField ? 16 : 0) + (WantsRawSizeField ? 16 : 0))); Response.BeginArray("files"sv); for (size_t Index = 0; Index < Count; Index++) { Response.BeginObject(); if (WantsIdField) { Response << "id"sv << Ids[Index]; } if (WantsServerPathField) { Response << "serverpath"sv << ServerPaths[Index]; } if (WantsClientPathField) { Response << "clientpath"sv << ClientPaths[Index]; } if (WantsSizeField && Sizes[Index] != (uint64_t)-1) { Response << "size"sv << Sizes[Index]; } if (WantsRawSizeField && RawSizes[Index] != (uint64_t)-1) { Response << "rawsize"sv << RawSizes[Index]; } Response.EndObject(); } Response.EndArray(); OutPayload = Response.Save(); return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::GetProjectChunkInfos(const std::string_view ProjectId, const std::string_view OplogId, const std::unordered_set& WantedFieldNames, CbObject& OutPayload) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("ProjectStore::GetProjectChunkInfos"); using namespace std::literals; Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ true, /*VerifyPathOnDisk*/ true); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); const bool WantsAllFields = WantedFieldNames.empty(); const bool WantsIdField = WantsAllFields || WantedFieldNames.contains("id"); const bool WantsRawHashField = WantsAllFields || WantedFieldNames.contains("rawhash"); const bool WantsRawSizeField = WantsAllFields || WantedFieldNames.contains("rawsize"); const bool WantsSizeField = WantsAllFields || WantedFieldNames.contains("size"); std::vector Ids; std::vector Hashes; std::vector RawSizes; std::vector Sizes; size_t Count = 0; size_t EstimatedCount = FoundLog->OplogCount(); if (WantsIdField) { Ids.reserve(EstimatedCount); } if (WantsRawHashField || WantsRawSizeField || WantsSizeField) { Hashes.reserve(EstimatedCount); } FoundLog->IterateChunkMap([&](const Oid& Id, const IoHash& Hash) { if (WantsIdField) { Ids.push_back(Id); } if (WantsRawHashField || WantsRawSizeField || WantsSizeField) { Hashes.push_back(Hash); } Count++; }); if (WantsRawSizeField || WantsSizeField) { if (WantsRawSizeField) { RawSizes.resize(Hashes.size(), 0u); } if (WantsSizeField) { Sizes.resize(Hashes.size(), 0u); } WorkerThreadPool& WorkerPool = GetSmallWorkerPool(EWorkloadType::Burst); // GetSyncWorkerPool(); (void)FoundLog->IterateChunks( Hashes, false, [&](size_t Index, const IoBuffer& Payload, uint64_t /*ModTag*/) -> bool { try { if (Payload) { if (Payload.GetContentType() == ZenContentType::kCompressedBinary) { if (WantsRawSizeField) { IoHash _; if (CompressedBuffer::ValidateCompressedHeader(Payload, _, RawSizes[Index])) { if (WantsSizeField) { Sizes[Index] = Payload.GetSize(); } } else { ZEN_WARN("oplog '{}/{}': payload for project chunk for id {} is not a valid compressed binary.", ProjectId, OplogId, Ids[Index]); } } else if (WantsSizeField) { Sizes[Index] = Payload.GetSize(); } } else { if (WantsSizeField) { Sizes[Index] = Payload.GetSize(); } if (WantsRawSizeField) { RawSizes[Index] = Payload.GetSize(); } } } else { ZEN_WARN("oplog '{}/{}': failed getting payload for chunk for id {}", ProjectId, OplogId, Ids[Index]); } } catch (const std::exception& Ex) { ZEN_WARN("oplog '{}/{}': failed getting project chunk info for id {}. Reason: '{}'", ProjectId, OplogId, Ids[Index], Ex.what()); } return true; }, &WorkerPool, 256u * 1024u); } CbObjectWriter Response(64u + Count * ((WantsIdField ? (5 + sizeof(Oid::OidBits)) : 0) + (WantsRawHashField ? (10 + sizeof(IoHash::Hash)) : 0) + (WantsSizeField ? 16 : 0) + (WantsRawSizeField ? 16 : 0))); Response.BeginArray("chunkinfos"sv); for (size_t Index = 0; Index < Count; Index++) { Response.BeginObject(); if (WantsIdField) { Response << "id"sv << Ids[Index]; } if (WantsRawHashField) { Response << "rawhash"sv << Hashes[Index]; } if (WantsSizeField && Sizes[Index] != (uint64_t)-1) { Response << "size"sv << Sizes[Index]; } if (WantsRawSizeField && RawSizes[Index] != (uint64_t)-1) { Response << "rawsize"sv << RawSizes[Index]; } Response.EndObject(); } Response.EndArray(); OutPayload = Response.Save(); return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::GetChunkInfo(const std::string_view ProjectId, const std::string_view OplogId, const std::string_view ChunkId, CbObject& OutPayload) { ZEN_MEMSCOPE(GetProjectstoreTag()); using namespace std::literals; Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Chunk info request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ false, /*VerifyPathOnDisk*/ false); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("Chunk info request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); if (ChunkId.size() != 2 * sizeof(Oid::OidBits)) { return {HttpResponseCode::BadRequest, fmt::format("Chunk info request for invalid chunk id '{}/{}'/'{}'", ProjectId, OplogId, ChunkId)}; } const Oid Obj = Oid::FromHexString(ChunkId); IoBuffer Chunk = FoundLog->FindChunk(Obj, nullptr); if (!Chunk) { return {HttpResponseCode::NotFound, {}}; } uint64_t ChunkSize = Chunk.GetSize(); if (Chunk.GetContentType() == HttpContentType::kCompressedBinary) { IoHash RawHash; uint64_t RawSize; bool IsCompressed = CompressedBuffer::ValidateCompressedHeader(Chunk, RawHash, RawSize); if (!IsCompressed) { return {HttpResponseCode::InternalServerError, fmt::format("Chunk info request for malformed chunk id '{}/{}'/'{}'", ProjectId, OplogId, ChunkId)}; } ChunkSize = RawSize; } CbObjectWriter Response; Response << "size"sv << ChunkSize; OutPayload = Response.Save(); return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::GetChunkRange(const std::string_view ProjectId, const std::string_view OplogId, const std::string_view ChunkId, uint64_t Offset, uint64_t Size, ZenContentType AcceptType, CompositeBuffer& OutChunk, ZenContentType& OutContentType, uint64_t* OptionalInOutModificationTag) { ZEN_MEMSCOPE(GetProjectstoreTag()); if (ChunkId.size() != 2 * sizeof(Oid::OidBits)) { return {HttpResponseCode::BadRequest, fmt::format("Chunk request for invalid chunk id '{}/{}'/'{}'", ProjectId, OplogId, ChunkId)}; } const Oid Obj = Oid::FromHexString(ChunkId); return GetChunkRange(ProjectId, OplogId, Obj, Offset, Size, AcceptType, OutChunk, OutContentType, OptionalInOutModificationTag); } static std::pair ExtractRange(IoBuffer&& Chunk, uint64_t Offset, uint64_t Size, ZenContentType AcceptType, ZenContentType& OutContentType, CompositeBuffer& OutChunk, IoHash& OutRawHash, uint64_t& OutRawSize) { ZEN_MEMSCOPE(GetProjectstoreTag()); OutContentType = Chunk.GetContentType(); if (OutContentType == ZenContentType::kCompressedBinary) { IoHash RawHash; uint64_t RawSize; CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(std::move(Chunk)), RawHash, RawSize); if (!Compressed) { return {HttpResponseCode::InternalServerError, "malformed compressed buffer"}; } const bool IsFullRange = (Offset == 0) && ((Size == ~(0ull)) || (Size == RawSize)); if (IsFullRange) { if (AcceptType == ZenContentType::kBinary) { OutChunk = Compressed.DecompressToComposite(); OutContentType = ZenContentType::kBinary; } else { OutChunk = Compressed.GetCompressed(); } OutRawSize = 0; } else { if (Size == ~(0ull) || (Offset + Size) > RawSize) { if (Offset < RawSize) { Size = RawSize - Offset; } else { Size = 0; } } if (Size == 0) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for range outside of compressed chunk. Offset: {}, Size: {}, ChunkSize: {}", Offset, Size, RawSize)}; } if (AcceptType == ZenContentType::kBinary) { OutChunk = CompositeBuffer(Compressed.Decompress(Offset, Size)); OutContentType = ZenContentType::kBinary; } else { // Value will be a range of compressed blocks that covers the requested range // The client will have to compensate for any offsets that do not land on an even block size multiple OutChunk = Compressed.GetRange(Offset, Size).GetCompressed(); } OutRawSize = RawSize; } OutRawHash = RawHash; } else { const uint64_t ChunkSize = Chunk.GetSize(); const bool IsFullRange = (Offset == 0) && ((Size == ~(0ull)) || (Size == ChunkSize)); if (IsFullRange) { OutChunk = CompositeBuffer(SharedBuffer(std::move(Chunk))); OutRawSize = 0; } else { if (Size == ~(0ull) || (Offset + Size) > ChunkSize) { if (Offset < ChunkSize) { Size = ChunkSize - Offset; } else { Size = 0; } } if (Size == 0) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for range outside of chunk. Offset: {}, Size: {}, ChunkSize: {}", Offset, Size, Size)}; } OutChunk = CompositeBuffer(SharedBuffer(IoBuffer(std::move(Chunk), Offset, Size))); OutRawSize = ChunkSize; } } return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::GetChunkRange(const std::string_view ProjectId, const std::string_view OplogId, Oid ChunkId, uint64_t Offset, uint64_t Size, ZenContentType AcceptType, CompositeBuffer& OutChunk, ZenContentType& OutContentType, uint64_t* OptionalInOutModificationTag) { ZEN_MEMSCOPE(GetProjectstoreTag()); Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ false, /*VerifyPathOnDisk*/ false); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); uint64_t OldTag = OptionalInOutModificationTag == nullptr ? 0 : *OptionalInOutModificationTag; IoBuffer Chunk = FoundLog->FindChunk(ChunkId, OptionalInOutModificationTag); if (!Chunk) { return {HttpResponseCode::NotFound, {}}; } if (OptionalInOutModificationTag != nullptr && OldTag == *OptionalInOutModificationTag) { return {HttpResponseCode::NotModified, {}}; } IoHash _; uint64_t __; std::pair Result = ExtractRange(std::move(Chunk), Offset, Size, AcceptType, OutContentType, OutChunk, /*OutRawHash*/ _, /*OutRawSize*/ __); if (Result.first != HttpResponseCode::OK) { return {Result.first, fmt::format("Chunk request for chunk {} in {}/{} failed. Reason: '{}'", ChunkId, ProjectId, OplogId, Result.second)}; } return Result; } std::pair ProjectStore::GetChunk(const std::string_view ProjectId, const std::string_view OplogId, const std::string_view Cid, IoBuffer& OutChunk, uint64_t* OptionalInOutModificationTag) { ZEN_MEMSCOPE(GetProjectstoreTag()); Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ false, /*VerifyPathOnDisk*/ false); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); if (Cid.length() != IoHash::StringLength) { return {HttpResponseCode::BadRequest, fmt::format("Chunk request for invalid chunk id '{}/{}'/'{}'", ProjectId, OplogId, Cid)}; } const IoHash Hash = IoHash::FromHexString(Cid); OutChunk = Project->GetCidStore().FindChunkByCid(Hash); if (!OutChunk) { return {HttpResponseCode::NotFound, fmt::format("chunk - '{}' MISSING", Cid)}; } if (OptionalInOutModificationTag != nullptr) { uint64_t OldTag = *OptionalInOutModificationTag; *OptionalInOutModificationTag = GetModificationTagFromRawHash(Hash); if (*OptionalInOutModificationTag == OldTag) { return {HttpResponseCode::NotModified, {}}; } } OutChunk.SetContentType(ZenContentType::kCompressedBinary); return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::PutChunk(const std::string_view ProjectId, const std::string_view OplogId, const std::string_view Cid, ZenContentType ContentType, IoBuffer&& Chunk) { ZEN_MEMSCOPE(GetProjectstoreTag()); Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Chunk put request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ false, /*VerifyPathOnDisk*/ false); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("Chunk put request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); if (Cid.length() != IoHash::StringLength) { return {HttpResponseCode::BadRequest, fmt::format("Chunk put request for invalid chunk hash '{}'", Cid)}; } const IoHash Hash = IoHash::FromHexString(Cid); if (ContentType != HttpContentType::kCompressedBinary) { return {HttpResponseCode::BadRequest, fmt::format("Chunk request for invalid content type for chunk '{}'", Cid)}; } IoHash RawHash; uint64_t RawSize; CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(Chunk), RawHash, RawSize); if (RawHash != Hash) { return {HttpResponseCode::BadRequest, fmt::format("Chunk request for invalid payload format for chunk '{}'", Cid)}; } FoundLog->CaptureAddedAttachments(std::vector{Hash}); CidStore::InsertResult Result = Project->GetCidStore().AddChunk(Chunk, Hash); return {Result.New ? HttpResponseCode::Created : HttpResponseCode::OK, {}}; } std::pair ProjectStore::GetChunks(const std::string_view ProjectId, const std::string_view OplogId, const CbObject& RequestObject, CbPackage& OutResponsePackage) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::GetChunks"); using namespace std::literals; Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("getchunks rpc request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* FoundLog = Project->OpenOplog(OplogId, /*AllowCompact*/ true, /*VerifyPathOnDisk*/ false); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("getchunks rpc request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); CidStore& ChunkStore = Project->GetCidStore(); if (RequestObject["chunks"sv].IsArray()) { // Legacy full chunks only by rawhash CbArrayView ChunksArray = RequestObject["chunks"sv].AsArrayView(); CbObjectWriter ResponseWriter; ResponseWriter.BeginArray("chunks"sv); for (CbFieldView FieldView : ChunksArray) { IoHash RawHash = FieldView.AsHash(); IoBuffer ChunkBuffer = ChunkStore.FindChunkByCid(RawHash); if (ChunkBuffer) { CompressedBuffer Compressed = CompressedBuffer::FromCompressedNoValidate(std::move(ChunkBuffer)); if (Compressed) { ResponseWriter.AddHash(RawHash); OutResponsePackage.AddAttachment(CbAttachment(std::move(Compressed), RawHash)); } else { ZEN_WARN("oplog '{}/{}': invalid compressed binary in cas store for {}", ProjectId, OplogId, RawHash); } } } ResponseWriter.EndArray(); OutResponsePackage.SetObject(ResponseWriter.Save()); return {HttpResponseCode::OK, {}}; } else if (auto RequestFieldView = RequestObject["Request"sv]; RequestFieldView.IsObject()) { CbObjectView RequestView = RequestFieldView.AsObjectView(); bool SkipData = RequestView["SkipData"].AsBool(false); CbArrayView ChunksArray = RequestView["Chunks"sv].AsArrayView(); struct Request { struct InputData { uint64_t Offset = 0; uint64_t Size = (uint64_t)-1; std::variant Id; std::optional ModTag; } Input; struct OutputData { bool Exists = false; IoBuffer ChunkBuffer; uint64_t ModTag = 0; } Output; }; std::vector Requests; size_t RequestCount = ChunksArray.Num(); if (RequestCount > 0) { Requests.reserve(RequestCount); std::vector ChunkRawHashes; std::vector ChunkRawHashesRequestIndex; std::vector ChunkIds; std::vector ChunkIdsRequestIndex; bool DoBatch = RequestCount > 1; if (DoBatch) { ChunkRawHashes.reserve(RequestCount); ChunkRawHashesRequestIndex.reserve(RequestCount); ChunkIds.reserve(RequestCount); ChunkIdsRequestIndex.reserve(RequestCount); } for (CbFieldView FieldView : ChunksArray) { CbObjectView ChunkObject = FieldView.AsObjectView(); Request ChunkRequest = { .Input{.Offset = ChunkObject["Offset"sv].AsUInt64(0), .Size = ChunkObject["Size"sv].AsUInt64((uint64_t)-1)}}; if (CbFieldView InputModificationTagView = ChunkObject.FindView("ModTag"); InputModificationTagView.IsInteger()) { ChunkRequest.Input.ModTag = InputModificationTagView.AsUInt64(); } if (CbFieldView RawHashView = ChunkObject.FindView("RawHash"sv); RawHashView.IsHash()) { const IoHash ChunkHash = RawHashView.AsHash(); ChunkRequest.Input.Id = ChunkHash; if (DoBatch) { ChunkRawHashes.push_back(ChunkHash); ChunkRawHashesRequestIndex.push_back(Requests.size()); } } else if (CbFieldView IdView = ChunkObject.FindView("Oid"sv); IdView.IsObjectId()) { const Oid ChunkId = IdView.AsObjectId(); ChunkRequest.Input.Id = ChunkId; if (DoBatch) { ChunkIds.push_back(ChunkId); ChunkIdsRequestIndex.push_back(Requests.size()); } } else { return {HttpResponseCode::BadRequest, fmt::format("oplog '{}/{}': malformed getchunks rpc request object, chunk request has no identifier", ProjectId, OplogId)}; } Requests.emplace_back(std::move(ChunkRequest)); } if (DoBatch) { WorkerThreadPool& WorkerPool = GetSmallWorkerPool(EWorkloadType::Burst); // GetSyncWorkerPool(); if (!ChunkRawHashes.empty()) { FoundLog->IterateChunks( ChunkRawHashes, true, [&](size_t Index, const IoBuffer& Payload, uint64_t ModTag) -> bool { if (Payload) { size_t RequestIndex = ChunkRawHashesRequestIndex[Index]; Requests[RequestIndex].Output.Exists = true; if (!SkipData) { Requests[RequestIndex].Output.ChunkBuffer = Payload; Requests[RequestIndex].Output.ChunkBuffer.MakeOwned(); } Requests[RequestIndex].Output.ModTag = ModTag; } return true; }, &WorkerPool, 8u * 1024); } if (!ChunkIdsRequestIndex.empty()) { FoundLog->IterateChunks( ChunkIds, true, [&](size_t Index, const IoBuffer& Payload, uint64_t ModTag) -> bool { if (Payload) { size_t RequestIndex = ChunkIdsRequestIndex[Index]; Requests[RequestIndex].Output.Exists = true; if (!SkipData) { Requests[RequestIndex].Output.ChunkBuffer = Payload; Requests[RequestIndex].Output.ChunkBuffer.MakeOwned(); } Requests[RequestIndex].Output.ModTag = ModTag; } return true; }, &WorkerPool, 8u * 1024); } } else { Request& ChunkRequest = Requests.front(); if (ChunkRequest.Input.Id.index() == 0) { const IoHash& ChunkHash = std::get(ChunkRequest.Input.Id); IoBuffer Payload = ChunkStore.FindChunkByCid(ChunkHash); if (Payload) { ChunkRequest.Output.Exists = true; ChunkRequest.Output.ModTag = GetModificationTagFromRawHash(ChunkHash); if (!SkipData) { ChunkRequest.Output.ChunkBuffer = Payload; } } } else { const Oid& ChunkId = std::get(ChunkRequest.Input.Id); uint64_t ModTag = 0; IoBuffer Payload = FoundLog->FindChunk(ChunkId, &ModTag); if (Payload) { ChunkRequest.Output.Exists = true; ChunkRequest.Output.ModTag = ModTag; if (!SkipData) { ChunkRequest.Output.ChunkBuffer = Payload; } } } } } CbObjectWriter ResponseWriter(32 + Requests.size() * 64u); ResponseWriter.BeginArray("Chunks"sv); { for (Request& ChunkRequest : Requests) { if (ChunkRequest.Output.Exists) { ResponseWriter.BeginObject(); { if (ChunkRequest.Input.Id.index() == 0) { const IoHash& RawHash = std::get(ChunkRequest.Input.Id); ResponseWriter.AddHash("Id", RawHash); } else { const Oid& Id = std::get(ChunkRequest.Input.Id); ResponseWriter.AddObjectId("Id", Id); } if (!ChunkRequest.Input.ModTag.has_value() || ChunkRequest.Input.ModTag.value() != ChunkRequest.Output.ModTag) { ResponseWriter.AddInteger("ModTag", ChunkRequest.Output.ModTag); if (!SkipData) { CompositeBuffer ChunkRange; ZenContentType ContentType; IoHash FullChunkRawHash; uint64_t FullChunkSize = 0; auto ExtractRangeResult = ExtractRange(std::move(ChunkRequest.Output.ChunkBuffer), ChunkRequest.Input.Offset, ChunkRequest.Input.Size, ZenContentType::kCompressedBinary, ContentType, ChunkRange, FullChunkRawHash, FullChunkSize); if (ExtractRangeResult.first == HttpResponseCode::OK) { if (ContentType == ZenContentType::kCompressedBinary) { ZEN_ASSERT(FullChunkRawHash != IoHash::Zero); CompressedBuffer CompressedValue = CompressedBuffer::FromCompressedNoValidate(std::move(ChunkRange)); ZEN_ASSERT(CompressedValue); if (FullChunkSize != 0) { // This really could use some thought so we don't send the same data if we get a request for // multiple ranges from the same chunk block uint64_t FragmentRawOffset = 0; OodleCompressor Compressor; OodleCompressionLevel CompressionLevel; uint64_t BlockSize = 0; if (CompressedValue.TryGetCompressParameters(Compressor, CompressionLevel, BlockSize)) { if (BlockSize > 0) { FragmentRawOffset = (ChunkRequest.Input.Offset / BlockSize) * BlockSize; } else { FragmentRawOffset = ChunkRequest.Input.Offset; } uint64_t FragmentRawLength = CompressedValue.DecodeRawSize(); IoHashStream FragmentHashStream; FragmentHashStream.Append(FullChunkRawHash.Hash, sizeof(FullChunkRawHash.Hash)); FragmentHashStream.Append(&FragmentRawOffset, sizeof(FragmentRawOffset)); FragmentHashStream.Append(&FragmentRawLength, sizeof(FragmentRawLength)); IoHash FragmentHash = FragmentHashStream.GetHash(); ResponseWriter.AddHash("FragmentHash", FragmentHash); ResponseWriter.AddInteger("FragmentOffset", FragmentRawOffset); ResponseWriter.AddInteger("RawSize", FullChunkSize); OutResponsePackage.AddAttachment(CbAttachment(CompressedValue, FragmentHash)); } else { std::string ErrorString = "Failed to get compression parameters from partial compressed buffer"; ResponseWriter.AddString("Error", ErrorString); ZEN_WARN("oplog '{}/{}': {}", ProjectId, OplogId, ErrorString); } } else { ResponseWriter.AddHash("RawHash"sv, FullChunkRawHash); OutResponsePackage.AddAttachment(CbAttachment(std::move(CompressedValue), FullChunkRawHash)); } } else { IoHashStream HashStream; ZEN_ASSERT(ChunkRequest.Input.Id.index() == 1); const Oid& Id = std::get(ChunkRequest.Input.Id); HashStream.Append(Id.OidBits, sizeof(Id.OidBits)); HashStream.Append(&ChunkRequest.Input.Offset, sizeof(ChunkRequest.Input.Offset)); HashStream.Append(&ChunkRequest.Input.Size, sizeof(ChunkRequest.Input.Size)); IoHash Hash = HashStream.GetHash(); ResponseWriter.AddHash("Hash"sv, Hash); if (FullChunkSize != 0) { ResponseWriter.AddInteger("Size", FullChunkSize); } OutResponsePackage.AddAttachment(CbAttachment(std::move(ChunkRange), Hash)); } } else { std::string ErrorString = fmt::format("Failed fetchiong chunk range ({})", ExtractRangeResult.second); ResponseWriter.AddString("Error", ErrorString); ZEN_WARN("oplog '{}/{}': {}", ProjectId, OplogId, ErrorString); } } } } ResponseWriter.EndObject(); } } } ResponseWriter.EndArray(); OutResponsePackage.SetObject(ResponseWriter.Save()); return {HttpResponseCode::OK, {}}; } else { return {HttpResponseCode::BadRequest, fmt::format("oplog '{}/{}': malformed getchunks rpc request object", ProjectId, OplogId)}; } } std::pair ProjectStore::WriteOplog(const std::string_view ProjectId, const std::string_view OplogId, IoBuffer&& Payload, CbObject& OutResponse) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::WriteOplog"); Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Write oplog request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* Oplog = Project->OpenOplog(OplogId, /*AllowCompact*/ true, /*VerifyPathOnDisk*/ false); if (!Oplog) { return {HttpResponseCode::NotFound, fmt::format("Write oplog request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); CbObject ContainerObject = LoadCompactBinaryObject(Payload); if (!ContainerObject) { return {HttpResponseCode::BadRequest, "Invalid payload format"}; } CidStore& ChunkStore = Project->GetCidStore(); RwLock AttachmentsLock; tsl::robin_set Attachments; auto HasAttachment = [&ChunkStore](const IoHash& RawHash) { return ChunkStore.ContainsChunk(RawHash); }; auto OnNeedBlock = [&AttachmentsLock, &Attachments](const IoHash& BlockHash, const std::vector&& ChunkHashes) { RwLock::ExclusiveLockScope _(AttachmentsLock); if (BlockHash != IoHash::Zero) { Attachments.insert(BlockHash); } else { Attachments.insert(ChunkHashes.begin(), ChunkHashes.end()); } }; auto OnNeedAttachment = [&AttachmentsLock, &Attachments](const IoHash& RawHash) { RwLock::ExclusiveLockScope _(AttachmentsLock); Attachments.insert(RawHash); }; auto OnChunkedAttachment = [](const ChunkedInfo&) {}; auto OnReferencedAttachments = [&Oplog](std::span RawHashes) { Oplog->CaptureAddedAttachments(RawHashes); }; // Make sure we retain any attachments we download before writing the oplog Oplog->EnableUpdateCapture(); auto _ = MakeGuard([&Oplog]() { Oplog->DisableUpdateCapture(); }); RemoteProjectStore::Result RemoteResult = SaveOplogContainer(*Oplog, ContainerObject, OnReferencedAttachments, HasAttachment, OnNeedBlock, OnNeedAttachment, OnChunkedAttachment, nullptr); if (RemoteResult.ErrorCode) { return ConvertResult(RemoteResult); } CbObjectWriter Cbo(1 + 1 + 5 + Attachments.size() * (1 + sizeof(IoHash::Hash)) + 1); Cbo.BeginArray("need"); { for (const IoHash& Hash : Attachments) { ZEN_DEBUG("Need attachment {}", Hash); Cbo << Hash; } } Cbo.EndArray(); // "need" OutResponse = Cbo.Save(); return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::ReadOplog(const std::string_view ProjectId, const std::string_view OplogId, const HttpServerRequest::QueryParams& Params, CbObject& OutResponse) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::ReadOplog"); Ref Project = OpenProject(ProjectId); if (!Project) { return {HttpResponseCode::NotFound, fmt::format("Read oplog request for unknown project '{}'", ProjectId)}; } Project->TouchProject(); ProjectStore::Oplog* Oplog = Project->OpenOplog(OplogId, /*AllowCompact*/ true, /*VerifyPathOnDisk*/ true); if (!Oplog) { return {HttpResponseCode::NotFound, fmt::format("Read oplog request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); size_t MaxBlockSize = RemoteStoreOptions::DefaultMaxBlockSize; if (auto Param = Params.GetValue("maxblocksize"); Param.empty() == false) { if (auto Value = ParseInt(Param)) { MaxBlockSize = Value.value(); } } size_t MaxChunkEmbedSize = RemoteStoreOptions::DefaultMaxChunkEmbedSize; if (auto Param = Params.GetValue("maxchunkembedsize"); Param.empty() == false) { if (auto Value = ParseInt(Param)) { MaxChunkEmbedSize = Value.value(); } } size_t ChunkFileSizeLimit = RemoteStoreOptions::DefaultChunkFileSizeLimit; if (auto Param = Params.GetValue("chunkfilesizelimit"); Param.empty() == false) { if (auto Value = ParseInt(Param)) { ChunkFileSizeLimit = Value.value(); } } CidStore& ChunkStore = Project->GetCidStore(); RemoteProjectStore::LoadContainerResult ContainerResult = BuildContainer( ChunkStore, *Project.Get(), *Oplog, MaxBlockSize, MaxChunkEmbedSize, ChunkFileSizeLimit, /* BuildBlocks */ false, /* IgnoreMissingAttachments */ false, /* AllowChunking*/ false, [](CompressedBuffer&&, ChunkBlockDescription&&) {}, [](const IoHash&, TGetAttachmentBufferFunc&&) {}, [](std::vector>&&) {}, /* EmbedLooseFiles*/ false); OutResponse = std::move(ContainerResult.ContainerObject); return ConvertResult(ContainerResult); } bool ProjectStore::Rpc(HttpServerRequest& HttpReq, const std::string_view ProjectId, const std::string_view OplogId, IoBuffer&& Payload, AuthMgr& AuthManager) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Rpc"); using namespace std::literals; HttpContentType PayloadContentType = HttpReq.RequestContentType(); CbPackage Package; CbObject Cb; switch (PayloadContentType) { case HttpContentType::kJSON: case HttpContentType::kUnknownContentType: case HttpContentType::kText: { std::string JsonText(reinterpret_cast(Payload.GetData()), Payload.GetSize()); Cb = LoadCompactBinaryFromJson(JsonText).AsObject(); if (!Cb) { HttpReq.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Content format not supported, expected JSON format"); return false; } } break; case HttpContentType::kCbObject: Cb = LoadCompactBinaryObject(Payload); if (!Cb) { HttpReq.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Content format not supported, expected compact binary format"); return false; } break; case HttpContentType::kCbPackage: try { Package = ParsePackageMessage(Payload); Cb = Package.GetObject(); } catch (const std::invalid_argument& ex) { HttpReq.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, fmt::format("Failed to parse package request, reason: '{}'", ex.what())); return false; } if (!Cb) { HttpReq.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Content format not supported, expected package message format"); return false; } break; default: HttpReq.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Invalid request content type"); return false; } Ref Project = OpenProject(ProjectId); if (!Project) { HttpReq.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, fmt::format("Rpc oplog request for unknown project '{}'", ProjectId)); return true; } Project->TouchProject(); std::string_view Method = Cb["method"sv].AsString(); bool VerifyPathOnDisk = Method != "getchunks"sv; ProjectStore::Oplog* Oplog = Project->OpenOplog(OplogId, /*AllowCompact*/ false, VerifyPathOnDisk); if (!Oplog) { HttpReq.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, fmt::format("Rpc oplog request for unknown oplog '{}/{}'", ProjectId, OplogId)); return true; } Project->TouchOplog(OplogId); CidStore& ChunkStore = Project->GetCidStore(); if (Method == "import"sv) { if (!AreDiskWritesAllowed()) { HttpReq.WriteResponse(HttpResponseCode::InsufficientStorage); return true; } std::pair Result = Import(*Project.Get(), *Oplog, Cb["params"sv].AsObjectView(), AuthManager); if (Result.second.empty()) { HttpReq.WriteResponse(Result.first); return Result.first != HttpResponseCode::BadRequest; } HttpReq.WriteResponse(Result.first, HttpContentType::kText, Result.second); return true; } else if (Method == "export"sv) { std::pair Result = Export(Project, *Oplog, Cb["params"sv].AsObjectView(), AuthManager); HttpReq.WriteResponse(Result.first, HttpContentType::kText, Result.second); return true; } else if (Method == "getchunks"sv) { ZEN_TRACE_CPU("Store::Rpc::getchunks"); RpcAcceptOptions AcceptFlags = static_cast(Cb["AcceptFlags"sv].AsUInt16(0u)); int32_t TargetProcessId = Cb["Pid"sv].AsInt32(0); CbPackage ResponsePackage; std::pair Result = GetChunks(ProjectId, OplogId, Cb, ResponsePackage); if (Result.first == HttpResponseCode::OK) { void* TargetProcessHandle = nullptr; FormatFlags Flags = FormatFlags::kDefault; if (EnumHasAllFlags(AcceptFlags, RpcAcceptOptions::kAllowLocalReferences)) { Flags |= FormatFlags::kAllowLocalReferences; if (!EnumHasAnyFlags(AcceptFlags, RpcAcceptOptions::kAllowPartialLocalReferences)) { Flags |= FormatFlags::kDenyPartialLocalReferences; } TargetProcessHandle = m_OpenProcessCache.GetProcessHandle(HttpReq.SessionId(), TargetProcessId); } CompositeBuffer RpcResponseBuffer = FormatPackageMessageBuffer(ResponsePackage, Flags, TargetProcessHandle); HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kCbPackage, RpcResponseBuffer); } else { HttpReq.WriteResponse(Result.first, HttpContentType::kText, Result.second); } return true; } else if (Method == "putchunks"sv) { ZEN_TRACE_CPU("Store::Rpc::putchunks"); if (!AreDiskWritesAllowed()) { HttpReq.WriteResponse(HttpResponseCode::InsufficientStorage); return true; } std::span Attachments = Package.GetAttachments(); if (!Attachments.empty()) { std::vector WriteAttachmentBuffers; std::vector WriteRawHashes; WriteAttachmentBuffers.reserve(Attachments.size()); WriteRawHashes.reserve(Attachments.size()); for (const CbAttachment& Attachment : Attachments) { IoHash RawHash = Attachment.GetHash(); const CompressedBuffer& Compressed = Attachment.AsCompressedBinary(); WriteAttachmentBuffers.push_back(Compressed.GetCompressed().Flatten().AsIoBuffer()); WriteRawHashes.push_back(RawHash); } Oplog->CaptureAddedAttachments(WriteRawHashes); ChunkStore.AddChunks(WriteAttachmentBuffers, WriteRawHashes, CidStore::InsertMode::kCopyOnly); } HttpReq.WriteResponse(HttpResponseCode::OK); return true; } else if (Method == "snapshot"sv) { ZEN_TRACE_CPU("Store::Rpc::snapshot"); if (!AreDiskWritesAllowed()) { HttpReq.WriteResponse(HttpResponseCode::InsufficientStorage); return true; } // Snapshot all referenced files. This brings the content of all // files into the CID store uint32_t OpCount = 0; uint64_t InlinedBytes = 0; uint64_t InlinedFiles = 0; uint64_t TotalBytes = 0; uint64_t TotalFiles = 0; std::vector NewOps; struct AddedChunk { IoBuffer Buffer; uint64_t RawSize = 0; }; tsl::robin_map AddedChunks; Oplog->IterateOplog( [&](CbObjectView Op) { bool OpRewritten = false; bool AllOk = true; CbWriter FilesArrayWriter; FilesArrayWriter.BeginArray("files"sv); for (CbFieldView& Field : Op["files"sv]) { bool CopyField = true; if (CbObjectView View = Field.AsObjectView()) { const IoHash DataHash = View["data"sv].AsHash(); if (DataHash == IoHash::Zero) { std::string_view ServerPath = View["serverpath"sv].AsString(); std::filesystem::path FilePath = Project->RootDir / ServerPath; BasicFile DataFile; std::error_code Ec; DataFile.Open(FilePath, BasicFile::Mode::kRead, Ec); if (Ec) { // Error... ZEN_ERROR("unable to read data from file '{}': {}", FilePath, Ec.message()); AllOk = false; } else { // Read file contents into memory, compress and keep in map of chunks to add to Cid store IoBuffer FileIoBuffer = DataFile.ReadAll(); CompressedBuffer Compressed = CompressedBuffer::Compress(SharedBuffer(std::move(FileIoBuffer))); const uint64_t RawSize = Compressed.DecodeRawSize(); const IoHash RawHash = Compressed.DecodeRawHash(); if (!AddedChunks.contains(RawHash)) { const std::filesystem::path TempChunkPath = Oplog->TempPath() / RawHash.ToHexString(); BasicFile ChunkTempFile; ChunkTempFile.Open(TempChunkPath, BasicFile::Mode::kTruncateDelete); ChunkTempFile.Write(Compressed.GetCompressed(), 0, Ec); if (Ec) { Oid ChunkId = View["id"sv].AsObjectId(); ZEN_ERROR("unable to write external file as compressed chunk '{}', id {}: {}", FilePath, ChunkId, Ec.message()); AllOk = false; } else { void* FileHandle = ChunkTempFile.Detach(); IoBuffer ChunkBuffer(IoBuffer::File, FileHandle, 0, Compressed.GetCompressed().GetSize(), /*IsWholeFile*/ true); ChunkBuffer.SetDeleteOnClose(true); AddedChunks.insert_or_assign(RawHash, AddedChunk{.Buffer = std::move(ChunkBuffer), .RawSize = RawSize}); } } TotalBytes += RawSize; ++TotalFiles; // Rewrite file array entry with new data reference CbObjectWriter Writer(View.GetSize()); RewriteCbObject(Writer, View, [&](CbObjectWriter&, CbFieldView Field) -> bool { if (Field.GetName() == "data"sv) { // omit this field as we will write it explicitly ourselves return true; } return false; }); Writer.AddBinaryAttachment("data"sv, RawHash); CbObject RewrittenOp = Writer.Save(); FilesArrayWriter.AddObject(std::move(RewrittenOp)); CopyField = false; } } } if (CopyField) { FilesArrayWriter.AddField(Field); } else { OpRewritten = true; } } if (OpRewritten && AllOk) { FilesArrayWriter.EndArray(); CbArray FilesArray = FilesArrayWriter.Save().AsArray(); CbObject RewrittenOp = RewriteCbObject(Op, [&](CbObjectWriter& NewWriter, CbFieldView Field) -> bool { if (Field.GetName() == "files"sv) { NewWriter.AddArray("files"sv, FilesArray); return true; } return false; }); NewOps.push_back(std::move(RewrittenOp)); } OpCount++; }, Oplog::Paging{}); CbObjectWriter ResponseObj; // Persist rewritten oplog entries if (!NewOps.empty()) { ResponseObj.BeginArray("rewritten_ops"); for (CbObject& NewOp : NewOps) { uint32_t NewLsn = Oplog->AppendNewOplogEntry(std::move(NewOp)); ZEN_DEBUG("appended rewritten op at LSN: {}", NewLsn); ResponseObj.AddInteger(NewLsn); } ResponseObj.EndArray(); } // Ops that have moved chunks to a compressed buffer for storage in ChunkStore have been rewritten with references to the new // chunk(s). Make sure we add the chunks to ChunkStore, and do it after we update the oplog so GC doesn't think we have // unreferenced chunks. for (auto It : AddedChunks) { const IoHash& RawHash = It.first; AddedChunk& Chunk = It.second; CidStore::InsertResult Result = ChunkStore.AddChunk(Chunk.Buffer, RawHash); if (Result.New) { InlinedBytes += Chunk.RawSize; ++InlinedFiles; } } ResponseObj << "inlined_bytes" << InlinedBytes << "inlined_files" << InlinedFiles; ResponseObj << "total_bytes" << TotalBytes << "total_files" << TotalFiles; ZEN_INFO("oplog '{}/{}': rewrote {} oplog entries (out of {})", ProjectId, OplogId, NewOps.size(), OpCount); HttpReq.WriteResponse(HttpResponseCode::OK, ResponseObj.Save()); return true; } HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, fmt::format("Unknown rpc method '{}'", Method)); return true; } std::pair ProjectStore::Export(Ref Project, ProjectStore::Oplog& Oplog, CbObjectView&& Params, AuthMgr& AuthManager) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Export"); using namespace std::literals; size_t MaxBlockSize = Params["maxblocksize"sv].AsUInt64(RemoteStoreOptions::DefaultMaxBlockSize); size_t MaxChunkEmbedSize = Params["maxchunkembedsize"sv].AsUInt64(RemoteStoreOptions::DefaultMaxChunkEmbedSize); size_t ChunkFileSizeLimit = Params["chunkfilesizelimit"sv].AsUInt64(RemoteStoreOptions::DefaultChunkFileSizeLimit); bool Force = Params["force"sv].AsBool(false); bool IgnoreMissingAttachments = Params["ignoremissingattachments"sv].AsBool(false); bool EmbedLooseFile = Params["embedloosefiles"sv].AsBool(false); CreateRemoteStoreResult RemoteStoreResult = CreateRemoteStore(Params, AuthManager, MaxBlockSize, MaxChunkEmbedSize, Oplog.TempPath()); if (RemoteStoreResult.Store == nullptr) { return {HttpResponseCode::BadRequest, RemoteStoreResult.Description}; } std::shared_ptr RemoteStore = std::move(RemoteStoreResult.Store); RemoteProjectStore::RemoteStoreInfo StoreInfo = RemoteStore->GetInfo(); ZEN_INFO("Saving oplog '{}/{}' to {}, maxblocksize {}, maxchunkembedsize {}", Project->Identifier, Oplog.OplogId(), StoreInfo.Description, NiceBytes(MaxBlockSize), NiceBytes(MaxChunkEmbedSize)); JobId JobId = m_JobQueue.QueueJob( fmt::format("Export oplog '{}/{}'", Project->Identifier, Oplog.OplogId()), [this, ActualRemoteStore = std::move(RemoteStore), Project, OplogPtr = &Oplog, MaxBlockSize, MaxChunkEmbedSize, ChunkFileSizeLimit, EmbedLooseFile, Force, IgnoreMissingAttachments](JobContext& Context) { RemoteProjectStore::Result Result = SaveOplog(Project->GetCidStore(), *ActualRemoteStore, *Project.Get(), *OplogPtr, MaxBlockSize, MaxChunkEmbedSize, ChunkFileSizeLimit, EmbedLooseFile, Force, IgnoreMissingAttachments, &Context); auto Response = ConvertResult(Result); ZEN_INFO("SaveOplog: Status: {} '{}'", ToString(Response.first), Response.second); if (!IsHttpSuccessCode(Response.first)) { throw JobError(Response.second.empty() ? fmt::format("Status: {}", ToString(Response.first)) : Response.second, (int)Response.first); } }); return {HttpResponseCode::Accepted, fmt::format("{}", JobId.Id)}; } std::pair ProjectStore::Import(ProjectStore::Project& Project, ProjectStore::Oplog& Oplog, CbObjectView&& Params, AuthMgr& AuthManager) { ZEN_MEMSCOPE(GetProjectstoreTag()); ZEN_TRACE_CPU("Store::Import"); using namespace std::literals; size_t MaxBlockSize = Params["maxblocksize"sv].AsUInt64(RemoteStoreOptions::DefaultMaxBlockSize); size_t MaxChunkEmbedSize = Params["maxchunkembedsize"sv].AsUInt64(RemoteStoreOptions::DefaultMaxChunkEmbedSize); bool Force = Params["force"sv].AsBool(false); bool IgnoreMissingAttachments = Params["ignoremissingattachments"sv].AsBool(false); bool CleanOplog = Params["clean"].AsBool(false); CreateRemoteStoreResult RemoteStoreResult = CreateRemoteStore(Params, AuthManager, MaxBlockSize, MaxChunkEmbedSize, Oplog.TempPath()); if (RemoteStoreResult.Store == nullptr) { return {HttpResponseCode::BadRequest, RemoteStoreResult.Description}; } std::shared_ptr RemoteStore = std::move(RemoteStoreResult.Store); RemoteProjectStore::RemoteStoreInfo StoreInfo = RemoteStore->GetInfo(); CidStore& ChunkStore = Project.GetCidStore(); ZEN_INFO("Loading oplog '{}/{}' from {}", Project.Identifier, Oplog.OplogId(), StoreInfo.Description); JobId JobId = m_JobQueue.QueueJob( fmt::format("Import oplog '{}/{}'", Project.Identifier, Oplog.OplogId()), [this, ChunkStore = &ChunkStore, ActualRemoteStore = std::move(RemoteStore), OplogPtr = &Oplog, Force, IgnoreMissingAttachments, CleanOplog](JobContext& Context) { RemoteProjectStore::Result Result = LoadOplog(*ChunkStore, *ActualRemoteStore, *OplogPtr, Force, IgnoreMissingAttachments, CleanOplog, &Context); auto Response = ConvertResult(Result); ZEN_INFO("LoadOplog: Status: {} '{}'", ToString(Response.first), Response.second); if (!IsHttpSuccessCode(Response.first)) { throw JobError(Response.second.empty() ? fmt::format("Status: {}", ToString(Response.first)) : Response.second, (int)Response.first); } }); return {HttpResponseCode::Accepted, fmt::format("{}", JobId.Id)}; } bool ProjectStore::AreDiskWritesAllowed() const { return (m_DiskWriteBlocker == nullptr || m_DiskWriteBlocker->AreDiskWritesAllowed()); } void ProjectStore::EnableUpdateCapture() { ZEN_MEMSCOPE(GetProjectstoreTag()); m_ProjectsLock.WithExclusiveLock([&]() { if (m_UpdateCaptureRefCounter == 0) { ZEN_ASSERT(!m_CapturedProjects); m_CapturedProjects = std::make_unique>(); } else { ZEN_ASSERT(m_CapturedProjects); } m_UpdateCaptureRefCounter++; }); } void ProjectStore::DisableUpdateCapture() { m_ProjectsLock.WithExclusiveLock([&]() { ZEN_ASSERT(m_CapturedProjects); ZEN_ASSERT(m_UpdateCaptureRefCounter > 0); m_UpdateCaptureRefCounter--; if (m_UpdateCaptureRefCounter == 0) { m_CapturedProjects.reset(); } }); } std::vector ProjectStore::GetCapturedProjectsLocked() { ZEN_MEMSCOPE(GetProjectstoreTag()); if (m_CapturedProjects) { return *m_CapturedProjects; } return {}; } std::string ProjectStore::GetGcName(GcCtx&) { ZEN_MEMSCOPE(GetProjectstoreTag()); return fmt::format("projectstore: '{}'", m_ProjectBasePath.string()); } class ProjectStoreGcStoreCompactor : public GcStoreCompactor { public: ProjectStoreGcStoreCompactor(ProjectStore& ProjectStore, const std::filesystem::path& BasePath, std::vector&& OplogPathsToRemove, std::vector&& ProjectPathsToRemove) : m_ProjectStore(ProjectStore) , m_BasePath(BasePath) , m_OplogPathsToRemove(std::move(OplogPathsToRemove)) , m_ProjectPathsToRemove(std::move(ProjectPathsToRemove)) { } virtual void CompactStore(GcCtx& Ctx, GcCompactStoreStats& Stats, const std::function&) override { ZEN_TRACE_CPU("Store::CompactStore"); ZEN_MEMSCOPE(GetProjectstoreTag()); auto Log = [&Ctx]() { return Ctx.Logger; }; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [COMPACT] '{}': RemovedDisk: {} in {}", m_BasePath, NiceBytes(Stats.RemovedDisk), NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); size_t CompactOplogCount = 0; if (Ctx.Settings.IsDeleteMode) { for (const std::filesystem::path& OplogPath : m_OplogPathsToRemove) { uint64_t OplogSize = ProjectStore::Oplog::TotalSize(OplogPath); if (DeleteDirectories(OplogPath)) { ZEN_DEBUG("GCV2: projectstore [COMPACT] '{}': removed oplog folder '{}', removed {}", m_BasePath, OplogPath, NiceBytes(OplogSize)); Stats.RemovedDisk += OplogSize; } else { ZEN_WARN("GCV2: projectstore [COMPACT] '{}': Failed to remove oplog folder '{}'", m_BasePath, OplogPath); } } for (const std::filesystem::path& ProjectPath : m_ProjectPathsToRemove) { uint64_t ProjectSize = ProjectStore::Project::TotalSize(ProjectPath); if (DeleteDirectories(ProjectPath)) { ZEN_DEBUG("GCV2: projectstore [COMPACT] '{}': removed project folder '{}', removed {}", m_BasePath, ProjectPath, NiceBytes(ProjectSize)); Stats.RemovedDisk += ProjectSize; } else { ZEN_WARN("GCV2: projectstore [COMPACT] '{}': Failed to remove project folder '{}'", m_BasePath, ProjectPath); } } } for (auto ProjectIt : m_ProjectStore.m_Projects) { Ref Project = ProjectIt.second; std::vector OplogsToCompact = Project->GetOplogsToCompact(); CompactOplogCount += OplogsToCompact.size(); for (const std::string& OplogId : OplogsToCompact) { ProjectStore::Oplog* OpLog = nullptr; { RwLock::SharedLockScope __(Project->m_ProjectLock); if (auto OpIt = Project->m_Oplogs.find(OplogId); OpIt != Project->m_Oplogs.end()) { OpLog = OpIt->second.get(); } else { std::filesystem::path OplogBasePath = Project->BasePathForOplog(OplogId); OpLog = new ProjectStore::Oplog(OplogId, Project.Get(), Project->m_CidStore, OplogBasePath, std::filesystem::path{}); OpLog->Read(); } if (OpLog) { const uint64_t PreSize = OpLog->TotalSize(); OpLog->Compact(!Ctx.Settings.IsDeleteMode, /*RetainLSNs*/ true, fmt::format("GCV2: projectstore [COMPACT] '{}': ", m_BasePath)); const uint64_t PostSize = OpLog->TotalSize(); const uint64_t FreedSize = (PreSize > PostSize) ? (PreSize - PostSize) : 0; Stats.RemovedDisk += FreedSize; } if (auto OpIt = Project->m_Oplogs.find(OplogId); OpIt == Project->m_Oplogs.end()) { delete OpLog; } } } } if (!Ctx.Settings.IsDeleteMode) { ZEN_DEBUG("GCV2: projectstore [COMPACT] '{}': Skipped deleting of {} oplogs and {} projects, skipped compacting {} oplogs", m_BasePath, m_OplogPathsToRemove.size(), m_ProjectPathsToRemove.size(), CompactOplogCount); } m_ProjectPathsToRemove.clear(); m_OplogPathsToRemove.clear(); } virtual std::string GetGcName(GcCtx&) override { return fmt::format("projectstore: '{}'", m_BasePath.string()); } private: ProjectStore& m_ProjectStore; std::filesystem::path m_BasePath; std::vector m_OplogPathsToRemove; std::vector m_ProjectPathsToRemove; }; GcStoreCompactor* ProjectStore::RemoveExpiredData(GcCtx& Ctx, GcStats& Stats) { ZEN_TRACE_CPU("Store::RemoveExpiredData"); ZEN_MEMSCOPE(GetProjectstoreTag()); auto Log = [&Ctx]() { return Ctx.Logger; }; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [REMOVE EXPIRED] '{}': Count: {}, Expired: {}, Deleted: {} in {}", m_ProjectBasePath, Stats.CheckedCount, Stats.FoundCount, Stats.DeletedCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); std::vector OplogPathsToRemove; std::vector ProjectPathsToRemove; std::vector> ExpiredProjects; std::vector> Projects; DiscoverProjects(); { RwLock::SharedLockScope Lock(m_ProjectsLock); for (auto& Kv : m_Projects) { Stats.CheckedCount++; if (Kv.second->IsExpired(Ctx.Settings.ProjectStoreExpireTime)) { ExpiredProjects.push_back(Kv.second); continue; } Projects.push_back(Kv.second); } } size_t ExpiredOplogCount = 0; for (const Ref& Project : Projects) { if (Ctx.IsCancelledFlag) { break; } std::vector ExpiredOplogs; std::vector OpLogs = Project->ScanForOplogs(); for (const std::string& OplogId : OpLogs) { Stats.CheckedCount++; if (Project->IsExpired(Ctx.Settings.ProjectStoreExpireTime, OplogId)) { ExpiredOplogs.push_back(OplogId); } } std::filesystem::path ProjectPath = BasePathForProject(Project->Identifier); ExpiredOplogCount += ExpiredOplogs.size(); if (Ctx.Settings.IsDeleteMode) { for (const std::string& OplogId : ExpiredOplogs) { std::filesystem::path RemovePath; if (Project->RemoveOplog(OplogId, RemovePath)) { if (!RemovePath.empty()) { OplogPathsToRemove.push_back(RemovePath); } Stats.DeletedCount++; } } Project->Flush(); } } if (Ctx.Settings.IsDeleteMode) { for (const Ref& Project : ExpiredProjects) { std::string ProjectId = Project->Identifier; { { if (!Project->IsExpired(Ctx.Settings.ProjectStoreExpireTime)) { ZEN_DEBUG( "GCV2: projectstore [REMOVE EXPIRED] '{}': skipped garbage collect of project '{}'. Project no longer " "expired.", m_ProjectBasePath, ProjectId); continue; } } std::filesystem::path RemovePath; bool Success = RemoveProject(ProjectId, RemovePath); if (!Success) { ZEN_DEBUG( "GCV2: projectstore [REMOVE EXPIRED] '{}': skipped garbage collect of project '{}'. Project folder is locked.", m_ProjectBasePath, ProjectId); continue; } if (!RemovePath.empty()) { ProjectPathsToRemove.push_back(RemovePath); } } } Stats.DeletedCount += ExpiredProjects.size(); } size_t ExpiredProjectCount = ExpiredProjects.size(); Stats.FoundCount += ExpiredOplogCount + ExpiredProjectCount; return new ProjectStoreGcStoreCompactor(*this, m_ProjectBasePath, std::move(OplogPathsToRemove), std::move(ProjectPathsToRemove)); } class ProjectStoreReferenceChecker : public GcReferenceChecker { public: ProjectStoreReferenceChecker(ProjectStore& InProjectStore) : m_ProjectStore(InProjectStore) { m_ProjectStore.EnableUpdateCapture(); } virtual ~ProjectStoreReferenceChecker() { try { m_ProjectStore.DisableUpdateCapture(); } catch (const std::exception& Ex) { ZEN_ERROR("~ProjectStoreReferenceChecker threw exception: '{}'", Ex.what()); } } virtual std::string GetGcName(GcCtx&) override { return "projectstore"; } virtual void PreCache(GcCtx&) override {} virtual void UpdateLockedState(GcCtx& Ctx) override { ZEN_TRACE_CPU("Store::UpdateLockedState"); auto Log = [&Ctx]() { return Ctx.Logger; }; Stopwatch Timer; std::vector AddedOplogs; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [LOCKSTATE] '{}': found {} references in {} in {} new oplogs", "projectstore", m_References.size(), NiceTimeSpanMs(Timer.GetElapsedTimeMs()), AddedOplogs.size()); }); std::vector AddedProjects = m_ProjectStore.GetCapturedProjectsLocked(); for (const std::string& AddedProject : AddedProjects) { if (auto It = m_ProjectStore.m_Projects.find(AddedProject); It != m_ProjectStore.m_Projects.end()) { ProjectStore::Project& Project = *It->second; for (auto& OplogPair : Project.m_Oplogs) { ProjectStore::Oplog* Oplog = OplogPair.second.get(); AddedOplogs.push_back(Oplog); } } } for (auto& ProjectPair : m_ProjectStore.m_Projects) { ProjectStore::Project& Project = *ProjectPair.second; std::vector AddedOplogNames(Project.GetCapturedOplogsLocked()); for (const std::string& OplogName : AddedOplogNames) { if (auto It = Project.m_Oplogs.find(OplogName); It != Project.m_Oplogs.end()) { ProjectStore::Oplog* Oplog = It->second.get(); AddedOplogs.push_back(Oplog); } } } for (ProjectStore::Oplog* Oplog : AddedOplogs) { size_t BaseReferenceCount = m_References.size(); Stopwatch InnerTimer; const auto __ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [LOCKSTATE] '{}': found {} references in {} from {}", Oplog->m_BasePath, m_References.size() - BaseReferenceCount, NiceTimeSpanMs(InnerTimer.GetElapsedTimeMs()), Oplog->OplogId()); }); Oplog->GetAttachmentsLocked(m_References, Ctx.Settings.StoreProjectAttachmentMetaData); if (std::vector PendingChunkReferences = Oplog->GetPendingChunkReferencesLocked(); !PendingChunkReferences.empty()) { m_References.insert(m_References.end(), PendingChunkReferences.begin(), PendingChunkReferences.end()); } } FilterReferences(Ctx, fmt::format("projectstore [LOCKSTATE] '{}'", "projectstore"), m_References); } virtual std::span GetUnusedReferences(GcCtx& Ctx, std::span IoCids) override { ZEN_TRACE_CPU("Store::GetUnusedReferences"); auto Log = [&Ctx]() { return Ctx.Logger; }; size_t InitialCount = IoCids.size(); size_t UsedCount = InitialCount; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [FILTER REFERENCES] '{}': filtered out {} used references out of {} in {}", "projectstore", UsedCount, InitialCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); std::span UnusedReferences = KeepUnusedReferences(m_References, IoCids); UsedCount = IoCids.size() - UnusedReferences.size(); return UnusedReferences; } private: ProjectStore& m_ProjectStore; std::vector m_References; }; class ProjectStoreOplogReferenceChecker : public GcReferenceChecker { public: ProjectStoreOplogReferenceChecker(ProjectStore& InProjectStore, Ref InProject, std::string_view InOplog) : m_ProjectStore(InProjectStore) , m_Project(InProject) , m_OplogId(InOplog) { m_Project->EnableUpdateCapture(); } virtual ~ProjectStoreOplogReferenceChecker() { try { m_Project->DisableUpdateCapture(); RwLock::SharedLockScope _(m_Project->m_ProjectLock); if (auto It = m_Project->m_Oplogs.find(m_OplogId); It != m_Project->m_Oplogs.end()) { ProjectStore::Oplog* Oplog = It->second.get(); if (Oplog == m_OplogWithUpdateCapture) { Oplog->DisableUpdateCapture(); } } } catch (const std::exception& Ex) { ZEN_ERROR("~ProjectStoreOplogReferenceChecker threw exception: '{}'", Ex.what()); } } virtual std::string GetGcName(GcCtx&) override { return fmt::format("oplog: '{}/{}'", m_Project->Identifier, m_OplogId); } virtual void PreCache(GcCtx& Ctx) override { ZEN_TRACE_CPU("Store::Oplog::PreCache"); auto Log = [&Ctx]() { return Ctx.Logger; }; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [PRECACHE] '{}': precached {} references in {} from {}/{}", m_OplogBasePath, m_References.size(), NiceTimeSpanMs(Timer.GetElapsedTimeMs()), m_Project->Identifier, m_OplogId); }); ProjectStore::Oplog* Oplog = nullptr; auto __ = MakeGuard([this, &Oplog]() { if (Oplog != nullptr && m_OplogWithUpdateCapture == nullptr) { delete Oplog; } }); m_OplogBasePath = m_Project->BasePathForOplog(m_OplogId); RwLock::SharedLockScope ___(m_Project->m_ProjectLock); if (auto It = m_Project->m_Oplogs.find(m_OplogId); It != m_Project->m_Oplogs.end()) { It->second->EnableUpdateCapture(); Oplog = It->second.get(); m_OplogWithUpdateCapture = Oplog; } else if (ProjectStore::Oplog::ExistsAt(m_OplogBasePath)) { Oplog = new ProjectStore::Oplog(m_OplogId, m_Project.Get(), m_Project->m_CidStore, m_OplogBasePath, std::filesystem::path{}); Oplog->Read(); } else { return; } RwLock::SharedLockScope ____(Oplog->m_OplogLock); if (Ctx.IsCancelledFlag) { return; } GcClock::TimePoint CompactExpireTime = GcClock::Now() - std::chrono::minutes(30); if (!m_Project->IsOplogTouchedSince(CompactExpireTime, m_OplogId)) { const uint32_t CompactUnusedThreshold = 25; if (Oplog->GetUnusedSpacePercent() >= CompactUnusedThreshold) { m_Project->AddOplogToCompact(m_OplogId); } } Oplog->GetAttachmentsLocked(m_References, Ctx.Settings.StoreProjectAttachmentMetaData); m_OplogAccessTime = m_Project->LastOplogAccessTime(m_OplogId); FilterReferences(Ctx, fmt::format("projectstore [PRECACHE] '{}'", m_OplogBasePath), m_References); } virtual void UpdateLockedState(GcCtx& Ctx) override { ZEN_TRACE_CPU("Store::Oplog::UpdateLockedState"); auto Log = [&Ctx]() { return Ctx.Logger; }; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [LOCKSTATE] '{}': found {} references in {} from {}/{}", m_OplogBasePath, m_AddedReferences.size(), NiceTimeSpanMs(Timer.GetElapsedTimeMs()), m_Project->Identifier, m_OplogId); }); if (auto It = m_Project->m_Oplogs.find(m_OplogId); It != m_Project->m_Oplogs.end()) { ProjectStore::Oplog* Oplog = It->second.get(); Oplog->IterateCapturedLSNsLocked([&](const CbObjectView& UpdateOp) -> bool { UpdateOp.IterateAttachments([&](CbFieldView Visitor) { m_AddedReferences.emplace_back(Visitor.AsAttachment()); }); return true; }); std::vector AddedAttachments = Oplog->GetCapturedAttachmentsLocked(); m_AddedReferences.insert(m_AddedReferences.end(), AddedAttachments.begin(), AddedAttachments.end()); if (std::vector PendingChunkReferences = Oplog->GetPendingChunkReferencesLocked(); !PendingChunkReferences.empty()) { m_AddedReferences.insert(m_AddedReferences.end(), PendingChunkReferences.begin(), PendingChunkReferences.end()); } } else if (m_Project->LastOplogAccessTime(m_OplogId) > m_OplogAccessTime && ProjectStore::Oplog::ExistsAt(m_OplogBasePath)) { ProjectStore::Oplog* Oplog = new ProjectStore::Oplog(m_OplogId, m_Project.Get(), m_Project->m_CidStore, m_OplogBasePath, std::filesystem::path{}); auto __ = MakeGuard([Oplog]() { if (Oplog != nullptr) { delete Oplog; } }); Oplog->Read(); Oplog->GetAttachmentsLocked(m_AddedReferences, Ctx.Settings.StoreProjectAttachmentMetaData); } FilterReferences(Ctx, fmt::format("projectstore [LOCKSTATE] '{}'", m_OplogBasePath), m_AddedReferences); } virtual std::span GetUnusedReferences(GcCtx& Ctx, std::span IoCids) override { ZEN_TRACE_CPU("Store::Oplog::GetUnusedReferences"); auto Log = [&Ctx]() { return Ctx.Logger; }; const size_t InitialCount = IoCids.size(); size_t UsedCount = InitialCount; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [FILTER REFERENCES] '{}': filtered out {} used references out of {} in {} from {}/{}", m_OplogBasePath, UsedCount, InitialCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs()), m_Project->Identifier, m_OplogId); }); std::span UnusedReferences = KeepUnusedReferences(m_References, IoCids); UnusedReferences = KeepUnusedReferences(m_AddedReferences, UnusedReferences); UsedCount = IoCids.size() - UnusedReferences.size(); return UnusedReferences; } ProjectStore& m_ProjectStore; Ref m_Project; std::string m_OplogId; std::filesystem::path m_OplogBasePath; ProjectStore::Oplog* m_OplogWithUpdateCapture = nullptr; std::vector m_References; std::vector m_AddedReferences; GcClock::TimePoint m_OplogAccessTime; }; std::vector ProjectStore::CreateReferenceCheckers(GcCtx& Ctx) { ZEN_TRACE_CPU("Store::CreateReferenceCheckers"); auto Log = [&Ctx]() { return Ctx.Logger; }; size_t ProjectCount = 0; size_t OplogCount = 0; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } ZEN_INFO("GCV2: projectstore [CREATE CHECKERS] '{}': opened {} projects and {} oplogs in {}", m_ProjectBasePath, ProjectCount, OplogCount, NiceTimeSpanMs(Timer.GetElapsedTimeMs())); }); DiscoverProjects(); std::vector> Projects; std::vector Checkers; Checkers.emplace_back(new ProjectStoreReferenceChecker(*this)); { RwLock::SharedLockScope Lock(m_ProjectsLock); Projects.reserve(m_Projects.size()); for (auto& Kv : m_Projects) { Projects.push_back(Kv.second); } } ProjectCount += Projects.size(); try { for (const Ref& Project : Projects) { std::vector OpLogs = Project->ScanForOplogs(); Checkers.reserve(Checkers.size() + OpLogs.size()); for (const std::string& OpLogId : OpLogs) { Checkers.emplace_back(new ProjectStoreOplogReferenceChecker(*this, Project, OpLogId)); OplogCount++; } } } catch (const std::exception&) { while (!Checkers.empty()) { delete Checkers.back(); Checkers.pop_back(); } throw; } return Checkers; } std::vector ProjectStore::LockState(GcCtx& Ctx) { ZEN_TRACE_CPU("Store::LockState"); auto Log = [&Ctx]() { return Ctx.Logger; }; std::vector Locks; Locks.emplace_back(RwLock::SharedLockScope(m_ProjectsLock)); for (auto& ProjectIt : m_Projects) { std::vector ProjectLocks = ProjectIt.second->GetGcReferencerLocks(); for (auto It = std::make_move_iterator(ProjectLocks.begin()); It != std::make_move_iterator(ProjectLocks.end()); It++) { Locks.emplace_back(std::move(*It)); } } return Locks; } class ProjectStoreOplogReferenceValidator : public GcReferenceValidator { public: ProjectStoreOplogReferenceValidator(ProjectStore& InProjectStore, std::string_view InProject, std::string_view InOplog) : m_ProjectStore(InProjectStore) , m_ProjectId(InProject) , m_OplogId(InOplog) { } virtual ~ProjectStoreOplogReferenceValidator() {} virtual std::string GetGcName(GcCtx&) override { return fmt::format("oplog: '{}/{}'", m_ProjectId, m_OplogId); } virtual void Validate(GcCtx& Ctx, GcReferenceValidatorStats& Stats) override { ZEN_TRACE_CPU("Store::Validate"); auto Log = [&Ctx]() { return Ctx.Logger; }; ProjectStore::Oplog::ValidationResult Result; Stopwatch Timer; const auto _ = MakeGuard([&] { if (!Ctx.Settings.Verbose) { return; } std::string Status = Result.IsEmpty() ? "OK" : "Missing data"; ZEN_INFO("GCV2: projectstore [VALIDATE] '{}/{}': Validated in {}. OpCount: {}, MinLSN: {}, MaxLSN: {}, Status: {}", m_ProjectId, m_OplogId, NiceTimeSpanMs(Timer.GetElapsedTimeMs()), Result.OpCount, Result.LSNLow, Result.LSNHigh, Status); }); ProjectStore::Oplog* TempOplog = nullptr; auto __ = MakeGuard([this, &TempOplog]() { if (TempOplog != nullptr) { delete TempOplog; } }); ProjectStore::Oplog* Oplog = nullptr; Ref Project = m_ProjectStore.OpenProject(m_ProjectId); if (Project) { RwLock::SharedLockScope ___(Project->m_ProjectLock); if (auto It = Project->m_Oplogs.find(m_OplogId); It != Project->m_Oplogs.end()) { Oplog = It->second.get(); } else { std::filesystem::path OplogBasePath = Project->BasePathForOplog(m_OplogId); TempOplog = new ProjectStore::Oplog(m_OplogId, Project.Get(), Project->m_CidStore, OplogBasePath, std::filesystem::path{}); Oplog = TempOplog; Oplog->Read(); if (Ctx.IsCancelledFlag) { return; } } if (Oplog != nullptr) { Result = Oplog->Validate(Ctx.IsCancelledFlag, nullptr); if (Ctx.IsCancelledFlag) { return; } Stats.CheckedCount = Result.OpCount; Stats.MissingChunks = Result.MissingChunks.size(); Stats.MissingFiles = Result.MissingFiles.size(); Stats.MissingMetas = Result.MissingMetas.size(); Stats.MissingAttachments = Result.MissingAttachments.size(); } if (!Result.IsEmpty()) { ZEN_WARN("GCV2: projectstore [VALIDATE] '{}/{}': Missing data: Files: {}, Chunks: {}, Metas: {}, Attachments: {}", m_ProjectId, m_OplogId, Result.MissingFiles.size(), Result.MissingChunks.size(), Result.MissingMetas.size(), Result.MissingAttachments.size()); } } } ProjectStore& m_ProjectStore; std::string m_ProjectId; std::string m_OplogId; }; std::vector ProjectStore::CreateReferenceValidators(GcCtx& Ctx) { if (Ctx.Settings.SkipCidDelete) { return {}; } DiscoverProjects(); std::vector> Oplogs; { RwLock::SharedLockScope _(m_ProjectsLock); for (auto& ProjectPair : m_Projects) { ProjectStore::Project& Project = *ProjectPair.second; std::vector OpLogs = Project.ScanForOplogs(); for (const std::string& OplogName : OpLogs) { Oplogs.push_back({Project.Identifier, OplogName}); } } } std::vector Validators; Validators.reserve(Oplogs.size()); for (const std::pair& Oplog : Oplogs) { Validators.push_back(new ProjectStoreOplogReferenceValidator(*this, Oplog.first, Oplog.second)); } return Validators; } ////////////////////////////////////////////////////////////////////////// Oid OpKeyStringAsOid(std::string_view OpKey) { using namespace std::literals; CbObjectWriter Writer; Writer << "key"sv << OpKey; const Oid OpId = ComputeOpKey(Writer.Save()); return OpId; } ////////////////////////////////////////////////////////////////////////// #if ZEN_WITH_TESTS namespace testutils { using namespace std::literals; std::string OidAsString(const Oid& Id) { StringBuilder<25> OidStringBuilder; Id.ToString(OidStringBuilder); return OidStringBuilder.ToString(); } CbPackage CreateBulkDataOplogPackage(const Oid& Id, const std::span>& Attachments) { CbPackage Package; CbObjectWriter Object; Object << "key"sv << OidAsString(Id); if (!Attachments.empty()) { Object.BeginArray("bulkdata"); for (const auto& Attachment : Attachments) { CbAttachment Attach(Attachment.second, Attachment.second.DecodeRawHash()); Object.BeginObject(); Object << "id"sv << Attachment.first; Object << "type"sv << "Standard"sv; Object << "data"sv << Attach; Object.EndObject(); Package.AddAttachment(Attach); } Object.EndArray(); } Package.SetObject(Object.Save()); return Package; }; CbPackage CreateFilesOplogPackage(const Oid& Id, const std::filesystem::path ProjectRootDir, const std::span>& Attachments) { CbPackage Package; CbObjectWriter Object; Object << "key"sv << OidAsString(Id); if (!Attachments.empty()) { Object.BeginArray("files"); for (const auto& Attachment : Attachments) { std::filesystem::path ServerPath = std::filesystem::relative(Attachment.second, ProjectRootDir); std::filesystem::path ClientPath = ServerPath; // dummy Object.BeginObject(); Object << "id"sv << Attachment.first; Object << "serverpath"sv << ServerPath.string(); Object << "clientpath"sv << ClientPath.string(); Object.EndObject(); } Object.EndArray(); } Package.SetObject(Object.Save()); return Package; }; std::vector> CreateAttachments( const std::span& Sizes, OodleCompressionLevel CompressionLevel = OodleCompressionLevel::VeryFast, uint64_t BlockSize = 0) { std::vector> Result; Result.reserve(Sizes.size()); for (size_t Size : Sizes) { CompressedBuffer Compressed = CompressedBuffer::Compress(SharedBuffer(CreateSemiRandomBlob(Size)), OodleCompressor::Mermaid, CompressionLevel, BlockSize); Result.emplace_back(std::pair(Oid::NewOid(), Compressed)); } return Result; } uint64_t GetCompressedOffset(const CompressedBuffer& Buffer, uint64_t RawOffset) { if (RawOffset > 0) { uint64_t BlockSize = 0; OodleCompressor Compressor; OodleCompressionLevel CompressionLevel; if (!Buffer.TryGetCompressParameters(Compressor, CompressionLevel, BlockSize)) { return 0; } return BlockSize > 0 ? RawOffset % BlockSize : 0; } return 0; } template CbObject BuildChunksRequest(bool SkipData, std::string_view IdName, const std::vector& Chunks, const std::vector>& Ranges, const std::vector& ModTags) { CbObjectWriter Request; Request.BeginObject("Request"sv); { if (SkipData) { Request.AddBool("SkipData"sv, true); } if (!Chunks.empty()) { Request.BeginArray("Chunks"); for (size_t Index = 0; Index < Chunks.size(); Index++) { Request.BeginObject(); { Request << IdName << Chunks[Index]; if (!ModTags.empty()) { Request << "ModTag" << ModTags[Index]; } if (!Ranges.empty()) { Request << "Offset" << Ranges[Index].first; Request << "Size" << Ranges[Index].second; } } Request.EndObject(); } Request.EndArray(); } } Request.EndObject(); return Request.Save(); }; CbObject BuildChunksRequest(bool SkipData, const std::vector& Chunks, const std::vector>& Ranges, const std::vector& ModTags) { return BuildChunksRequest(SkipData, "Oid", Chunks, Ranges, ModTags); } CbObject BuildChunksRequest(bool SkipData, const std::vector& Chunks, const std::vector>& Ranges, const std::vector& ModTags) { return BuildChunksRequest(SkipData, "RawHash", Chunks, Ranges, ModTags); } ProjectStore::GetCidStoreFunc SingleChunkStore(CidStore& ChunkStore) { return [ChunkStore = &ChunkStore](std::string_view) -> CidStore& { return *ChunkStore; }; } } // namespace testutils TEST_CASE("project.opkeys") { using namespace std::literals; const std::string_view LongKey = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"sv; // Not a test per se, this code just exercises the key computation logic to ensure all // edge cases are handled by the bug workaround logic for (int i = 1; i < 300; ++i) { CbObjectWriter Cbo; Cbo << "key"sv << LongKey.substr(0, i); const Oid KeyId = ComputeOpKey(Cbo.Save()); } { CbObjectWriter Cbo; Cbo << "key"sv << "abcdef"; const Oid KeyId = ComputeOpKey(Cbo.Save()); const Oid CorrectId = Oid::FromHexString( "7a03540e" "ecb0daa9" "00f2949e"); CHECK(KeyId == CorrectId); } { CbObjectWriter Cbo; Cbo << "key"sv << "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; const Oid KeyId = ComputeOpKey(Cbo.Save()); const Oid CorrectId = Oid::FromHexString( "c5e88c79" "06b7fa38" "7b0d2efd"); CHECK(KeyId == CorrectId); } } TEST_CASE("project.store.create") { using namespace std::literals; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::string_view ProjectName("proj1"sv); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"; std::filesystem::path ProjectRootDir = TempDir.Path() / "game"; std::filesystem::path ProjectFilePath = TempDir.Path() / "game" / "game.uproject"; Ref Project(ProjectStore.NewProject(BasePath / ProjectName, ProjectName, RootDir.string(), EngineRootDir.string(), ProjectRootDir.string(), ProjectFilePath.string())); CHECK(ProjectStore.DeleteProject(ProjectName)); CHECK(!Project->Exists(BasePath)); } TEST_CASE("project.store.lifetimes") { using namespace std::literals; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"; std::filesystem::path ProjectRootDir = TempDir.Path() / "game"; std::filesystem::path ProjectFilePath = TempDir.Path() / "game" / "game.uproject"; Ref Project(ProjectStore.NewProject(BasePath / "proj1"sv, "proj1"sv, RootDir.string(), EngineRootDir.string(), ProjectRootDir.string(), ProjectFilePath.string())); ProjectStore::Oplog* Oplog = Project->NewOplog("oplog1", {}); CHECK(Oplog != nullptr); std::filesystem::path DeletePath; CHECK(Project->PrepareForDelete(DeletePath)); CHECK(!DeletePath.empty()); CHECK(Project->OpenOplog("oplog1", /*AllowCompact*/ false, /*VerifyPathOnDisk*/ true) == nullptr); // Oplog is now invalid, but pointer can still be accessed since we store old oplog pointers CHECK(Oplog->OplogCount() == 0); // Project is still valid since we have a Ref to it CHECK(Project->Identifier == "proj1"sv); } struct ExportForceDisableBlocksTrue_ForceTempBlocksFalse { static const bool ForceDisableBlocks = true; static const bool ForceEnableTempBlocks = false; }; struct ExportForceDisableBlocksFalse_ForceTempBlocksFalse { static const bool ForceDisableBlocks = false; static const bool ForceEnableTempBlocks = false; }; struct ExportForceDisableBlocksFalse_ForceTempBlocksTrue { static const bool ForceDisableBlocks = false; static const bool ForceEnableTempBlocks = true; }; TEST_CASE_TEMPLATE("project.store.export", Settings, ExportForceDisableBlocksTrue_ForceTempBlocksFalse, ExportForceDisableBlocksFalse_ForceTempBlocksFalse, ExportForceDisableBlocksFalse_ForceTempBlocksTrue) { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; ScopedTemporaryDirectory ExportDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"; std::filesystem::path ProjectRootDir = TempDir.Path() / "game"; std::filesystem::path ProjectFilePath = TempDir.Path() / "game" / "game.uproject"; Ref Project(ProjectStore.NewProject(BasePath / "proj1"sv, "proj1"sv, RootDir.string(), EngineRootDir.string(), ProjectRootDir.string(), ProjectFilePath.string())); ProjectStore::Oplog* Oplog = Project->NewOplog("oplog1", {}); CHECK(Oplog != nullptr); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{77}))); Oplog->AppendNewOplogEntry( CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{7123, 583, 690, 99}))); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{55, 122}))); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage( Oid::NewOid(), CreateAttachments(std::initializer_list{256u * 1024u, 92u * 1024u}, OodleCompressionLevel::None))); FileRemoteStoreOptions Options = { RemoteStoreOptions{.MaxBlockSize = 64u * 1024, .MaxChunkEmbedSize = 32 * 1024u, .ChunkFileSizeLimit = 64u * 1024u}, /*.FolderPath = */ ExportDir.Path(), /*.Name = */ std::string("oplog1"), /*OptionalBaseName = */ std::string(), /*.ForceDisableBlocks = */ Settings::ForceDisableBlocks, /*.ForceEnableTempBlocks = */ Settings::ForceEnableTempBlocks}; std::shared_ptr RemoteStore = CreateFileRemoteStore(Options); RemoteProjectStore::RemoteStoreInfo StoreInfo = RemoteStore->GetInfo(); RemoteProjectStore::Result ExportResult = SaveOplog(ChunkStore, *RemoteStore, *Project.Get(), *Oplog, Options.MaxBlockSize, Options.MaxChunkEmbedSize, Options.ChunkFileSizeLimit, true, false, false, nullptr); CHECK(ExportResult.ErrorCode == 0); ProjectStore::Oplog* OplogImport = Project->NewOplog("oplog2", {}); CHECK(OplogImport != nullptr); RemoteProjectStore::Result ImportResult = LoadOplog(ChunkStore, *RemoteStore, *OplogImport, /*Force*/ false, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ false, nullptr); CHECK(ImportResult.ErrorCode == 0); RemoteProjectStore::Result ImportForceResult = LoadOplog(ChunkStore, *RemoteStore, *OplogImport, /*Force*/ true, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ false, nullptr); CHECK(ImportForceResult.ErrorCode == 0); RemoteProjectStore::Result ImportCleanResult = LoadOplog(ChunkStore, *RemoteStore, *OplogImport, /*Force*/ false, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ true, nullptr); CHECK(ImportCleanResult.ErrorCode == 0); RemoteProjectStore::Result ImportForceCleanResult = LoadOplog(ChunkStore, *RemoteStore, *OplogImport, /*Force*/ true, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ true, nullptr); CHECK(ImportForceCleanResult.ErrorCode == 0); } TEST_CASE("project.store.gc") { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"; std::filesystem::path Project1RootDir = TempDir.Path() / "game1"; std::filesystem::path Project1FilePath = TempDir.Path() / "game1" / "game.uproject"; { CreateDirectories(Project1FilePath.parent_path()); BasicFile ProjectFile; ProjectFile.Open(Project1FilePath, BasicFile::Mode::kTruncate); } std::filesystem::path Project1OplogPath = TempDir.Path() / "game1" / "saves" / "cooked" / ".projectstore"; { CreateDirectories(Project1OplogPath.parent_path()); BasicFile OplogFile; OplogFile.Open(Project1OplogPath, BasicFile::Mode::kTruncate); } std::filesystem::path Project2RootDir = TempDir.Path() / "game2"; std::filesystem::path Project2FilePath = TempDir.Path() / "game2" / "game.uproject"; { CreateDirectories(Project2FilePath.parent_path()); BasicFile ProjectFile; ProjectFile.Open(Project2FilePath, BasicFile::Mode::kTruncate); } std::filesystem::path Project2Oplog1Path = TempDir.Path() / "game1" / "saves" / "cooked" / ".projectstore"; { CreateDirectories(Project2Oplog1Path.parent_path()); BasicFile OplogFile; OplogFile.Open(Project2Oplog1Path, BasicFile::Mode::kTruncate); } std::filesystem::path Project2Oplog2Path = TempDir.Path() / "game2" / "saves" / "cooked" / ".projectstore"; { CreateDirectories(Project2Oplog2Path.parent_path()); BasicFile OplogFile; OplogFile.Open(Project2Oplog2Path, BasicFile::Mode::kTruncate); } { Ref Project1(ProjectStore.NewProject(BasePath / "proj1"sv, "proj1"sv, RootDir.string(), EngineRootDir.string(), Project1RootDir.string(), Project1FilePath.string())); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1", Project1OplogPath); CHECK(Oplog != nullptr); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{77}))); Oplog->AppendNewOplogEntry( CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{7123, 583, 690, 99}))); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{55, 122}))); } { Ref Project2(ProjectStore.NewProject(BasePath / "proj2"sv, "proj2"sv, RootDir.string(), EngineRootDir.string(), Project2RootDir.string(), Project2FilePath.string())); { ProjectStore::Oplog* Oplog = Project2->NewOplog("oplog2", Project2Oplog1Path); CHECK(Oplog != nullptr); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{177}))); Oplog->AppendNewOplogEntry( CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{9123, 383, 590, 96}))); Oplog->AppendNewOplogEntry( CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{535, 221}))); } { ProjectStore::Oplog* Oplog = Project2->NewOplog("oplog3", Project2Oplog2Path); CHECK(Oplog != nullptr); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{137}))); Oplog->AppendNewOplogEntry( CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{9723, 683, 594, 98}))); Oplog->AppendNewOplogEntry( CreateBulkDataOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{531, 271}))); } } { GcSettings Settings = {.CacheExpireTime = GcClock::Now() - std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() - std::chrono::hours(24), .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(5u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(21u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(ProjectStore.OpenProject("proj1"sv)); CHECK(ProjectStore.OpenProject("proj2"sv)); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now() + std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() + std::chrono::hours(24), .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(5u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(21u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(ProjectStore.OpenProject("proj1"sv)); CHECK(ProjectStore.OpenProject("proj2"sv)); } RemoveFile(Project1FilePath); { GcSettings Settings = {.CacheExpireTime = GcClock::Now() - std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() - std::chrono::hours(24), .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(5u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(21u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(ProjectStore.OpenProject("proj1"sv)); CHECK(ProjectStore.OpenProject("proj2"sv)); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now() + std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() + std::chrono::hours(24), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(4u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(1u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(21u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(7u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(!ProjectStore.OpenProject("proj1"sv)); CHECK(ProjectStore.OpenProject("proj2"sv)); } RemoveFile(Project2Oplog1Path); { GcSettings Settings = {.CacheExpireTime = GcClock::Now() - std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() - std::chrono::hours(24), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(3u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(14u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(!ProjectStore.OpenProject("proj1"sv)); CHECK(ProjectStore.OpenProject("proj2"sv)); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now() + std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() + std::chrono::hours(24), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(3u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(14u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(0u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(!ProjectStore.OpenProject("proj1"sv)); CHECK(ProjectStore.OpenProject("proj2"sv)); } RemoveFile(Project2FilePath); { GcSettings Settings = {.CacheExpireTime = GcClock::Now() + std::chrono::hours(24), .ProjectStoreExpireTime = GcClock::Now() + std::chrono::hours(24), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); CHECK_EQ(1u, Result.ReferencerStatSum.RemoveExpiredDataStats.CheckedCount); CHECK_EQ(1u, Result.ReferencerStatSum.RemoveExpiredDataStats.DeletedCount); CHECK_EQ(14u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.CheckedCount); CHECK_EQ(14u, Result.ReferenceStoreStatSum.RemoveUnreferencedDataStats.DeletedCount); CHECK(!ProjectStore.OpenProject("proj1"sv)); CHECK(!ProjectStore.OpenProject("proj2"sv)); } } TEST_CASE("project.store.gc.prep") { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"; std::filesystem::path Project1RootDir = TempDir.Path() / "game1"; std::filesystem::path Project1FilePath = TempDir.Path() / "game1" / "game.uproject"; { CreateDirectories(Project1FilePath.parent_path()); BasicFile ProjectFile; ProjectFile.Open(Project1FilePath, BasicFile::Mode::kTruncate); } std::filesystem::path Project1OplogPath = TempDir.Path() / "game1" / "saves" / "cooked" / ".projectstore"; { CreateDirectories(Project1OplogPath.parent_path()); BasicFile OplogFile; OplogFile.Open(Project1OplogPath, BasicFile::Mode::kTruncate); } std::vector> OpAttachments = CreateAttachments(std::initializer_list{7123, 583, 690, 99}); std::vector OpChunkHashes; for (const auto& Chunk : OpAttachments) { OpChunkHashes.push_back(Chunk.second.DecodeRawHash()); } { Ref Project1(ProjectStore.NewProject(BasePath / "proj1"sv, "proj1"sv, RootDir.string(), EngineRootDir.string(), Project1RootDir.string(), Project1FilePath.string())); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, Project1OplogPath); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), OpAttachments)); } { Ref Project1 = ProjectStore.OpenProject("proj1"sv); Project1->DeleteOplog("oplog1"sv); } // Equivalent of a `prep` existance check call for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } // If a gc comes in between our prep and op write the chunks will be removed for (auto Attachment : OpAttachments) { CHECK(!ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { // Make sure the chunks are stored but not the referencing op Ref Project1 = ProjectStore.OpenProject("proj1"sv); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, Project1OplogPath); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), OpAttachments)); Project1->DeleteOplog("oplog1"sv); } { Ref Project1 = ProjectStore.OpenProject("proj1"sv); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, Project1OplogPath); // Equivalent of a `prep` call with tracking of ops CHECK(Oplog->CheckPendingChunkReferences(OpChunkHashes, std::chrono::hours(1)).empty()); } for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } // Attachments should now be retained for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } // Attachments should now be retained across multiple GCs if retain time is still valud for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { Ref Project1 = ProjectStore.OpenProject("proj1"sv); ProjectStore::Oplog* Oplog = Project1->OpenOplog("oplog1"sv, true, true); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), OpAttachments)); Oplog->RemovePendingChunkReferences(OpChunkHashes); CHECK(Oplog->GetPendingChunkReferencesLocked().size() == 0); } for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { Ref Project1 = ProjectStore.OpenProject("proj1"sv); Project1->DeleteOplog("oplog1"sv); } { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } for (auto Attachment : OpAttachments) { CHECK(!ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } { Ref Project1 = ProjectStore.OpenProject("proj1"sv); Project1->DeleteOplog("oplog1"sv); } { // Make sure the chunks are stored but not the referencing op Ref Project1 = ProjectStore.OpenProject("proj1"sv); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, Project1OplogPath); Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(Oid::NewOid(), OpAttachments)); Project1->DeleteOplog("oplog1"sv); } // Caution - putting breakpoints and stepping through this part of the test likely makes it fails due to expiry time of pending chunks { Ref Project1 = ProjectStore.OpenProject("proj1"sv); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, Project1OplogPath); CHECK(Oplog->CheckPendingChunkReferences(OpChunkHashes, std::chrono::milliseconds(100)).empty()); } // This pass they should be retained and while the ops are picked up in GC we are blocked from adding our op { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } Sleep(200); // This pass they should also be retained since our age retention has kept them alive and they will now be picked up and the retention // cleared { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } for (auto Attachment : OpAttachments) { CHECK(ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } // This pass the retention time has expired and the last GC pass cleared the entries { GcSettings Settings = {.CacheExpireTime = GcClock::Now(), .ProjectStoreExpireTime = GcClock::Now(), .CollectSmallObjects = true, .IsDeleteMode = true}; GcResult Result = Gc.CollectGarbage(Settings); } for (auto Attachment : OpAttachments) { CHECK(!ChunkStore.ContainsChunk(Attachment.second.DecodeRawHash())); } } TEST_CASE("project.store.rpc.getchunks") { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas"sv, .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"sv; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"sv; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"sv; std::filesystem::path Project1RootDir = TempDir.Path() / "game1"sv; std::filesystem::path Project1FilePath = TempDir.Path() / "game1"sv / "game.uproject"sv; { CreateDirectories(Project1FilePath.parent_path()); BasicFile ProjectFile; ProjectFile.Open(Project1FilePath, BasicFile::Mode::kTruncate); } std::vector OpIds; OpIds.insert(OpIds.end(), {Oid::NewOid(), Oid::NewOid(), Oid::NewOid(), Oid::NewOid()}); std::unordered_map>, Oid::Hasher> Attachments; Oid FilesOpId = Oid::NewOid(); std::vector> FilesOpIdAttachments; { Ref Project1(ProjectStore.NewProject(BasePath / "proj1"sv, "proj1"sv, RootDir.string(), EngineRootDir.string(), Project1RootDir.string(), Project1FilePath.string())); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, {}); CHECK(Oplog != nullptr); Attachments[OpIds[0]] = {}; Attachments[OpIds[1]] = CreateAttachments(std::initializer_list{77}); Attachments[OpIds[2]] = CreateAttachments(std::initializer_list{200 * 1024, 314 * 1024, 690, 99}, OodleCompressionLevel::VeryFast, 128 * 1024); Attachments[OpIds[3]] = CreateAttachments(std::initializer_list{55, 122}); for (auto It : Attachments) { Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(It.first, It.second)); } std::filesystem::path UncompressedFilePath = RootDir / "content" / "uncompressed_file"; CreateDirectories(UncompressedFilePath.parent_path()); IoBuffer FileBlob = CreateRandomBlob(81823 * 2); WriteFile(UncompressedFilePath, FileBlob); FilesOpIdAttachments.push_back({Oid::NewOid(), UncompressedFilePath}); Oplog->AppendNewOplogEntry(CreateFilesOplogPackage(FilesOpId, RootDir, FilesOpIdAttachments)); } // Invalid request { CbObjectWriter Request; Request.BeginObject("WrongName"sv); Request.EndObject(); CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, Request.Save(), Response); CHECK_EQ(HttpResponseCode::BadRequest, Result.first); } // Empty request { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, std::vector{}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(0, Chunks.Num()); } // Single non-existing chunk by RawHash IoHash NotFoundIoHash = IoHash::Max; { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {NotFoundIoHash}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(0, Chunks.Num()); } { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {NotFoundIoHash}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(0, Chunks.Num()); } // Single non-existing chunk by Id Oid NotFoundId = Oid::NewOid(); { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {NotFoundId}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(0, Chunks.Num()); } { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {NotFoundId}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(0, Chunks.Num()); } // Single existing chunk by RawHash { // Fresh fetch IoHash FirstAttachmentHash = Attachments[OpIds[2]][1].second.DecodeRawHash(); uint64_t ResponseModTag = 0; { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentHash}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); CHECK_EQ(FirstAttachmentHash, Id); ResponseModTag = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTag); IoHash AttachmentHash = Chunk["RawHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK_EQ(AttachmentHash, Buffer.DecodeRawHash()); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Fetch with matching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentHash}, {}, {ResponseModTag}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); CHECK_EQ(FirstAttachmentHash, Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Fetch with mismatching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks( "proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentHash}, {}, {uint64_t(ResponseModTag + 1)}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); CHECK_EQ(FirstAttachmentHash, Id); ResponseModTag = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTag); IoHash AttachmentHash = Chunk["RawHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK_EQ(AttachmentHash, Buffer.DecodeRawHash()); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Fresh modtime query { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentHash}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); CHECK_EQ(FirstAttachmentHash, Id); uint64_t ResponseModTag2 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag2); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Modtime query with matching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentHash}, {}, {ResponseModTag}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); CHECK_EQ(FirstAttachmentHash, Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Modtime query with mismatching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks( "proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentHash}, {}, {uint64_t(ResponseModTag + 1)}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); CHECK_EQ(FirstAttachmentHash, Id); uint64_t ResponseModTag2 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag2); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } // Single existing CID chunk by Id { Oid FirstAttachmentId = Attachments[OpIds[2]][1].first; uint64_t ResponseModTag = 0; { // Full chunk request CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); ResponseModTag = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTag); IoHash AttachmentHash = Chunk["RawHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK_EQ(AttachmentHash, Buffer.DecodeRawHash()); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } { // Partial chunk request CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {{130 * 1024, 8100}}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); ResponseModTag = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTag); IoHash AttachmentHash = Chunk["FragmentHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); std::uint64_t FragmentStart = Chunk["FragmentOffset"].AsUInt64(); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK(FragmentStart <= 130 * 1024); CHECK(FragmentStart + Buffer.DecodeRawSize() >= 130 * 1024 + 8100); auto ResponseDecompressedBuffer = Buffer.Decompress(130 * 1024 - FragmentStart, 8100); auto ExpectedDecompressedBuffer = Attachments[OpIds[2]][1].second.Decompress(130 * 1024, 8100); CHECK(ResponseDecompressedBuffer.AsIoBuffer().GetView().EqualBytes(ExpectedDecompressedBuffer.AsIoBuffer().GetView())); CHECK_EQ(Chunk["RawSize"sv].AsUInt64(), Attachments[OpIds[2]][1].second.DecodeRawSize()); CHECK(!Chunk.FindView("Size")); } { // Fetch with matching ModTag CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {}, {ResponseModTag}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } { // Fetch with mismatching ModTag CbPackage Response; auto Result = ProjectStore.GetChunks( "proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {}, {uint64_t(ResponseModTag + 1)}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); uint64_t ResponseModTag3 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag3); IoHash AttachmentHash = Chunk["RawHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK_EQ(AttachmentHash, Buffer.DecodeRawHash()); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Fresh modtime query { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentId}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); uint64_t ResponseModTag2 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag2); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Modtime query with matching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentId}, {}, {ResponseModTag}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Modtime query with mismatching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks( "proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentId}, {}, {uint64_t(ResponseModTag + 1)}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); uint64_t ResponseModTag2 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag2); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } // Single existing file chunk by Id { Oid FirstAttachmentId = FilesOpIdAttachments[0].first; uint64_t ResponseModTag = 0; { // Full chunk request CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); ResponseModTag = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTag); IoHash AttachmentHash = Chunk["Hash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompositeBuffer Buffer = Attachment->AsCompositeBinary(); CHECK_EQ(IoHash::HashBuffer(ReadFile(FilesOpIdAttachments[0].second).Flatten()), IoHash::HashBuffer(Buffer)); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } { // Partial chunk request CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {{81823, 5434}}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); ResponseModTag = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTag); IoHash AttachmentHash = Chunk["Hash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompositeBuffer Buffer = Attachment->AsCompositeBinary(); CHECK_EQ(IoHash::HashBuffer(IoBuffer(ReadFile(FilesOpIdAttachments[0].second).Flatten(), 81823, 5434)), IoHash::HashBuffer(Buffer)); CHECK_EQ(Chunk["Size"sv].AsUInt64(), FileSizeFromPath(FilesOpIdAttachments[0].second)); CHECK(!Chunk.FindView("RawSize")); } { // Fetch with matching ModTag CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {}, {ResponseModTag}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("Hash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } { // Fetch with mismatching ModTag CbPackage Response; auto Result = ProjectStore.GetChunks( "proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, {FirstAttachmentId}, {}, {uint64_t(ResponseModTag + 1)}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(1, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); uint64_t ResponseModTag3 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag3); IoHash AttachmentHash = Chunk["Hash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompositeBuffer Buffer = Attachment->AsCompositeBinary(); CHECK_EQ(IoHash::HashBuffer(ReadFile(FilesOpIdAttachments[0].second).Flatten()), IoHash::HashBuffer(Buffer)); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Fresh modtime query { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentId}, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); uint64_t ResponseModTag2 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag2); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Modtime query with matching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentId}, {}, {ResponseModTag}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("Hash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } // Modtime query with mismatching ModTag { CbPackage Response; auto Result = ProjectStore.GetChunks( "proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, {FirstAttachmentId}, {}, {uint64_t(ResponseModTag + 1)}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(1, Chunks.Num()); CbObjectView Chunk = (*begin(Chunks)).AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); CHECK_EQ(FirstAttachmentId, Id); uint64_t ResponseModTag2 = Chunk["ModTag"].AsUInt64(); CHECK_EQ(ResponseModTag, ResponseModTag2); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } // Multi RawHash Request { std::vector AttachmentBuffers{Attachments[OpIds[1]][0].second, Attachments[OpIds[2]][0].second, Attachments[OpIds[2]][1].second}; std::vector AttachmentHashes{AttachmentBuffers[0].DecodeRawHash(), AttachmentBuffers[1].DecodeRawHash(), AttachmentBuffers[2].DecodeRawHash()}; std::vector ResponseModTags(3, 0); { // Fresh fetch CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, AttachmentHashes, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(3, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); auto It = std::find(AttachmentHashes.begin(), AttachmentHashes.end(), Id); CHECK(It != AttachmentHashes.end()); ptrdiff_t Index = std::distance(AttachmentHashes.begin(), It); CHECK_EQ(AttachmentHashes[Index], Id); ResponseModTags[Index] = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTags[Index]); IoHash AttachmentHash = Chunk["RawHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK_EQ(AttachmentHash, Buffer.DecodeRawHash()); CHECK(AttachmentBuffers[Index].GetCompressed().Flatten().GetView().EqualBytes(Buffer.GetCompressed().Flatten().GetView())); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Fetch with matching ModTag CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, AttachmentHashes, {}, ResponseModTags), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); auto It = std::find(AttachmentHashes.begin(), AttachmentHashes.end(), Id); CHECK(It != AttachmentHashes.end()); ptrdiff_t Index = std::distance(AttachmentHashes.begin(), It); CHECK_EQ(AttachmentHashes[Index], Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Fresh modtime query CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, AttachmentHashes, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); auto It = std::find(AttachmentHashes.begin(), AttachmentHashes.end(), Id); CHECK(It != AttachmentHashes.end()); ptrdiff_t Index = std::distance(AttachmentHashes.begin(), It); CHECK_EQ(AttachmentHashes[Index], Id); CHECK_EQ(ResponseModTags[Index], Chunk["ModTag"].AsUInt64()); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Modtime query with matching ModTags CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, AttachmentHashes, {}, ResponseModTags), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); auto It = std::find(AttachmentHashes.begin(), AttachmentHashes.end(), Id); CHECK(It != AttachmentHashes.end()); ptrdiff_t Index = std::distance(AttachmentHashes.begin(), It); CHECK_EQ(AttachmentHashes[Index], Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Modtime query with mismatching ModTags std::vector MismatchingModTags(ResponseModTags); for (uint64_t& Tag : MismatchingModTags) { Tag++; } CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, AttachmentHashes, {}, MismatchingModTags), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); IoHash Id = Chunk["Id"].AsHash(); auto It = std::find(AttachmentHashes.begin(), AttachmentHashes.end(), Id); CHECK(It != AttachmentHashes.end()); ptrdiff_t Index = std::distance(AttachmentHashes.begin(), It); CHECK_EQ(AttachmentHashes[Index], Id); CHECK(Chunk["ModTag"].AsUInt64() == ResponseModTags[Index]); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } } // Multi Id Request { std::vector AttachmentBuffers{Attachments[OpIds[1]][0].second, Attachments[OpIds[2]][0].second, Attachments[OpIds[2]][1].second}; std::vector AttachmentHashes{AttachmentBuffers[0].DecodeRawHash(), AttachmentBuffers[1].DecodeRawHash(), AttachmentBuffers[2].DecodeRawHash()}; std::vector AttachedIds{Attachments[OpIds[1]][0].first, Attachments[OpIds[2]][0].first, Attachments[OpIds[2]][1].first}; std::vector ResponseModTags(3, 0); { // Fresh fetch CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, AttachedIds, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(3, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); auto It = std::find(AttachedIds.begin(), AttachedIds.end(), Id); CHECK(It != AttachedIds.end()); ptrdiff_t Index = std::distance(AttachedIds.begin(), It); CHECK_EQ(AttachedIds[Index], Id); ResponseModTags[Index] = Chunk["ModTag"].AsUInt64(); CHECK_NE(0, ResponseModTags[Index]); IoHash AttachmentHash = Chunk["RawHash"].AsHash(); const CbAttachment* Attachment = Response.FindAttachment(AttachmentHash); CHECK_NE(nullptr, Attachment); CompressedBuffer Buffer = Attachment->AsCompressedBinary(); CHECK_EQ(AttachmentHash, Buffer.DecodeRawHash()); CHECK(AttachmentBuffers[Index].GetCompressed().Flatten().GetView().EqualBytes(Buffer.GetCompressed().Flatten().GetView())); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Fetch with matching ModTag CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ false, AttachedIds, {}, ResponseModTags), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); auto It = std::find(AttachedIds.begin(), AttachedIds.end(), Id); CHECK(It != AttachedIds.end()); ptrdiff_t Index = std::distance(AttachedIds.begin(), It); CHECK_EQ(AttachedIds[Index], Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Fresh modtime query CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, AttachedIds, {}, {}), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); auto It = std::find(AttachedIds.begin(), AttachedIds.end(), Id); CHECK(It != AttachedIds.end()); ptrdiff_t Index = std::distance(AttachedIds.begin(), It); CHECK_EQ(AttachedIds[Index], Id); CHECK_EQ(ResponseModTags[Index], Chunk["ModTag"].AsUInt64()); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Modtime query with matching ModTags CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, AttachedIds, {}, ResponseModTags), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); auto It = std::find(AttachedIds.begin(), AttachedIds.end(), Id); CHECK(It != AttachedIds.end()); ptrdiff_t Index = std::distance(AttachedIds.begin(), It); CHECK_EQ(AttachedIds[Index], Id); CHECK(!Chunk.FindView("ModTag")); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } { // Modtime query with mismatching ModTags std::vector MismatchingModTags(ResponseModTags); for (uint64_t& Tag : MismatchingModTags) { Tag++; } CbPackage Response; auto Result = ProjectStore.GetChunks("proj1"sv, "oplog1"sv, testutils::BuildChunksRequest(/*SkipData*/ true, AttachedIds, {}, MismatchingModTags), Response); CHECK_EQ(HttpResponseCode::OK, Result.first); CHECK_EQ(0, Response.GetAttachments().size()); CbArrayView Chunks = Response.GetObject()["Chunks"].AsArrayView(); CHECK_EQ(3, Chunks.Num()); for (CbFieldView ChunkView : Chunks) { CbObjectView Chunk = ChunkView.AsObjectView(); Oid Id = Chunk["Id"].AsObjectId(); auto It = std::find(AttachedIds.begin(), AttachedIds.end(), Id); CHECK(It != AttachedIds.end()); ptrdiff_t Index = std::distance(AttachedIds.begin(), It); CHECK_EQ(AttachedIds[Index], Id); CHECK(Chunk["ModTag"].AsUInt64() == ResponseModTags[Index]); CHECK(!Chunk.FindView("RawHash")); CHECK(!Chunk.FindView("Size")); CHECK(!Chunk.FindView("RawSize")); } } } } TEST_CASE("project.store.partial.read") { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas"sv, .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"sv; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"sv; std::filesystem::path EngineRootDir = TempDir.Path() / "engine"sv; std::filesystem::path Project1RootDir = TempDir.Path() / "game1"sv; std::filesystem::path Project1FilePath = TempDir.Path() / "game1"sv / "game.uproject"sv; { CreateDirectories(Project1FilePath.parent_path()); BasicFile ProjectFile; ProjectFile.Open(Project1FilePath, BasicFile::Mode::kTruncate); } std::vector OpIds; OpIds.insert(OpIds.end(), {Oid::NewOid(), Oid::NewOid(), Oid::NewOid(), Oid::NewOid()}); std::unordered_map>, Oid::Hasher> Attachments; { Ref Project1(ProjectStore.NewProject(BasePath / "proj1"sv, "proj1"sv, RootDir.string(), EngineRootDir.string(), Project1RootDir.string(), Project1FilePath.string())); ProjectStore::Oplog* Oplog = Project1->NewOplog("oplog1"sv, {}); CHECK(Oplog != nullptr); Attachments[OpIds[0]] = {}; Attachments[OpIds[1]] = CreateAttachments(std::initializer_list{77}); Attachments[OpIds[2]] = CreateAttachments(std::initializer_list{7123, 9583, 690, 99}); Attachments[OpIds[3]] = CreateAttachments(std::initializer_list{55, 122}); for (auto It : Attachments) { Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(It.first, It.second)); } } { uint64_t ModificationTag = 0; IoBuffer Chunk; CHECK(ProjectStore .GetChunk("proj1"sv, "oplog1"sv, Attachments[OpIds[1]][0].second.DecodeRawHash().ToHexString(), Chunk, &ModificationTag) .first == HttpResponseCode::OK); IoHash RawHash; uint64_t RawSize; CompressedBuffer Attachment = CompressedBuffer::FromCompressed(SharedBuffer(Chunk), RawHash, RawSize); CHECK(RawSize == Attachments[OpIds[1]][0].second.DecodeRawSize()); CHECK(ModificationTag != 0); CHECK(ProjectStore .GetChunk("proj1"sv, "oplog1"sv, Attachments[OpIds[1]][0].second.DecodeRawHash().ToHexString(), Chunk, &ModificationTag) .first == HttpResponseCode::NotModified); } { uint64_t FullChunkModificationTag = 0; { CompositeBuffer ChunkResult; HttpContentType ContentType; CHECK(ProjectStore .GetChunkRange("proj1"sv, "oplog1"sv, OidAsString(Attachments[OpIds[2]][1].first), 0, ~0ull, HttpContentType::kCompressedBinary, ChunkResult, ContentType, &FullChunkModificationTag) .first == HttpResponseCode::OK); CHECK(ChunkResult); CHECK(CompressedBuffer::FromCompressedNoValidate(std::move(ChunkResult)).DecodeRawSize() == Attachments[OpIds[2]][1].second.DecodeRawSize()); } { CompositeBuffer ChunkResult; HttpContentType ContentType; CHECK(ProjectStore .GetChunkRange("proj1"sv, "oplog1"sv, OidAsString(Attachments[OpIds[2]][1].first), 0, ~0ull, HttpContentType::kCompressedBinary, ChunkResult, ContentType, &FullChunkModificationTag) .first == HttpResponseCode::NotModified); } } { CompositeBuffer PartialChunkResult; uint64_t PartialChunkModificationTag = 0; { CompositeBuffer ChunkResult; HttpContentType ContentType; CHECK(ProjectStore .GetChunkRange("proj1"sv, "oplog1"sv, OidAsString(Attachments[OpIds[2]][1].first), 5, 1773, HttpContentType::kCompressedBinary, PartialChunkResult, ContentType, &PartialChunkModificationTag) .first == HttpResponseCode::OK); CHECK(PartialChunkResult); IoHash PartialRawHash; uint64_t PartialRawSize; CompressedBuffer PartialCompressedResult = CompressedBuffer::FromCompressed(PartialChunkResult, PartialRawHash, PartialRawSize); CHECK(PartialRawSize >= 1773); uint64_t RawOffsetInPartialCompressed = GetCompressedOffset(PartialCompressedResult, 5); SharedBuffer PartialDecompressed = PartialCompressedResult.Decompress(RawOffsetInPartialCompressed); SharedBuffer FullDecompressed = Attachments[OpIds[2]][1].second.Decompress(); const uint8_t* FullDataPtr = &(reinterpret_cast(FullDecompressed.GetView().GetData())[5]); const uint8_t* PartialDataPtr = reinterpret_cast(PartialDecompressed.GetView().GetData()); CHECK(FullDataPtr[0] == PartialDataPtr[0]); } { CompositeBuffer ChunkResult; HttpContentType ContentType; CHECK(ProjectStore .GetChunkRange("proj1"sv, "oplog1"sv, OidAsString(Attachments[OpIds[2]][1].first), 5, 1773, HttpContentType::kCompressedBinary, PartialChunkResult, ContentType, &PartialChunkModificationTag) .first == HttpResponseCode::NotModified); } } } TEST_CASE("project.store.block") { using namespace std::literals; using namespace testutils; std::vector AttachmentSizes({7633, 6825, 5738, 8031, 7225, 566, 3656, 6006, 24, 3466, 1093, 4269, 2257, 3685, 3489, 7194, 6151, 5482, 6217, 3511, 6738, 5061, 7537, 2759, 1916, 8210, 2235, 4024, 1582, 5251, 491, 5464, 4607, 8135, 3767, 4045, 4415, 5007, 8876, 6761, 3359, 8526, 4097, 4855, 8225}); std::vector> AttachmentsWithId = CreateAttachments(AttachmentSizes); std::vector> Chunks; Chunks.reserve(AttachmentSizes.size()); for (const auto& It : AttachmentsWithId) { Chunks.push_back( std::make_pair(It.second.DecodeRawHash(), [Buffer = It.second](const IoHash&) -> std::pair { return {Buffer.DecodeRawSize(), Buffer}; })); } ChunkBlockDescription Block; CompressedBuffer BlockBuffer = GenerateChunkBlock(std::move(Chunks), Block); uint64_t HeaderSize; CHECK(IterateChunkBlock( BlockBuffer.Decompress(), [](CompressedBuffer&&, const IoHash&) {}, HeaderSize)); } TEST_CASE("project.store.iterateoplog") { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); OpenProcessCache ProcessCache; GcManager Gc; CidStore ChunkStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas"sv, .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; ChunkStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"sv; ProjectStore ProjectStore(testutils::SingleChunkStore(ChunkStore), BasePath, Gc, *JobQueue, ProcessCache, ProjectStore::Configuration{}); std::filesystem::path RootDir = TempDir.Path() / "root"sv; std::filesystem::path EngineRootDir = TempDir.Path() / "enginesv"; std::filesystem::path ProjectRootDir = TempDir.Path() / "game"sv; std::filesystem::path ProjectFilePath = TempDir.Path() / "game"sv / "game.uproject"sv; { CreateDirectories(ProjectFilePath.parent_path()); BasicFile ProjectFile; ProjectFile.Open(ProjectFilePath, BasicFile::Mode::kTruncate); } std::filesystem::path ProjectOplogPath = TempDir.Path() / "game"sv / "saves"sv / "cooked"sv / ".projectstore"sv; { CreateDirectories(ProjectOplogPath.parent_path()); BasicFile OplogFile; OplogFile.Open(ProjectOplogPath, BasicFile::Mode::kTruncate); } Ref TestProject(ProjectStore.NewProject(BasePath / "proj"sv, "proj"sv, RootDir.string(), EngineRootDir.string(), ProjectRootDir.string(), ProjectFilePath.string())); ProjectStore::Oplog* Oplog = TestProject->NewOplog("oplog"sv, ProjectOplogPath); CHECK(Oplog != nullptr); struct TestOidData { Oid KeyAsOidNotOplogId = Oid::NewOid(); std::string Key = KeyAsOidNotOplogId.ToString(); bool bFound = false; }; constexpr int NumTestOids = 4; TestOidData TestOids[NumTestOids]; for (const TestOidData& TestOid : TestOids) { Oplog->AppendNewOplogEntry(CreateBulkDataOplogPackage(TestOid.KeyAsOidNotOplogId, {})); } int Count = 0; auto ResetTest = [&Count, &TestOids]() { Count = 0; for (TestOidData& TestOid : TestOids) { TestOid.bFound = false; } }; auto IncrementCount = [&Count](CbObjectView /* Op */) { ++Count; }; auto MarkFound = [&TestOids, &Count](uint32_t /* LSN */, const Oid& /* InId */, CbObjectView Op) { for (TestOidData& TestOid : TestOids) { if (Op["key"sv].AsString() == TestOid.Key) { TestOid.bFound = true; ++Count; } } }; // Tests of IterateOpLog and IterateOplogWithKey, with various Paging arguments { ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{}); CHECK(Count == NumTestOids); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{}); CHECK(Count == NumTestOids); ResetTest(); Oplog->IterateOplogWithKey(MarkFound); CHECK(Count == NumTestOids); Count = 0; for (int Start = 0; Start < NumTestOids; ++Start) { for (int Size = 0; Size < NumTestOids - Start; ++Size) { ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{Start, Size}); CHECK(Count == Size); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{Start, Size}); CHECK(Count == Size); } // Out of range Size arguments ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{Start, -1}); CHECK(Count == NumTestOids - Start); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{Start, NumTestOids * 2}); CHECK(Count == NumTestOids - Start); } // Out of range Start arguments for (int Size = 0; Size < NumTestOids; ++Size) { ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{-1, Size}); CHECK(Count == Size); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{-1, Size}); CHECK(Count == Size); ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{NumTestOids, Size}); CHECK(Count == 0); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{NumTestOids, Size}); CHECK(Count == 0); } // Out of range Start and Size arguments ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{-1, -1}); CHECK(Count == NumTestOids); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{-1, -1}); CHECK(Count == NumTestOids); ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{-1, 2 * NumTestOids}); CHECK(Count == NumTestOids); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{-1, 2 * NumTestOids}); CHECK(Count == NumTestOids); ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{NumTestOids, -1}); CHECK(Count == 0); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{NumTestOids, -1}); CHECK(Count == 0); ResetTest(); Oplog->IterateOplog(IncrementCount, ProjectStore::Oplog::Paging{NumTestOids, 2 * NumTestOids}); CHECK(Count == 0); ResetTest(); Oplog->IterateOplogWithKey(MarkFound, ProjectStore::Oplog::Paging{NumTestOids, 2 * NumTestOids}); CHECK(Count == 0); } } #endif void prj_forcelink() { } } // namespace zen