2018-03-12 18:26:40 +08:00
|
|
|
#include <atomic>
|
2017-12-19 19:40:30 +08:00
|
|
|
#include <experimental/optional>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
2017-12-22 21:36:25 +08:00
|
|
|
#include <unordered_set>
|
2017-12-19 19:40:30 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2018-01-22 23:59:40 +08:00
|
|
|
#include "gmock/gmock.h"
|
2017-12-19 19:40:30 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2018-04-03 22:19:17 +08:00
|
|
|
#include "communication/rpc/client_pool.hpp"
|
2018-01-24 19:16:14 +08:00
|
|
|
#include "communication/rpc/server.hpp"
|
2018-04-03 22:19:17 +08:00
|
|
|
#include "distributed/cluster_discovery_master.hpp"
|
|
|
|
#include "distributed/cluster_discovery_worker.hpp"
|
2017-12-19 19:40:30 +08:00
|
|
|
#include "distributed/coordination_master.hpp"
|
|
|
|
#include "distributed/coordination_worker.hpp"
|
2018-04-03 22:19:17 +08:00
|
|
|
#include "distributed/rpc_worker_clients.hpp"
|
2018-01-15 21:03:07 +08:00
|
|
|
#include "io/network/endpoint.hpp"
|
2017-12-19 19:40:30 +08:00
|
|
|
|
2018-02-23 17:56:56 +08:00
|
|
|
using communication::rpc::Server;
|
2018-04-03 22:19:17 +08:00
|
|
|
using communication::rpc::ClientPool;
|
2017-12-19 19:40:30 +08:00
|
|
|
using namespace distributed;
|
2018-01-24 19:16:14 +08:00
|
|
|
using namespace std::literals::chrono_literals;
|
2017-12-19 19:40:30 +08:00
|
|
|
|
|
|
|
const int kWorkerCount = 5;
|
|
|
|
const std::string kLocal = "127.0.0.1";
|
|
|
|
|
2018-03-12 18:26:40 +08:00
|
|
|
class WorkerCoordinationInThread {
|
2017-12-19 19:40:30 +08:00
|
|
|
public:
|
2018-03-12 18:26:40 +08:00
|
|
|
WorkerCoordinationInThread(io::network::Endpoint master_endpoint,
|
2018-04-10 19:07:01 +08:00
|
|
|
int desired_id) {
|
2018-03-12 18:26:40 +08:00
|
|
|
std::atomic<bool> init_done{false};
|
|
|
|
worker_thread_ =
|
|
|
|
std::thread([this, master_endpoint, desired_id, &init_done] {
|
|
|
|
server_.emplace(Endpoint(kLocal, 0));
|
|
|
|
coord_.emplace(*server_, master_endpoint);
|
2018-04-03 22:19:17 +08:00
|
|
|
client_pool_.emplace(master_endpoint);
|
|
|
|
discovery_.emplace(*server_, *coord_, *client_pool_);
|
2018-04-10 19:07:01 +08:00
|
|
|
// Try and register the worker with the desired id. If another worker
|
|
|
|
// is already using the desired id it will exit here.
|
|
|
|
discovery_->RegisterWorker(desired_id);
|
|
|
|
worker_id_ = desired_id;
|
2018-03-12 18:26:40 +08:00
|
|
|
init_done = true;
|
|
|
|
coord_->WaitForShutdown();
|
|
|
|
});
|
|
|
|
|
|
|
|
while (!init_done) std::this_thread::sleep_for(10ms);
|
2017-12-19 19:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int worker_id() const { return worker_id_; }
|
2018-02-23 17:56:56 +08:00
|
|
|
auto endpoint() const { return server_->endpoint(); }
|
2017-12-19 19:40:30 +08:00
|
|
|
auto worker_endpoint(int worker_id) { return coord_->GetEndpoint(worker_id); }
|
2018-04-03 22:19:17 +08:00
|
|
|
auto worker_ids() { return coord_->GetWorkerIds(); }
|
2017-12-19 19:40:30 +08:00
|
|
|
void join() { worker_thread_.join(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::thread worker_thread_;
|
2018-02-23 17:56:56 +08:00
|
|
|
std::experimental::optional<Server> server_;
|
2017-12-19 19:40:30 +08:00
|
|
|
std::experimental::optional<WorkerCoordination> coord_;
|
2018-04-03 22:19:17 +08:00
|
|
|
std::experimental::optional<ClientPool> client_pool_;
|
|
|
|
std::experimental::optional<ClusterDiscoveryWorker> discovery_;
|
2017-12-19 19:40:30 +08:00
|
|
|
std::atomic<int> worker_id_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Distributed, Coordination) {
|
2018-02-23 17:56:56 +08:00
|
|
|
Server master_server({kLocal, 0});
|
2018-03-12 18:26:40 +08:00
|
|
|
std::vector<std::unique_ptr<WorkerCoordinationInThread>> workers;
|
2018-01-10 20:56:12 +08:00
|
|
|
{
|
2018-04-03 22:19:17 +08:00
|
|
|
MasterCoordination master_coord(master_server.endpoint());
|
|
|
|
RpcWorkerClients rpc_worker_clients(master_coord);
|
|
|
|
ClusterDiscoveryMaster master_discovery_(master_server, master_coord,
|
|
|
|
rpc_worker_clients);
|
2018-01-10 20:56:12 +08:00
|
|
|
|
2018-04-10 19:07:01 +08:00
|
|
|
for (int i = 1; i <= kWorkerCount; ++i)
|
2018-03-12 18:26:40 +08:00
|
|
|
workers.emplace_back(std::make_unique<WorkerCoordinationInThread>(
|
2018-04-10 19:07:01 +08:00
|
|
|
master_server.endpoint(), i));
|
2018-01-10 20:56:12 +08:00
|
|
|
|
|
|
|
// Expect that all workers have a different ID.
|
|
|
|
std::unordered_set<int> worker_ids;
|
|
|
|
for (const auto &w : workers) worker_ids.insert(w->worker_id());
|
2018-03-12 18:26:40 +08:00
|
|
|
ASSERT_EQ(worker_ids.size(), kWorkerCount);
|
2018-01-10 20:56:12 +08:00
|
|
|
|
|
|
|
// Check endpoints.
|
|
|
|
for (auto &w1 : workers) {
|
|
|
|
for (auto &w2 : workers) {
|
|
|
|
EXPECT_EQ(w1->worker_endpoint(w2->worker_id()), w2->endpoint());
|
|
|
|
}
|
2017-12-19 19:40:30 +08:00
|
|
|
}
|
2018-01-10 20:56:12 +08:00
|
|
|
} // Coordinated shutdown.
|
2017-12-19 19:40:30 +08:00
|
|
|
|
|
|
|
for (auto &worker : workers) worker->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Distributed, DesiredAndUniqueId) {
|
2018-02-23 17:56:56 +08:00
|
|
|
Server master_server({kLocal, 0});
|
2018-03-12 18:26:40 +08:00
|
|
|
std::vector<std::unique_ptr<WorkerCoordinationInThread>> workers;
|
2018-01-10 20:56:12 +08:00
|
|
|
{
|
2018-04-03 22:19:17 +08:00
|
|
|
MasterCoordination master_coord(master_server.endpoint());
|
|
|
|
RpcWorkerClients rpc_worker_clients(master_coord);
|
|
|
|
ClusterDiscoveryMaster master_discovery_(master_server, master_coord,
|
|
|
|
rpc_worker_clients);
|
2017-12-19 19:40:30 +08:00
|
|
|
|
2018-03-12 18:26:40 +08:00
|
|
|
workers.emplace_back(std::make_unique<WorkerCoordinationInThread>(
|
|
|
|
master_server.endpoint(), 42));
|
2018-01-10 20:56:12 +08:00
|
|
|
EXPECT_EQ(workers[0]->worker_id(), 42);
|
2018-04-10 19:07:01 +08:00
|
|
|
|
|
|
|
EXPECT_DEATH(
|
|
|
|
workers.emplace_back(std::make_unique<WorkerCoordinationInThread>(
|
|
|
|
master_server.endpoint(), 42)),
|
|
|
|
"");
|
2018-01-10 20:56:12 +08:00
|
|
|
}
|
2017-12-19 19:40:30 +08:00
|
|
|
|
2018-01-10 20:56:12 +08:00
|
|
|
for (auto &worker : workers) worker->join();
|
2017-12-19 19:40:30 +08:00
|
|
|
}
|
2018-01-22 23:59:40 +08:00
|
|
|
|
|
|
|
TEST(Distributed, CoordinationWorkersId) {
|
2018-02-23 17:56:56 +08:00
|
|
|
Server master_server({kLocal, 0});
|
2018-03-12 18:26:40 +08:00
|
|
|
std::vector<std::unique_ptr<WorkerCoordinationInThread>> workers;
|
2018-01-22 23:59:40 +08:00
|
|
|
{
|
2018-04-03 22:19:17 +08:00
|
|
|
MasterCoordination master_coord(master_server.endpoint());
|
|
|
|
RpcWorkerClients rpc_worker_clients(master_coord);
|
|
|
|
ClusterDiscoveryMaster master_discovery_(master_server, master_coord,
|
|
|
|
rpc_worker_clients);
|
2018-01-22 23:59:40 +08:00
|
|
|
|
2018-03-12 18:26:40 +08:00
|
|
|
workers.emplace_back(std::make_unique<WorkerCoordinationInThread>(
|
|
|
|
master_server.endpoint(), 42));
|
|
|
|
workers.emplace_back(std::make_unique<WorkerCoordinationInThread>(
|
2018-04-10 19:07:01 +08:00
|
|
|
master_server.endpoint(), 43));
|
2018-01-22 23:59:40 +08:00
|
|
|
|
|
|
|
std::vector<int> ids;
|
|
|
|
ids.push_back(0);
|
|
|
|
|
|
|
|
for (auto &worker : workers) ids.push_back(worker->worker_id());
|
|
|
|
EXPECT_THAT(master_coord.GetWorkerIds(),
|
|
|
|
testing::UnorderedElementsAreArray(ids));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &worker : workers) worker->join();
|
|
|
|
}
|
2018-04-03 22:19:17 +08:00
|
|
|
|
|
|
|
TEST(Distributed, ClusterDiscovery) {
|
|
|
|
Server master_server({kLocal, 0});
|
|
|
|
std::vector<std::unique_ptr<WorkerCoordinationInThread>> workers;
|
|
|
|
{
|
|
|
|
MasterCoordination master_coord(master_server.endpoint());
|
|
|
|
RpcWorkerClients rpc_worker_clients(master_coord);
|
|
|
|
ClusterDiscoveryMaster master_discovery_(master_server, master_coord,
|
|
|
|
rpc_worker_clients);
|
|
|
|
std::vector<int> ids;
|
|
|
|
int worker_count = 10;
|
|
|
|
|
|
|
|
ids.push_back(0);
|
|
|
|
for (int i = 1; i <= worker_count; ++i) {
|
|
|
|
workers.emplace_back(std::make_unique<WorkerCoordinationInThread>(
|
|
|
|
master_server.endpoint(), i));
|
|
|
|
|
|
|
|
ids.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_THAT(master_coord.GetWorkerIds(),
|
|
|
|
testing::UnorderedElementsAreArray(ids));
|
|
|
|
for (auto &worker : workers) {
|
|
|
|
EXPECT_THAT(worker->worker_ids(),
|
|
|
|
testing::UnorderedElementsAreArray(ids));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &worker : workers) worker->join();
|
|
|
|
}
|