// 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 "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 { namespace { bool PrepareDirectoryDelete(const std::filesystem::path& Dir, std::filesystem::path& OutDeleteDir) { std::filesystem::path DroppedBucketPath; do { if (!std::filesystem::exists(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 (std::filesystem::exists(DroppedBucketPath)) { if (!DeleteDirectories(DroppedBucketPath)) { ZEN_INFO("Drop directory '{}' for '{}' already exists but could not be removed, attempting different name.", DroppedBucketPath, Dir); continue; } if (std::filesystem::exists(DroppedBucketPath)) { ZEN_INFO("Drop directory '{}' for '{}' still exists after remove, attempting different name.", DroppedBucketPath, Dir); continue; } } int RenameAttempt = 0; do { std::error_code Ec; std::filesystem::rename(Dir, DroppedBucketPath, Ec); if (!Ec) { OutDeleteDir = DroppedBucketPath; return true; } if (std::filesystem::exists(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) { 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::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, ForceDisableBlocks, ForceDisableTempBlocks, AssumeHttp2}; RemoteStore = CreateJupiterRemoteStore(Options, TempFilePath); } 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 (!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); } // namespace ////////////////////////////////////////////////////////////////////////// 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 std::filesystem::exists(GetLogPath(BasePath)) && std::filesystem::exists(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; std::filesystem::remove(GetLogPath(), Ec); std::filesystem::remove(GetBlobsPath(), Ec); } static bool Delete(const std::filesystem::path& BasePath) { return DeleteDirectories(BasePath); } uint64_t OpBlobsSize() const { return std::filesystem::file_size(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 std::filesystem::file_size(GetLogPath(BasePath)) + std::filesystem::file_size(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_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 { 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_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(); std::filesystem::rename(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 std::filesystem::remove(OpBlobs.GetPath(), Ec); std::filesystem::remove(GetLogPath(), Ec); std::filesystem::remove(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*/) { std::filesystem::remove(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_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_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_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) { 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); XXH3_128Stream KeyHasher; Core["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); }); XXH3_128 KeyHash128 = KeyHasher.GetHash(); memcpy(&OpData.KeyHash, KeyHash128.Hash, sizeof OpData.KeyHash); return OpData; } OplogEntry AppendOp(const AppendOpData& OpData) { 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_Oplog.Append(Entry); m_OpBlobs.Write(OpData.Buffer.GetData(), WriteSize, WriteOffset); return Entry; } std::vector AppendOps(std::span Ops) { 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) { 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; std::filesystem::remove(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); } ProjectStore::Oplog::~Oplog() { if (m_Storage) { Flush(); } } void ProjectStore::Oplog::Flush() { ZEN_TRACE_CPU("Oplog::Flush"); RwLock::SharedLockScope Lock(m_OplogLock); ZEN_ASSERT(m_Storage); m_Storage->Flush(); if (!m_MetaValid) { std::error_code DummyEc; std::filesystem::remove(m_MetaPath, DummyEc); } uint64_t LogCount = m_Storage->LogCount(); if (m_LogFlushPosition != LogCount) { WriteIndexSnapshot(); } } void ProjectStore::Oplog::ScrubStorage(ScrubContext& Ctx) { 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()); }); { XXH3_128Stream KeyHasher; Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); }); XXH3_128 KeyHash128 = KeyHasher.GetHash(); Oid KeyHash; memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash); 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) { using namespace std::literals; uint64_t Size = OplogStorage::OpsSize(BasePath); std::filesystem::path StateFilePath = BasePath / "oplog.zcb"sv; if (std::filesystem::exists(StateFilePath)) { Size += std::filesystem::file_size(StateFilePath); } std::filesystem::path MetaFilePath = BasePath / "ops.meta"sv; if (std::filesystem::exists(MetaFilePath)) { Size += std::filesystem::file_size(MetaFilePath); } std::filesystem::path IndexFilePath = BasePath / "ops.zidx"sv; if (std::filesystem::exists(IndexFilePath)) { Size += std::filesystem::file_size(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) { RwLock::ExclusiveLockScope _(m_OplogLock); 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 std::filesystem::is_regular_file(StateFilePath); } void ProjectStore::Oplog::Read() { 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; std::filesystem::remove(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 OplogEntryMapping::Mapping& Chunk : OpMapping.Chunks) { m_ChunkMap.insert_or_assign(Chunk.Id, Chunk.Hash); } for (const OplogEntryMapping::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 OplogEntryMapping::Mapping& 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() { 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() { 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); 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_TRACE_CPU("Oplog::ReadStateFile"); using namespace std::literals; std::filesystem::path StateFilePath = BasePath / "oplog.zcb"sv; if (std::filesystem::is_regular_file(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 '{}'", int(ValidationError), StateFilePath); return CbObject(); } return LoadCompactBinaryObject(Obj); } ZEN_INFO("config for oplog not found at '{}'. Assuming legacy store", StateFilePath); return {}; } void ProjectStore::Oplog::WriteIndexSnapshot() { 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; fs::path IndexPath = m_BasePath / "ops.zidx"; fs::path TempIndexPath = m_BasePath / "ops.zidx.tmp"; // Move index away, we keep it if something goes wrong if (fs::is_regular_file(TempIndexPath)) { std::error_code Ec; if (!fs::remove(TempIndexPath, Ec) || Ec) { ZEN_WARN("oplog '{}/{}': snapshot failed to clean up temp snapshot at {}, reason: '{}'", GetOuterProject()->Identifier, m_OplogId, TempIndexPath, Ec.message()); return; } } try { if (fs::is_regular_file(IndexPath)) { fs::rename(IndexPath, TempIndexPath); } // 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 += sizeof(OplogIndexHeader); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(LSNEntries.data(), LSNEntries.size() * sizeof(uint32_t), Offset); Offset += LSNEntries.size() * sizeof(uint32_t); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(Keys.data(), Keys.size() * sizeof(Oid), Offset); Offset += Keys.size() * sizeof(Oid); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(AddressMapEntries.data(), AddressMapEntries.size() * sizeof(OplogEntryAddress), Offset); Offset += AddressMapEntries.size() * sizeof(OplogEntryAddress); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(LatestOpMapEntries.data(), LatestOpMapEntries.size() * sizeof(uint32_t), Offset); Offset += LatestOpMapEntries.size() * sizeof(uint32_t); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(ChunkMapEntries.data(), ChunkMapEntries.size() * sizeof(IoHash), Offset); Offset += ChunkMapEntries.size() * sizeof(IoHash); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(MetaMapEntries.data(), MetaMapEntries.size() * sizeof(IoHash), Offset); Offset += MetaMapEntries.size() * sizeof(IoHash); Offset = RoundUp(Offset, OplogIndexHeader::DataAlignment); IndexFile.Write(FilePathLengths.data(), FilePathLengths.size() * sizeof(uint32_t), Offset); Offset += FilePathLengths.size() * sizeof(uint32_t); Offset = RoundUp(Offset, 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("Failed to move temp file '{}' to '{}'", ObjectIndexFile.GetPath(), IndexPath)); } EntryCount = LSNEntries.size(); m_LogFlushPosition = IndexLogPosition; } catch (const std::exception& Err) { ZEN_WARN("oplog '{}/{}': snapshot FAILED, reason: '{}'", m_OuterProject->Identifier, m_OplogId, Err.what()); // Restore any previous snapshot if (fs::is_regular_file(TempIndexPath)) { std::error_code Ec; fs::remove(IndexPath, Ec); // We don't care if this fails, we try to move the old temp file regardless fs::rename(TempIndexPath, IndexPath, Ec); if (Ec) { ZEN_WARN("oplog '{}/{}': snapshot failed to restore old snapshot from {}, reason: '{}'", m_OuterProject->Identifier, m_OplogId, TempIndexPath, Ec.message()); } } } if (fs::is_regular_file(TempIndexPath)) { std::error_code Ec; if (!fs::remove(TempIndexPath, Ec) || Ec) { ZEN_WARN("oplog '{}/{}': snapshot failed to remove temporary file {}, reason: '{}'", m_OuterProject->Identifier, m_OplogId, TempIndexPath, Ec.message()); } } } void ProjectStore::Oplog::ReadIndexSnapshot() { ZEN_TRACE_CPU("Oplog::ReadIndexSnapshot"); std::filesystem::path IndexPath = m_BasePath / "ops.zidx"; if (std::filesystem::is_regular_file(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_view { 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_view((const char*)StringData.GetData(), Length); }); for (uint64_t FileLengthOffset = 0; FileLengthOffset < FilePathLengths.size();) { std::string_view ServerPath = ReadString(FilePathLengths[FileLengthOffset++]); std::string_view ClientPath = ReadString(FilePathLengths[FileLengthOffset++]); m_FileMap.insert_or_assign( Keys[KeyOffset++], FileMapEntry{.ServerPath = std::string(ServerPath), .ClientPath = std::string(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 { 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) { 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, const std::function& AsyncCallback, WorkerThreadPool* OptionalWorkerPool) { return m_CidStore.IterateChunks(RawHashes, AsyncCallback, OptionalWorkerPool); } bool ProjectStore::Oplog::IterateChunks(std::span ChunkIds, const std::function& AsyncCallback, WorkerThreadPool* OptionalWorkerPool) { 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); } } } m_CidStore.IterateChunks( CidChunkHashes, [&](size_t Index, const IoBuffer& Payload) { return AsyncCallback(CidChunkIndexes[Index], Payload); }, OptionalWorkerPool); if (OptionalWorkerPool) { std::atomic_bool Result = true; Latch WorkLatch(1); for (size_t ChunkIndex = 0; ChunkIndex < FileChunkIndexes.size(); ChunkIndex++) { if (Result.load() == false) { break; } WorkLatch.AddCount(1); OptionalWorkerPool->ScheduleWork([this, &WorkLatch, ChunkIndex, &FileChunkIndexes, &FileChunkPaths, &AsyncCallback, &Result]() { auto _ = MakeGuard([&WorkLatch]() { WorkLatch.CountDown(); }); if (Result.load() == false) { return; } size_t FileChunkIndex = FileChunkIndexes[ChunkIndex]; const std::filesystem::path& FilePath = FileChunkPaths[ChunkIndex]; try { IoBuffer Payload = IoBufferBuilder::MakeFromFile(FilePath); if (Payload) { if (!AsyncCallback(FileChunkIndex, Payload)) { Result.store(false); } } } 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()); } }); } WorkLatch.CountDown(); WorkLatch.Wait(); return Result.load(); } else { 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); if (!Result) { return false; } } } } return true; } IoBuffer ProjectStore::Oplog::FindChunk(const Oid& ChunkId) { 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(); return m_CidStore.FindChunkByCid(ChunkHash); } if (auto FileIt = m_FileMap.find(ChunkId); FileIt != m_FileMap.end()) { std::filesystem::path FilePath = m_OuterProject->RootDir / FileIt->second.ServerPath; OplogLock.ReleaseNow(); return IoBufferBuilder::MakeFromFile(FilePath); } if (auto MetaIt = m_MetaMap.find(ChunkId); MetaIt != m_MetaMap.end()) { IoHash ChunkHash = MetaIt->second; OplogLock.ReleaseNow(); return m_CidStore.FindChunkByCid(ChunkHash); } return {}; } std::vector ProjectStore::Oplog::GetAllChunksInfo() { // 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)) { 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); } void ProjectStore::Oplog::IterateOplogLocked(std::function&& Handler, const Paging& EntryPaging) { 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 = Entries; int32_t Size = int32_t(Entries.size()); int32_t Start = std::clamp(EntryPaging.Start, 0, Size); int32_t End = std::clamp(EntryPaging.Start + EntryPaging.Count, 0, Size); EntrySpan = EntrySpan.subspan(Start, End - Start); 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_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) { XXH3_128Stream KeyHasher; Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); }); XXH3_128 KeyHash128 = KeyHasher.GetHash(); Oid KeyHash; memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash); 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) { 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]); } } size_t EntryIndex = 0; m_Storage->ReplayLogEntries(SortedEntries, [&](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::CaptureUpdatedLSNs(std::span LSNs) { m_UpdateCaptureLock.WithExclusiveLock([&]() { if (m_CapturedLSNs) { m_CapturedLSNs->reserve(m_CapturedLSNs->size() + LSNs.size()); m_CapturedLSNs->insert(m_CapturedLSNs->end(), LSNs.begin(), LSNs.end()); } }); } void ProjectStore::Oplog::CaptureAddedAttachments(std::span AttachmentHashes) { m_UpdateCaptureLock.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() { m_UpdateCaptureLock.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() { m_UpdateCaptureLock.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::IterateCapturedLSNs(std::function&& Callback) { m_UpdateCaptureLock.WithExclusiveLock([&]() { 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::GetCapturedAttachments() { RwLock::SharedLockScope _(m_UpdateCaptureLock); if (m_CapturedAttachments) { return *m_CapturedAttachments; } return {}; } 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) { 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(OplogEntryMapping::Mapping{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(OplogEntryMapping::Mapping{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(OplogEntryMapping::Mapping{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(OplogEntryMapping::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(OplogEntryMapping::Mapping{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) { // 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 OplogEntryMapping::Mapping& Chunk : OpMapping.Chunks) { AddChunkMapping(OplogLock, Chunk.Id, Chunk.Hash); } for (const OplogEntryMapping::FileMapping& File : OpMapping.Files) { AddFileMapping(OplogLock, File.Id, File.Hash, File.ServerPath, File.ClientPath); } for (const OplogEntryMapping::Mapping& 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_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() { RefPtr Storage; { RwLock::SharedLockScope _(m_OplogLock); Storage = m_Storage; } return Storage; } uint32_t ProjectStore::Oplog::AppendNewOplogEntry(CbObjectView Core) { 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); CaptureUpdatedLSNs(std::array({EntryId})); m_MetaValid = false; return EntryId; } std::vector ProjectStore::Oplog::AppendNewOplogEntries(std::span Cores) { 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); for (size_t OpIndex = 0; OpIndex < OpCount; OpIndex++) { EntryIds[OpIndex] = RegisterOplogEntry(OplogLock, Mappings[OpIndex], OpEntries[OpIndex]); } } CaptureUpdatedLSNs(EntryIds); 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 std::filesystem::exists(BasePath / "Project.zcb"); } void ProjectStore::Project::Read() { 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 '{}'", int(ValidationError), ProjectStateFilePath); } ReadAccessTimes(); } void ProjectStore::Project::Write() { 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() { using namespace std::literals; std::filesystem::path ProjectAccessTimesFilePath = m_OplogStoragePath / "AccessTimes.zcb"sv; if (!std::filesystem::exists(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, int(ValidationError), ProjectAccessTimesFilePath); } } void ProjectStore::Project::WriteAccessTimes() { using namespace std::literals; CbObjectWriter Writer; { 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) { 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(); m_UpdateCaptureLock.WithExclusiveLock([&]() { 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) { ZEN_TRACE_CPU("Store::OpenOplog"); { RwLock::SharedLockScope _(m_ProjectLock); auto OplogIt = m_Oplogs.find(std::string(OplogId)); if (OplogIt != m_Oplogs.end()) { 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) { 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) { { 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) { 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 { RwLock::SharedLockScope _(m_ProjectLock); std::vector Oplogs; if (Project::Exists(m_OplogStoragePath)) { DirectoryContent DirContent; GetDirectoryContent(m_OplogStoragePath, DirectoryContent::IncludeDirsFlag, DirContent); Oplogs.reserve(DirContent.Directories.size()); for (const std::filesystem::path& DirPath : DirContent.Directories) { Oplogs.push_back(DirPath.filename().string()); } } return Oplogs; } void ProjectStore::Project::IterateOplogs(std::function&& Fn) const { RwLock::SharedLockScope Lock(m_ProjectLock); for (auto& Kv : m_Oplogs) { Fn(Lock, *Kv.second); } } void ProjectStore::Project::IterateOplogs(std::function&& Fn) { RwLock::SharedLockScope Lock(m_ProjectLock); for (auto& Kv : m_Oplogs) { Fn(Lock, *Kv.second); } } void ProjectStore::Project::Flush() { 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::ScrubStorage(ScrubContext& Ctx) { // Scrubbing needs to check all existing oplogs std::vector OpLogs = ScanForOplogs(); for (const std::string& OpLogId : OpLogs) { OpenOplog(OpLogId, /*AllowCompact*/ false); } IterateOplogs([&](const RwLock::SharedLockScope&, Oplog& Ops) { if (!IsExpired(GcClock::TimePoint::min(), Ops)) { Ops.ScrubStorage(Ctx); } }); } uint64_t ProjectStore::Project::TotalSize(const std::filesystem::path& BasePath) { using namespace std::literals; uint64_t Size = 0; std::filesystem::path AccessTimesFilePath = BasePath / "AccessTimes.zcb"sv; if (std::filesystem::exists(AccessTimesFilePath)) { Size += std::filesystem::file_size(AccessTimesFilePath); } std::filesystem::path ProjectFilePath = BasePath / "Project.zcb"sv; if (std::filesystem::exists(ProjectFilePath)) { Size += std::filesystem::file_size(ProjectFilePath); } return Size; } uint64_t ProjectStore::Project::TotalSize() const { 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) { 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() { m_UpdateCaptureLock.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() { m_UpdateCaptureLock.WithExclusiveLock([&]() { ZEN_ASSERT(m_CapturedOplogs); ZEN_ASSERT(m_UpdateCaptureRefCounter > 0); m_UpdateCaptureRefCounter--; if (m_UpdateCaptureRefCounter == 0) { m_CapturedOplogs.reset(); } }); } std::vector ProjectStore::Project::GetCapturedOplogs() { RwLock::SharedLockScope _(m_UpdateCaptureLock); if (m_CapturedOplogs) { return *m_CapturedOplogs; } return {}; } std::vector ProjectStore::Project::GetGcReferencerLocks() { 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 { if (!MarkerPath.empty()) { std::error_code Ec; if (std::filesystem::exists(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 { 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() { RwLock::ExclusiveLockScope _(m_LastAccessTimesLock); m_LastAccessTimes.insert_or_assign(std::string(), GcClock::TickCount()); } void ProjectStore::Project::TouchOplog(std::string_view Oplog) { 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(CidStore& Store, std::filesystem::path BasePath, GcManager& Gc, JobQueue& JobQueue, const Configuration& Config) : m_Log(logging::Get("project")) , m_Gc(Gc) , m_CidStore(Store) , m_JobQueue(JobQueue) , 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() { if (!std::filesystem::exists(m_ProjectBasePath)) { return; } DirectoryContent DirContent; GetDirectoryContent(m_ProjectBasePath, DirectoryContent::IncludeDirsFlag, 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_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); Latch WorkLatch(1); for (const Ref& Project : Projects) { WorkLatch.AddCount(1); WorkerPool.ScheduleWork([&WorkLatch, Project]() { auto _ = MakeGuard([&WorkLatch]() { WorkLatch.CountDown(); }); Project->Flush(); }); } WorkLatch.CountDown(); WorkLatch.Wait(); } void ProjectStore::ScrubStorage(ScrubContext& Ctx) { 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->ScrubStorage(Ctx); } } GcStorageSize ProjectStore::StorageSize() const { ZEN_TRACE_CPU("Store::StorageSize"); using namespace std::literals; GcStorageSize Result; { if (std::filesystem::exists(m_ProjectBasePath)) { DirectoryContent ProjectsFolderContent; GetDirectoryContent(m_ProjectBasePath, DirectoryContent::IncludeDirsFlag, ProjectsFolderContent); for (const std::filesystem::path& ProjectBasePath : ProjectsFolderContent.Directories) { std::filesystem::path ProjectStateFilePath = ProjectBasePath / "Project.zcb"sv; if (std::filesystem::exists(ProjectStateFilePath)) { Result.DiskSize += Project::TotalSize(ProjectBasePath); DirectoryContent DirContent; GetDirectoryContent(ProjectBasePath, DirectoryContent::IncludeDirsFlag, DirContent); for (const std::filesystem::path& OplogBasePath : DirContent.Directories) { Result.DiskSize += Oplog::TotalSize(OplogBasePath); } } } } } return Result; } Ref ProjectStore::OpenProject(std::string_view ProjectId) { 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; } } 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, m_CidStore, 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_TRACE_CPU("Store::NewProject"); 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, m_CidStore, BasePath))) .first->second; Prj->Identifier = ProjectId; Prj->RootDir = RootDir; Prj->EngineRootDir = EngineRootDir; Prj->ProjectRootDir = ProjectRootDir; Prj->ProjectFilePath = ProjectFilePath; Prj->Write(); m_UpdateCaptureLock.WithExclusiveLock([&]() { 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_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) { 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_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_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_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); 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(), 0u); } if (WantsRawSizeField) { RawSizes.resize(Ids.size(), 0u); } FoundLog->IterateChunks( Ids, [&](size_t Index, const IoBuffer& Payload) { try { uint64_t Size = Payload.GetSize(); if (WantsRawSizeField) { uint64_t RawSize = Size; if (Payload.GetContentType() == ZenContentType::kCompressedBinary) { IoHash __; (void)CompressedBuffer::FromCompressed(SharedBuffer(Payload), __, RawSize); } RawSizes[Index] = RawSize; } if (WantsSizeField) { Sizes[Index] = Size; } } 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)); } CbObjectWriter Response; 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) { Response << "size"sv << Sizes[Index]; } if (WantsRawSizeField) { 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_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); 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; 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, [&](size_t Index, const IoBuffer& Chunk) -> bool { try { uint64_t Size = Chunk.GetSize(); if (WantsRawSizeField) { uint64_t RawSize = Size; if (Chunk.GetContentType() == ZenContentType::kCompressedBinary) { IoHash __; (void)CompressedBuffer::FromCompressed(SharedBuffer(Chunk), __, RawSize); } RawSizes[Index] = RawSize; } if (WantsSizeField) { Sizes[Index] = Size; } } catch (const std::exception& Ex) { ZEN_WARN("oplog '{}/{}': failed getting chunk info for id {}. Reason: '{}'", ProjectId, OplogId, Ids[Index], Ex.what()); } return true; }, &WorkerPool); } CbObjectWriter Response; 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) { Response << "size"sv << Sizes[Index]; } if (WantsRawSizeField) { 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) { 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*/ true); 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); 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) { 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); } 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) { bool IsOffset = Offset != 0 || Size != ~(0ull); 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*/ true); if (!FoundLog) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for unknown oplog '{}/{}'", ProjectId, OplogId)}; } Project->TouchOplog(OplogId); IoBuffer Chunk = FoundLog->FindChunk(ChunkId); if (!Chunk) { return {HttpResponseCode::NotFound, {}}; } OutContentType = Chunk.GetContentType(); if (OutContentType == ZenContentType::kCompressedBinary) { IoHash RawHash; uint64_t RawSize; CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(std::move(Chunk)), RawHash, RawSize); ZEN_ASSERT(!Compressed.IsNull()); if (IsOffset) { if (Size == ~(0ull) || (Offset + Size) > RawSize) { Size = RawSize - Offset; } if (Size == 0) { return { HttpResponseCode::NotFound, fmt::format("Chunk request for range outside of chunk '{}/{}'. Request: Chunk: {}, Offset: {}, Size: {}, ChunkSize: {}", ProjectId, OplogId, ChunkId, 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(); } } else { if (AcceptType == ZenContentType::kBinary) { OutChunk = Compressed.DecompressToComposite(); } else { OutChunk = Compressed.GetCompressed(); OutContentType = ZenContentType::kCompressedBinary; } } } else if (IsOffset) { if (Size == ~(0ull) || (Offset + Size) > Chunk.GetSize()) { Size = Chunk.GetSize() - Offset; } if (Size == 0) { return {HttpResponseCode::NotFound, fmt::format("Chunk request for range outside of chunk '{}/{}'. Request: Chunk: {}, Offset: {}, Size: {}, ChunkSize: {}", ProjectId, OplogId, ChunkId, Offset, Size, Chunk.GetSize())}; } OutChunk = CompositeBuffer(SharedBuffer(IoBuffer(std::move(Chunk), Offset, Size))); } else { OutChunk = CompositeBuffer(SharedBuffer(std::move(Chunk))); } return {HttpResponseCode::OK, {}}; } std::pair ProjectStore::GetChunk(const std::string_view ProjectId, const std::string_view OplogId, const std::string_view Cid, ZenContentType AcceptType, IoBuffer& OutChunk) { 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*/ true); 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 = m_CidStore.FindChunkByCid(Hash); if (!OutChunk) { return {HttpResponseCode::NotFound, fmt::format("chunk - '{}' MISSING", Cid)}; } if (AcceptType == ZenContentType::kUnknownContentType || AcceptType == ZenContentType::kBinary) { CompressedBuffer Compressed = CompressedBuffer::FromCompressedNoValidate(std::move(OutChunk)); OutChunk = Compressed.Decompress().AsIoBuffer(); OutChunk.SetContentType(ZenContentType::kBinary); } else { 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) { 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*/ true); 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)}; } CidStore::InsertResult Result = m_CidStore.AddChunk(Chunk, Hash); return {Result.New ? HttpResponseCode::Created : HttpResponseCode::OK, {}}; } std::pair ProjectStore::WriteOplog(const std::string_view ProjectId, const std::string_view OplogId, IoBuffer&& Payload, CbObject& OutResponse) { 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); 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 = m_CidStore; 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; 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_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); 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 = m_CidStore; RemoteProjectStore::LoadContainerResult ContainerResult = BuildContainer( ChunkStore, *Project.Get(), *Oplog, MaxBlockSize, MaxChunkEmbedSize, ChunkFileSizeLimit, /* BuildBlocks */ false, /* IgnoreMissingAttachments */ false, /* AllowChunking*/ false, [](CompressedBuffer&&, const IoHash&) {}, [](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_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(); ProjectStore::Oplog* Oplog = Project->OpenOplog(OplogId, /*AllowCompact*/ true); if (!Oplog) { HttpReq.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, fmt::format("Rpc oplog request for unknown oplog '{}/{}'", ProjectId, OplogId)); return true; } Project->TouchOplog(OplogId); std::string_view Method = Cb["method"sv].AsString(); 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"); CbPackage ResponsePackage; { CbArrayView ChunksArray = Cb["chunks"sv].AsArrayView(); CbObjectWriter ResponseWriter; ResponseWriter.BeginArray("chunks"sv); for (CbFieldView FieldView : ChunksArray) { IoHash RawHash = FieldView.AsHash(); IoBuffer ChunkBuffer = m_CidStore.FindChunkByCid(RawHash); if (ChunkBuffer) { CompressedBuffer Compressed = CompressedBuffer::FromCompressedNoValidate(std::move(ChunkBuffer)); if (Compressed) { ResponseWriter.AddHash(RawHash); ResponsePackage.AddAttachment(CbAttachment(std::move(Compressed), RawHash)); } else { ZEN_WARN("oplog '{}/{}': invalid compressed binary in cas store for {}", ProjectId, OplogId, RawHash); } } } ResponseWriter.EndArray(); ResponsePackage.SetObject(ResponseWriter.Save()); } CompositeBuffer RpcResponseBuffer = FormatPackageMessageBuffer(ResponsePackage, FormatFlags::kDefault); HttpReq.WriteResponse(HttpResponseCode::OK, HttpContentType::kCbPackage, RpcResponseBuffer); 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); } m_CidStore.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; std::unordered_map NewChunkMappings; Oplog->IterateOplog( [&](CbObjectView Op) { bool OpRewritten = false; bool AllOk = true; CbWriter Cbo; Cbo.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 store in CidStore Oid ChunkId = View["id"sv].AsObjectId(); IoBuffer FileIoBuffer = DataFile.ReadAll(); CompressedBuffer Compressed = CompressedBuffer::Compress(SharedBuffer(std::move(FileIoBuffer))); const IoHash RawHash = Compressed.DecodeRawHash(); const uint64_t RawSize = Compressed.DecodeRawSize(); IoBuffer CompressedBuffer = Compressed.GetCompressed().Flatten().AsIoBuffer(); CidStore::InsertResult Result = m_CidStore.AddChunk(CompressedBuffer, RawHash); TotalBytes += RawSize; ++TotalFiles; if (Result.New) { InlinedBytes += RawSize; ++InlinedFiles; } // Rewrite file array entry with new data reference CbObjectWriter Writer; 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(); Cbo.AddObject(std::move(RewrittenOp)); CopyField = false; NewChunkMappings.insert_or_assign(ChunkId, RawHash); } } } if (CopyField) { Cbo.AddField(Field); } else { OpRewritten = true; } } if (OpRewritten && AllOk) { Cbo.EndArray(); CbArray FilesArray = Cbo.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{}); // Make sure we have references to our attachments Oplog->AddChunkMappings(NewChunkMappings); 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(); } 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_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 '{}/{}' to {}", Project->Identifier, Oplog.OplogId(), StoreInfo.Description), [this, ActualRemoteStore = std::move(RemoteStore), Project, OplogPtr = &Oplog, MaxBlockSize, MaxChunkEmbedSize, ChunkFileSizeLimit, EmbedLooseFile, Force, IgnoreMissingAttachments](JobContext& Context) { RemoteProjectStore::Result Result = SaveOplog(m_CidStore, *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 std::runtime_error(Response.second.empty() ? fmt::format("Status: {}", ToString(Response.first)) : Response.second); } }); return {HttpResponseCode::Accepted, fmt::format("{}", JobId.Id)}; } std::pair ProjectStore::Import(ProjectStore::Project& Project, ProjectStore::Oplog& Oplog, CbObjectView&& Params, AuthMgr& AuthManager) { 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(); ZEN_INFO("Loading oplog '{}/{}' from {}", Project.Identifier, Oplog.OplogId(), StoreInfo.Description); JobId JobId = m_JobQueue.QueueJob( fmt::format("Import oplog '{}/{}' from {}", Project.Identifier, Oplog.OplogId(), StoreInfo.Description), [this, ActualRemoteStore = std::move(RemoteStore), OplogPtr = &Oplog, Force, IgnoreMissingAttachments, CleanOplog]( JobContext& Context) { RemoteProjectStore::Result Result = LoadOplog(m_CidStore, *ActualRemoteStore, *OplogPtr, Force, IgnoreMissingAttachments, CleanOplog, &Context); auto Response = ConvertResult(Result); ZEN_INFO("LoadOplog: Status: {} '{}'", ToString(Response.first), Response.second); if (!IsHttpSuccessCode(Response.first)) { throw std::runtime_error(Response.second.empty() ? fmt::format("Status: {}", ToString(Response.first)) : Response.second); } }); return {HttpResponseCode::Accepted, fmt::format("{}", JobId.Id)}; } bool ProjectStore::AreDiskWritesAllowed() const { return (m_DiskWriteBlocker == nullptr || m_DiskWriteBlocker->AreDiskWritesAllowed()); } void ProjectStore::EnableUpdateCapture() { m_UpdateCaptureLock.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_UpdateCaptureLock.WithExclusiveLock([&]() { ZEN_ASSERT(m_CapturedProjects); ZEN_ASSERT(m_UpdateCaptureRefCounter > 0); m_UpdateCaptureRefCounter--; if (m_UpdateCaptureRefCounter == 0) { m_CapturedProjects.reset(); } }); } std::vector ProjectStore::GetCapturedProjects() { RwLock::SharedLockScope _(m_UpdateCaptureLock); if (m_CapturedProjects) { return *m_CapturedProjects; } return {}; } std::string ProjectStore::GetGcName(GcCtx&) { 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"); 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"); 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.GetCapturedProjects(); 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.GetCapturedOplogs()); 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); } 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->IterateCapturedLSNs([&](const CbObjectView& UpdateOp) -> bool { UpdateOp.IterateAttachments([&](CbFieldView Visitor) { m_AddedReferences.emplace_back(Visitor.AsAttachment()); }); return true; }); std::vector AddedAttachments = Oplog->GetCapturedAttachments(); m_AddedReferences.insert(m_AddedReferences.end(), AddedAttachments.begin(), AddedAttachments.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; } ////////////////////////////////////////////////////////////////////////// #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 CreateOplogPackage(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; }; std::vector> CreateAttachments( const std::span& Sizes, OodleCompressionLevel CompressionLevel = OodleCompressionLevel::VeryFast) { std::vector> Result; Result.reserve(Sizes.size()); for (size_t Size : Sizes) { CompressedBuffer Compressed = CompressedBuffer::Compress(SharedBuffer(CreateRandomBlob(Size)), OodleCompressor::Mermaid, CompressionLevel); 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; } } // namespace testutils TEST_CASE("project.store.create") { using namespace std::literals; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); GcManager Gc; CidStore CidStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; CidStore.Initialize(CidConfig); std::string_view ProjectName("proj1"sv); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(CidStore, BasePath, Gc, *JobQueue, 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); GcManager Gc; CidStore CidStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; CidStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(CidStore, BasePath, Gc, *JobQueue, 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) == 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); GcManager Gc; CidStore CidStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; CidStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(CidStore, BasePath, Gc, *JobQueue, 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(CreateOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{77}))); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{7123, 583, 690, 99}))); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{55, 122}))); Oplog->AppendNewOplogEntry( CreateOplogPackage(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(CidStore, *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(CidStore, *RemoteStore, *OplogImport, /*Force*/ false, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ false, nullptr); CHECK(ImportResult.ErrorCode == 0); RemoteProjectStore::Result ImportForceResult = LoadOplog(CidStore, *RemoteStore, *OplogImport, /*Force*/ true, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ false, nullptr); CHECK(ImportForceResult.ErrorCode == 0); RemoteProjectStore::Result ImportCleanResult = LoadOplog(CidStore, *RemoteStore, *OplogImport, /*Force*/ false, /*IgnoreMissingAttachments*/ false, /*CleanOplog*/ true, nullptr); CHECK(ImportCleanResult.ErrorCode == 0); RemoteProjectStore::Result ImportForceCleanResult = LoadOplog(CidStore, *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); GcManager Gc; CidStore CidStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas", .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; CidStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"; ProjectStore ProjectStore(CidStore, BasePath, Gc, *JobQueue, 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(CreateOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{77}))); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{7123, 583, 690, 99}))); Oplog->AppendNewOplogEntry(CreateOplogPackage(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(CreateOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{177}))); Oplog->AppendNewOplogEntry( CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{9123, 383, 590, 96}))); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{535, 221}))); } { ProjectStore::Oplog* Oplog = Project2->NewOplog("oplog3", Project2Oplog2Path); CHECK(Oplog != nullptr); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), {})); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{137}))); Oplog->AppendNewOplogEntry( CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{9723, 683, 594, 98}))); Oplog->AppendNewOplogEntry(CreateOplogPackage(Oid::NewOid(), CreateAttachments(std::initializer_list{531, 271}))); } } SUBCASE("v2") { { 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)); } std::filesystem::remove(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)); } std::filesystem::remove(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)); } std::filesystem::remove(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.partial.read") { using namespace std::literals; using namespace testutils; ScopedTemporaryDirectory TempDir; auto JobQueue = MakeJobQueue(1, ""sv); GcManager Gc; CidStore CidStore(Gc); CidStoreConfiguration CidConfig = {.RootDirectory = TempDir.Path() / "cas"sv, .TinyValueThreshold = 1024, .HugeValueThreshold = 4096}; CidStore.Initialize(CidConfig); std::filesystem::path BasePath = TempDir.Path() / "projectstore"sv; ProjectStore ProjectStore(CidStore, BasePath, Gc, *JobQueue, 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(CreateOplogPackage(It.first, It.second)); } } { IoBuffer Chunk; CHECK(ProjectStore .GetChunk("proj1"sv, "oplog1"sv, Attachments[OpIds[1]][0].second.DecodeRawHash().ToHexString(), HttpContentType::kCompressedBinary, Chunk) .first == HttpResponseCode::OK); IoHash RawHash; uint64_t RawSize; CompressedBuffer Attachment = CompressedBuffer::FromCompressed(SharedBuffer(Chunk), RawHash, RawSize); CHECK(RawSize == Attachments[OpIds[1]][0].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) .first == HttpResponseCode::OK); CHECK(ChunkResult); CHECK(CompressedBuffer::FromCompressedNoValidate(std::move(ChunkResult)).DecodeRawSize() == Attachments[OpIds[2]][1].second.DecodeRawSize()); CompositeBuffer PartialChunkResult; CHECK(ProjectStore .GetChunkRange("proj1"sv, "oplog1"sv, OidAsString(Attachments[OpIds[2]][1].first), 5, 1773, HttpContentType::kCompressedBinary, PartialChunkResult, ContentType) .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]); } 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.GetCompressed().Flatten().AsIoBuffer()](const IoHash&) -> CompositeBuffer { return CompositeBuffer(SharedBuffer(Buffer)); })); } CompressedBuffer Block = GenerateBlock(std::move(Chunks)); CHECK(IterateBlock(Block.Decompress(), [](CompressedBuffer&&, const IoHash&) {})); } #endif void prj_forcelink() { } } // namespace zen