2019-01-04 23:07:57 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <gflags/gflags.h>
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include "communication/bolt/client.hpp"
|
|
|
|
#include "io/network/endpoint.hpp"
|
|
|
|
#include "io/network/utils.hpp"
|
|
|
|
#include "utils/timer.hpp"
|
|
|
|
|
|
|
|
DEFINE_string(address, "127.0.0.1", "Server address");
|
|
|
|
DEFINE_int32(port, 7687, "Server port");
|
|
|
|
DEFINE_int32(cluster_size, 3, "Size of the raft cluster.");
|
|
|
|
DEFINE_int32(expected_results, -1, "Number of expected nodes.");
|
|
|
|
DEFINE_string(username, "", "Username for the database");
|
|
|
|
DEFINE_string(password, "", "Password for the database");
|
|
|
|
DEFINE_bool(use_ssl, false, "Set to true to connect with SSL to the server.");
|
|
|
|
|
|
|
|
DEFINE_string(step, "", "The step to execute (available: create, count)");
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
|
google::InitGoogleLogging(argv[0]);
|
|
|
|
|
|
|
|
communication::Init();
|
|
|
|
|
2019-02-25 23:51:17 +08:00
|
|
|
bool successful = false;
|
|
|
|
for (int retry = 0; !successful && retry < 10; ++retry) {
|
|
|
|
for (int i = 0; !successful && i < FLAGS_cluster_size; ++i) {
|
2019-01-04 23:07:57 +08:00
|
|
|
try {
|
|
|
|
communication::ClientContext context(FLAGS_use_ssl);
|
|
|
|
communication::bolt::Client client(&context);
|
|
|
|
|
|
|
|
uint16_t port = FLAGS_port + i;
|
|
|
|
io::network::Endpoint endpoint{FLAGS_address, port};
|
|
|
|
client.Connect(endpoint, FLAGS_username, FLAGS_password);
|
|
|
|
|
|
|
|
if (FLAGS_step == "create") {
|
|
|
|
client.Execute("create (:Node)", {});
|
2019-02-25 23:51:17 +08:00
|
|
|
successful = true;
|
2019-01-04 23:07:57 +08:00
|
|
|
|
|
|
|
} else if (FLAGS_step == "count") {
|
|
|
|
auto result = client.Execute("match (n) return n", {});
|
|
|
|
|
|
|
|
if (result.records.size() != FLAGS_expected_results) {
|
|
|
|
LOG(WARNING) << "Missing data: expected " << FLAGS_expected_results
|
|
|
|
<< ", got " << result.records.size();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:51:17 +08:00
|
|
|
successful = true;
|
2019-01-04 23:07:57 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
LOG(FATAL) << "Unexpected client step!";
|
|
|
|
}
|
|
|
|
} catch (const communication::bolt::ClientQueryException &) {
|
|
|
|
// This one is not the leader, continue.
|
|
|
|
continue;
|
|
|
|
} catch (const communication::bolt::ClientFatalException &) {
|
|
|
|
// This one seems to be down, continue.
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-14 21:56:44 +08:00
|
|
|
LOG(INFO) << "Current Raft cluster leader is " << i;
|
2019-01-04 23:07:57 +08:00
|
|
|
}
|
2019-02-25 23:51:17 +08:00
|
|
|
if (!successful) {
|
2019-01-04 23:07:57 +08:00
|
|
|
LOG(INFO) << "Couldn't find Raft cluster leader, retrying.";
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:51:17 +08:00
|
|
|
if (!successful) {
|
2019-01-04 23:07:57 +08:00
|
|
|
LOG(WARNING) << "Couldn't find Raft cluster leader.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|