Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Only start API service when peering service is stable.

kishorenc43231eaae3ca
diff --git a/include/raft_server.h b/include/raft_server.h
index f80249b2..914ad6fe 100644
--- a/include/raft_server.h
+++ b/include/raft_server.h
@@ -85,8 +85,8 @@ private:
ThreadPool* thread_pool;
http_message_dispatcher* message_dispatcher;
- butil::atomic<bool> has_initialized;
- std::promise<bool>* ready;
+ std::atomic<size_t> init_readiness_count;
+
bool create_init_db_snapshot;
public:
@@ -96,7 +96,7 @@ public:
static constexpr const char* snapshot_dir_name = "snapshot";
ReplicationState(Store* store, ThreadPool* thread_pool, http_message_dispatcher* message_dispatcher,
- std::promise<bool>* ready, bool create_init_db_snapshot);
+ bool create_init_db_snapshot);
~ReplicationState() {
delete node;
@@ -119,6 +119,12 @@ public:
return leader_term.load(butil::memory_order_acquire) > 0;
}
+ bool is_active() const {
+ braft::NodeStatus node_status;
+ node->get_status(&node_status);
+ return node_status.state == braft::State::STATE_CANDIDATE || node_status.state == braft::State::STATE_LEADER;
+ }
+
// Shut this node down.
void shutdown() {
if (node) {
@@ -137,6 +143,8 @@ public:
void reset_db();
+ size_t get_init_readiness_count() const;
+
static constexpr const char* REPLICATION_MSG = "raft_replication";
private:
@@ -168,6 +176,8 @@ private:
write(request, response);
}
+ init_readiness_count++;
+
LOG(INFO) << "Node becomes leader, term: " << term;
}
@@ -190,6 +200,7 @@ private:
void on_start_following(const ::braft::LeaderChangeContext& ctx) {
LOG(INFO) << "Node start following " << ctx;
+ init_readiness_count++;
}
void on_stop_following(const ::braft::LeaderChangeContext& ctx) {
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 7fe56d7a..c0f78a24 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -270,10 +270,7 @@ int ReplicationState::init_db() {
return 1;
}
- if(!has_initialized.load()) {
- has_initialized.store(true, butil::memory_order_release);
- ready->set_value(true);
- }
+ init_readiness_count++;
return 0;
}
@@ -320,9 +317,9 @@ void ReplicationState::refresh_peers(const std::string & peers) {
}
ReplicationState::ReplicationState(Store *store, ThreadPool* thread_pool, http_message_dispatcher *message_dispatcher,
- std::promise<bool> *ready, bool create_init_db_snapshot):
+ bool create_init_db_snapshot):
node(nullptr), leader_term(-1), store(store), thread_pool(thread_pool),
- message_dispatcher(message_dispatcher),has_initialized(false), ready(ready),
+ message_dispatcher(message_dispatcher), init_readiness_count(0),
create_init_db_snapshot(create_init_db_snapshot) {
}
@@ -331,6 +328,10 @@ void ReplicationState::reset_db() {
store->close();
}
+size_t ReplicationState::get_init_readiness_count() const {
+ return init_readiness_count.load();
+}
+
void InitSnapshotClosure::Run() {
// Auto delete this after Run()
std::unique_ptr<InitSnapshotClosure> self_guard(this);
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 990d58b3..0b4a1a99 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -305,21 +305,21 @@ int run_server(const Config & config, const std::string & version,
server->on(ReplicationState::REPLICATION_MSG, async_write_request);
// first we start the raft service
- std::promise<bool> ready_promise;
- std::future<bool> ready_future = ready_promise.get_future();
ThreadPool thread_pool(4);
- ReplicationState replication_state(&store, &thread_pool, server->get_message_dispatcher(), &ready_promise,
- create_init_db_snapshot);
+ ReplicationState replication_state(&store, &thread_pool, server->get_message_dispatcher(), create_init_db_snapshot);
std::thread raft_thread([&replication_state, &config, &state_dir]() {
std::string path_to_peers = config.get_raft_peers();
start_raft_server(replication_state, state_dir, path_to_peers, config.get_listen_port(), config.get_raft_port());
});
- // wait for raft service to be ready before starting http
- // TODO: should not return until either follower or leader has started
- ready_future.get();
+ // Wait for raft service to be ready before starting http
+ // Follower or leader must have started AND data must also have been loaded
+ LOG(INFO) << "Waiting for peering service to be ready before starting API service...";
+ while(replication_state.get_init_readiness_count() >= 2) {
+ std::this_thread::sleep_for(std::chrono::milliseconds(500));
+ }
if(config.get_master().empty()) {
master_server_routes();