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

Status endpoint for tracking lag against leader.

Kishore Nallan9e5c3ed8b2db
diff --git a/include/core_api.h b/include/core_api.h
index a321d986..6888ff8f 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -102,7 +102,7 @@ bool get_metrics_json(const std::shared_ptr<http_req>& req, const std::shared_pt
bool get_stats_json(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
-bool get_log_sequence(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+bool get_status(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
// operations
diff --git a/include/http_server.h b/include/http_server.h
index 328518aa..0b976cc2 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -202,6 +202,8 @@ private:
ThreadPool* thread_pool;
+ ThreadPool* meta_thread_pool;
+
bool (*auth_handler)(std::map<std::string, std::string>& params,
std::vector<nlohmann::json>& embedded_params_vec,
const std::string& body, const route_path& rpath,
@@ -259,6 +261,8 @@ public:
uint64_t node_state() const;
+ nlohmann::json node_status();
+
void set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params,
std::vector<nlohmann::json>& embedded_params_vec, const std::string& body,
const route_path & rpath, const std::string & auth_key));
@@ -304,13 +308,16 @@ public:
ThreadPool* get_thread_pool() const;
+ ThreadPool* get_meta_thread_pool() const;
+
static constexpr const char* STOP_SERVER_MESSAGE = "STOP_SERVER";
static constexpr const char* STREAM_RESPONSE_MESSAGE = "STREAM_RESPONSE";
static constexpr const char* REQUEST_PROCEED_MESSAGE = "REQUEST_PROCEED";
static constexpr const char* DEFER_PROCESSING_MESSAGE = "DEFER_PROCESSING";
- static int process_request(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response, route_path *rpath,
- const h2o_custom_req_handler_t *req_handler);
+ static int process_request(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response,
+ route_path *rpath, const h2o_custom_req_handler_t *req_handler,
+ bool use_meta_thread_pool);
static void on_deferred_process_request(h2o_timer_t *entry);
diff --git a/include/raft_server.h b/include/raft_server.h
index 4463ddb3..c55ebf0c 100644
--- a/include/raft_server.h
+++ b/include/raft_server.h
@@ -253,6 +253,8 @@ public:
bool is_leader();
+ nlohmann::json get_status();
+
private:
friend class ReplicationClosure;
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 613217dd..547c64b9 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -282,11 +282,9 @@ bool get_stats_json(const std::shared_ptr<http_req>& req, const std::shared_ptr<
return true;
}
-bool get_log_sequence(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
- CollectionManager & collectionManager = CollectionManager::get_instance();
- const uint64_t seq_num = collectionManager.get_store()->get_latest_seq_number();
- res->content_type_header = "text/plain; charset=utf8";
- res->set_body(200, std::to_string(seq_num));
+bool get_status(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ nlohmann::json status = server->node_status();
+ res->set_body(200, status.dump());
return true;
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index b5cdec99..e791d114 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -38,6 +38,8 @@ HttpServer::HttpServer(const std::string & version, const std::string & listen_a
ssl_refresh_timer.timer.expire_at = 0;
metrics_refresh_timer.timer.expire_at = 0;
+ meta_thread_pool = new ThreadPool(4);
+
accept_ctx->ssl_ctx = nullptr;
}
@@ -414,9 +416,12 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
!(
root_resource == "health" || root_resource == "debug" ||
root_resource == "stats.json" || root_resource == "metrics.json" ||
- root_resource == "sequence" || root_resource == "operations" || root_resource == "config"
+ root_resource == "sequence" || root_resource == "operations" ||
+ root_resource == "config" || root_resource == "status"
);
+ bool use_meta_thread_pool = (root_resource == "status");
+
if(needs_readiness_check) {
bool write_op = is_write_request(root_resource, http_method);
bool read_op = !write_op;
@@ -487,7 +492,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
// Full request body is already available, so we don't care if handler is async or not
//LOG(INFO) << "Full request body is already available: " << req->entity.len;
request->last_chunk_aggregate = true;
- return process_request(request, response, rpath, h2o_handler);
+ return process_request(request, response, rpath, h2o_handler, use_meta_thread_pool);
} else {
// Only partial request body is available.
// If rpath->async_req is true, the request handler function will be invoked multiple times, for each chunk
@@ -610,7 +615,7 @@ int HttpServer::async_req_cb(void *ctx, h2o_iovec_t chunk, int is_end_stream) {
// default value for last_chunk_aggregate is false
request->last_chunk_aggregate = (is_end_stream == 1);
- process_request(request, response, custom_generator->rpath, custom_generator->h2o_handler);
+ process_request(request, response, custom_generator->rpath, custom_generator->h2o_handler, false);
return 0;
}
@@ -634,7 +639,8 @@ int HttpServer::async_req_cb(void *ctx, h2o_iovec_t chunk, int is_end_stream) {
}
int HttpServer::process_request(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response,
- route_path *rpath, const h2o_custom_req_handler_t *handler) {
+ route_path *rpath, const h2o_custom_req_handler_t *handler,
+ const bool use_meta_thread_pool) {
//LOG(INFO) << "process_request called";
const std::string& root_resource = (rpath->path_parts.empty()) ? "" : rpath->path_parts[0];
@@ -657,11 +663,13 @@ int HttpServer::process_request(const std::shared_ptr<http_req>& request, const
return 0;
}
- auto http_server = handler->http_server;
auto message_dispatcher = handler->http_server->get_message_dispatcher();
+ auto thread_pool = use_meta_thread_pool ? handler->http_server->get_meta_thread_pool() :
+ handler->http_server->get_thread_pool();
+
// LOG(INFO) << "Before enqueue res: " << response
- handler->http_server->get_thread_pool()->enqueue([rpath, message_dispatcher, request, response]() {
+ thread_pool->enqueue([rpath, message_dispatcher, request, response]() {
// call the API handler
//LOG(INFO) << "Wait for response " << response.get() << ", action: " << rpath->_get_action();
(rpath->handler)(request, response);
@@ -904,6 +912,9 @@ HttpServer::~HttpServer() {
SSL_CTX_free(accept_ctx->ssl_ctx);
delete accept_ctx;
+
+ meta_thread_pool->shutdown();
+ delete meta_thread_pool;
}
http_message_dispatcher* HttpServer::get_message_dispatcher() const {
@@ -933,6 +944,10 @@ uint64_t HttpServer::node_state() const {
return replication_state->node_state();
}
+nlohmann::json HttpServer::node_status() {
+ return replication_state->get_status();
+}
+
bool HttpServer::on_stream_response_message(void *data) {
//LOG(INFO) << "on_stream_response_message";
auto req_res = static_cast<async_req_res_t *>(data);
@@ -1067,3 +1082,7 @@ int64_t HttpServer::get_num_queued_writes() {
bool HttpServer::is_leader() const {
return replication_state->is_leader();
}
+
+ThreadPool* HttpServer::get_meta_thread_pool() const {
+ return meta_thread_pool;
+}
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index ca5e3a3b..7a950e30 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -70,7 +70,7 @@ void master_server_routes() {
server->get("/debug", get_debug);
server->get("/health", get_health);
server->post("/health", post_health);
- server->get("/sequence", get_log_sequence);
+ server->get("/status", get_status);
server->post("/operations/snapshot", post_snapshot, false, true);
server->post("/operations/vote", post_vote, false, false);
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index fcf830d8..327c1fca 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -572,7 +572,8 @@ void ReplicationState::refresh_catchup_status(bool log_msg) {
return ;
}
- bool leader_or_follower = (node->is_leader() || !node->leader_id().is_empty());
+ bool is_leader = node->is_leader();
+ bool leader_or_follower = (is_leader || !node->leader_id().is_empty());
if(!leader_or_follower) {
read_caught_up = write_caught_up = false;
return ;
@@ -625,6 +626,52 @@ void ReplicationState::refresh_catchup_status(bool log_msg) {
this->write_caught_up = true;
}
}
+
+ if(is_leader || !this->read_caught_up) {
+ // no need to re-check status with leader
+ return ;
+ }
+
+ lock.lock();
+
+ if(node->leader_id().is_empty()) {
+ LOG(ERROR) << "Could not get leader status, as node does not have a leader!";
+ return ;
+ }
+
+ const std::string & leader_addr = node->leader_id().to_string();
+ lock.unlock();
+
+ const std::string protocol = api_uses_ssl ? "https" : "http";
+ std::string url = get_node_url_path(leader_addr, "/status", protocol);
+
+ std::string api_res;
+ std::map<std::string, std::string> res_headers;
+ long status_code = HttpClient::get_response(url, api_res, res_headers);
+ if(status_code == 404) {
+ // earlier versions don't have this end-point, so we just ignore
+ return ;
+ }
+
+ if(status_code != 200) {
+ this->read_caught_up = false;
+ return ;
+ }
+
+ // compare leader's applied log with local applied to see if we are lagging
+ nlohmann::json leader_status = nlohmann::json::parse(api_res);
+ if(leader_status.contains("committed_index")) {
+ int64_t leader_committed_index = leader_status["committed_index"].get<int64_t>();
+ if(leader_committed_index <= n_status.committed_index) {
+ // this can happen due to network latency in making the /status call
+ // we will refrain from changing current status
+ return ;
+ }
+ this->read_caught_up = ((leader_committed_index - n_status.committed_index) < healthy_read_lag);
+ } else {
+ // we will refrain from changing current status
+ LOG(ERROR) << "Error, `committed_index` key not found in /status response from leader.";
+ }
}
ReplicationState::ReplicationState(HttpServer* server, BatchedIndexer* batched_indexer,
@@ -774,6 +821,25 @@ bool ReplicationState::is_leader() {
return node->is_leader();
}
+nlohmann::json ReplicationState::get_status() {
+ nlohmann::json status;
+
+ std::shared_lock lock(node_mutex);
+ if(!node) {
+ return status;
+ }
+
+ braft::NodeStatus node_status;
+ node->get_status(&node_status);
+ lock.unlock();
+
+ status["state"] = braft::state2str(node_status.state);
+ status["committed_index"] = node_status.committed_index;
+ status["queued_writes"] = batched_indexer->get_queued_writes();
+
+ return status;
+}
+
void ReplicationState::do_snapshot(const std::string& nodes) {
auto current_ts = std::time(nullptr);
if(current_ts - last_snapshot_ts < snapshot_interval_s) {