memgraph/experimental/distributed/src/graph.hpp

163 lines
4.7 KiB
C++
Raw Normal View History

Merged experimental repo. Summary: Fixed distributed init. Add CMakeLists to build experimentall/distribuedClosing unused Channels, work in progress. Make System the owner of Reactor. This entails changing shared_ptr -> unique_ptr and some pointers to references. Merged experimental repository into memgraph. Moved experimental repo to experimental directory. Removed obsolete experimental files. Added comments. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Subscription service unsubscribe. Add Close method on EventStream. Add placeholder for the configuration class. Remove comments. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Clean-up parameters for EventQueue. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add Channel::serialize method implementation. Merge. Add docs on event stream. Clang-format merge conflicts. First implementations of serialize methods. Add hostname, port, and names as methods in Channel base class. Add reactor name and name methods to LocalChannel. Add reactor name to LocalChannel. Add name to LocalChannel. Add serialization service. Serialize_test removed. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Move Message to the end of communications files. Full example of serialization with cereal. Fix constructor calls. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Avoid using `FindChannel` in the transaction code. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Init script creates libs folder. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add System pointer to Network. serialized_test binary is removed from the repo. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Cereal basic example. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Callbacks finished. Always open the main channel by default. Fixed callbacks, wrong number of emplace arguments. Callbacks WIP. Raise connector mutex to reactor level. Add argument to LockedPush. Fix data race in connector closing. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add functional header. Fixed to make the changes work. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Refactored connectors into Reactors Use shared pointer for the mutex. Rename to Open and Close in implementation file. Rename Create to Open and Destroy to Close. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Adding callback to Reactors; work in progress Add stubs for asynchronous channel resolution. Add stubs for the networking service. Replace reactor pointers with shared ptrs, disable System assignment. Forbid assignment. Replace raw channel pointers with shared pointers. Replace raw event stream pointer with shared pointer. Rename default stream name. Use recursive mutex in System. Main uses Spawn method. All files are formatted. Move thread local to a cpp file. Work in progress on Spawn method. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Kill out graph.hpp to make it compile Add Spawn method prototype. Fix return type. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add method used to create nameless channels. Add format script. Introduce the Reactor base class. Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add compile script. added comments about class terminology Spinner rewrite (graph data structures and algo) Organize Spinner code Create working version Improves Spinner implementation and testing Spinner fix .arcconfig Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add graph Spinner work Spinner added Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Add communication .clang-format + ycm config. Init. Distributed hackaton. Implementation of lock-free list from Petar Sirkovic. pro compiler Merge branch 'master' of https://phabricator.memgraph.io/source/experimental Implement Match Add test data. Insert quotes before and after props and labels Multiple node declarations, along with edges. After merge. Node property creations work now. Bug fix in visitor After merge. Implement node creation with labels. Implement boolean operators Tidy up ImplementedVistor. Implement expression6 (addition) Implement basic type visitor functions Cypher Visitor Implementation class created. Fix style. Fix template synrax in main.cpp Merge remote-tracking branch 'origin/master' Add pretty_print Update main and BaseVisitor to present return value. Headers included. Temporary fix. Antlr4 module reintroduced. Updateao git config. Fix trailing space. CMake 2.8 fix rerolled, 3.1 minimum version req. Fix for Cmake version 2.8 compatibility. Build works. Tidy src folder. Include generated files for antlr. Included antlr generated files. Changed directory structure. Cmake: include subdirectory. GenerateRuntime, partial. Add GenerateParser target to cmake. Remove main.cpp Merge remote-tracking branch 'origin/master' Add requirements Main file added. Run the lexer and parser with this. Add antlr_generated to baby_compiler Experimental memory_tracker and opencypher tck tests Reviewers: mislav.bradac Reviewed By: mislav.bradac Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D627
2017-08-03 18:08:39 +08:00
#pragma once
#include <cassert>
#include <unordered_map>
#include <vector>
#include "uid.hpp"
enum class EdgeType { OUTGOING, INCOMING };
/** A node in the graph. Has incoming and outgoing edges which
* are defined as global addresses of other nodes */
class Node {
public:
Node(const GlobalId &id) : id_(id) {}
const auto &id() const { return id_; };
const auto &edges_out() const { return edges_out_; }
const auto &edges_in() const { return edges_in_; }
void AddConnection(EdgeType edge_type, const GlobalAddress &gad) {
(edge_type == EdgeType::INCOMING ? edges_in_ : edges_out_)
.emplace_back(gad);
}
/** Changes all old_address edges to have the new_worker */
void RedirectEdges(const GlobalAddress old_address, size_t new_worker) {
for (auto &address : edges_in_)
if (address == old_address) address.worker_id_ = new_worker;
for (auto &address : edges_out_)
if (address == old_address) address.worker_id_ = new_worker;
}
private:
// TODO remove id_ from Node if not necessary
GlobalId id_;
// global addresses of nodes this node is connected to
std::vector<GlobalAddress> edges_out_;
std::vector<GlobalAddress> edges_in_;
};
/** A worker / shard in the distributed system */
class Worker {
public:
// unique worker ID. uniqueness is ensured by the worker
// owner (the Distributed class)
const int64_t id_;
Worker(int64_t id) : id_(id) {}
int64_t NodeCount() const { return nodes_.size(); }
/** Gets a node. */
Node &GetNode(const GlobalId &gid) {
auto found = nodes_.find(gid);
assert(found != nodes_.end());
return found->second;
}
/** Returns the number of edges that cross from this
* graph / worker into another one */
int64_t BoundaryEdgeCount() const {
int64_t count = 0;
auto count_f = [this, &count](const auto &edges) {
for (const GlobalAddress &address : edges)
if (address.worker_id_ != id_) count++;
};
for (const auto &node : nodes_) {
count_f(node.second.edges_out());
count_f(node.second.edges_in());
}
return count;
}
/** Creates a new node on this worker. Returns it's global id */
const GlobalId &MakeNode() {
GlobalId new_id(id_, next_node_sequence_++);
auto new_node = nodes_.emplace(std::make_pair(new_id, Node(new_id)));
return new_node.first->first;
};
/** Places the existing node on this worker */
void PlaceNode(const GlobalId &gid, const Node &node) {
nodes_.emplace(gid, node);
}
/** Removes the node with the given ID from this worker */
void RemoveNode(const GlobalId &gid) { nodes_.erase(gid); }
auto begin() const { return nodes_.begin(); }
auto end() const { return nodes_.end(); }
private:
// counter of sequences numbers of nodes created on this worker
int64_t next_node_sequence_{0};
// node storage of this worker
std::unordered_map<GlobalId, Node> nodes_;
};
/**
* A distributed system consisting of mulitple workers.
* For the time being it's not modelling a distributed
* system correctly in terms of message passing (as opposed
* to operating on workers and their data directly).
*/
class Distributed {
public:
/** Creates a distributed with the given number of workers */
Distributed(int initial_worker_count = 0) {
for (int worker_id = 0; worker_id < initial_worker_count; worker_id++)
AddWorker();
}
int64_t AddWorker() {
int64_t new_worker_id = workers_.size();
workers_.emplace_back(new_worker_id);
return new_worker_id;
}
int WorkerCount() const { return workers_.size(); }
auto &GetWorker(int64_t worker_id) { return workers_[worker_id]; }
GlobalAddress MakeNode(int64_t worker_id) {
return {worker_id, workers_[worker_id].MakeNode()};
}
Node &GetNode(const GlobalAddress &address) {
return workers_[address.worker_id_].GetNode(address.id_);
}
/** Moves a node with the given global id to the given worker */
void MoveNode(const GlobalAddress &gid, int64_t destination) {
const Node &node = GetNode(gid);
// make sure that all edges to and from the node are updated
for (auto &edge : node.edges_in())
GetNode(edge).RedirectEdges(gid, destination);
for (auto &edge : node.edges_out())
GetNode(edge).RedirectEdges(gid, destination);
// change node destination
workers_[destination].PlaceNode(gid.id_, node);
workers_[gid.worker_id_].RemoveNode(gid.id_);
}
void MakeEdge(const GlobalAddress &from, const GlobalAddress &to) {
GetNode(from).AddConnection(EdgeType::OUTGOING, to);
GetNode(to).AddConnection(EdgeType::INCOMING, from);
}
auto begin() const { return workers_.begin(); }
auto end() const { return workers_.end(); }
private:
std::vector<Worker> workers_;
};