diff --git a/services/backup/docker-server/contents/server/src/Constants.h b/services/backup/docker-server/contents/server/src/Constants.h --- a/services/backup/docker-server/contents/server/src/Constants.h +++ b/services/backup/docker-server/contents/server/src/Constants.h @@ -30,5 +30,12 @@ const std::string BACKUP_TABLE_NAME = "backup-service-backup"; #endif +// This has to be smaller than GRPC_CHUNK_SIZE_LIMIT because we need to +// recognize if we may receive multiple chunks or just one. If it was larger +// than the chunk limit, once we get the amount of data of size equal to the +// limit, we wouldn't know if we should put this in the database right away or +// wait for more data. +const size_t LOG_DATA_SIZE_DATABASE_LIMIT = 1 * 1024 * 1024; + } // namespace network } // namespace comm diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/SendLogReactor.h b/services/backup/docker-server/contents/server/src/Reactors/server/SendLogReactor.h --- a/services/backup/docker-server/contents/server/src/Reactors/server/SendLogReactor.h +++ b/services/backup/docker-server/contents/server/src/Reactors/server/SendLogReactor.h @@ -1,5 +1,6 @@ #pragma once +#include "Constants.h" #include "ServerReadReactorBase.h" #include "../_generated/backup.grpc.pb.h" @@ -21,9 +22,22 @@ LOG_CHUNK = 2, }; + enum class PersistenceMethod { + UNKNOWN = 0, + DB = 1, + BLOB = 2, + }; + State state = State::USER_ID; + PersistenceMethod persistenceMethod = PersistenceMethod::UNKNOWN; std::string userID; + void storeInDatabase(const std::string &data) { + } + + void storeInBlob(const std::string &data) { + } + public: using ServerReadReactorBase:: ServerReadReactorBase; @@ -48,8 +62,35 @@ if (!request.has_logdata()) { throw std::runtime_error("log data expected but not received"); } - std::string chunk = request.logdata(); - std::cout << "log data received " << chunk << std::endl; + if (this->persistenceMethod == PersistenceMethod::DB) { + throw std::runtime_error( + "storing multiple chunks in the database is not allowed"); + } + std::string *chunk = request.mutable_logdata(); + // decide if keep in DB or upload to blob + if (chunk->size() <= LOG_DATA_SIZE_DATABASE_LIMIT) { + if (this->persistenceMethod == PersistenceMethod::UNKNOWN) { + this->persistenceMethod = PersistenceMethod::DB; + this->storeInDatabase(*chunk); + } else if (this->persistenceMethod == PersistenceMethod::BLOB) { + this->storeInBlob(*chunk); + } else { + throw std::runtime_error( + "error - invalid persistence state for chunk smaller than " + "database limit"); + } + } else { + if (this->persistenceMethod != PersistenceMethod::UNKNOWN && + this->persistenceMethod != PersistenceMethod::BLOB) { + throw std::runtime_error( + "error - invalid persistence state, uploading to blob should be " + "continued but it is not"); + } + this->persistenceMethod = PersistenceMethod::BLOB; + this->storeInBlob(*chunk); + } + std::cout << "log data received " << chunk->size() << std::endl; + return nullptr; }; } throw std::runtime_error("send log - invalid state");