2017-10-31 16:49:33 +08:00
|
|
|
#include <csignal>
|
2017-05-30 22:26:16 +08:00
|
|
|
#include <experimental/filesystem>
|
2017-01-23 19:02:11 +08:00
|
|
|
#include <iostream>
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2017-07-06 19:53:39 +08:00
|
|
|
#include <gflags/gflags.h>
|
|
|
|
#include <glog/logging.h>
|
2017-05-22 18:31:04 +08:00
|
|
|
|
2017-03-06 20:37:51 +08:00
|
|
|
#include "communication/bolt/v1/session.hpp"
|
|
|
|
#include "communication/server.hpp"
|
2017-10-25 21:28:10 +08:00
|
|
|
#include "config.hpp"
|
2017-03-06 20:37:51 +08:00
|
|
|
#include "io/network/network_endpoint.hpp"
|
|
|
|
#include "io/network/network_error.hpp"
|
2016-08-10 16:39:02 +08:00
|
|
|
#include "io/network/socket.hpp"
|
2017-06-09 21:48:40 +08:00
|
|
|
#include "utils/flag_validation.hpp"
|
2017-09-22 20:24:53 +08:00
|
|
|
#include "utils/scheduler.hpp"
|
2016-12-16 20:56:36 +08:00
|
|
|
#include "utils/signals/handler.hpp"
|
2017-06-13 21:43:00 +08:00
|
|
|
#include "utils/stacktrace.hpp"
|
2017-09-22 20:24:53 +08:00
|
|
|
#include "utils/sysinfo/memory.hpp"
|
2017-01-13 17:47:17 +08:00
|
|
|
#include "utils/terminate_handler.hpp"
|
2017-09-26 15:43:43 +08:00
|
|
|
#include "version.hpp"
|
|
|
|
|
2017-05-30 22:26:16 +08:00
|
|
|
namespace fs = std::experimental::filesystem;
|
2017-10-30 17:43:25 +08:00
|
|
|
using communication::bolt::SessionData;
|
2017-10-17 20:05:08 +08:00
|
|
|
using io::network::NetworkEndpoint;
|
|
|
|
using io::network::Socket;
|
|
|
|
using SessionT = communication::bolt::Session<Socket>;
|
|
|
|
using ResultStreamT = SessionT::ResultStreamT;
|
|
|
|
using ServerT = communication::Server<SessionT, SessionData>;
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-09-25 21:56:14 +08:00
|
|
|
DEFINE_string(interface, "0.0.0.0",
|
|
|
|
"Communication interface on which to listen.");
|
Flags cleanup and QueryEngine removal
Summary:
I started with cleaning flags up (removing unused ones, documenting undocumented ones). There were some flags to remove in `QueryEngine`. Seeing how we never use hardcoded queries (AFAIK last Mislav's testing also indicated they aren't faster then interpretation), when removing those unused flags the `QueryEngine` becomes obsolete. That means that a bunch of other stuff becomes obsolete, along with the hardcoded queries. So I removed it all (this has been discussed and approved on the daily).
Some flags that were previously undocumented in `docs/user_technical/installation` are now documented. The following flags are NOT documented and in my opinion should not be displayed when starting `./memgraph --help` (@mferencevic):
```
query_vertex_count_to_expand_existsing (from rule_based_planner.cpp)
query_max_plans (rule_based_planner.cpp)
```
If you think that another organization is needed w.r.t. flag visibility, comment.
@teon.banek: I had to remove some stuff from CMakeLists to make it buildable. Please review what I removed and clean up if necessary if/when this lands. If the needed changes are minor, you can also comment.
Reviewers: buda, mislav.bradac, teon.banek, mferencevic
Reviewed By: buda, mislav.bradac
Subscribers: pullbot, mferencevic, teon.banek
Differential Revision: https://phabricator.memgraph.io/D825
2017-09-22 22:17:09 +08:00
|
|
|
DEFINE_string(port, "7687", "Communication port on which to listen.");
|
2017-06-09 21:48:40 +08:00
|
|
|
DEFINE_VALIDATED_int32(num_workers,
|
|
|
|
std::max(std::thread::hardware_concurrency(), 1U),
|
|
|
|
"Number of workers", FLAG_IN_RANGE(1, INT32_MAX));
|
2017-10-17 20:05:08 +08:00
|
|
|
DEFINE_string(log_file, "", "Path to where the log should be stored.");
|
2017-10-04 22:20:38 +08:00
|
|
|
DEFINE_string(log_link_basename, "",
|
|
|
|
"Basename used for symlink creation to the last log file.");
|
2017-09-25 21:56:14 +08:00
|
|
|
DEFINE_uint64(memory_warning_threshold, 1024,
|
|
|
|
"Memory warning treshold, in MB. If Memgraph detects there is "
|
|
|
|
"less available RAM available it will log a warning. Set to 0 to "
|
|
|
|
"disable.");
|
2016-08-01 01:58:12 +08:00
|
|
|
|
2017-10-31 16:49:33 +08:00
|
|
|
// Needed to correctly handle memgraph destruction from a signal handler.
|
|
|
|
// Without having some sort of a flag, it is possible that a signal is handled
|
|
|
|
// when we are exiting main, inside destructors of GraphDb and similar. The
|
|
|
|
// signal handler may then initiate another shutdown on memgraph which is in
|
|
|
|
// half destructed state, causing invalid memory access and crash.
|
|
|
|
volatile sig_atomic_t is_shutting_down = 0;
|
|
|
|
|
2017-01-23 19:02:11 +08:00
|
|
|
int main(int argc, char **argv) {
|
2017-10-03 16:36:16 +08:00
|
|
|
google::SetUsageMessage("Memgraph database server");
|
2017-09-26 15:43:43 +08:00
|
|
|
gflags::SetVersionString(version_string);
|
2017-10-03 16:36:16 +08:00
|
|
|
|
2017-10-25 21:28:10 +08:00
|
|
|
// Load config before parsing arguments, so that flags from the command line
|
|
|
|
// overwrite the config.
|
|
|
|
LoadConfig();
|
|
|
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
2017-06-08 19:30:59 +08:00
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
|
|
|
google::SetLogDestination(google::INFO, FLAGS_log_file.c_str());
|
2017-10-04 22:20:38 +08:00
|
|
|
google::SetLogSymlink(google::INFO, FLAGS_log_link_basename.c_str());
|
2017-01-23 19:02:11 +08:00
|
|
|
|
2017-06-06 21:48:04 +08:00
|
|
|
// Unhandled exception handler init.
|
2017-01-23 19:02:11 +08:00
|
|
|
std::set_terminate(&terminate_handler);
|
|
|
|
|
2017-10-30 17:43:25 +08:00
|
|
|
// Initialize bolt session data (GraphDb and Interpreter).
|
2017-10-17 20:05:08 +08:00
|
|
|
SessionData session_data;
|
2017-09-18 20:58:30 +08:00
|
|
|
|
2017-06-06 21:48:04 +08:00
|
|
|
// Initialize endpoint.
|
2017-10-17 20:05:08 +08:00
|
|
|
NetworkEndpoint endpoint = [&] {
|
|
|
|
try {
|
|
|
|
return NetworkEndpoint(FLAGS_interface, FLAGS_port);
|
|
|
|
} catch (io::network::NetworkEndpointException &e) {
|
|
|
|
LOG(FATAL) << e.what();
|
|
|
|
}
|
|
|
|
}();
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-06-06 21:48:04 +08:00
|
|
|
// Initialize server.
|
2017-10-17 20:05:08 +08:00
|
|
|
ServerT server(endpoint, session_data);
|
2017-06-07 21:56:26 +08:00
|
|
|
|
2017-10-31 16:49:33 +08:00
|
|
|
// Handler for regular termination signals
|
2017-10-30 17:43:25 +08:00
|
|
|
auto shutdown = [&server, &session_data]() {
|
2017-10-31 16:49:33 +08:00
|
|
|
if (is_shutting_down) return;
|
|
|
|
is_shutting_down = 1;
|
2017-10-30 17:43:25 +08:00
|
|
|
// Server needs to be shutdown first and then the database. This prevents a
|
|
|
|
// race condition when a transaction is accepted during server shutdown.
|
|
|
|
server.Shutdown();
|
|
|
|
session_data.db.Shutdown();
|
|
|
|
};
|
2017-01-23 19:02:11 +08:00
|
|
|
|
2017-10-31 16:49:33 +08:00
|
|
|
// Prevent handling shutdown inside a shutdown. For example, SIGINT handler
|
|
|
|
// being interrupted by SIGTERM before is_shutting_down is set, thus causing
|
|
|
|
// double shutdown.
|
|
|
|
sigset_t block_shutdown_signals;
|
|
|
|
sigemptyset(&block_shutdown_signals);
|
|
|
|
sigaddset(&block_shutdown_signals, SIGTERM);
|
|
|
|
sigaddset(&block_shutdown_signals, SIGINT);
|
|
|
|
|
|
|
|
CHECK(SignalHandler::RegisterHandler(Signal::Terminate, shutdown,
|
|
|
|
block_shutdown_signals))
|
|
|
|
<< "Unable to register SIGTERM handler!";
|
|
|
|
CHECK(SignalHandler::RegisterHandler(Signal::Interupt, shutdown,
|
|
|
|
block_shutdown_signals))
|
|
|
|
<< "Unable to register SIGINT handler!";
|
2017-06-08 19:30:59 +08:00
|
|
|
|
2017-09-22 20:24:53 +08:00
|
|
|
// Start memory warning logger.
|
|
|
|
Scheduler mem_log_scheduler;
|
|
|
|
if (FLAGS_memory_warning_threshold > 0) {
|
|
|
|
mem_log_scheduler.Run(std::chrono::seconds(3), [] {
|
|
|
|
auto free_ram_mb = utils::AvailableMem() / 1024;
|
|
|
|
if (free_ram_mb < FLAGS_memory_warning_threshold)
|
|
|
|
LOG(WARNING) << "Running out of available RAM, only " << free_ram_mb
|
|
|
|
<< " MB left.";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 21:48:04 +08:00
|
|
|
// Start worker threads.
|
|
|
|
server.Start(FLAGS_num_workers);
|
2017-01-23 19:02:11 +08:00
|
|
|
|
2017-10-09 16:53:03 +08:00
|
|
|
return 0;
|
2016-08-01 01:58:12 +08:00
|
|
|
}
|