diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h index f94bd5f75..066d76110 100644 --- a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h +++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h @@ -1,124 +1,126 @@ #pragma once #include #include #include #include #include namespace comm { namespace network { namespace reactor { struct ServerBidiReactorStatus { grpc::Status status; bool sendLastResponse; ServerBidiReactorStatus( grpc::Status status = grpc::Status::OK, bool sendLastResponse = false) : status(status), sendLastResponse(sendLastResponse) { } }; template class ServerBidiReactorBase : public grpc::ServerBidiReactor { Request request; Response response; std::atomic finished = false; protected: ServerBidiReactorStatus status; bool readingAborted = false; public: ServerBidiReactorBase(); void OnDone() override; void OnReadDone(bool ok) override; void OnWriteDone(bool ok) override; void terminate(ServerBidiReactorStatus status); virtual std::unique_ptr handleRequest(Request request, Response *response) = 0; virtual void initialize(){}; virtual void validate(){}; virtual void doneCallback(){}; virtual void terminateCallback(){}; }; template ServerBidiReactorBase::ServerBidiReactorBase() { this->initialize(); this->StartRead(&this->request); } template void ServerBidiReactorBase::OnDone() { this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerBidiReactorBase::terminate( ServerBidiReactorStatus status) { this->status = status; try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { this->status = ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::INTERNAL, e.what())); } if (this->finished) { return; } if (this->status.sendLastResponse) { this->StartWriteAndFinish( &this->response, grpc::WriteOptions(), this->status.status); } else { this->Finish(this->status.status); } this->finished = true; } template void ServerBidiReactorBase::OnReadDone(bool ok) { if (!ok) { this->readingAborted = true; // Ending a connection on the other side results in the `ok` flag being set // to false. It makes it impossible to detect a failure based just on the // flag. We should manually check if the data we received is valid this->terminate(ServerBidiReactorStatus(grpc::Status::OK)); return; } try { this->response = Response(); std::unique_ptr status = this->handleRequest(this->request, &this->response); if (status != nullptr) { this->terminate(*status); return; } this->StartWrite(&this->response); } catch (std::runtime_error &e) { this->terminate(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::INTERNAL, e.what()))); } } template void ServerBidiReactorBase::OnWriteDone(bool ok) { if (!ok) { this->terminate(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::ABORTED, "write failed"))); return; } this->StartRead(&this->request); } } // namespace reactor } // namespace network } // namespace comm diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h index 4cf93b92f..49efb664a 100644 --- a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h +++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h @@ -1,95 +1,97 @@ #pragma once #include #include #include #include #include namespace comm { namespace network { namespace reactor { template class ServerReadReactorBase : public grpc::ServerReadReactor { Request request; std::atomic finished = false; void terminate(grpc::Status status); protected: Response *response; grpc::Status status; public: ServerReadReactorBase(Response *response); void OnDone() override; void OnReadDone(bool ok) override; virtual std::unique_ptr readRequest(Request request) = 0; virtual void initialize(){}; virtual void validate(){}; virtual void doneCallback(){}; virtual void terminateCallback(){}; }; template void ServerReadReactorBase::terminate(grpc::Status status) { this->status = status; try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { this->status = grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } if (!this->status.ok()) { std::cout << "error: " << this->status.error_message() << std::endl; } if (this->finished) { return; } this->Finish(this->status); this->finished = true; } template ServerReadReactorBase::ServerReadReactorBase( Response *response) : response(response) { this->initialize(); this->StartRead(&this->request); } template void ServerReadReactorBase::OnDone() { this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerReadReactorBase::OnReadDone(bool ok) { if (!ok) { // Ending a connection on the other side results in the `ok` flag being set // to false. It makes it impossible to detect a failure based just on the // flag. We should manually check if the data we received is valid this->terminate(grpc::Status::OK); return; } try { std::unique_ptr status = this->readRequest(this->request); if (status != nullptr) { this->terminate(*status); return; } } catch (std::runtime_error &e) { this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what())); return; } this->StartRead(&this->request); } } // namespace reactor } // namespace network } // namespace comm diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h index 58df4b634..8496afa52 100644 --- a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h +++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h @@ -1,108 +1,110 @@ #pragma once #include #include #include #include #include namespace comm { namespace network { namespace reactor { template class ServerWriteReactorBase : public grpc::ServerWriteReactor { Response response; bool initialized = false; std::atomic finished = false; void terminate(grpc::Status status); protected: // this is a const ref since it's not meant to be modified const Request &request; grpc::Status status; public: ServerWriteReactorBase(const Request *request); virtual void NextWrite(); void OnDone() override; void OnWriteDone(bool ok) override; virtual std::unique_ptr writeResponse(Response *response) = 0; virtual void initialize(){}; virtual void validate(){}; virtual void doneCallback(){}; virtual void terminateCallback(){}; }; template void ServerWriteReactorBase::terminate(grpc::Status status) { this->status = status; try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { this->status = grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } if (!this->status.ok()) { std::cout << "error: " << this->status.error_message() << std::endl; } if (this->finished) { return; } this->Finish(this->status); this->finished = true; } template ServerWriteReactorBase::ServerWriteReactorBase( const Request *request) : request(*request) { // we cannot call this->NextWrite() here because it's going to call it on // the base class, not derived leading to the runtime error of calling // a pure virtual function // NextWrite has to be exposed as a public function and called explicitly // to initialize writing } template void ServerWriteReactorBase::NextWrite() { try { if (!this->initialized) { this->initialize(); this->initialized = true; } this->response = Response(); std::unique_ptr status = this->writeResponse(&this->response); if (status != nullptr) { this->terminate(*status); return; } this->StartWrite(&this->response); } catch (std::runtime_error &e) { std::cout << "error: " << e.what() << std::endl; this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what())); } } template void ServerWriteReactorBase::OnDone() { this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerWriteReactorBase::OnWriteDone(bool ok) { if (!ok) { this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, "writing error")); return; } this->NextWrite(); } } // namespace reactor } // namespace network } // namespace comm diff --git a/services/blob/src/Reactors/server/base-reactors/ServerBidiReactorBase.h b/services/blob/src/Reactors/server/base-reactors/ServerBidiReactorBase.h index 25ddacbb1..85ae54215 100644 --- a/services/blob/src/Reactors/server/base-reactors/ServerBidiReactorBase.h +++ b/services/blob/src/Reactors/server/base-reactors/ServerBidiReactorBase.h @@ -1,124 +1,126 @@ #pragma once #include #include #include #include #include namespace comm { namespace network { namespace reactor { struct ServerBidiReactorStatus { grpc::Status status; bool sendLastResponse; ServerBidiReactorStatus( grpc::Status status = grpc::Status::OK, bool sendLastResponse = false) : status(status), sendLastResponse(sendLastResponse) { } }; template class ServerBidiReactorBase : public grpc::ServerBidiReactor { Request request; Response response; std::atomic finished = false; protected: ServerBidiReactorStatus status; bool readingAborted = false; public: ServerBidiReactorBase(); void OnDone() override; void OnReadDone(bool ok) override; void OnWriteDone(bool ok) override; void terminate(ServerBidiReactorStatus status); virtual std::unique_ptr handleRequest(Request request, Response *response) = 0; virtual void initialize(){}; virtual void validate(){}; virtual void doneCallback(){}; virtual void terminateCallback(){}; }; template ServerBidiReactorBase::ServerBidiReactorBase() { this->initialize(); this->StartRead(&this->request); } template void ServerBidiReactorBase::OnDone() { this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerBidiReactorBase::terminate( ServerBidiReactorStatus status) { this->status = status; try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { this->status = ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::INTERNAL, e.what())); } if (this->finished) { return; } if (this->status.sendLastResponse) { this->StartWriteAndFinish( &this->response, grpc::WriteOptions(), this->status.status); } else { this->Finish(this->status.status); } this->finished = true; } template void ServerBidiReactorBase::OnReadDone(bool ok) { if (!ok) { this->readingAborted = true; // Ending a connection on the other side results in the `ok` flag being set // to false. It makes it impossible to detect a failure based just on the // flag. We should manually check if the data we received is valid this->terminate(ServerBidiReactorStatus(grpc::Status::OK)); return; } try { this->response = Response(); std::unique_ptr status = this->handleRequest(this->request, &this->response); if (status != nullptr) { this->terminate(*status); return; } this->StartWrite(&this->response); } catch (std::runtime_error &e) { this->terminate(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::INTERNAL, e.what()))); } } template void ServerBidiReactorBase::OnWriteDone(bool ok) { if (!ok) { this->terminate(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::ABORTED, "write failed"))); return; } this->StartRead(&this->request); } } // namespace reactor } // namespace network } // namespace comm diff --git a/services/blob/src/Reactors/server/base-reactors/ServerReadReactorBase.h b/services/blob/src/Reactors/server/base-reactors/ServerReadReactorBase.h index e0f48bd8f..94d1cfe3a 100644 --- a/services/blob/src/Reactors/server/base-reactors/ServerReadReactorBase.h +++ b/services/blob/src/Reactors/server/base-reactors/ServerReadReactorBase.h @@ -1,95 +1,97 @@ #pragma once #include #include #include #include #include namespace comm { namespace network { namespace reactor { template class ServerReadReactorBase : public grpc::ServerReadReactor { Request request; std::atomic finished = false; void terminate(grpc::Status status); protected: Response *response; grpc::Status status; public: ServerReadReactorBase(Response *response); void OnDone() override; void OnReadDone(bool ok) override; virtual std::unique_ptr readRequest(Request request) = 0; virtual void initialize(){}; virtual void validate(){}; virtual void doneCallback(){}; virtual void terminateCallback(){}; }; template void ServerReadReactorBase::terminate(grpc::Status status) { this->status = status; try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { this->status = grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } if (!this->status.ok()) { std::cout << "error: " << this->status.error_message() << std::endl; } if (this->finished) { return; } this->Finish(this->status); this->finished = true; } template ServerReadReactorBase::ServerReadReactorBase( Response *response) : response(response) { this->initialize(); this->StartRead(&this->request); } template void ServerReadReactorBase::OnDone() { this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerReadReactorBase::OnReadDone(bool ok) { if (!ok) { // Ending a connection on the other side results in the `ok` flag being set // to false. It makes it impossible to detect a failure based just on the // flag. We should manually check if the data we received is valid this->terminate(grpc::Status::OK); return; } try { std::unique_ptr status = this->readRequest(this->request); if (status != nullptr) { this->terminate(*status); return; } } catch (std::runtime_error &e) { this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what())); return; } this->StartRead(&this->request); } } // namespace reactor } // namespace network } // namespace comm diff --git a/services/blob/src/Reactors/server/base-reactors/ServerWriteReactorBase.h b/services/blob/src/Reactors/server/base-reactors/ServerWriteReactorBase.h index 58df4b634..8496afa52 100644 --- a/services/blob/src/Reactors/server/base-reactors/ServerWriteReactorBase.h +++ b/services/blob/src/Reactors/server/base-reactors/ServerWriteReactorBase.h @@ -1,108 +1,110 @@ #pragma once #include #include #include #include #include namespace comm { namespace network { namespace reactor { template class ServerWriteReactorBase : public grpc::ServerWriteReactor { Response response; bool initialized = false; std::atomic finished = false; void terminate(grpc::Status status); protected: // this is a const ref since it's not meant to be modified const Request &request; grpc::Status status; public: ServerWriteReactorBase(const Request *request); virtual void NextWrite(); void OnDone() override; void OnWriteDone(bool ok) override; virtual std::unique_ptr writeResponse(Response *response) = 0; virtual void initialize(){}; virtual void validate(){}; virtual void doneCallback(){}; virtual void terminateCallback(){}; }; template void ServerWriteReactorBase::terminate(grpc::Status status) { this->status = status; try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { this->status = grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } if (!this->status.ok()) { std::cout << "error: " << this->status.error_message() << std::endl; } if (this->finished) { return; } this->Finish(this->status); this->finished = true; } template ServerWriteReactorBase::ServerWriteReactorBase( const Request *request) : request(*request) { // we cannot call this->NextWrite() here because it's going to call it on // the base class, not derived leading to the runtime error of calling // a pure virtual function // NextWrite has to be exposed as a public function and called explicitly // to initialize writing } template void ServerWriteReactorBase::NextWrite() { try { if (!this->initialized) { this->initialize(); this->initialized = true; } this->response = Response(); std::unique_ptr status = this->writeResponse(&this->response); if (status != nullptr) { this->terminate(*status); return; } this->StartWrite(&this->response); } catch (std::runtime_error &e) { std::cout << "error: " << e.what() << std::endl; this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what())); } } template void ServerWriteReactorBase::OnDone() { this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerWriteReactorBase::OnWriteDone(bool ok) { if (!ok) { this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, "writing error")); return; } this->NextWrite(); } } // namespace reactor } // namespace network } // namespace comm