Temporary workaround for raft network problem

Reviewers: mtomic

Reviewed By: mtomic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1085
This commit is contained in:
Mislav Bradac 2017-12-27 14:30:18 +01:00
parent d3623585e7
commit 8952df06c1
2 changed files with 30 additions and 8 deletions

View File

@ -9,7 +9,7 @@ System::System(const std::string &address, uint16_t port)
StartServer(4); StartServer(4);
} }
System::System(const io::network::NetworkEndpoint &endpoint) System::System(const io::network::NetworkEndpoint &endpoint)
: System(endpoint.address(), endpoint.port()) {} : System(endpoint.address(), endpoint.port()) {}
System::~System() { System::~System() {

View File

@ -1,4 +1,5 @@
#include <sstream> #include <sstream>
#include <unordered_map>
#include "boost/archive/binary_iarchive.hpp" #include "boost/archive/binary_iarchive.hpp"
#include "boost/archive/binary_oarchive.hpp" #include "boost/archive/binary_oarchive.hpp"
@ -66,20 +67,41 @@ bool SendLength(Socket &socket, SizeT length) {
return socket.Write(reinterpret_cast<uint8_t *>(&length), sizeof(SizeT)); return socket.Write(reinterpret_cast<uint8_t *>(&length), sizeof(SizeT));
} }
struct PairHash {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const {
return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
}
};
void SendMessage(const std::string &address, uint16_t port, void SendMessage(const std::string &address, uint16_t port,
const std::string &channel, std::unique_ptr<Message> message) { const std::string &channel, std::unique_ptr<Message> message) {
static thread_local std::unordered_map<std::pair<std::string, uint16_t>,
Socket, PairHash>
cache;
CHECK(message) << "Trying to send nullptr instead of message"; CHECK(message) << "Trying to send nullptr instead of message";
// Initialize endpoint. auto it = cache.find({address, port});
Endpoint endpoint(address.c_str(), port); if (it == cache.end()) {
// Initialize endpoint.
Endpoint endpoint(address.c_str(), port);
Socket socket; Socket socket;
if (!socket.Connect(endpoint)) { if (!socket.Connect(endpoint)) {
LOG(INFO) << "Couldn't connect to remote address: " << address << ":" LOG(INFO) << "Couldn't connect to remote address: " << address << ":"
<< port; << port;
return; return;
}
it = cache
.emplace(std::piecewise_construct,
std::forward_as_tuple(address, port),
std::forward_as_tuple(std::move(socket)))
.first;
} }
auto &socket = it->second;
if (!SendLength(socket, channel.size())) { if (!SendLength(socket, channel.size())) {
LOG(INFO) << "Couldn't send channel size!"; LOG(INFO) << "Couldn't send channel size!";
return; return;