Skip to content

Commit

Permalink
allow fetching PersisterRequestResult from PersisterFacade
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Nov 3, 2024
1 parent b704e9a commit d019be7
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 29 deletions.
1 change: 1 addition & 0 deletions source/Base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(Base
Singleton.h
StringHelper.cpp
StringHelper.h
UnlockGuard.h
Vector2D.cpp
Vector2D.h
VersionChecker.cpp
Expand Down
17 changes: 17 additions & 0 deletions source/Base/UnlockGuard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <mutex>

class UnlockGuard
{
public:
UnlockGuard(std::unique_lock<std::mutex>& lock)
: _lock(lock)
{
_lock.unlock();
}
~UnlockGuard() { _lock.lock(); }

private:
std::unique_lock<std::mutex>& _lock;
};
1 change: 0 additions & 1 deletion source/PersisterImpl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_library(PersisterImpl
PersisterRequest.h
PersisterRequestError.cpp
PersisterRequestError.h
PersisterRequestResult.h
PersisterWorker.cpp
PersisterWorker.h)

Expand Down
20 changes: 17 additions & 3 deletions source/PersisterImpl/PersisterFacadeImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "PersisterFacadeImpl.h"

#include "PersisterInterface/DeserializedSimulation.h"
#include "EngineInterface/SimulationFacade.h"

#include "PersisterRequestResult.h"
#include "PersisterInterface/DeserializedSimulation.h"
#include "PersisterInterface/PersisterRequestResult.h"

_PersisterFacadeImpl::~_PersisterFacadeImpl()
{
Expand Down Expand Up @@ -46,6 +45,11 @@ std::optional<PersisterRequestState> _PersisterFacadeImpl::getRequestState(Persi
return _worker->getRequestState(id);
}

PersisterRequestResult _PersisterFacadeImpl::fetchPersisterRequestResult(PersisterRequestId const& id)
{
return _worker->fetchRequestResult(id);
}

std::vector<PersisterErrorInfo> _PersisterFacadeImpl::fetchAllErrorInfos(SenderId const& senderId)
{
return _worker->fetchAllErrorInfos(senderId);
Expand Down Expand Up @@ -186,6 +190,16 @@ GetPeakSimulationResultData _PersisterFacadeImpl::fetchGetPeakSimulationData(Per
return fetchData<_GetPeakSimulationRequestResult, GetPeakSimulationResultData>(id);
}

PersisterRequestId _PersisterFacadeImpl::scheduleSaveDeserializedSimulation(SenderInfo const& senderInfo, SaveDeserializedSimulationRequestData const& data)
{
return scheduleRequest<_SaveDeserializedSimulationRequest>(senderInfo, data);
}

SaveDeserializedSimulationResultData _PersisterFacadeImpl::fetchSaveDeserializedSimulationData(PersisterRequestId const& id)
{
return fetchData<_SaveDeserializedSimulationRequestResult, SaveDeserializedSimulationResultData>(id);
}

PersisterRequestId _PersisterFacadeImpl::generateNewRequestId()
{
++_latestRequestId;
Expand Down
4 changes: 4 additions & 0 deletions source/PersisterImpl/PersisterFacadeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class _PersisterFacadeImpl : public _PersisterFacade

bool isBusy() const override;
std::optional<PersisterRequestState> getRequestState(PersisterRequestId const& id) const override;
PersisterRequestResult fetchPersisterRequestResult(PersisterRequestId const& id) override;
std::vector<PersisterErrorInfo> fetchAllErrorInfos(SenderId const& senderId) override;
PersisterErrorInfo fetchError(PersisterRequestId const& id) override;

Expand Down Expand Up @@ -61,6 +62,9 @@ class _PersisterFacadeImpl : public _PersisterFacade
PersisterRequestId scheduleGetPeakSimulation(SenderInfo const& senderInfo, GetPeakSimulationRequestData const& data) override;
GetPeakSimulationResultData fetchGetPeakSimulationData(PersisterRequestId const& id) override;

PersisterRequestId scheduleSaveDeserializedSimulation(SenderInfo const& senderInfo, SaveDeserializedSimulationRequestData const& data) override;
SaveDeserializedSimulationResultData fetchSaveDeserializedSimulationData(PersisterRequestId const& id) override;

private:
static auto constexpr MaxWorkerThreads = 4;

Expand Down
6 changes: 5 additions & 1 deletion source/PersisterImpl/PersisterRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#include "PersisterInterface/ReadSimulationRequestData.h"
#include "PersisterInterface/PersisterRequestId.h"
#include "PersisterInterface/ReplaceNetworkResourceRequestData.h"
#include "PersisterInterface/SenderInfo.h"
#include "PersisterInterface/SaveDeserializedSimulationRequestData.h"
#include "PersisterInterface/SaveSimulationRequestData.h"
#include "PersisterInterface/SenderInfo.h"
#include "PersisterInterface/ToggleReactionNetworkResourceRequestData.h"
#include "PersisterInterface/UploadNetworkResourceRequestData.h"

Expand Down Expand Up @@ -95,3 +96,6 @@ using ToggleReactionNetworkResourceRequest = std::shared_ptr<_ToggleReactionNetw

using _GetPeakSimulationRequest = _ConcreteRequest<GetPeakSimulationRequestData>;
using GetPeakSimulationRequest = std::shared_ptr<_GetPeakSimulationRequest>;

using _SaveDeserializedSimulationRequest = _ConcreteRequest<SaveDeserializedSimulationRequestData>;
using SaveDeserializedSimulationRequest = std::shared_ptr<_SaveDeserializedSimulationRequest>;
68 changes: 51 additions & 17 deletions source/PersisterImpl/PersisterWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include "Base/LoggingService.h"
#include "Base/StringHelper.h"
#include "Base/UnlockGuard.h"
#include "PersisterInterface/SerializerService.h"
#include "PersisterInterface/PersisterRequestResult.h"
#include "EngineInterface/SimulationFacade.h"
#include "EngineInterface/GenomeDescriptionService.h"
#include "Network/NetworkService.h"
Expand Down Expand Up @@ -156,6 +158,8 @@ void _PersisterWorker::processRequests(std::unique_lock<std::mutex>& lock)
processingResult = processRequest(lock, concreteRequest);
} else if (auto const& concreteRequest = std::dynamic_pointer_cast<_GetPeakSimulationRequest>(request)) {
processingResult = processRequest(lock, concreteRequest);
} else if (auto const& concreteRequest = std::dynamic_pointer_cast<_SaveDeserializedSimulationRequest>(request)) {
processingResult = processRequest(lock, concreteRequest);
}
auto inProgressJobsIter = std::ranges::find_if(
_inProgressRequests, [&](PersisterRequest const& otherRequest) { return otherRequest->getRequestId() == request->getRequestId(); });
Expand All @@ -176,15 +180,17 @@ void _PersisterWorker::processRequests(std::unique_lock<std::mutex>& lock)

namespace
{
class UnlockGuard
std::filesystem::path generateFilename(std::filesystem::path const& directory, uint64_t timestep)
{
public:
UnlockGuard(std::unique_lock<std::mutex>& lock) : _lock(lock) { _lock.unlock(); }
~UnlockGuard() { _lock.lock(); }

private:
std::unique_lock<std::mutex>& _lock;
};
std::filesystem::path result;
int i = 0;
do {
auto postfix = i == 0 ? std::string() : "-" + std::to_string(i);
result = directory / ("save_" + StringHelper::format(timestep, '_') + postfix + ".sim");
++i;
} while (std::filesystem::exists(result) && i < 100);
return result;
}
}

auto _PersisterWorker::processRequest(std::unique_lock<std::mutex>& lock, SaveSimulationRequest const& request) -> PersisterRequestResultOrError
Expand Down Expand Up @@ -215,14 +221,7 @@ auto _PersisterWorker::processRequest(std::unique_lock<std::mutex>& lock, SaveSi
try {
auto filename = requestData.filename;
if (requestData.generateNameFromTimestep) {
std::filesystem::path fullFilename;
int i = 0;
do {
auto postfix = i == 0 ? std::string() : "-" + std::to_string(i);
fullFilename = filename / ("save_" + StringHelper::format(deserializedData.auxiliaryData.timestep, '_') + postfix + ".sim");
++i;
} while (std::filesystem::exists(fullFilename) && i < 100);
filename = fullFilename;
filename = generateFilename(filename, deserializedData.auxiliaryData.timestep);
}
if (!SerializerService::get().serializeSimulationToFiles(filename, deserializedData)) {
throw std::runtime_error("Error");
Expand Down Expand Up @@ -640,11 +639,46 @@ _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest
deserializedSimulation.auxiliaryData.simulationParameters = _simulationFacade->getSimulationParameters();
deserializedSimulation.auxiliaryData.timestep = static_cast<uint32_t>(_simulationFacade->getCurrentTimestep());
deserializedSimulation.mainData = _simulationFacade->getClusteredSimulationData();
requestData.peakDeserializedSimulation->setDeserializedSimulation(std::move(deserializedSimulation));
requestData.peakDeserializedSimulation->set(std::move(deserializedSimulation));
}
return std::make_shared<_GetPeakSimulationRequestResult>(request->getRequestId(), GetPeakSimulationResultData());
} catch (...) {
return std::make_shared<_PersisterRequestError>(
request->getRequestId(), request->getSenderInfo().senderId, PersisterErrorInfo{"No valid data could be obtained from the GPU."});
}
}

_PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest(
std::unique_lock<std::mutex>& lock,
SaveDeserializedSimulationRequest const& request)
{
try {
UnlockGuard unlockGuard(lock);

auto const& requestData = request->getData();

auto timestamp = std::chrono::system_clock::now();
auto deserializedData = requestData.sharedDeserializedSimulation->get();

auto filename = requestData.filename;
if (requestData.generateNameFromTimestep) {
filename = generateFilename(filename, deserializedData.auxiliaryData.timestep);
}
if (!SerializerService::get().serializeSimulationToFiles(filename, deserializedData)) {
throw std::runtime_error("Error");
}

return std::make_shared<_SaveDeserializedSimulationRequestResult>(
request->getRequestId(),
SaveDeserializedSimulationResultData{
.filename = filename,
.projectName = deserializedData.auxiliaryData.simulationParameters.projectName,
.timestep = deserializedData.auxiliaryData.timestep,
.timestamp = timestamp});
} catch (...) {
return std::make_shared<_PersisterRequestError>(
request->getRequestId(),
request->getSenderInfo().senderId,
PersisterErrorInfo{"The simulation could not be saved because an error occurred when writing the data to the specified file."});
}
}
3 changes: 2 additions & 1 deletion source/PersisterImpl/PersisterWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <condition_variable>

#include "PersisterInterface/PersisterRequestState.h"
#include "PersisterInterface/PersisterRequestResult.h"

#include "Definitions.h"
#include "PersisterRequest.h"
#include "PersisterRequestError.h"
#include "PersisterRequestResult.h"

class _PersisterWorker
{
Expand Down Expand Up @@ -47,6 +47,7 @@ class _PersisterWorker
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, MoveNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, ToggleReactionNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, GetPeakSimulationRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, SaveDeserializedSimulationRequest const& request);

SimulationFacade _simulationFacade;

Expand Down
1 change: 1 addition & 0 deletions source/PersisterInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_library(PersisterInterface
PersisterErrorInfo.h
PersisterFacade.h
PersisterRequestId.h
PersisterRequestResult.h
PersisterRequestState.h
ReadSimulationRequestData.h
ReadSimulationResultData.h
Expand Down
7 changes: 7 additions & 0 deletions source/PersisterInterface/PersisterFacade.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "PersisterRequestState.h"
#include "ReplaceNetworkResourceRequestData.h"
#include "ReplaceNetworkResourceResultData.h"
#include "SaveDeserializedSimulationRequestData.h"
#include "SaveDeserializedSimulationResultData.h"
#include "SaveSimulationResultData.h"
#include "SaveSimulationRequestData.h"
#include "SenderId.h"
Expand All @@ -37,6 +39,7 @@
#include "ToggleReactionNetworkResourceResultData.h"
#include "UploadNetworkResourceRequestData.h"
#include "UploadNetworkResourceResultData.h"
#include "PersisterRequestResult.h"

class _PersisterFacade
{
Expand All @@ -51,6 +54,7 @@ class _PersisterFacade
//generic logic
virtual bool isBusy() const = 0;
virtual std::optional<PersisterRequestState> getRequestState(PersisterRequestId const& id) const = 0;
virtual PersisterRequestResult fetchPersisterRequestResult(PersisterRequestId const& id) = 0;
virtual std::vector<PersisterErrorInfo> fetchAllErrorInfos(SenderId const& senderId) = 0;
virtual PersisterErrorInfo fetchError(PersisterRequestId const& id) = 0;

Expand Down Expand Up @@ -93,4 +97,7 @@ class _PersisterFacade

virtual PersisterRequestId scheduleGetPeakSimulation(SenderInfo const& senderInfo, GetPeakSimulationRequestData const& data) = 0;
virtual GetPeakSimulationResultData fetchGetPeakSimulationData(PersisterRequestId const& id) = 0;

virtual PersisterRequestId scheduleSaveDeserializedSimulation(SenderInfo const& senderInfo, SaveDeserializedSimulationRequestData const& data) = 0;
virtual SaveDeserializedSimulationResultData fetchSaveDeserializedSimulationData(PersisterRequestId const& id) = 0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
#include "PersisterInterface/ReadSimulationResultData.h"
#include "PersisterInterface/PersisterRequestId.h"
#include "PersisterInterface/SaveSimulationResultData.h"
#include "PersisterInterface/LoginResultData.h"
#include "PersisterInterface/GetNetworkResourcesResultData.h"
#include "PersisterInterface/DownloadNetworkResourceResultData.h"
#include "PersisterInterface/UploadNetworkResourceResultData.h"
#include "PersisterInterface/ReplaceNetworkResourceResultData.h"
#include "PersisterInterface/GetUserNamesForReactionResultData.h"
#include "PersisterInterface/DeleteNetworkResourceResultData.h"
#include "PersisterInterface/EditNetworkResourceResultData.h"
#include "PersisterInterface/MoveNetworkResourceResultData.h"
#include "PersisterInterface/ToggleReactionNetworkResourceResultData.h"
#include "PersisterInterface/GetPeakSimulationResultData.h"
#include "PersisterInterface/SaveDeserializedSimulationResultData.h"

class _PersisterRequestResult
{
Expand Down Expand Up @@ -52,3 +63,4 @@ using _EditNetworkResourceRequestResult = _ConcreteRequestResult<EditNetworkReso
using _MoveNetworkResourceRequestResult = _ConcreteRequestResult<MoveNetworkResourceResultData>;
using _ToggleReactionNetworkResourceRequestResult = _ConcreteRequestResult<ToggleReactionNetworkResourceResultData>;
using _GetPeakSimulationRequestResult = _ConcreteRequestResult<GetPeakSimulationResultData>;
using _SaveDeserializedSimulationRequestResult = _ConcreteRequestResult<SaveDeserializedSimulationResultData>;
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#pragma once

#include <filesystem>
#include <string>

#include "Base/Vector2D.h"

#include "SharedDeserializedSimulation.h"

struct SaveDeserializedSimulationRequestData
{
std::filesystem::path filename;
float zoom = 1.0f;
RealVector2D center;
SharedDeserializedSimulation sharedDeserializedSimulation;
bool generateNameFromTimestep = false;
};
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#pragma once

struct SaveDeserializedSimulationResultData
{
std::filesystem::path filename;
std::string projectName;
uint64_t timestep = 0;
std::chrono::system_clock::time_point timestamp;
};
8 changes: 7 additions & 1 deletion source/PersisterInterface/SharedDeserializedSimulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ class _SharedDeserializedSimulation
return _deserializedSimulation.statistics.back();
}

void setDeserializedSimulation(DeserializedSimulation&& value)
void set(DeserializedSimulation&& value)
{
std::lock_guard lock(_mutex);
_deserializedSimulation = std::move(value);
}

DeserializedSimulation get() const
{
std::lock_guard lock(_mutex);
return _deserializedSimulation;
}

private:
mutable std::mutex _mutex;
DeserializedSimulation _deserializedSimulation;
Expand Down

0 comments on commit d019be7

Please sign in to comment.