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

Synchronize start of API and state services.

kishorencb9bc0bd31a47
diff --git a/include/http_data.h b/include/http_data.h
index cd7d1634..35d5ecdb 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -89,6 +89,11 @@ struct http_res {
body = "{\"message\": \"" + message + "\"}";
}
+ void send_503(const std::string & message) {
+ status_code = 503;
+ body = "{\"message\": \"" + message + "\"}";
+ }
+
void send(uint32_t code, const std::string & message) {
status_code = code;
body = "{\"message\": \"" + message + "\"}";
diff --git a/include/http_server.h b/include/http_server.h
index 70928dc6..38a2dfe5 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -61,7 +61,7 @@ private:
static int catch_all_handler(h2o_handler_t *self, h2o_req_t *req);
- static int send_401_unauthorized(h2o_req_t *req);
+ static int send_response(h2o_req_t *req, int status_code, const std::string & message);
public:
HttpServer(const std::string & version,
diff --git a/include/raft_server.h b/include/raft_server.h
index 9f3b1f0a..1c3fe625 100644
--- a/include/raft_server.h
+++ b/include/raft_server.h
@@ -120,10 +120,8 @@ 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;
+ bool is_ready() const {
+ return init_readiness_count >= 2;
}
// Shut this node down.
@@ -172,7 +170,7 @@ private:
// have to do a dummy write, otherwise snapshot will not trigger
if(create_init_db_snapshot) {
- http_req* request = new http_req(nullptr, "", 0, {}, "INIT_SNAPSHOT");
+ http_req* request = new http_req(nullptr, "POST", 0, {}, "INIT_SNAPSHOT");
http_res* response = new http_res();
write(request, response);
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index e6645142..7ed0ec24 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -243,6 +243,13 @@ uint64_t HttpServer::find_route(const std::vector<std::string> & path_parts, con
int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
h2o_custom_req_handler_t *self = (h2o_custom_req_handler_t *)_self;
+ // Wait for replicating state to be ready before starting http
+ // Follower or leader must have started AND data must also have been loaded
+ if(!self->http_server->get_replication_state()->is_ready()) {
+ std::string message = "{ \"message\": \"Not Ready\"}";
+ return send_response(req, 503, message);
+ }
+
const std::string & http_method = std::string(req->method.base, req->method.len);
const std::string & path = std::string(req->path.base, req->path.len);
@@ -314,7 +321,9 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
if(route_hash != static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {
bool authenticated = self->http_server->auth_handler(*rpath, auth_key_from_header);
if(!authenticated) {
- return send_401_unauthorized(req);
+ std::string message = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER +
+ "` header must be sent.\"}";
+ return send_response(req, 401, message);
}
// routes match and is an authenticated request - iterate and extract path params
@@ -345,15 +354,8 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
return 0;
}
- h2o_generator_t generator = {NULL, NULL};
- h2o_iovec_t res_body = h2o_strdup(&req->pool, "{ \"message\": \"Not Found\"}", SIZE_MAX);
- req->res.status = 404;
- req->res.reason = http_res::get_status_reason(404);
- h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("application/json; charset=utf-8"));
- h2o_start_response(req, &generator);
- h2o_send(req, &res_body, 1, H2O_SEND_STATE_FINAL);
-
- return 0;
+ std::string message = "{ \"message\": \"Not Found\"}";
+ return send_response(req, 404, message);
}
void HttpServer::send_message(const std::string & type, void* data) {
@@ -375,12 +377,10 @@ void HttpServer::send_response(http_req* request, const http_res* response) {
delete response;
}
-int HttpServer::send_401_unauthorized(h2o_req_t *req) {
+int HttpServer::send_response(h2o_req_t *req, int status_code, const std::string & message) {
h2o_generator_t generator = {NULL, NULL};
- std::string res_body = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER +
- "` header must be sent.\"}";
- h2o_iovec_t body = h2o_strdup(&req->pool, res_body.c_str(), SIZE_MAX);
- req->res.status = 401;
+ h2o_iovec_t body = h2o_strdup(&req->pool, message.c_str(), SIZE_MAX);
+ req->res.status = status_code;
req->res.reason = http_res::get_status_reason(req->res.status);
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("application/json; charset=utf-8"));
h2o_start_response(req, &generator);
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 3813b1ee..c4eb23b1 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -64,10 +64,9 @@ int ReplicationState::start(const std::string & peering_address, int peering_por
bool snapshot_exists = dir_enum_count(snapshot_dir) > 0;
if(snapshot_exists) {
- // we will be assured of on_snapshot() firing and we will wait for that to init_db()
+ // we will be assured of on_snapshot_load() firing and we will wait for that to init_db()
} else if(!create_init_db_snapshot) {
- // `create_init_db_snapshot` will be handled only after leader starts, otherwise:
-
+ // `create_init_db_snapshot` can be handled separately only after leader starts
LOG(INFO) << "Snapshot does not exist. We will remove db dir and init db fresh.";
reset_db();
@@ -98,7 +97,6 @@ int ReplicationState::start(const std::string & peering_address, int peering_por
if(!status.ok()) {
LOG(ERROR) << status.error_str();
}
- init_readiness_count++; // sometimes, single node on_leader event does not fire
}
this->node = node;
@@ -183,7 +181,6 @@ void ReplicationState::read(http_res* response) {
void ReplicationState::on_apply(braft::Iterator& iter) {
// A batch of tasks are committed, which must be processed through
// |iter|
- LOG(INFO) << "on_apply called";
for (; iter.valid(); iter.next()) {
http_res* response;
http_req* request;
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 6e003862..d67eee99 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -324,15 +324,9 @@ int run_server(const Config & config, const std::string & version, void (*master
config.get_peering_address(), config.get_peering_port(), config.get_api_port());
});
- // Wait for peering 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 && !quit_raft_service) {
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- }
+ LOG(INFO) << "Starting API service...";
master_server_routes();
-
int ret_code = server->run(&replication_state);
// we are out of the event loop here