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

Fix blocking of http thread during writes.

Kishore Nallan073667e844d2
diff --git a/include/core_api.h b/include/core_api.h
index 59e8f596..470f7cdf 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -102,6 +102,3 @@ bool post_config(const std::shared_ptr<http_req>& req, const std::shared_ptr<htt
void get_collections_for_auth(std::map<std::string, std::string> &req_params, const std::string &body,
const route_path &rpath, std::vector<std::string> &collections);
-bool raft_write_send_response(void *data);
-
-static constexpr const char* SEND_RESPONSE_MSG = "send_response";
diff --git a/include/http_data.h b/include/http_data.h
index ca48e070..536e0976 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -453,12 +453,3 @@ struct http_message_dispatcher {
message_handlers.emplace(message, handler);
}
};
-
-struct request_response_t {
- std::shared_ptr<http_req> req;
- std::shared_ptr<http_res> res;
-
- request_response_t(const std::shared_ptr<http_req> &req, const std::shared_ptr<http_res> &res): req(req), res(res) {
-
- }
-};
diff --git a/include/http_server.h b/include/http_server.h
index 3d976dd8..3e384342 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -54,6 +54,18 @@ struct deferred_req_res_t {
};
+struct defer_processing_t {
+ const std::shared_ptr<http_req> req;
+ const std::shared_ptr<http_res> res;
+ size_t timeout_ms;
+ HttpServer* server;
+
+ defer_processing_t(const std::shared_ptr<http_req> &req, const std::shared_ptr<http_res> &res,
+ size_t timeout_ms, HttpServer* server)
+ : req(req), res(res), timeout_ms(timeout_ms), server(server) {}
+
+};
+
class HttpServer {
private:
h2o_globalconf_t config;
@@ -184,6 +196,8 @@ public:
static bool on_request_proceed_message(void *data);
+ static bool on_deferred_processing_message(void *data);
+
std::string get_version();
ThreadPool* get_thread_pool() const;
@@ -191,6 +205,7 @@ public:
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);
diff --git a/include/raft_server.h b/include/raft_server.h
index a6f43a91..7a0cb7de 100644
--- a/include/raft_server.h
+++ b/include/raft_server.h
@@ -11,6 +11,7 @@
#include "http_data.h"
#include "threadpool.h"
+#include "http_server.h"
class Store;
class ReplicationState;
@@ -89,6 +90,7 @@ private:
butil::atomic<int64_t> leader_term;
std::set<braft::PeerId> peers;
+ HttpServer* server;
Store *store;
ThreadPool* thread_pool;
http_message_dispatcher* message_dispatcher;
@@ -122,7 +124,7 @@ public:
static constexpr const char* meta_dir_name = "meta";
static constexpr const char* snapshot_dir_name = "snapshot";
- ReplicationState(Store* store, ThreadPool* thread_pool, http_message_dispatcher* message_dispatcher,
+ ReplicationState(HttpServer* server, Store* store, ThreadPool* thread_pool, http_message_dispatcher* message_dispatcher,
bool api_uses_ssl, size_t catchup_min_sequence_diff, size_t catch_up_threshold_percentage,
size_t num_collections_parallel_load, size_t num_documents_parallel_load);
@@ -184,8 +186,6 @@ public:
cv.notify_all();
}
- static constexpr const char* REPLICATION_MSG = "raft_replication";
-
private:
friend class ReplicationClosure;
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 6a59bf17..7dbff5be 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -27,6 +27,16 @@ bool handle_authentication(std::map<std::string, std::string>& req_params, const
return collectionManager.auth_key_matches(auth_key, rpath.action, collections, req_params);
}
+void stream_response(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ auto req_res = new deferred_req_res_t(req, res, server, true);
+ server->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
+}
+
+void defer_processing(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res, size_t timeout_ms) {
+ defer_processing_t* defer = new defer_processing_t(req, res, timeout_ms, server);
+ server->get_message_dispatcher()->send_message(HttpServer::DEFER_PROCESSING_MESSAGE, defer);
+}
+
void get_collections_for_auth(std::map<std::string, std::string> &req_params, const std::string &body,
const route_path &rpath, std::vector<std::string> &collections) {
if(req_params.count("collection") != 0) {
@@ -291,7 +301,7 @@ bool get_export_documents(const std::shared_ptr<http_req>& req, const std::share
req->last_chunk_aggregate = true;
res->final = true;
res->set_404();
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -326,7 +336,7 @@ bool get_export_documents(const std::shared_ptr<http_req>& req, const std::share
res->content_type_header = "application/octet-stream";
res->status_code = 200;
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return true;
}
@@ -352,14 +362,14 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
if(!StringUtils::is_uint32_t(req->params[BATCH_SIZE])) {
res->final = true;
res->set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
if(req->params[ACTION] != "create" && req->params[ACTION] != "update" && req->params[ACTION] != "upsert") {
res->final = true;
res->set_400("Parameter `" + std::string(ACTION) + "` must be a create|update|upsert.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -368,7 +378,7 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
if(IMPORT_BATCH_SIZE == 0) {
res->final = true;
res->set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -385,7 +395,7 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
if(collection == nullptr) {
res->final = true;
res->set_404();
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -466,10 +476,10 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
if(stream_proceed) {
res->final = req->last_chunk_aggregate;
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
} else {
// push handler back onto the event loop: we must process the next batch without blocking the event loop
- server->defer_processing(req, res, 1);
+ defer_processing(req, res, 0);
}
return true;
@@ -606,7 +616,7 @@ bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::share
req->last_chunk_aggregate = true;
res->final = true;
res->set_404();
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -621,7 +631,7 @@ bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::share
req->last_chunk_aggregate = true;
res->final = true;
res->set_400("Parameter `" + std::string(FILTER_BY) + "` must be provided.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -629,7 +639,7 @@ bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::share
req->last_chunk_aggregate = true;
res->final = true;
res->set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -639,7 +649,7 @@ bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::share
req->last_chunk_aggregate = true;
res->final = true;
res->set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -659,7 +669,7 @@ bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::share
res->set(filter_ids_op.code(), filter_ids_op.error());
req->last_chunk_aggregate = true;
res->final = true;
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
@@ -696,7 +706,7 @@ bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::share
}
}
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return true;
}
@@ -1015,40 +1025,6 @@ bool del_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_re
return true;
}
-bool raft_write_send_response(void *data) {
- request_response_t* index_arg = static_cast<request_response_t*>(data);
- //LOG(INFO) << "raft_write_send_response " << index_arg->res.get();
- std::unique_ptr<request_response_t> index_arg_guard(index_arg);
-
- bool async_res = false;
-
- if(index_arg->req->route_hash == static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {
- // route not found
- index_arg->res->set_400("Not found.");
- } else if(index_arg->req->route_hash != static_cast<uint64_t>(ROUTE_CODES::ALREADY_HANDLED)) {
- route_path* found_rpath = nullptr;
- bool route_found = server->get_route(index_arg->req->route_hash, &found_rpath);
- if(route_found) {
- async_res = found_rpath->async_res;
- found_rpath->handler(index_arg->req, index_arg->res);
- } else {
- index_arg->res->set_404();
- }
- }
-
- //LOG(INFO) << "raft_write_send_response, async_res=" << async_res;
-
- // only handle synchronous responses as async ones are handled by their handlers
- if(!async_res) {
- // send response and return control back to raft replication thread
- //LOG(INFO) << "raft_write_send_response: sending response";
- server->send_response(index_arg->req, index_arg->res);
- index_arg->res->notify();
- }
-
- return true;
-}
-
bool post_snapshot(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
const std::string SNAPSHOT_PATH = "snapshot_path";
@@ -1059,7 +1035,7 @@ bool post_snapshot(const std::shared_ptr<http_req>& req, const std::shared_ptr<h
req->last_chunk_aggregate = true;
res->final = true;
res->set_400(std::string("Parameter `") + SNAPSHOT_PATH + "` is required.");
- HttpServer::stream_response(req, res);
+ stream_response(req, res);
return false;
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 35898bbd..c20d6b37 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -529,7 +529,10 @@ void HttpServer::on_deferred_process_request(h2o_timer_t *entry) {
route_path* found_rpath = nullptr;
deferred_req_res->server->get_route(deferred_req_res->req->route_hash, &found_rpath);
if(found_rpath) {
- found_rpath->handler(deferred_req_res->req, deferred_req_res->res);
+ // must be called on a separate thread so as not to block http thread
+ deferred_req_res->server->thread_pool->enqueue([found_rpath, deferred_req_res]() {
+ found_rpath->handler(deferred_req_res->req, deferred_req_res->res);
+ });
}
}
@@ -830,6 +833,14 @@ bool HttpServer::on_request_proceed_message(void *data) {
return true;
}
+bool HttpServer::on_deferred_processing_message(void *data) {
+ //LOG(INFO) << "on_deferred_processing_message";
+ defer_processing_t* defer = static_cast<defer_processing_t *>(data);
+ defer->server->defer_processing(defer->req, defer->res, defer->timeout_ms);
+ delete defer;
+ return true;
+}
+
bool HttpServer::has_exited() const {
return exit_loop;
}
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 01cd65a7..23b40a9a 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -171,9 +171,8 @@ void ReplicationState::write_to_leader(const std::shared_ptr<http_req>& request,
}
response->set_500("Could not find a leader.");
- auto replication_arg = new request_response_t{request, response};
- replication_arg->req->route_hash = static_cast<uint64_t>(ROUTE_CODES::ALREADY_HANDLED);
- return message_dispatcher->send_message(REPLICATION_MSG, replication_arg);
+ auto req_res = new deferred_req_res_t(request, response, server, true);
+ return message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
}
if (request->_req->proceed_req && response->proxied_stream) {
@@ -242,10 +241,8 @@ void ReplicationState::write_to_leader(const std::shared_ptr<http_req>& request,
response->set_500(err);
}
- auto replication_arg = new request_response_t{request, response};
- replication_arg->req->route_hash = static_cast<uint64_t>(ROUTE_CODES::ALREADY_HANDLED);
- message_dispatcher->send_message(REPLICATION_MSG, replication_arg);
-
+ auto req_res = new deferred_req_res_t(request, response, server, true);
+ message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
pending_writes--;
});
}
@@ -294,8 +291,22 @@ void ReplicationState::on_apply(braft::Iterator& iter) {
// Call http server thread for write and response back to client (if `response` is NOT null)
// We use a future to block current thread until the async flow finishes
- request_response_t* replication_arg = new request_response_t{request_generated, response_generated};
- message_dispatcher->send_message(REPLICATION_MSG, replication_arg);
+ bool async_res = false;
+
+ route_path* found_rpath = nullptr;
+ bool route_found = server->get_route(request_generated->route_hash, &found_rpath);
+
+ if(route_found) {
+ async_res = found_rpath->async_res;
+ found_rpath->handler(request_generated, response_generated);
+ } else {
+ response_generated->set_404();
+ }
+
+ if(!async_res) {
+ deferred_req_res_t* req_res = new deferred_req_res_t(request_generated, response_generated, server, true);
+ message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
+ }
//LOG(INFO) << "Raft write pre wait " << response_generated.get();
response_generated->wait();
@@ -536,11 +547,12 @@ void ReplicationState::refresh_nodes(const std::string & nodes) {
}
}
-ReplicationState::ReplicationState(Store *store, ThreadPool* thread_pool, http_message_dispatcher *message_dispatcher,
+ReplicationState::ReplicationState(HttpServer* server, Store *store, ThreadPool* thread_pool,
+ http_message_dispatcher *message_dispatcher,
bool api_uses_ssl, size_t catchup_min_sequence_diff,
size_t catch_up_threshold_percentage,
size_t num_collections_parallel_load, size_t num_documents_parallel_load):
- node(nullptr), leader_term(-1), store(store), thread_pool(thread_pool),
+ node(nullptr), leader_term(-1), server(server), store(store), thread_pool(thread_pool),
message_dispatcher(message_dispatcher),
catchup_min_sequence_diff(catchup_min_sequence_diff), catch_up_threshold_percentage(catch_up_threshold_percentage),
num_collections_parallel_load(num_collections_parallel_load),
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 596b1bf5..6a1167eb 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -137,14 +137,6 @@ int init_logger(Config & config, const std::string & server_version) {
return 0;
}
-bool on_send_response(void *data) {
- request_response_t* req_res = static_cast<request_response_t*>(data);
- server->send_response(req_res->req, req_res->res);
- delete req_res;
-
- return true;
-}
-
Option<std::string> fetch_nodes_config(const std::string& path_to_nodes) {
std::string nodes_config;
@@ -377,16 +369,15 @@ int run_server(const Config & config, const std::string & version, void (*master
server->set_auth_handler(handle_authentication);
- server->on(SEND_RESPONSE_MSG, on_send_response);
- server->on(ReplicationState::REPLICATION_MSG, raft_write_send_response);
server->on(HttpServer::STREAM_RESPONSE_MESSAGE, HttpServer::on_stream_response_message);
server->on(HttpServer::REQUEST_PROCEED_MESSAGE, HttpServer::on_request_proceed_message);
+ server->on(HttpServer::DEFER_PROCESSING_MESSAGE, HttpServer::on_deferred_processing_message);
bool ssl_enabled = (!config.get_ssl_cert().empty() && !config.get_ssl_cert_key().empty());
// first we start the peering service
- ReplicationState replication_state(&store, &app_thread_pool, server->get_message_dispatcher(),
+ ReplicationState replication_state(server, &store, &app_thread_pool, server->get_message_dispatcher(),
ssl_enabled, config.get_catch_up_min_sequence_diff(),
config.get_catch_up_threshold_percentage(),
num_collections_parallel_load,