diff options
Diffstat (limited to 'src/zenserver')
| -rw-r--r-- | src/zenserver/admin/admin.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/config.cpp | 6 | ||||
| -rw-r--r-- | src/zenserver/config/luaconfig.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/diag/diagsvcs.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/main.cpp | 14 | ||||
| -rw-r--r-- | src/zenserver/projectstore/fileremoteprojectstore.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/projectstore/jupiterremoteprojectstore.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/projectstore/projectstore.cpp | 12 | ||||
| -rw-r--r-- | src/zenserver/projectstore/remoteprojectstore.cpp | 8 | ||||
| -rw-r--r-- | src/zenserver/projectstore/zenremoteprojectstore.cpp | 259 | ||||
| -rw-r--r-- | src/zenserver/projectstore/zenremoteprojectstore.h | 2 | ||||
| -rw-r--r-- | src/zenserver/sentryintegration.cpp | 17 | ||||
| -rw-r--r-- | src/zenserver/upstream/upstreamcache.cpp | 20 | ||||
| -rw-r--r-- | src/zenserver/vfs/vfsimpl.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/vfs/vfsservice.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/zenserver.cpp | 4 |
16 files changed, 120 insertions, 242 deletions
diff --git a/src/zenserver/admin/admin.cpp b/src/zenserver/admin/admin.cpp index 8093a0735..75ff03912 100644 --- a/src/zenserver/admin/admin.cpp +++ b/src/zenserver/admin/admin.cpp @@ -603,7 +603,7 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, EmitStats("cas", Stats.CasStats); EmitStats("project", Stats.ProjectStats); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { ZEN_WARN("exception in disk stats gathering for '{}': {}", m_ServerOptions.DataDir, Ex.what()); } @@ -622,7 +622,7 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, Obj.EndArray(); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { ZEN_WARN("exception in state gathering for '{}': {}", m_ServerOptions.SystemRootDir, Ex.what()); } diff --git a/src/zenserver/config.cpp b/src/zenserver/config.cpp index 00581d758..aa0eedb0e 100644 --- a/src/zenserver/config.cpp +++ b/src/zenserver/config.cpp @@ -128,7 +128,7 @@ ReadAllCentralManifests(const std::filesystem::path& SystemRoot) ZEN_WARN("failed to load manifest '{}': {}", File, ToString(ValidateError)); } } - catch (std::exception& Ex) + catch (const std::exception& Ex) { ZEN_WARN("failed to load manifest '{}': {}", File, Ex.what()); } @@ -1004,7 +1004,7 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) { Result = options.parse(argc, argv); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { throw zen::OptionParseException(Ex.what()); } @@ -1069,7 +1069,7 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) ValidateOptions(ServerOptions); } - catch (zen::OptionParseException& e) + catch (const zen::OptionParseException& e) { ZEN_CONSOLE_ERROR("Error parsing zenserver arguments: {}\n\n{}", e.what(), options.help()); diff --git a/src/zenserver/config/luaconfig.cpp b/src/zenserver/config/luaconfig.cpp index cdc808cf6..f742fa34a 100644 --- a/src/zenserver/config/luaconfig.cpp +++ b/src/zenserver/config/luaconfig.cpp @@ -280,7 +280,7 @@ Options::Parse(const std::filesystem::path& Path, const cxxopts::ParseResult& Cm config(); } - catch (std::exception& e) + catch (const std::exception& e) { throw std::runtime_error(fmt::format("failed to load config script ('{}'): {}", Path, e.what()).c_str()); } diff --git a/src/zenserver/diag/diagsvcs.cpp b/src/zenserver/diag/diagsvcs.cpp index 1a10782e9..f0aec98ab 100644 --- a/src/zenserver/diag/diagsvcs.cpp +++ b/src/zenserver/diag/diagsvcs.cpp @@ -36,7 +36,7 @@ ReadLogFile(const std::string& Path, StringBuilderBase& Out) return true; } - catch (std::exception&) + catch (const std::exception&) { Out.Reset(); return false; diff --git a/src/zenserver/main.cpp b/src/zenserver/main.cpp index 7a6d2dd22..6b31dc82e 100644 --- a/src/zenserver/main.cpp +++ b/src/zenserver/main.cpp @@ -247,7 +247,12 @@ ZenEntryPoint::Run() Server.Run(); } - catch (std::exception& e) + catch (const AssertException& AssertEx) + { + ZEN_CRITICAL("Caught assert exception in main for process {}: {}", zen::GetCurrentProcessId(), AssertEx.FullDescription()); + RequestApplicationExit(1); + } + catch (const std::exception& e) { ZEN_CRITICAL("Caught exception in main for process {}: {}", zen::GetCurrentProcessId(), e.what()); RequestApplicationExit(1); @@ -407,7 +412,12 @@ main(int argc, char* argv[]) return App.Run(); #endif } - catch (std::exception& Ex) + catch (const AssertException& AssertEx) + { + fprintf(stderr, "ERROR: Caught assert exception in main: '%s'", AssertEx.FullDescription().c_str()); + return 1; + } + catch (const std::exception& Ex) { fprintf(stderr, "ERROR: Caught exception in main: '%s'", Ex.what()); diff --git a/src/zenserver/projectstore/fileremoteprojectstore.cpp b/src/zenserver/projectstore/fileremoteprojectstore.cpp index 4248bbf2a..764bea355 100644 --- a/src/zenserver/projectstore/fileremoteprojectstore.cpp +++ b/src/zenserver/projectstore/fileremoteprojectstore.cpp @@ -79,7 +79,7 @@ public: } Result.RawHash = IoHash::HashBuffer(Payload); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError); Result.Reason = fmt::format("Failed saving oplog container to '{}'. Reason: {}", ContainerPath, Ex.what()); @@ -108,7 +108,7 @@ public: Offset += Segment.GetSize(); } } - catch (std::exception& Ex) + catch (const std::exception& Ex) { Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError); Result.Reason = fmt::format("Failed saving oplog attachment to '{}'. Reason: {}", ChunkPath, Ex.what()); diff --git a/src/zenserver/projectstore/jupiterremoteprojectstore.cpp b/src/zenserver/projectstore/jupiterremoteprojectstore.cpp index 1508dbc3f..6b1f591f0 100644 --- a/src/zenserver/projectstore/jupiterremoteprojectstore.cpp +++ b/src/zenserver/projectstore/jupiterremoteprojectstore.cpp @@ -276,7 +276,7 @@ CreateJupiterRemoteStore(const JupiterRemoteStoreOptions& Options, const std::fi .Timeout = std::chrono::milliseconds(1800000), .AssumeHttp2 = Options.AssumeHttp2, .AllowResume = true, - .RetryCount = 2}; + .RetryCount = 4}; // 1) Access token as parameter in request // 2) Environment variable (different win vs linux/mac) // 3) openid-provider (assumes oidctoken.exe -Zen true has been run with matching Options.OpenIdProvider diff --git a/src/zenserver/projectstore/projectstore.cpp b/src/zenserver/projectstore/projectstore.cpp index dd390d08c..84ed6f842 100644 --- a/src/zenserver/projectstore/projectstore.cpp +++ b/src/zenserver/projectstore/projectstore.cpp @@ -209,7 +209,7 @@ namespace { std::string(Url), std::string(Project), std::string(Oplog)}; - RemoteStore = CreateZenRemoteStore(Options); + RemoteStore = CreateZenRemoteStore(Options, TempFilePath); } if (!RemoteStore) @@ -1679,7 +1679,7 @@ ProjectStore::Project::WriteAccessTimes() WriteFile(ProjectAccessTimesFilePath, Data.GetBuffer().AsIoBuffer()); } - catch (std::exception& Err) + catch (const std::exception& Err) { ZEN_WARN("writing access times FAILED, reason: '{}'", Err.what()); } @@ -1714,7 +1714,7 @@ ProjectStore::Project::NewOplog(std::string_view OplogId, const std::filesystem: Log->Write(); return Log; } - catch (std::exception&) + catch (const std::exception&) { // In case of failure we need to ensure there's no half constructed entry around // @@ -1760,7 +1760,7 @@ ProjectStore::Project::OpenOplog(std::string_view OplogId) return Log; } - catch (std::exception& ex) + catch (const std::exception& ex) { ZEN_WARN("failed to open oplog '{}' @ '{}': {}", OplogId, OplogBasePath, ex.what()); @@ -2371,7 +2371,7 @@ ProjectStore::OpenProject(std::string_view ProjectId) Prj->Read(); return Prj; } - catch (std::exception& e) + catch (const std::exception& e) { ZEN_WARN("failed to open {} @ {} ({})", ProjectId, BasePath, e.what()); m_Projects.erase(std::string{ProjectId}); @@ -4017,7 +4017,7 @@ ProjectStore::CreateReferenceCheckers(GcCtx& Ctx) } } } - catch (std::exception&) + catch (const std::exception&) { while (!Checkers.empty()) { diff --git a/src/zenserver/projectstore/remoteprojectstore.cpp b/src/zenserver/projectstore/remoteprojectstore.cpp index ae4777278..8efb92e6b 100644 --- a/src/zenserver/projectstore/remoteprojectstore.cpp +++ b/src/zenserver/projectstore/remoteprojectstore.cpp @@ -835,7 +835,7 @@ BuildContainer(CidStore& ChunkStore, } } } - catch (std::exception& Ex) + catch (const std::exception& Ex) { RemoteResult.SetError(gsl::narrow<int>(HttpResponseCode::NotFound), fmt::format("Failed to resolve attachment {}", RawHash), @@ -1216,7 +1216,7 @@ BuildContainer(CidStore& ChunkStore, return {}; } } - catch (std::exception& Ex) + catch (const std::exception& Ex) { BlockCreateLatch.CountDown(); while (!BlockCreateLatch.Wait(1000)) @@ -1740,7 +1740,7 @@ SaveOplog(CidStore& ChunkStore, CreatedBlocks.insert({BlockHash, std::move(BlockBuffer)}); ZEN_DEBUG("Saved temp block to '{}', {}", AttachmentTempPath, NiceBytes(BlockBuffer.GetSize())); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { RemoteResult.SetError(gsl::narrow<int32_t>(HttpResponseCode::InternalServerError), Ex.what(), @@ -2658,7 +2658,7 @@ LoadOplog(CidStore& ChunkStore, ReportMessage(OptionalContext, fmt::format("Loaded oplog '{}' {} in {} ({}), Blocks: {} ({}), Attachments: {} ({}), Stored: {} ({}), Missing: {}", RemoteStoreInfo.ContainerName, - RemoteResult.GetError() == 0 ? "SUCCESS" : "FAILURE", + Result.ErrorCode == 0 ? "SUCCESS" : "FAILURE", NiceTimeSpanMs(static_cast<uint64_t>(Result.ElapsedSeconds * 1000.0)), NiceBytes(Info.OplogSizeBytes), Info.AttachmentBlocksDownloaded.load(), diff --git a/src/zenserver/projectstore/zenremoteprojectstore.cpp b/src/zenserver/projectstore/zenremoteprojectstore.cpp index cfb558040..600338843 100644 --- a/src/zenserver/projectstore/zenremoteprojectstore.cpp +++ b/src/zenserver/projectstore/zenremoteprojectstore.cpp @@ -6,15 +6,10 @@ #include <zencore/compactbinarypackage.h> #include <zencore/compositebuffer.h> #include <zencore/fmtutils.h> -#include <zencore/scopeguard.h> #include <zencore/stream.h> -#include <zencore/timer.h> +#include <zenhttp/httpclient.h> #include <zenutil/packageformat.h> -ZEN_THIRD_PARTY_INCLUDES_START -#include <cpr/cpr.h> -ZEN_THIRD_PARTY_INCLUDES_END - namespace zen { using namespace std::literals; @@ -22,17 +17,16 @@ using namespace std::literals; class ZenRemoteStore : public RemoteProjectStore { public: - ZenRemoteStore(std::string_view HostAddress, - std::string_view Project, - std::string_view Oplog, - size_t MaxBlockSize, - size_t MaxChunkEmbedSize) + ZenRemoteStore(std::string_view HostAddress, + std::string_view Project, + std::string_view Oplog, + const std::filesystem::path& TempFilePath) : m_HostAddress(HostAddress) , m_ProjectStoreUrl(fmt::format("{}/prj"sv, m_HostAddress)) , m_Project(Project) , m_Oplog(Oplog) - , m_MaxBlockSize(MaxBlockSize) - , m_MaxChunkEmbedSize(MaxChunkEmbedSize) + , m_TempFilePath(TempFilePath) + , m_Client(m_ProjectStoreUrl, {.LogCategory = "ZenRemoteStore", .RetryCount = 2}) { } @@ -47,39 +41,27 @@ public: virtual SaveResult SaveContainer(const IoBuffer& Payload) override { - Stopwatch Timer; - - std::unique_ptr<cpr::Session> Session(AllocateSession()); - auto _ = MakeGuard([this, &Session]() { ReleaseSession(std::move(Session)); }); - - std::string SaveRequest = fmt::format("{}/{}/oplog/{}/save"sv, m_ProjectStoreUrl, m_Project, m_Oplog); - Session->SetUrl({SaveRequest}); - Session->SetHeader({{"Content-Type", std::string(MapContentTypeToString(HttpContentType::kCbObject))}}); - MemoryView Data(Payload.GetView()); - Session->SetBody({reinterpret_cast<const char*>(Data.GetData()), Data.GetSize()}); - cpr::Response Response = Session->Post(); - SaveResult Result = SaveResult{ConvertResult(Response)}; + std::string SaveRequest = fmt::format("/{}/oplog/{}/save"sv, m_Project, m_Oplog); + HttpClient::Response Response = m_Client.Post(SaveRequest, Payload, ZenContentType::kCbObject); + SaveResult Result = SaveResult{ConvertResult(Response)}; if (Result.ErrorCode) { - Result.Reason = fmt::format("Failed saving oplog container to {}/{}/{}. Reason: '{}'", + Result.Reason = fmt::format("Failed saving oplog container to {}/{}/{}. Reason: '{}'", m_ProjectStoreUrl, m_Project, m_Oplog, Result.Reason); - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; return Result; } - IoBuffer ResponsePayload(IoBuffer::Wrap, Response.text.data(), Response.text.size()); - CbObject ResponseObject = LoadCompactBinaryObject(ResponsePayload); + CbObject ResponseObject = Response.AsObject(); if (!ResponseObject) { - Result.Reason = fmt::format("The response for {}/{}/{} is not formatted as a compact binary object"sv, - m_ProjectStoreUrl, - m_Project, - m_Oplog); - Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError); - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; + Result.Reason = fmt::format("The response for {}/{}/{} is not formatted as a compact binary object"sv, + m_ProjectStoreUrl, + m_Project, + m_Oplog); + Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError); return Result; } CbArrayView NeedsArray = ResponseObject["need"sv].AsArrayView(); @@ -89,33 +71,15 @@ public: Result.Needs.insert(ChunkHash); } - Result.RawHash = IoHash::HashBuffer(Payload); - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; + Result.RawHash = IoHash::HashBuffer(Payload); return Result; } virtual SaveAttachmentResult SaveAttachment(const CompositeBuffer& Payload, const IoHash& RawHash) override { - Stopwatch Timer; - - std::unique_ptr<cpr::Session> Session(AllocateSession()); - auto _ = MakeGuard([this, &Session]() { ReleaseSession(std::move(Session)); }); - - std::string SaveRequest = fmt::format("{}/{}/oplog/{}/{}"sv, m_ProjectStoreUrl, m_Project, m_Oplog, RawHash); - Session->SetUrl({SaveRequest}); - Session->SetHeader({{"Content-Type", std::string(MapContentTypeToString(HttpContentType::kCompressedBinary))}}); - uint64_t SizeLeft = Payload.GetSize(); - CompositeBuffer::Iterator BufferIt = Payload.GetIterator(0); - auto ReadCallback = [&Payload, &BufferIt, &SizeLeft](char* buffer, size_t& size, intptr_t) { - size = Min<size_t>(size, SizeLeft); - MutableMemoryView Data(buffer, size); - Payload.CopyTo(Data, BufferIt); - SizeLeft -= size; - return true; - }; - Session->SetReadCallback(cpr::ReadCallback(gsl::narrow<cpr::cpr_off_t>(SizeLeft), ReadCallback)); - cpr::Response Response = Session->Post(); - SaveAttachmentResult Result = SaveAttachmentResult{ConvertResult(Response)}; + std::string SaveRequest = fmt::format("/{}/oplog/{}/{}"sv, m_Project, m_Oplog, RawHash); + HttpClient::Response Response = m_Client.Post(SaveRequest, Payload, ZenContentType::kCompressedBinary); + SaveAttachmentResult Result = SaveAttachmentResult{ConvertResult(Response)}; if (Result.ErrorCode) { Result.Reason = fmt::format("Failed saving oplog attachment to {}/{}/{}/{}. Reason: '{}'", @@ -125,14 +89,11 @@ public: RawHash, Result.Reason); } - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; return Result; } virtual SaveAttachmentsResult SaveAttachments(const std::vector<SharedBuffer>& Chunks) override { - Stopwatch Timer; - CbPackage RequestPackage; { CbObjectWriter RequestWriter; @@ -151,26 +112,10 @@ public: RequestWriter.EndArray(); // "chunks" RequestPackage.SetObject(RequestWriter.Save()); } - CompositeBuffer Payload = FormatPackageMessageBuffer(RequestPackage, FormatFlags::kDefault); - - std::unique_ptr<cpr::Session> Session(AllocateSession()); - auto _ = MakeGuard([this, &Session]() { ReleaseSession(std::move(Session)); }); - std::string SaveRequest = fmt::format("{}/{}/oplog/{}/rpc"sv, m_ProjectStoreUrl, m_Project, m_Oplog); - Session->SetUrl({SaveRequest}); - Session->SetHeader({{"Content-Type", std::string(MapContentTypeToString(HttpContentType::kCbPackage))}}); + std::string SaveRequest = fmt::format("/{}/oplog/{}/rpc"sv, m_Project, m_Oplog); + HttpClient::Response Response = m_Client.Post(SaveRequest, RequestPackage); - uint64_t SizeLeft = Payload.GetSize(); - CompositeBuffer::Iterator BufferIt = Payload.GetIterator(0); - auto ReadCallback = [&Payload, &BufferIt, &SizeLeft](char* buffer, size_t& size, intptr_t) { - size = Min<size_t>(size, SizeLeft); - MutableMemoryView Data(buffer, size); - Payload.CopyTo(Data, BufferIt); - SizeLeft -= size; - return true; - }; - Session->SetReadCallback(cpr::ReadCallback(gsl::narrow<cpr::cpr_off_t>(SizeLeft), ReadCallback)); - cpr::Response Response = Session->Post(); - SaveAttachmentsResult Result = SaveAttachmentsResult{ConvertResult(Response)}; + SaveAttachmentsResult Result = SaveAttachmentsResult{ConvertResult(Response)}; if (Result.ErrorCode) { Result.Reason = fmt::format("Failed saving {} oplog attachments to {}/{}/{}. Reason: '{}'", @@ -180,17 +125,12 @@ public: m_Oplog, Result.Reason); } - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; return Result; } virtual LoadAttachmentsResult LoadAttachments(const std::vector<IoHash>& RawHashes) override { - Stopwatch Timer; - - std::unique_ptr<cpr::Session> Session(AllocateSession()); - auto _ = MakeGuard([this, &Session]() { ReleaseSession(std::move(Session)); }); - std::string SaveRequest = fmt::format("{}/{}/oplog/{}/rpc"sv, m_ProjectStoreUrl, m_Project, m_Oplog); + std::string LoadRequest = fmt::format("/{}/oplog/{}/rpc"sv, m_Project, m_Oplog); CbObject Request; { @@ -206,17 +146,22 @@ public: RequestWriter.EndArray(); // "chunks" Request = RequestWriter.Save(); } - IoBuffer Payload = Request.GetBuffer().AsIoBuffer(); - Session->SetBody(cpr::Body{(const char*)Payload.GetData(), Payload.GetSize()}); - Session->SetUrl(SaveRequest); - Session->SetHeader({{"Content-Type", std::string(MapContentTypeToString(HttpContentType::kCbObject))}, - {"Accept", std::string(MapContentTypeToString(HttpContentType::kCbPackage))}}); - cpr::Response Response = Session->Post(); - LoadAttachmentsResult Result = LoadAttachmentsResult{ConvertResult(Response)}; - if (!Result.ErrorCode) + HttpClient::Response Response = m_Client.Post(LoadRequest, Request, HttpClient::Accept(ZenContentType::kCbPackage)); + + LoadAttachmentsResult Result = LoadAttachmentsResult{ConvertResult(Response)}; + if (Result.ErrorCode) { - CbPackage Package = ParsePackageMessage(IoBuffer(IoBuffer::Wrap, Response.text.data(), Response.text.size())); + Result.Reason = fmt::format("Failed fetching {} oplog attachments from {}/{}/{}. Reason: '{}'", + RawHashes.size(), + m_ProjectStoreUrl, + m_Project, + m_Oplog, + Result.Reason); + } + else + { + CbPackage Package = Response.AsPackage(); std::span<const CbAttachment> Attachments = Package.GetAttachments(); Result.Chunks.reserve(Attachments.size()); for (const CbAttachment& Attachment : Attachments) @@ -225,42 +170,17 @@ public: std::pair<IoHash, CompressedBuffer>{Attachment.GetHash(), Attachment.AsCompressedBinary().MakeOwned()}); } } - else - { - Result.Reason = fmt::format("Failed fetching {} oplog attachments from {}/{}/{}. Reason: '{}'", - RawHashes.size(), - m_ProjectStoreUrl, - m_Project, - m_Oplog, - Result.Reason); - } - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; return Result; }; - virtual FinalizeResult FinalizeContainer(const IoHash&) override - { - Stopwatch Timer; - - RwLock::ExclusiveLockScope _(SessionsLock); - Sessions.clear(); - return FinalizeResult{Result{.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0}}; - } + virtual FinalizeResult FinalizeContainer(const IoHash&) override { return FinalizeResult{Result{}}; } virtual LoadContainerResult LoadContainer() override { - Stopwatch Timer; + std::string LoadRequest = fmt::format("/{}/oplog/{}/load"sv, m_Project, m_Oplog); - std::unique_ptr<cpr::Session> Session(AllocateSession()); - auto _ = MakeGuard([this, &Session]() { ReleaseSession(std::move(Session)); }); - std::string SaveRequest = fmt::format("{}/{}/oplog/{}/load"sv, m_ProjectStoreUrl, m_Project, m_Oplog); - Session->SetUrl(SaveRequest); - Session->SetHeader({{"Accept", std::string(MapContentTypeToString(HttpContentType::kCbObject))}}); - Session->SetParameters( - {{"maxblocksize", fmt::format("{}", m_MaxBlockSize)}, {"maxchunkembedsize", fmt::format("{}", m_MaxChunkEmbedSize)}}); - cpr::Response Response = Session->Get(); - - LoadContainerResult Result = LoadContainerResult{ConvertResult(Response)}; + HttpClient::Response Response = m_Client.Get(LoadRequest, HttpClient::Accept(ZenContentType::kCbObject)); + LoadContainerResult Result = LoadContainerResult{ConvertResult(Response)}; if (Result.ErrorCode) { Result.Reason = fmt::format("Failed fetching oplog container from {}/{}/{}. Reason: '{}'", @@ -271,7 +191,7 @@ public: } else { - Result.ContainerObject = LoadCompactBinaryObject(IoBuffer(IoBuffer::Clone, Response.text.data(), Response.text.size())); + Result.ContainerObject = Response.AsObject(); if (!Result.ContainerObject) { Result.Reason = fmt::format("The response for {}/{}/{} is not formatted as a compact binary object"sv, @@ -281,7 +201,6 @@ public: Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError); } } - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; return Result; } @@ -299,19 +218,14 @@ public: virtual LoadAttachmentResult LoadAttachment(const IoHash& RawHash) override { - Stopwatch Timer; - - std::unique_ptr<cpr::Session> Session(AllocateSession()); - auto _ = MakeGuard([this, &Session]() { ReleaseSession(std::move(Session)); }); - - std::string LoadRequest = fmt::format("{}/{}/oplog/{}/{}"sv, m_ProjectStoreUrl, m_Project, m_Oplog, RawHash); - Session->SetUrl({LoadRequest}); - Session->SetHeader({{"Accept", std::string(MapContentTypeToString(HttpContentType::kCompressedBinary))}}); - cpr::Response Response = Session->Get(); - LoadAttachmentResult Result = LoadAttachmentResult{ConvertResult(Response)}; + std::string LoadRequest = fmt::format("/{}/oplog/{}/{}"sv, m_Project, m_Oplog, RawHash); + HttpClient::Response Response = + m_Client.Download(LoadRequest, m_TempFilePath, HttpClient::Accept(ZenContentType::kCompressedBinary)); + LoadAttachmentResult Result = LoadAttachmentResult{ConvertResult(Response)}; if (!Result.ErrorCode) { - Result.Bytes = IoBufferBuilder::MakeCloneFromMemory(Response.text.data(), Response.text.size()); + Result.Bytes = Response.ResponsePayload; + Result.Bytes.MakeOwned(); } if (!Result.ErrorCode) { @@ -322,73 +236,40 @@ public: RawHash, Result.Reason); } - Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.0; return Result; } private: - std::unique_ptr<cpr::Session> AllocateSession() - { - RwLock::ExclusiveLockScope _(SessionsLock); - if (Sessions.empty()) - { - Sessions.emplace_back(std::make_unique<cpr::Session>()); - } - std::unique_ptr<cpr::Session> Session = std::move(Sessions.back()); - Sessions.pop_back(); - return Session; - } - - void ReleaseSession(std::unique_ptr<cpr::Session>&& Session) + static Result ConvertResult(const HttpClient::Response& Response, const std::string_view ErrorPrefix = ""sv) { - RwLock::ExclusiveLockScope _(SessionsLock); - Sessions.emplace_back(std::move(Session)); - } - - static Result ConvertResult(const cpr::Response& Response) - { - std::string Text; - std::string Reason = Response.reason; - int32_t ErrorCode = 0; - if (Response.error.code != cpr::ErrorCode::OK) + if (Response.Error) { - ErrorCode = static_cast<int32_t>(Response.error.code); - if (!Response.error.message.empty()) - { - Reason = Response.error.message; - } + return {.ErrorCode = Response.Error.value().ErrorCode, + .ElapsedSeconds = Response.ElapsedSeconds, + .Reason = Response.ErrorMessage(""), + .Text = Response.ToText()}; } - else if (!IsHttpSuccessCode(Response.status_code)) + if (!Response.IsSuccess()) { - ErrorCode = static_cast<int32_t>(Response.status_code); - - if (auto It = Response.header.find("Content-Type"); It != Response.header.end()) - { - zen::HttpContentType ContentType = zen::ParseContentType(It->second); - if (ContentType == zen::HttpContentType::kText) - { - Text = Response.text; - } - } - - Reason = fmt::format("{}"sv, Response.status_code); + return {.ErrorCode = static_cast<int32_t>(Response.StatusCode), + .ElapsedSeconds = Response.ElapsedSeconds, + .Reason = Response.ErrorMessage(ErrorPrefix), + .Text = Response.ToText()}; } - return {.ErrorCode = ErrorCode, .ElapsedSeconds = Response.elapsed, .Reason = Reason, .Text = Text}; + return {.ErrorCode = 0, .ElapsedSeconds = Response.ElapsedSeconds}; } - RwLock SessionsLock; - std::vector<std::unique_ptr<cpr::Session>> Sessions; + const std::string m_HostAddress; + const std::string m_ProjectStoreUrl; + const std::string m_Project; + const std::string m_Oplog; + const std::filesystem::path m_TempFilePath; - const std::string m_HostAddress; - const std::string m_ProjectStoreUrl; - const std::string m_Project; - const std::string m_Oplog; - const size_t m_MaxBlockSize; - const size_t m_MaxChunkEmbedSize; + HttpClient m_Client; }; std::shared_ptr<RemoteProjectStore> -CreateZenRemoteStore(const ZenRemoteStoreOptions& Options) +CreateZenRemoteStore(const ZenRemoteStoreOptions& Options, const std::filesystem::path& TempFilePath) { std::string Url = Options.Url; if (Url.find("://"sv) == std::string::npos) @@ -397,7 +278,7 @@ CreateZenRemoteStore(const ZenRemoteStoreOptions& Options) Url = fmt::format("http://{}"sv, Url); } std::shared_ptr<RemoteProjectStore> RemoteStore = - std::make_shared<ZenRemoteStore>(Url, Options.ProjectId, Options.OplogId, Options.MaxBlockSize, Options.MaxChunkEmbedSize); + std::make_shared<ZenRemoteStore>(Url, Options.ProjectId, Options.OplogId, TempFilePath); return RemoteStore; } diff --git a/src/zenserver/projectstore/zenremoteprojectstore.h b/src/zenserver/projectstore/zenremoteprojectstore.h index 9f079ee74..7c81a597d 100644 --- a/src/zenserver/projectstore/zenremoteprojectstore.h +++ b/src/zenserver/projectstore/zenremoteprojectstore.h @@ -13,6 +13,6 @@ struct ZenRemoteStoreOptions : RemoteStoreOptions std::string OplogId; }; -std::shared_ptr<RemoteProjectStore> CreateZenRemoteStore(const ZenRemoteStoreOptions& Options); +std::shared_ptr<RemoteProjectStore> CreateZenRemoteStore(const ZenRemoteStoreOptions& Options, const std::filesystem::path& TempFilePath); } // namespace zen diff --git a/src/zenserver/sentryintegration.cpp b/src/zenserver/sentryintegration.cpp index 11bf78a75..a8d967985 100644 --- a/src/zenserver/sentryintegration.cpp +++ b/src/zenserver/sentryintegration.cpp @@ -31,13 +31,10 @@ namespace sentry { struct SentryAssertImpl : zen::AssertImpl { - ZEN_FORCENOINLINE ZEN_DEBUG_SECTION SentryAssertImpl(); - virtual ZEN_FORCENOINLINE ZEN_DEBUG_SECTION ~SentryAssertImpl(); virtual void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION OnAssert(const char* Filename, int LineNumber, const char* FunctionName, const char* Msg) override; - AssertImpl* PrevAssertImpl; }; class sentry_sink final : public spdlog::sinks::base_sink<spdlog::details::null_mutex> @@ -85,7 +82,7 @@ sentry_sink::sink_it_(const spdlog::details::log_msg& msg) sentry_event_value_add_stacktrace(event, NULL, 0); sentry_capture_event(event); } - catch (std::exception&) + catch (const std::exception&) { // If our logging with Message formatting fails we do a non-allocating version and just post the msg.payload raw char TmpBuffer[256]; @@ -105,16 +102,6 @@ sentry_sink::flush_() { } -SentryAssertImpl::SentryAssertImpl() : PrevAssertImpl(CurrentAssertImpl) -{ - CurrentAssertImpl = this; -} - -SentryAssertImpl::~SentryAssertImpl() -{ - CurrentAssertImpl = PrevAssertImpl; -} - void SentryAssertImpl::OnAssert(const char* Filename, int LineNumber, const char* FunctionName, const char* Msg) { @@ -128,7 +115,7 @@ SentryAssertImpl::OnAssert(const char* Filename, int LineNumber, const char* Fun sentry_event_value_add_stacktrace(event, NULL, 0); sentry_capture_event(event); } - catch (std::exception&) + catch (const std::exception&) { // If our logging with Message formatting fails we do a non-allocating version and just post the Msg raw sentry_value_t event = sentry_value_new_message_event( diff --git a/src/zenserver/upstream/upstreamcache.cpp b/src/zenserver/upstream/upstreamcache.cpp index dac29c273..6d1d026cc 100644 --- a/src/zenserver/upstream/upstreamcache.cpp +++ b/src/zenserver/upstream/upstreamcache.cpp @@ -152,7 +152,7 @@ namespace detail { return m_Status.EndpointStatus(); } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -292,7 +292,7 @@ namespace detail { return {.Status = {.Error{.ErrorCode = Result.ErrorCode, .Reason = std::move(Result.Reason)}}}; } } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -388,7 +388,7 @@ namespace detail { return {.Status = {.Error{.ErrorCode = Result.ErrorCode, .Reason = std::move(Result.Reason)}}}; } } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -615,7 +615,7 @@ namespace detail { }); } } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -825,7 +825,7 @@ namespace detail { return m_Status.EndpointStatus(); } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -861,7 +861,7 @@ namespace detail { return {.Status = {.Error{.ErrorCode = Result.ErrorCode, .Reason = std::move(Result.Reason)}}}; } } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -984,7 +984,7 @@ namespace detail { return {.Status = {.Error{.ErrorCode = Result.ErrorCode, .Reason = std::move(Result.Reason)}}}; } } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -1405,7 +1405,7 @@ namespace detail { .ElapsedSeconds = TotalElapsedSeconds, .Success = Result.Success}; } - catch (std::exception& Err) + catch (const std::exception& Err) { m_Status.Set(UpstreamEndpointState::kError, Err.what()); @@ -1980,7 +1980,7 @@ private: { ProcessCacheRecord(std::move(CacheRecord)); } - catch (std::exception& Err) + catch (const std::exception& Err) { ZEN_ERROR("upload cache record '{}/{}/{}' FAILED, reason '{}'", CacheRecord.Namespace, @@ -2052,7 +2052,7 @@ private: } } } - catch (std::exception& Err) + catch (const std::exception& Err) { ZEN_ERROR("check endpoint(s) health FAILED, reason '{}'", Err.what()); } diff --git a/src/zenserver/vfs/vfsimpl.cpp b/src/zenserver/vfs/vfsimpl.cpp index 5ef89ee77..5c9f32c69 100644 --- a/src/zenserver/vfs/vfsimpl.cpp +++ b/src/zenserver/vfs/vfsimpl.cpp @@ -238,7 +238,7 @@ VfsService::Impl::VfsThread() m_VfsThreadRunning.Set(); m_VfsHost->Run(); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { ZEN_WARN("exception caught in VFS thread: {}", Ex.what()); diff --git a/src/zenserver/vfs/vfsservice.cpp b/src/zenserver/vfs/vfsservice.cpp index 04ba29ed2..d302a10ec 100644 --- a/src/zenserver/vfs/vfsservice.cpp +++ b/src/zenserver/vfs/vfsservice.cpp @@ -105,7 +105,7 @@ VfsService::VfsService() { m_Impl->Mount(Mountpath); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, Ex.what()); } @@ -123,7 +123,7 @@ VfsService::VfsService() { m_Impl->Unmount(); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, Ex.what()); } diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp index d1faeb8b6..e6e451952 100644 --- a/src/zenserver/zenserver.cpp +++ b/src/zenserver/zenserver.cpp @@ -761,7 +761,7 @@ ZenServer::Cleanup() m_Http = {}; m_JobQueue.reset(); } - catch (std::exception& Ex) + catch (const std::exception& Ex) { ZEN_ERROR("exception thrown during Cleanup() in {}: '{}'", ZEN_APP_NAME, Ex.what()); } @@ -831,7 +831,7 @@ ZenServer::CheckStateMarker() return; } } - catch (std::exception& Ex) + catch (const std::exception& Ex) { ZEN_WARN("state marker at {} could not be checked, reason: '{}'", StateMarkerPath, Ex.what()); RequestExit(1); |