2018-03-07 20:59:52 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "communication/result_stream_faker.hpp"
|
|
|
|
#include "database/graph_db_accessor.hpp"
|
Extract communication to static library
Summary:
Session specifics have been move out of the Bolt `executing` state, and
are accessed via pure virtual Session type. Our server is templated on
the session and we are setting the concrete type, so there should be no
virtual call overhead. Abstract Session is used to indicate the
interface, this could have also been templated, but the explicit
interface definition makes it clearer.
Specific session implementation for running Memgraph is now implemented
in memgraph_bolt, which instantiates the concrete session type. This may
not be 100% appropriate place, but Memgraph specific session isn't
needed anywhere else.
Bolt/communication tests now use a dummy session and depend only on
communication, which significantly improves test run times.
All these changes make the communication a library which doesn't depend
on storage nor the database. Only shared connection points, which aren't
part of the base communication library are:
* glue/conversion -- which converts between storage and bolt types, and
* communication/result_stream_faker -- templated, but used in tests and query/repl
Depends on D1453
Reviewers: mferencevic, buda, mtomic, msantl
Reviewed By: mferencevic, mtomic
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D1456
2018-07-10 22:18:19 +08:00
|
|
|
#include "glue/conversion.hpp"
|
2018-03-07 20:59:52 +08:00
|
|
|
#include "query/interpreter.hpp"
|
|
|
|
#include "query/typed_value.hpp"
|
|
|
|
|
|
|
|
class WorkerInThread {
|
|
|
|
public:
|
|
|
|
explicit WorkerInThread(database::Config config) : worker_(config) {
|
|
|
|
thread_ = std::thread([this, config] { worker_.WaitForShutdown(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerInThread() {
|
|
|
|
if (thread_.joinable()) thread_.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
database::Worker worker_;
|
|
|
|
std::thread thread_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Cluster {
|
|
|
|
const std::chrono::microseconds kInitTime{200};
|
|
|
|
const std::string kLocal = "127.0.0.1";
|
|
|
|
|
|
|
|
public:
|
|
|
|
Cluster(int worker_count) {
|
|
|
|
database::Config masterconfig;
|
|
|
|
masterconfig.master_endpoint = {kLocal, 0};
|
|
|
|
master_ = std::make_unique<database::Master>(masterconfig);
|
2018-03-13 17:35:14 +08:00
|
|
|
interpreter_ = std::make_unique<query::Interpreter>(*master_);
|
2018-03-07 20:59:52 +08:00
|
|
|
std::this_thread::sleep_for(kInitTime);
|
|
|
|
|
|
|
|
auto worker_config = [this](int worker_id) {
|
|
|
|
database::Config config;
|
|
|
|
config.worker_id = worker_id;
|
|
|
|
config.master_endpoint = master_->endpoint();
|
|
|
|
config.worker_endpoint = {kLocal, 0};
|
|
|
|
return config;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < worker_count; ++i) {
|
|
|
|
workers_.emplace_back(
|
|
|
|
std::make_unique<WorkerInThread>(worker_config(i + 1)));
|
|
|
|
std::this_thread::sleep_for(kInitTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() {
|
2018-03-13 17:35:14 +08:00
|
|
|
interpreter_ = nullptr;
|
2018-07-11 20:34:19 +08:00
|
|
|
auto t = std::thread([this]() { master_ = nullptr; });
|
2018-03-07 20:59:52 +08:00
|
|
|
workers_.clear();
|
2018-07-11 20:34:19 +08:00
|
|
|
if (t.joinable()) t.join();
|
2018-03-07 20:59:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~Cluster() {
|
|
|
|
if (master_) Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Execute(const std::string &query,
|
|
|
|
std::map<std::string, query::TypedValue> params = {}) {
|
|
|
|
database::GraphDbAccessor dba(*master_);
|
2018-07-02 21:34:33 +08:00
|
|
|
ResultStreamFaker<query::TypedValue> result;
|
2018-03-13 17:35:14 +08:00
|
|
|
interpreter_->operator()(query, dba, params, false).PullAll(result);
|
2018-03-07 20:59:52 +08:00
|
|
|
dba.Commit();
|
|
|
|
return result.GetResults();
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<database::Master> master_;
|
|
|
|
std::vector<std::unique_ptr<WorkerInThread>> workers_;
|
2018-03-13 17:35:14 +08:00
|
|
|
std::unique_ptr<query::Interpreter> interpreter_;
|
2018-03-07 20:59:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void CheckResults(
|
|
|
|
const std::vector<std::vector<query::TypedValue>> &results,
|
|
|
|
const std::vector<std::vector<query::TypedValue>> &expected_rows,
|
|
|
|
const std::string &msg) {
|
|
|
|
query::TypedValue::BoolEqual equality;
|
|
|
|
CHECK(results.size() == expected_rows.size())
|
|
|
|
<< msg << " (expected " << expected_rows.size() << " rows "
|
|
|
|
<< ", got " << results.size() << ")";
|
|
|
|
for (size_t row_id = 0; row_id < results.size(); ++row_id) {
|
|
|
|
auto &result = results[row_id];
|
|
|
|
auto &expected = expected_rows[row_id];
|
|
|
|
CHECK(result.size() == expected.size())
|
|
|
|
<< msg << " (expected " << expected.size() << " elements in row "
|
|
|
|
<< row_id << ", got " << result.size() << ")";
|
|
|
|
for (size_t col_id = 0; col_id < result.size(); ++col_id) {
|
|
|
|
CHECK(equality(result[col_id], expected[col_id]))
|
|
|
|
<< msg << " (expected value '" << expected[col_id] << "' got '"
|
|
|
|
<< result[col_id] << "' in row " << row_id << " col " << col_id
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|