2018-01-31 22:58:48 +08:00
|
|
|
#include <memory>
|
2018-01-22 19:36:14 +08:00
|
|
|
#include <thread>
|
2018-01-31 22:58:48 +08:00
|
|
|
#include <unordered_set>
|
2018-01-22 19:36:14 +08:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2018-10-05 18:37:23 +08:00
|
|
|
#include "database/distributed/graph_db.hpp"
|
2018-01-22 19:36:14 +08:00
|
|
|
#include "distributed/coordination.hpp"
|
|
|
|
#include "distributed/coordination_master.hpp"
|
|
|
|
#include "distributed/coordination_worker.hpp"
|
2018-03-23 22:21:46 +08:00
|
|
|
#include "distributed/data_rpc_clients.hpp"
|
|
|
|
#include "distributed/data_rpc_server.hpp"
|
2018-01-24 16:54:38 +08:00
|
|
|
#include "distributed/plan_consumer.hpp"
|
|
|
|
#include "distributed/plan_dispatcher.hpp"
|
2018-03-23 22:21:46 +08:00
|
|
|
#include "distributed/pull_rpc_clients.hpp"
|
2018-02-08 20:57:03 +08:00
|
|
|
#include "distributed_common.hpp"
|
2018-01-22 19:36:14 +08:00
|
|
|
#include "io/network/endpoint.hpp"
|
2018-01-25 17:09:54 +08:00
|
|
|
#include "query/frontend/ast/ast.hpp"
|
|
|
|
#include "query/frontend/ast/cypher_main_visitor.hpp"
|
|
|
|
#include "query/frontend/semantic/symbol_generator.hpp"
|
|
|
|
#include "query/frontend/semantic/symbol_table.hpp"
|
2018-02-02 18:08:27 +08:00
|
|
|
#include "query/interpreter.hpp"
|
2018-02-08 20:57:03 +08:00
|
|
|
#include "query/plan/planner.hpp"
|
2018-02-02 18:08:27 +08:00
|
|
|
#include "query/typed_value.hpp"
|
2018-01-25 17:09:54 +08:00
|
|
|
#include "query_common.hpp"
|
|
|
|
#include "query_plan_common.hpp"
|
2018-09-05 02:30:58 +08:00
|
|
|
#include "transactions/distributed/engine_master.hpp"
|
2018-01-25 17:09:54 +08:00
|
|
|
|
Split GraphDb to distributed and single node files
Summary:
This change, hopefully, simplifies the implementation of different kinds
of GraphDb. The pimpl idiom is now simplified by removing all of the
crazy inheritance. Implementations classes are just plain data stores,
without any methods. The interface classes now have a more flat
hierarchy:
```
GraphDb (pure interface)
|
+----+---------- DistributedGraphDb (pure interface)
| |
Single Node +-----+------+
| |
Master Worker
```
DistributedGraphDb is used as an intermediate interface for all the
things that should work only in distributed. Therefore, virtual calls
for distributed stuff have been removed from GraphDb. Some are exposed
via DistributedGraphDb, other's are only in concrete Master and Worker
classes. The code which relied on those virtual calls has been
refactored to either use DistributedGraphDb, take a pointer to what is
actually needed or use dynamic_cast. Obviously, dynamic_cast is a
temporary solution and should be replaced with another mechanism (e.g.
virtual call, or some other function pointer style).
The cost of the above change is some code duplication in constructors
and destructors of classes. This duplication has a lot of little tweaks
that make it hard to generalize, not to mention that virtual calls do
not work in constructor and destructor. If we really care about
generalizing this, we should think about abandoning RAII in favor of
constructor + Init method.
The next steps for splitting the dependencies that seem logical are:
1) Split GraphDbAccessor implementation, either via inheritance or
passing in an implementation pointer. GraphDbAccessor should then
only be created by a virtual call on GraphDb.
2) Split Interpreter implementation. Besides allowing single node
interpreter to exist without depending on distributed, this will
enable the planner and operators to be correctly separated.
Reviewers: msantl, mferencevic, ipaljak
Reviewed By: msantl
Subscribers: dgleich, pullbot
Differential Revision: https://phabricator.memgraph.io/D1493
2018-07-19 23:00:50 +08:00
|
|
|
using database::GraphDbAccessor;
|
2018-01-22 19:36:14 +08:00
|
|
|
using namespace distributed;
|
2018-06-15 03:15:01 +08:00
|
|
|
using namespace std::literals::chrono_literals;
|
2018-01-22 19:36:14 +08:00
|
|
|
|
2018-07-05 16:55:00 +08:00
|
|
|
class DistributedGraphDb : public DistributedGraphDbTest {
|
|
|
|
public:
|
|
|
|
DistributedGraphDb() : DistributedGraphDbTest("distributed_graph") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(DistributedGraphDb, Coordination) {
|
2018-01-22 19:36:14 +08:00
|
|
|
EXPECT_NE(master().endpoint().port(), 0);
|
2018-02-05 23:28:22 +08:00
|
|
|
EXPECT_NE(worker(1).endpoint().port(), 0);
|
|
|
|
EXPECT_NE(worker(2).endpoint().port(), 0);
|
|
|
|
|
|
|
|
EXPECT_EQ(master().GetEndpoint(1), worker(1).endpoint());
|
|
|
|
EXPECT_EQ(master().GetEndpoint(2), worker(2).endpoint());
|
|
|
|
EXPECT_EQ(worker(1).GetEndpoint(0), master().endpoint());
|
|
|
|
EXPECT_EQ(worker(1).GetEndpoint(2), worker(2).endpoint());
|
|
|
|
EXPECT_EQ(worker(2).GetEndpoint(0), master().endpoint());
|
|
|
|
EXPECT_EQ(worker(2).GetEndpoint(1), worker(1).endpoint());
|
2018-01-22 19:36:14 +08:00
|
|
|
}
|
|
|
|
|
2018-07-05 16:55:00 +08:00
|
|
|
TEST_F(DistributedGraphDb, TxEngine) {
|
2018-07-26 15:08:21 +08:00
|
|
|
auto *tx1 = master().tx_engine().Begin();
|
|
|
|
auto *tx2 = master().tx_engine().Begin();
|
2018-01-22 19:36:14 +08:00
|
|
|
EXPECT_EQ(tx2->snapshot().size(), 1);
|
|
|
|
EXPECT_EQ(
|
2018-02-05 23:28:22 +08:00
|
|
|
worker(1).tx_engine().RunningTransaction(tx1->id_)->snapshot().size(), 0);
|
|
|
|
EXPECT_EQ(worker(2).tx_engine().RunningTransaction(tx2->id_)->snapshot(),
|
2018-01-22 19:36:14 +08:00
|
|
|
tx2->snapshot());
|
|
|
|
|
2018-01-24 19:16:14 +08:00
|
|
|
::testing::FLAGS_gtest_death_test_style = "threadsafe";
|
2018-02-05 23:28:22 +08:00
|
|
|
EXPECT_DEATH(worker(2).tx_engine().RunningTransaction(123), "");
|
2018-01-22 19:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TType>
|
|
|
|
using mapper_vec =
|
|
|
|
std::vector<std::reference_wrapper<storage::ConcurrentIdMapper<TType>>>;
|
|
|
|
|
2018-07-05 16:55:00 +08:00
|
|
|
TEST_F(DistributedGraphDb, StorageTypes) {
|
2018-01-22 19:36:14 +08:00
|
|
|
auto test_mappers = [](auto mappers, auto ids) {
|
|
|
|
for (size_t i = 0; i < mappers.size(); ++i) {
|
|
|
|
ids.emplace_back(
|
|
|
|
mappers[i].get().value_to_id("value" + std::to_string(i)));
|
|
|
|
}
|
|
|
|
EXPECT_GT(ids.size(), 0);
|
|
|
|
for (size_t i = 0; i < mappers.size(); ++i) {
|
|
|
|
for (size_t j = 0; j < ids.size(); ++j) {
|
|
|
|
EXPECT_EQ(mappers[i].get().id_to_value(ids[j]),
|
|
|
|
"value" + std::to_string(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test_mappers(mapper_vec<storage::Label>{master().label_mapper(),
|
2018-02-05 23:28:22 +08:00
|
|
|
worker(1).label_mapper(),
|
|
|
|
worker(2).label_mapper()},
|
2018-01-22 19:36:14 +08:00
|
|
|
std::vector<storage::Label>{});
|
|
|
|
test_mappers(mapper_vec<storage::EdgeType>{master().edge_type_mapper(),
|
2018-02-05 23:28:22 +08:00
|
|
|
worker(1).edge_type_mapper(),
|
|
|
|
worker(2).edge_type_mapper()},
|
2018-01-22 19:36:14 +08:00
|
|
|
std::vector<storage::EdgeType>{});
|
|
|
|
test_mappers(mapper_vec<storage::Property>{master().property_mapper(),
|
2018-02-05 23:28:22 +08:00
|
|
|
worker(1).property_mapper(),
|
|
|
|
worker(2).property_mapper()},
|
2018-01-22 19:36:14 +08:00
|
|
|
std::vector<storage::Property>{});
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:55:00 +08:00
|
|
|
TEST_F(DistributedGraphDb, DispatchPlan) {
|
2018-01-24 16:54:38 +08:00
|
|
|
auto kRPCWaitTime = 600ms;
|
|
|
|
int64_t plan_id = 5;
|
|
|
|
SymbolTable symbol_table;
|
2018-05-22 22:45:52 +08:00
|
|
|
AstStorage storage;
|
2018-01-24 16:54:38 +08:00
|
|
|
|
|
|
|
auto scan_all = MakeScanAll(storage, symbol_table, "n");
|
|
|
|
|
|
|
|
master().plan_dispatcher().DispatchPlan(plan_id, scan_all.op_, symbol_table);
|
|
|
|
std::this_thread::sleep_for(kRPCWaitTime);
|
|
|
|
|
2018-01-25 17:09:54 +08:00
|
|
|
auto check_for_worker = [plan_id, &symbol_table](auto &worker) {
|
|
|
|
auto &cached = worker.plan_consumer().PlanForId(plan_id);
|
|
|
|
EXPECT_NE(dynamic_cast<query::plan::ScanAll *>(cached.plan.get()), nullptr);
|
|
|
|
EXPECT_EQ(cached.symbol_table.max_position(), symbol_table.max_position());
|
|
|
|
EXPECT_EQ(cached.symbol_table.table(), symbol_table.table());
|
|
|
|
};
|
2018-02-05 23:28:22 +08:00
|
|
|
check_for_worker(worker(1));
|
|
|
|
check_for_worker(worker(2));
|
2018-03-13 17:35:14 +08:00
|
|
|
|
|
|
|
master().plan_dispatcher().RemovePlan(plan_id);
|
|
|
|
::testing::FLAGS_gtest_death_test_style = "threadsafe";
|
|
|
|
EXPECT_DEATH(check_for_worker(worker(1)), "Missing plan*");
|
2018-01-25 17:09:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-05 16:55:00 +08:00
|
|
|
TEST_F(DistributedGraphDb, BuildIndexDistributed) {
|
2018-01-26 00:19:33 +08:00
|
|
|
storage::Label label;
|
|
|
|
storage::Property property;
|
|
|
|
|
|
|
|
{
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba0 = master().Access();
|
|
|
|
label = dba0->Label("label");
|
|
|
|
property = dba0->Property("property");
|
|
|
|
auto tx_id = dba0->transaction_id();
|
2018-01-26 00:19:33 +08:00
|
|
|
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba1 = worker(1).Access(tx_id);
|
|
|
|
auto dba2 = worker(2).Access(tx_id);
|
2018-01-26 00:19:33 +08:00
|
|
|
auto add_vertex = [label, property](GraphDbAccessor &dba) {
|
|
|
|
auto vertex = dba.InsertVertex();
|
|
|
|
vertex.add_label(label);
|
|
|
|
vertex.PropsSet(property, 1);
|
|
|
|
};
|
2018-07-26 15:08:21 +08:00
|
|
|
for (int i = 0; i < 100; ++i) add_vertex(*dba0);
|
|
|
|
for (int i = 0; i < 50; ++i) add_vertex(*dba1);
|
|
|
|
for (int i = 0; i < 300; ++i) add_vertex(*dba2);
|
|
|
|
dba0->Commit();
|
2018-01-26 00:19:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba = master().Access();
|
2018-10-15 22:12:02 +08:00
|
|
|
dba->BuildIndex(label, property, false);
|
2018-07-26 15:08:21 +08:00
|
|
|
EXPECT_TRUE(dba->LabelPropertyIndexExists(label, property));
|
|
|
|
EXPECT_EQ(CountIterable(dba->Vertices(label, property, false)), 100);
|
2018-01-26 00:19:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba_master = master().Access();
|
2018-01-26 00:19:33 +08:00
|
|
|
|
|
|
|
{
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba = worker(1).Access(dba_master->transaction_id());
|
|
|
|
EXPECT_TRUE(dba->LabelPropertyIndexExists(label, property));
|
|
|
|
EXPECT_EQ(CountIterable(dba->Vertices(label, property, false)), 50);
|
2018-01-26 00:19:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba = worker(2).Access(dba_master->transaction_id());
|
|
|
|
EXPECT_TRUE(dba->LabelPropertyIndexExists(label, property));
|
|
|
|
EXPECT_EQ(CountIterable(dba->Vertices(label, property, false)), 300);
|
2018-01-26 00:19:33 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 17:58:56 +08:00
|
|
|
|
2018-07-11 20:34:19 +08:00
|
|
|
TEST_F(DistributedGraphDb, BuildIndexConcurrentInsert) {
|
|
|
|
storage::Label label;
|
|
|
|
storage::Property property;
|
|
|
|
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba0 = master().Access();
|
|
|
|
label = dba0->Label("label");
|
|
|
|
property = dba0->Property("property");
|
2018-07-11 20:34:19 +08:00
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
auto add_vertex = [label, property, &cnt](GraphDbAccessor &dba) {
|
|
|
|
auto vertex = dba.InsertVertex();
|
|
|
|
vertex.add_label(label);
|
|
|
|
vertex.PropsSet(property, ++cnt);
|
|
|
|
};
|
2018-07-26 15:08:21 +08:00
|
|
|
dba0->Commit();
|
2018-07-11 20:34:19 +08:00
|
|
|
|
|
|
|
auto worker_insert = std::thread([this, &add_vertex]() {
|
|
|
|
for (int i = 0; i < 10000; ++i) {
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba1 = worker(1).Access();
|
|
|
|
add_vertex(*dba1);
|
|
|
|
dba1->Commit();
|
2018-07-11 20:34:19 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(0.5s);
|
|
|
|
{
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba = master().Access();
|
2018-10-15 22:12:02 +08:00
|
|
|
dba->BuildIndex(label, property, false);
|
2018-07-26 15:08:21 +08:00
|
|
|
EXPECT_TRUE(dba->LabelPropertyIndexExists(label, property));
|
2018-07-11 20:34:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
worker_insert.join();
|
|
|
|
{
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba = worker(1).Access();
|
|
|
|
EXPECT_TRUE(dba->LabelPropertyIndexExists(label, property));
|
|
|
|
EXPECT_EQ(CountIterable(dba->Vertices(label, property, false)), 10000);
|
2018-07-11 20:34:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:55:00 +08:00
|
|
|
TEST_F(DistributedGraphDb, WorkerOwnedDbAccessors) {
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba_w1 = worker(1).Access();
|
|
|
|
auto v = dba_w1->InsertVertex();
|
|
|
|
auto prop = dba_w1->Property("p");
|
2018-02-01 17:58:56 +08:00
|
|
|
v.PropsSet(prop, 42);
|
|
|
|
auto v_ga = v.GlobalAddress();
|
2018-07-26 15:08:21 +08:00
|
|
|
dba_w1->Commit();
|
2018-02-01 17:58:56 +08:00
|
|
|
|
2018-07-26 15:08:21 +08:00
|
|
|
auto dba_w2 = worker(2).Access();
|
|
|
|
VertexAccessor v_in_w2{v_ga, *dba_w2};
|
2018-02-01 17:58:56 +08:00
|
|
|
EXPECT_EQ(v_in_w2.PropsAt(prop).Value<int64_t>(), 42);
|
|
|
|
}
|