memgraph/tests/manual/ssl_server.cpp
Matej Ferencevic 53c405c699 Throw exceptions on RPC failure and Distributed error handling
Summary:
This diff changes the RPC layer to directly return `TResponse` to the user when
issuing a `Call<...>` RPC call. The call throws an exception on failure
(instead of the previous return `nullopt`).

All servers (network, RPC and distributed) are set to have explicit `Shutdown`
methods so that a controlled shutdown can always be performed. The object
destructors now have `CHECK`s to enforce that the `AwaitShutdown` methods were
called.

The distributed memgraph is changed that none of the binaries (master/workers)
crash when there is a communication failure. Instead, the whole cluster starts
a graceful shutdown when a persistent communication error is detected.
Transient errors are allowed during execution. The transaction that errored out
will be aborted on the whole cluster. The cluster state is managed using a new
Heartbeat RPC call.

Reviewers: buda, teon.banek, msantl

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1604
2018-09-27 16:27:40 +02:00

78 lines
2.3 KiB
C++

#include <atomic>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "communication/server.hpp"
#include "utils/exceptions.hpp"
DEFINE_string(address, "127.0.0.1", "Server address");
DEFINE_int32(port, 54321, "Server port");
DEFINE_string(cert_file, "", "Certificate file to use.");
DEFINE_string(key_file, "", "Key file to use.");
DEFINE_string(ca_file, "", "CA file to use.");
DEFINE_bool(verify_peer, false, "Set to true to verify the peer.");
struct EchoData {
std::atomic<bool> alive{true};
};
class EchoSession {
public:
EchoSession(EchoData *data, const io::network::Endpoint &,
communication::InputStream *input_stream,
communication::OutputStream *output_stream)
: data_(data),
input_stream_(input_stream),
output_stream_(output_stream) {}
void Execute() {
if (input_stream_->size() < 2) return;
const uint8_t *data = input_stream_->data();
uint16_t size = *reinterpret_cast<const uint16_t *>(input_stream_->data());
input_stream_->Resize(size + 2);
if (input_stream_->size() < size + 2) return;
if (size == 0) {
LOG(INFO) << "Server received EOF message";
data_->alive.store(false);
return;
}
LOG(INFO) << "Server received '"
<< std::string(reinterpret_cast<const char *>(data + 2), size)
<< "'";
if (!output_stream_->Write(data + 2, size)) {
throw utils::BasicException("Output stream write failed!");
}
input_stream_->Shift(size + 2);
}
private:
EchoData *data_;
communication::InputStream *input_stream_;
communication::OutputStream *output_stream_;
};
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
communication::Init();
// Initialize the server.
EchoData echo_data;
io::network::Endpoint endpoint(FLAGS_address, FLAGS_port);
communication::ServerContext context(FLAGS_key_file, FLAGS_cert_file,
FLAGS_ca_file, FLAGS_verify_peer);
communication::Server<EchoSession, EchoData> server(endpoint, &echo_data,
&context, -1, "SSL", 1);
while (echo_data.alive) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
server.Shutdown();
server.AwaitShutdown();
return 0;
}