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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Refactor http code to remove circular references.
kishorenca975f3968573
diff --git a/include/core_api.h b/include/core_api.hindex 0a0e2eb7..6ccb3850 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -4,34 +4,32 @@bool handle_authentication(const route_path & rpath, const std::string & auth_key);-void get_collections(http_req & req, http_res & res);+bool get_collections(http_req & req, http_res & res);-void post_create_collection(http_req & req, http_res & res);+bool post_create_collection(http_req & req, http_res & res);-void del_drop_collection(http_req & req, http_res & res);+bool del_drop_collection(http_req & req, http_res & res);-void get_debug(http_req & req, http_res & res);+bool get_debug(http_req & req, http_res & res);-void get_health(http_req & req, http_res & res);+bool get_health(http_req & req, http_res & res);-void get_search(http_req & req, http_res & res);+bool get_search(http_req & req, http_res & res);-void get_collection_summary(http_req & req, http_res & res);+bool get_collection_summary(http_req & req, http_res & res);-void get_export_documents(http_req & req, http_res & res);+bool get_export_documents(http_req & req, http_res & res);-void post_add_document(http_req & req, http_res & res);+bool post_add_document(http_req & req, http_res & res);-void post_import_documents(http_req & req, http_res & res);+bool post_import_documents(http_req & req, http_res & res);-void get_fetch_document(http_req & req, http_res & res);+bool get_fetch_document(http_req & req, http_res & res);-void del_remove_document(http_req & req, http_res & res);+bool del_remove_document(http_req & req, http_res & res);-void get_replication_updates(http_req &req, http_res &res);+bool get_replication_updates(http_req &req, http_res &res);-void on_send_response(void *data);--void collection_export_handler(http_req* req, http_res* res, void* data);+bool collection_export_handler(http_req* req, http_res* res, void* data);static constexpr const char* SEND_RESPONSE_MSG = "send_response";diff --git a/include/http_data.h b/include/http_data.hindex 9b1534a0..a0b2ed84 100644--- a/include/http_data.h+++ b/include/http_data.h@@ -1,6 +1,6 @@#pragma once-#include <stdint.h>+#include <cstdint>#include <string>#include <map>#include <vector>@@ -11,17 +11,28 @@ extern "C" {#include "h2o.h"}-class HttpServer;-struct http_res {uint32_t status_code;std::string content_type_header;std::string body;- HttpServer* server;bool final;- http_res(): content_type_header("application/json; charset=utf-8"), final(true) {+ http_res(): status_code(501), content_type_header("application/json; charset=utf-8"), final(true) {++ }+ static const char* get_status_reason(uint32_t status_code) {+ switch(status_code) {+ case 200: return "OK";+ case 201: return "Created";+ case 400: return "Bad Request";+ case 401: return "Unauthorized";+ case 404: return "Not Found";+ case 409: return "Conflict";+ case 422: return "Unprocessable Entity";+ case 500: return "Internal Server Error";+ default: return "";+ }}void send_200(const std::string & res_body) {@@ -77,14 +88,17 @@ struct http_res {struct http_req {h2o_req_t* _req;+ std::string http_method;std::string path_without_query;std::map<std::string, std::string> params;std::string body;http_req() {}- http_req(h2o_req_t* _req, const std::string & path_without_query, const std::map<std::string, std::string> & params,- std::string body): _req(_req), path_without_query(path_without_query), params(params), body(body) {}+ http_req(h2o_req_t* _req, const std::string & http_method, const std::string & path_without_query,+ const std::map<std::string, std::string> & params,+ std::string body): _req(_req), http_method(http_method), path_without_query(path_without_query),+ params(params), body(body) {}void deserialize(const std::string& serialized_content) {nlohmann::json content = nlohmann::json::parse(serialized_content);@@ -108,13 +122,79 @@ struct http_req {}};+struct request_response {+ http_req* req;+ http_res* res;+};+struct route_path {std::string http_method;std::vector<std::string> path_parts;- void (*handler)(http_req &, http_res &);+ bool (*handler)(http_req &, http_res &);bool async;inline bool operator< (const route_path& rhs) const {return true;}+};++struct h2o_custom_generator_t {+ h2o_generator_t super;+ bool (*handler)(http_req* req, http_res* res, void* data);+ http_req* req;+ http_res* res;+ void* data;+};++struct h2o_custom_res_message_t {+ h2o_multithread_message_t super;+ std::map<std::string, bool (*)(void*)> *message_handlers;+ std::string type;+ void* data;+};++struct http_message_dispatcher {+ h2o_multithread_queue_t* message_queue;+ h2o_multithread_receiver_t* message_receiver;+ std::map<std::string, bool (*)(void*)> message_handlers;++ void init(h2o_loop_t *loop) {+ message_queue = h2o_multithread_create_queue(loop);+ message_receiver = new h2o_multithread_receiver_t();+ h2o_multithread_register_receiver(message_queue, message_receiver, on_message);+ }++ ~http_message_dispatcher() {+ // drain existing messages+ on_message(message_receiver, &message_receiver->_messages);++ h2o_multithread_unregister_receiver(message_queue, message_receiver);+ h2o_multithread_destroy_queue(message_queue);+ free(message_queue);+ delete message_receiver;+ }++ static void on_message(h2o_multithread_receiver_t *receiver, h2o_linklist_t *messages) {+ while (!h2o_linklist_is_empty(messages)) {+ h2o_multithread_message_t *message = H2O_STRUCT_FROM_MEMBER(h2o_multithread_message_t, link, messages->next);+ h2o_custom_res_message_t *custom_message = reinterpret_cast<h2o_custom_res_message_t*>(message);++ if(custom_message->message_handlers->count(custom_message->type) != 0) {+ auto handler = custom_message->message_handlers->at(custom_message->type);+ (handler)(custom_message->data);+ }++ h2o_linklist_unlink(&message->link);+ delete custom_message;+ }+ }++ void send_message(const std::string & type, void* data) {+ h2o_custom_res_message_t* message = new h2o_custom_res_message_t{{{NULL, NULL}}, &message_handlers, type.c_str(), data};+ h2o_multithread_send_message(message_receiver, &message->super);+ }++ void on(const std::string & message, bool (*handler)(void*)) {+ message_handlers.emplace(message, handler);+ }};\ No newline at end of filediff --git a/include/http_server.h b/include/http_server.hindex 35cc6053..0166646f 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -11,13 +11,11 @@ extern "C" {#include <map>#include <string>-#include <stdio.h>+#include <cstdio>#include "http_data.h"+#include "option.h"-struct request_response {- http_req* req;- http_res* response;-};+class ReplicationState;class HttpServer {private:@@ -27,16 +25,17 @@ private:h2o_accept_ctx_t* accept_ctx;h2o_hostconf_t *hostconf;h2o_socket_t* listener_socket;- h2o_multithread_queue_t* message_queue;- h2o_multithread_receiver_t* message_receiver;++ http_message_dispatcher* message_dispatcher;++ ReplicationState* replication_state;+bool exit_loop = false;std::string version;std::vector<route_path> routes;- std::map<std::string, void (*)(void*)> message_handlers;-const std::string listen_address;const uint32_t listen_port;@@ -58,14 +57,10 @@ private:h2o_pathconf_t *register_handler(h2o_hostconf_t *hostconf, const char *path,int (*on_req)(h2o_handler_t *, h2o_req_t *));- static const char* get_status_reason(uint32_t status_code);-static std::map<std::string, std::string> parse_query(const std::string& query);static int catch_all_handler(h2o_handler_t *self, h2o_req_t *req);- static void on_message(h2o_multithread_receiver_t *receiver, h2o_linklist_t *messages);-static int send_401_unauthorized(h2o_req_t *req);public:@@ -75,36 +70,36 @@ public:~HttpServer();+ http_message_dispatcher* get_message_dispatcher() const;++ ReplicationState* get_replication_state() const;+void set_auth_handler(bool (*handler)(const route_path & rpath, const std::string & auth_key));- void get(const std::string & path, void (*handler)(http_req & req, http_res & res), bool async = false);+ void get(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async = false);- void post(const std::string & path, void (*handler)(http_req & req, http_res & res), bool async = false);+ void post(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async = false);- void put(const std::string & path, void (*handler)(http_req & req, http_res & res), bool async = false);+ void put(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async = false);- void del(const std::string & path, void (*handler)(http_req & req, http_res & res), bool async = false);+ void del(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async = false);- void on(const std::string & message, void (*handler)(void*));+ void on(const std::string & message, bool (*handler)(void*));void send_message(const std::string & type, void* data);void send_response(http_req* request, const http_res* response);- void stream_response(void (*handler)(http_req* req, http_res* res, void* data), http_req & request,- http_res & response, void* data);-- static void response_proceed(h2o_generator_t *generator, h2o_req_t *req);-- static void response_stop(h2o_generator_t *generator, h2o_req_t *req);+ bool find_route(const std::vector<std::string> & path_parts, const std::string & http_method,+ route_path** found_rpath);- int run();+ int run(ReplicationState* replication_state);void stop();void clear_timeouts(const std::vector<h2o_timeout_t*> & timeouts);- static void on_stop_server(void *data);+ static bool on_stop_server(void *data);std::string get_version();diff --git a/include/raft_server.h b/include/raft_server.hindex bae7a347..a248bdd5 100644--- a/include/raft_server.h+++ b/include/raft_server.h@@ -9,30 +9,25 @@#include <rocksdb/db.h>#include "http_data.h"-#include "http_server.h"-class ReplicationState;// Implements the callback for the state machineclass ReplicationClosure : public braft::Closure {private:- ReplicationState *searchStore;- HttpServer* httpServer;- const http_req* request;+ http_message_dispatcher* message_dispatcher;+ http_req* request;http_res* response;- google::protobuf::Closure* done;public:- ReplicationClosure(ReplicationState *searchStore, HttpServer* httpServer, const http_req* request, http_res* response,- google::protobuf::Closure* done):- searchStore(searchStore), httpServer(httpServer),- request(request), response(response), done(done) {+ ReplicationClosure(http_message_dispatcher* message_dispatcher,+ http_req* request, http_res* response): message_dispatcher(message_dispatcher),+ request(request), response(response) {}~ReplicationClosure() {}- const http_req* get_request() const {+ http_req* get_request() const {return request;}@@ -69,11 +64,13 @@ private:braft::Node* volatile _node;butil::atomic<int64_t> _value;butil::atomic<int64_t> _leader_term;- rocksdb::DB* _db;- HttpServer* _http_server;+ rocksdb::DB* db;+ http_message_dispatcher* message_dispatcher;public:- ReplicationState(): _node(NULL), _value(0), _leader_term(-1) {+ ReplicationState(http_message_dispatcher* message_dispatcher, rocksdb::DB* db):+ _node(NULL), _value(0), _leader_term(-1), db(db),+ message_dispatcher(message_dispatcher) {}@@ -86,7 +83,7 @@ public:const std::string & data_path, const std::string & peers);// Generic write method for synchronizing all writes- void write(const http_req* request, http_res* response, google::protobuf::Closure* done);+ void write(http_req* request, http_res* response);// Generic read method for consistent readsvoid read(http_res* response);@@ -112,12 +109,23 @@ public:}}+ static constexpr const char* REPLICATION_MSG = "raft_replication";++ static bool on_raft_replication(void *data);+private:+friend class ReplicationClosure;// redirecting request to leadervoid redirect(http_res* response);+ struct ReplicationArg {+ http_req* req;+ http_res* res;+ braft::Closure* done;+ };+// actual application of writes onto the WALvoid on_apply(braft::Iterator& iter);diff --git a/include/replicator.h b/include/replicator.hindex 020167f3..09f361b5 100644--- a/include/replicator.h+++ b/include/replicator.h@@ -1,7 +1,7 @@#pragma once#include <string>-#include "http_server.h"+#include "http_data.h"#include "store.h"static constexpr const char* REPLICATION_EVENT_MSG = "replication_event";@@ -19,9 +19,9 @@ struct ReplicationEvent {class IterateBatchHandler: public rocksdb::WriteBatch::Handler {private:- HttpServer* server;+ http_message_dispatcher* message_dispatcher;public:- IterateBatchHandler(HttpServer* server): server(server) {+ IterateBatchHandler(http_message_dispatcher* message_dispatcher): message_dispatcher(message_dispatcher) {}@@ -34,7 +34,7 @@ public:class Replicator {public:- static void start(HttpServer* server, const std::string & master_host_port, const std::string & api_key, Store& store);+ static void start(http_message_dispatcher* message_dispatcher, const std::string & master_host_port, const std::string & api_key, Store& store);- static void on_replication_event(void *data);+ static bool on_replication_event(void *data);};diff --git a/include/typesense_server_utils.h b/include/typesense_server_utils.hindex 2e40c518..c9147348 100644--- a/include/typesense_server_utils.h+++ b/include/typesense_server_utils.h@@ -7,11 +7,11 @@#include "logger.h"#include "store.h"#include "collection_manager.h"-#include "http_server.h"#include "replicator.h"-#include <signal.h>+#include <csignal>#include <sys/types.h>#include <sys/stat.h>+#include "http_server.h"extern HttpServer* server;@@ -19,9 +19,12 @@ void catch_interrupt(int sig);bool directory_exists(const std::string & dir_path);+void stream_response(bool (*handler)(http_req* req, http_res* res, void* data),+ http_req & request, http_res & response, void* data);+void init_cmdline_options(cmdline::parser & options, int argc, char **argv);int init_logger(Config & config, const std::string & server_version, std::unique_ptr<g3::LogWorker> & log_worker);int run_server(const Config & config, const std::string & version,- void (*master_server_routes)(), void (*replica_server_routes)());\ No newline at end of file+ void (*master_server_routes)(), void (*replica_server_routes)());diff --git a/src/core_api.cpp b/src/core_api.cppindex b5cf6e75..3e83cf85 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -2,6 +2,7 @@#include <chrono>#include <thread>#include <sys/resource.h>+#include "typesense_server_utils.h"#include "core_api.h"#include "string_utils.h"#include "collection.h"@@ -37,7 +38,7 @@ bool handle_authentication(const route_path & rpath, const std::string & auth_ke(rpath.handler == get_search && collectionManager.search_only_auth_key_matches(auth_key));}-void get_collections(http_req & req, http_res & res) {+bool get_collections(http_req & req, http_res & res) {CollectionManager & collectionManager = CollectionManager::get_instance();std::vector<Collection*> collections = collectionManager.get_collections();nlohmann::json json_response = nlohmann::json::array();@@ -48,16 +49,18 @@ void get_collections(http_req & req, http_res & res) {}res.send_200(json_response.dump());+ return true;}-void post_create_collection(http_req & req, http_res & res) {+bool post_create_collection(http_req & req, http_res & res) {nlohmann::json req_json;try {req_json = nlohmann::json::parse(req.body);} catch(const std::exception& e) {- LOG(ERR) << "JSON error: " << e.what();- return res.send_400("Bad JSON.");+ //LOG(ERR) << "JSON error: " << e.what();+ res.send_400("Bad JSON.");+ return false;}CollectionManager & collectionManager = CollectionManager::get_instance();@@ -65,26 +68,31 @@ void post_create_collection(http_req & req, http_res & res) {// validate presence of mandatory fieldsif(req_json.count("name") == 0) {- return res.send_400("Parameter `name` is required.");+ res.send_400("Parameter `name` is required.");+ return false;}if(req_json.count("fields") == 0) {- return res.send_400("Parameter `fields` is required.");+ res.send_400("Parameter `fields` is required.");+ return false;}const char* DEFAULT_SORTING_FIELD = "default_sorting_field";if(req_json.count(DEFAULT_SORTING_FIELD) == 0) {- return res.send_400("Parameter `default_sorting_field` is required.");+ res.send_400("Parameter `default_sorting_field` is required.");+ return false;}if(!req_json[DEFAULT_SORTING_FIELD].is_string()) {- return res.send_400(std::string("`") + DEFAULT_SORTING_FIELD ++ res.send_400(std::string("`") + DEFAULT_SORTING_FIELD +"` should be a string. It should be the name of an int32/float field.");+ return false;}if(collectionManager.get_collection(req_json["name"]) != nullptr) {- return res.send_409("Collection with name `" + req_json["name"].get<std::string>() + "` already exists.");+ res.send_409("Collection with name `" + req_json["name"].get<std::string>() + "` already exists.");+ return false;}// field specific validation@@ -92,8 +100,9 @@ void post_create_collection(http_req & req, http_res & res) {std::vector<field> fields;if(!req_json["fields"].is_array() || req_json["fields"].size() == 0) {- return res.send_400("Wrong format for `fields`. It should be an array like: "+ res.send_400("Wrong format for `fields`. It should be an array like: ""[{\"name\": \"<field_name>\", \"type\": \"<field_type>\"}]");+ return false;}for(nlohmann::json & field_json: req_json["fields"]) {@@ -101,13 +110,15 @@ void post_create_collection(http_req & req, http_res & res) {field_json.count(fields::name) == 0 || field_json.count(fields::type) == 0 ||!field_json.at(fields::name).is_string() || !field_json.at(fields::type).is_string()) {- return res.send_400("Wrong format for `fields`. It should be an array of objects containing "+ res.send_400("Wrong format for `fields`. It should be an array of objects containing ""`name`, `type` and optionally, `facet` properties.");+ return false;}if(field_json.count("facet") != 0 && !field_json.at(fields::facet).is_boolean()) {- return res.send_400(std::string("The `facet` property of the field `") ++ res.send_400(std::string("The `facet` property of the field `") +field_json.at(fields::name).get<std::string>() + "` should be a boolean.");+ return false;}if(field_json.count("facet") == 0) {@@ -129,44 +140,52 @@ void post_create_collection(http_req & req, http_res & res) {if(collection_op.ok()) {nlohmann::json json_response = collection_summary_json(collection_op.get());- return res.send_201(json_response.dump());+ res.send_201(json_response.dump());+ return true;}- return res.send(collection_op.code(), collection_op.error());+ res.send(collection_op.code(), collection_op.error());+ return false;}-void del_drop_collection(http_req & req, http_res & res) {+bool del_drop_collection(http_req & req, http_res & res) {std::string doc_id = req.params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(!collection) {- return res.send_404();+ res.send_404();+ return false;}nlohmann::json collection_json = collection_summary_json(collection);Option<bool> drop_result = collectionManager.drop_collection(req.params["collection"]);if(!drop_result.ok()) {- return res.send(drop_result.code(), drop_result.error());+ res.send(drop_result.code(), drop_result.error());+ return false;}res.send_200(collection_json.dump());+ return true;}-void get_debug(http_req & req, http_res & res) {+bool get_debug(http_req & req, http_res & res) {nlohmann::json result;- result["version"] = res.server->get_version();+ result["version"] = server->get_version();res.send_200(result.dump());++ return true;}-void get_health(http_req & req, http_res & res) {+bool get_health(http_req & req, http_res & res) {nlohmann::json result;result["ok"] = true;res.send_200(result.dump());+ return true;}-void get_search(http_req & req, http_res & res) {+bool get_search(http_req & req, http_res & res) {auto begin = std::chrono::high_resolution_clock::now();const char *NUM_TYPOS = "num_typos";@@ -213,11 +232,13 @@ void get_search(http_req & req, http_res & res) {}if(req.params.count(QUERY) == 0) {- return res.send_400(std::string("Parameter `") + QUERY + "` is required.");+ res.send_400(std::string("Parameter `") + QUERY + "` is required.");+ return false;}if(req.params.count(QUERY_BY) == 0) {- return res.send_400(std::string("Parameter `") + QUERY_BY + "` is required.");+ res.send_400(std::string("Parameter `") + QUERY_BY + "` is required.");+ return false;}if(req.params.count(MAX_FACET_VALUES) == 0) {@@ -262,23 +283,28 @@ void get_search(http_req & req, http_res & res) {}if(!StringUtils::is_uint64_t(req.params[DROP_TOKENS_THRESHOLD])) {- return res.send_400("Parameter `" + std::string(DROP_TOKENS_THRESHOLD) + "` must be an unsigned integer.");+ res.send_400("Parameter `" + std::string(DROP_TOKENS_THRESHOLD) + "` must be an unsigned integer.");+ return false;}if(!StringUtils::is_uint64_t(req.params[TYPO_TOKENS_THRESHOLD])) {- return res.send_400("Parameter `" + std::string(TYPO_TOKENS_THRESHOLD) + "` must be an unsigned integer.");+ res.send_400("Parameter `" + std::string(TYPO_TOKENS_THRESHOLD) + "` must be an unsigned integer.");+ return false;}if(!StringUtils::is_uint64_t(req.params[NUM_TYPOS])) {- return res.send_400("Parameter `" + std::string(NUM_TYPOS) + "` must be an unsigned integer.");+ res.send_400("Parameter `" + std::string(NUM_TYPOS) + "` must be an unsigned integer.");+ return false;}if(!StringUtils::is_uint64_t(req.params[PER_PAGE])) {- return res.send_400("Parameter `" + std::string(PER_PAGE) + "` must be an unsigned integer.");+ res.send_400("Parameter `" + std::string(PER_PAGE) + "` must be an unsigned integer.");+ return false;}if(!StringUtils::is_uint64_t(req.params[PAGE])) {- return res.send_400("Parameter `" + std::string(PAGE) + "` must be an unsigned integer.");+ res.send_400("Parameter `" + std::string(PAGE) + "` must be an unsigned integer.");+ return false;}std::string filter_str = req.params.count(FILTER) != 0 ? req.params[FILTER] : "";@@ -304,7 +330,8 @@ void get_search(http_req & req, http_res & res) {StringUtils::split(req.params[SORT_BY], sort_field_strs, ",");if(sort_field_strs.size() > 2) {- return res.send_400("Only upto 2 sort fields are allowed.");+ res.send_400("Only upto 2 sort fields are allowed.");+ return false;}for(const std::string & sort_field_str: sort_field_strs) {@@ -312,7 +339,8 @@ void get_search(http_req & req, http_res & res) {StringUtils::split(sort_field_str, expression_parts, ":");if(expression_parts.size() != 2) {- return res.send_400(std::string("Parameter `") + SORT_BY + "` is malformed.");+ res.send_400(std::string("Parameter `") + SORT_BY + "` is malformed.");+ return false;}StringUtils::toupper(expression_parts[1]);@@ -324,7 +352,8 @@ void get_search(http_req & req, http_res & res) {Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {- return res.send_404();+ res.send_404();+ return false;}bool prefix = (req.params[PREFIX] == "true");@@ -359,7 +388,8 @@ void get_search(http_req & req, http_res & res) {if(!result_op.ok()) {const std::string & json_res_body = (req.params.count(CALLBACK) == 0) ? result_op.error() :(req.params[CALLBACK] + "(" + result_op.error() + ");");- return res.send(result_op.code(), json_res_body);+ res.send(result_op.code(), json_res_body);+ return false;}nlohmann::json result = result_op.get();@@ -378,26 +408,31 @@ void get_search(http_req & req, http_res & res) {}//LOG(INFO) << "Time taken: " << timeMillis << "ms";+ return true;}-void get_collection_summary(http_req & req, http_res & res) {+bool get_collection_summary(http_req & req, http_res & res) {CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {- return res.send_404();+ res.send_404();+ return false;}nlohmann::json json_response = collection_summary_json(collection);res.send_200(json_response.dump());++ return true;}-void collection_export_handler(http_req* req, http_res* res, void* data) {+bool collection_export_handler(http_req* req, http_res* res, void* data) {CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req->params["collection"]);if(!collection) {- return res->send_404();+ res->send_404();+ return false;}const std::string seq_id_prefix = collection->get_seq_id_collection_prefix();@@ -418,16 +453,18 @@ void collection_export_handler(http_req* req, http_res* res, void* data) {res->final = true;delete it;}++ return true;}-void get_export_documents(http_req & req, http_res & res) {+bool get_export_documents(http_req & req, http_res & res) {CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {res.send_404();- res.server->send_message(SEND_RESPONSE_MSG, new request_response{&req, &res});- return ;+ server->send_message(SEND_RESPONSE_MSG, new request_response{&req, &res});+ return false;}const std::string seq_id_prefix = collection->get_seq_id_collection_prefix();@@ -437,92 +474,106 @@ void get_export_documents(http_req & req, http_res & res) {res.content_type_header = "application/octet-stream";res.status_code = 200;- res.server->stream_response(collection_export_handler, req, res, (void *) it);+ stream_response(collection_export_handler, req, res, (void *) it);+ return true;}-void post_add_document(http_req & req, http_res & res) {+bool post_add_document(http_req & req, http_res & res) {CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {- return res.send_404();+ res.send_404();+ return false;}Option<nlohmann::json> inserted_doc_op = collection->add(req.body);if(!inserted_doc_op.ok()) {res.send(inserted_doc_op.code(), inserted_doc_op.error());- } else {- res.send_201(inserted_doc_op.get().dump());+ return false;}++ res.send_201(inserted_doc_op.get().dump());+ return true;}-void post_import_documents(http_req & req, http_res & res) {+bool post_import_documents(http_req & req, http_res & res) {CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {- return res.send_404();+ res.send_404();+ return false;}Option<nlohmann::json> result = collection->add_many(req.body);if(!result.ok()) {res.send(result.code(), result.error());- return ;+ return false;}res.send_200(result.get().dump());+ return true;}-void get_fetch_document(http_req & req, http_res & res) {+bool get_fetch_document(http_req & req, http_res & res) {std::string doc_id = req.params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {- return res.send_404();+ res.send_404();+ return false;}Option<nlohmann::json> doc_option = collection->get(doc_id);if(!doc_option.ok()) {res.send(doc_option.code(), doc_option.error());- } else {- res.send_200(doc_option.get().dump());+ return false;}++ res.send_200(doc_option.get().dump());+ return true;}-void del_remove_document(http_req & req, http_res & res) {+bool del_remove_document(http_req & req, http_res & res) {std::string doc_id = req.params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();Collection* collection = collectionManager.get_collection(req.params["collection"]);if(collection == nullptr) {- return res.send_404();+ res.send_404();+ return false;}Option<nlohmann::json> doc_option = collection->get(doc_id);if(!doc_option.ok()) {- return res.send(doc_option.code(), doc_option.error());+ res.send(doc_option.code(), doc_option.error());+ return false;}Option<std::string> deleted_id_op = collection->remove(doc_id);if(!deleted_id_op.ok()) {res.send(deleted_id_op.code(), deleted_id_op.error());- } else {- nlohmann::json doc = doc_option.get();- res.send_200(doc.dump());+ return false;}++ nlohmann::json doc = doc_option.get();+ res.send_200(doc.dump());+ return true;}-void get_replication_updates(http_req & req, http_res & res) {+bool get_replication_updates(http_req & req, http_res & res) {// Could be heavy - spawn a new thread so we don't block the main threadstd::thread response_thread([&]() {if(!StringUtils::is_uint64_t(req.params["seq_number"])) {- return res.send_400("The value of the parameter `seq_number` must be an unsigned integer.");+ res.send_400("The value of the parameter `seq_number` must be an unsigned integer.");+ return false;}const uint64_t MAX_UPDATES_TO_SEND = 10000;@@ -533,8 +584,8 @@ void get_replication_updates(http_req & req, http_res & res) {Option<std::vector<std::string>*> updates_op = store->get_updates_since(seq_number, MAX_UPDATES_TO_SEND);if(!updates_op.ok()) {res.send(updates_op.code(), updates_op.error());- res.server->send_message(SEND_RESPONSE_MSG, new request_response{&req, &res});- return ;+ server->send_message(SEND_RESPONSE_MSG, new request_response{&req, &res});+ return false;}nlohmann::json json_response;@@ -549,15 +600,10 @@ void get_replication_updates(http_req & req, http_res & res) {json_response["latest_seq_num"] = latest_seq_num;res.send_200(json_response.dump());- res.server->send_message(SEND_RESPONSE_MSG, new request_response{&req, &res});+ server->send_message(SEND_RESPONSE_MSG, new request_response{&req, &res});delete updates;});response_thread.detach();-}--void on_send_response(void *data) {- request_response* req_res = static_cast<request_response*>(data);- req_res->response->server->send_response(req_res->req, req_res->response);- delete req_res;+ return true;}diff --git a/src/http_server.cpp b/src/http_server.cppindex ae6eb0cd..d4d5f749 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -6,6 +6,7 @@#include <signal.h>#include <h2o.h>#include <iostream>+#include "raft_server.h"#include "logger.h"struct h2o_custom_req_handler_t {@@ -13,20 +14,6 @@ struct h2o_custom_req_handler_t {HttpServer* http_server;};-struct h2o_custom_res_message_t {- h2o_multithread_message_t super;- HttpServer* http_server;- std::string type;- void* data;-};--struct h2o_custom_generator_t {- h2o_generator_t super;- void (*handler)(http_req* req, http_res* res, void* data);- request_response req_res;- void* data;-};-HttpServer::HttpServer(const std::string & version, const std::string & listen_address,uint32_t listen_port, const std::string & ssl_cert_path,const std::string & ssl_cert_key_path, bool cors_enabled):@@ -36,6 +23,7 @@ HttpServer::HttpServer(const std::string & version, const std::string & listen_ah2o_config_init(&config);hostconf = h2o_config_register_host(&config, h2o_iovec_init(H2O_STRLIT("default")), 65535);register_handler(hostconf, "/", catch_all_handler);+ message_dispatcher = new http_message_dispatcher;}void HttpServer::on_accept(h2o_socket_t *listener, const char *err) {@@ -127,13 +115,12 @@ int HttpServer::create_listener(void) {return 0;}-int HttpServer::run() {+int HttpServer::run(ReplicationState* replication_state) {signal(SIGPIPE, SIG_IGN);h2o_context_init(&ctx, h2o_evloop_create(), &config);- message_queue = h2o_multithread_create_queue(ctx.loop);- message_receiver = new h2o_multithread_receiver_t();- h2o_multithread_register_receiver(message_queue, message_receiver, on_message);+ this->replication_state = replication_state;+ message_dispatcher->init(ctx.loop);if (create_listener() != 0) {LOG(ERR) << "Failed to listen on " << listen_address << ":" << listen_port << " - " << strerror(errno);@@ -142,7 +129,7 @@ int HttpServer::run() {LOG(INFO) << "Typesense has started. Ready to accept requests on port " << listen_port;}- on(STOP_SERVER_MESSAGE, HttpServer::on_stop_server);+ message_dispatcher->on(STOP_SERVER_MESSAGE, HttpServer::on_stop_server);while(!exit_loop) {h2o_evloop_run(ctx.loop, INT32_MAX);@@ -151,8 +138,9 @@ int HttpServer::run() {return 0;}-void HttpServer::on_stop_server(void *data) {+bool HttpServer::on_stop_server(void *data) {// do nothing+ return true;}std::string HttpServer::get_version() {@@ -179,22 +167,7 @@ void HttpServer::stop() {exit_loop = true;// send a message to activate the idle event loop to exit, just in case- send_message(STOP_SERVER_MESSAGE, nullptr);-}--void HttpServer::on_message(h2o_multithread_receiver_t *receiver, h2o_linklist_t *messages) {- while (!h2o_linklist_is_empty(messages)) {- h2o_multithread_message_t *message = H2O_STRUCT_FROM_MEMBER(h2o_multithread_message_t, link, messages->next);- h2o_custom_res_message_t *custom_message = reinterpret_cast<h2o_custom_res_message_t*>(message);-- if(custom_message->http_server->message_handlers.count(custom_message->type) != 0) {- auto handler = custom_message->http_server->message_handlers.at(custom_message->type);- (handler)(custom_message->data);- }-- h2o_linklist_unlink(&message->link);- delete custom_message;- }+ message_dispatcher->send_message(STOP_SERVER_MESSAGE, nullptr);}h2o_pathconf_t* HttpServer::register_handler(h2o_hostconf_t *hostconf, const char *path,@@ -213,21 +186,6 @@ h2o_pathconf_t* HttpServer::register_handler(h2o_hostconf_t *hostconf, const chareturn pathconf;}-const char* HttpServer::get_status_reason(uint32_t status_code) {- switch(status_code) {- case 200: return "OK";- case 201: return "Created";- case 400: return "Bad Request";- case 401: return "Unauthorized";- case 404: return "Not Found";- case 409: return "Conflict";- case 422: return "Unprocessable Entity";- case 500: return "Internal Server Error";- default: return "";- }-}--std::map<std::string, std::string> HttpServer::parse_query(const std::string& query) {std::map<std::string, std::string> query_map;std::regex pattern("([\\w+%-]+)=([^&]*)");@@ -249,6 +207,32 @@ std::map<std::string, std::string> HttpServer::parse_query(const std::string& qureturn query_map;}+bool HttpServer::find_route(const std::vector<std::string> & path_parts, const std::string & http_method, route_path** found_rpath) {+ for(const route_path & rpath: routes) {+ if(rpath.path_parts.size() != path_parts.size() || rpath.http_method != http_method) {+ continue;+ }++ bool found = true;++ for(size_t i = 0; i < rpath.path_parts.size(); i++) {+ const std::string & rpart = rpath.path_parts[i];+ const std::string & given_part = path_parts[i];+ if(rpart != given_part && rpart[0] != ':') {+ found = false;+ break;+ }+ }++ if(found) {+ *found_rpath = const_cast<route_path *>(&rpath);+ return true;+ }+ }++ return false;+}+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;@@ -298,7 +282,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {h2o_generator_t generator = {NULL, NULL};h2o_iovec_t res_body = h2o_strdup(&req->pool, "", SIZE_MAX);req->res.status = 200;- req->res.reason = get_status_reason(200);+ req->res.reason = http_res::get_status_reason(200);h2o_add_header_by_str(&req->pool, &req->res.headers,H2O_STRLIT("access-control-allow-methods"),@@ -317,57 +301,48 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {}}- for(const route_path & rpath: self->http_server->routes) {- if(rpath.path_parts.size() != path_parts.size() || rpath.http_method != http_method) {- continue;- }-- bool found = true;+ route_path *rpath = nullptr;+ bool found = self->http_server->find_route(path_parts, http_method, &rpath);- for(size_t i = 0; i < rpath.path_parts.size(); i++) {- const std::string & rpart = rpath.path_parts[i];- const std::string & given_part = path_parts[i];- if(rpart != given_part && rpart[0] != ':') {- found = false;- goto check_next_route;- }+ if(found) {+ bool authenticated = self->http_server->auth_handler(*rpath, auth_key_from_header);+ if(!authenticated) {+ return send_401_unauthorized(req);}- check_next_route:-- if(found) {- bool authenticated = self->http_server->auth_handler(rpath, auth_key_from_header);- if(!authenticated) {- return send_401_unauthorized(req);+ // routes match and is an authenticated request - iterate and extract path params+ for(size_t i = 0; i < rpath->path_parts.size(); i++) {+ const std::string & path_part = rpath->path_parts[i];+ if(path_part[0] == ':') {+ query_map.emplace(path_part.substr(1), path_parts[i]);}+ }- // routes match and is an authenticated request - iterate and extract path params- for(size_t i = 0; i < rpath.path_parts.size(); i++) {- const std::string & path_part = rpath.path_parts[i];- if(path_part[0] == ':') {- query_map.emplace(path_part.substr(1), path_parts[i]);- }- }+ http_req* request = new http_req(req, http_method, path_without_query, query_map, req_body);+ http_res* response = new http_res();- http_req* request = new http_req(req, path_without_query, query_map, req_body);- http_res* response = new http_res();- response->server = self->http_server;- (rpath.handler)(*request, *response);+ // for writes, we defer to replication_state+ if(http_method != "GET") {+ LOG(INFO) << "*** server thread id: " << std::this_thread::get_id();+ self->http_server->get_replication_state()->write(request, response);+ return 0;+ }- if(!rpath.async) {- // If a handler is marked async, it's assumed that it's responsible for sending the response itself- // later in an async fashion by calling into the main http thread via a message- self->http_server->send_response(request, response);- }+ (rpath->handler)(*request, *response);- return 0;+ if(!rpath->async) {+ // If a handler is marked async, it's assumed that it's responsible for sending the response itself+ // later in an async fashion by calling into the main http thread via a message+ self->http_server->send_response(request, response);}++ 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 = get_status_reason(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);@@ -376,8 +351,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {}void HttpServer::send_message(const std::string & type, void* data) {- h2o_custom_res_message_t* message = new h2o_custom_res_message_t{{{NULL, NULL}}, this, type.c_str(), data};- h2o_multithread_send_message(message_receiver, &message->super);+ message_dispatcher->send_message(type, data);}void HttpServer::send_response(http_req* request, const http_res* response) {@@ -386,7 +360,7 @@ void HttpServer::send_response(http_req* request, const http_res* response) {h2o_iovec_t body = h2o_strdup(&req->pool, response->body.c_str(), SIZE_MAX);req->res.status = response->status_code;- req->res.reason = get_status_reason(response->status_code);+ req->res.reason = http_res::get_status_reason(response->status_code);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, &body, 1, H2O_SEND_STATE_FINAL);@@ -395,57 +369,13 @@ void HttpServer::send_response(http_req* request, const http_res* response) {delete response;}-void HttpServer::response_proceed(h2o_generator_t *generator, h2o_req_t *req) {- h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t*>(generator);- custom_generator->handler(custom_generator->req_res.req, custom_generator->req_res.response,- custom_generator->data);-- h2o_iovec_t body = h2o_strdup(&req->pool, custom_generator->req_res.response->body.c_str(), SIZE_MAX);- const h2o_send_state_t state = custom_generator->req_res.response->final ?- H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;- h2o_send(req, &body, 1, state);-- if(custom_generator->req_res.response->final) {- h2o_dispose_request(req);- delete custom_generator->req_res.req;- delete custom_generator->req_res.response;- delete custom_generator;- }-}--void HttpServer::response_stop(h2o_generator_t *generator, h2o_req_t *req) {- h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t*>(generator);-- h2o_dispose_request(req);- delete custom_generator->req_res.req;- delete custom_generator->req_res.response;- delete custom_generator;-}--void HttpServer::stream_response(void (*handler)(http_req* req, http_res* res, void* data),- http_req & request, http_res & response, void* data) {- h2o_req_t* req = request._req;- h2o_custom_generator_t* custom_generator = new h2o_custom_generator_t {- h2o_generator_t {response_proceed, response_stop}, handler, request_response {&request, &response}, data- };-- req->res.status = response.status_code;- req->res.reason = get_status_reason(response.status_code);- h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, response.content_type_header.c_str(),- response.content_type_header.size());- h2o_start_response(req, &custom_generator->super);-- h2o_iovec_t body = h2o_strdup(&req->pool, "", SIZE_MAX);- h2o_send(req, &body, 1, H2O_SEND_STATE_IN_PROGRESS);-}-int HttpServer::send_401_unauthorized(h2o_req_t *req) {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;- req->res.reason = get_status_reason(req->res.status);+ 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);h2o_send(req, &body, 1, H2O_SEND_STATE_FINAL);@@ -456,46 +386,40 @@ void HttpServer::set_auth_handler(bool (*handler)(const route_path & rpath, consauth_handler = handler;}-void HttpServer::get(const std::string & path, void (*handler)(http_req &, http_res &), bool async) {+void HttpServer::get(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath = {"GET", path_parts, handler, async};routes.push_back(rpath);}-void HttpServer::post(const std::string & path, void (*handler)(http_req &, http_res &), bool async) {+void HttpServer::post(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath = {"POST", path_parts, handler, async};routes.push_back(rpath);}-void HttpServer::put(const std::string & path, void (*handler)(http_req &, http_res &), bool async) {+void HttpServer::put(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath = {"PUT", path_parts, handler, async};routes.push_back(rpath);}-void HttpServer::del(const std::string & path, void (*handler)(http_req &, http_res &), bool async) {+void HttpServer::del(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath = {"DELETE", path_parts, handler, async};routes.push_back(rpath);}-void HttpServer::on(const std::string & message, void (*handler)(void*)) {- message_handlers.emplace(message, handler);+void HttpServer::on(const std::string & message, bool (*handler)(void*)) {+ message_dispatcher->on(message, handler);}HttpServer::~HttpServer() {- // drain all existing messages- on_message(message_receiver, &message_receiver->_messages);-- h2o_multithread_unregister_receiver(message_queue, message_receiver);- h2o_multithread_destroy_queue(message_queue);- free(message_queue);- delete message_receiver;+ delete message_dispatcher;// remove all timeouts defined in: https://github.com/h2o/h2o/blob/v2.2.2/lib/core/context.c#L142std::vector<h2o_timeout_t*> timeouts = {@@ -520,4 +444,12 @@ HttpServer::~HttpServer() {SSL_CTX_free(accept_ctx->ssl_ctx);delete accept_ctx;-}\ No newline at end of file+}++http_message_dispatcher* HttpServer::get_message_dispatcher() const {+ return message_dispatcher;+}++ReplicationState* HttpServer::get_replication_state() const {+ return replication_state;+}diff --git a/src/raft_server.cpp b/src/raft_server.cppindex d5b817e5..2c94cb49 100644--- a/src/raft_server.cpp+++ b/src/raft_server.cpp@@ -1,15 +1,15 @@#include "raft_server.h"#include <butil/files/file_enumerator.h>#include <thread>+#include <string_utils.h>#include "rocksdb/utilities/checkpoint.h"void ReplicationClosure::Run() {- // Auto delete this after Run()+ // Auto delete `this` after Run()std::unique_ptr<ReplicationClosure> self_guard(this);// Respond to this RPC.- brpc::ClosureGuard done_guard(done);if (status().ok()) {return;}@@ -51,13 +51,14 @@ int ReplicationState::start(int port, int election_timeout_ms, int snapshot_intereturn 0;}-void ReplicationState::write(const http_req* request, http_res* response, google::protobuf::Closure* done) {- brpc::ClosureGuard done_guard(done);-+void ReplicationState::write(http_req* request, http_res* response) {if (!is_leader()) {+ LOG(INFO) << "Write called on a follower.";// TODO: return redirect(response);}+ LOG(INFO) << "*** write thread id: " << std::this_thread::get_id();+// Serialize request to replicated WAL so that all the peers in the group receive it as well.// NOTE: actual write must be done only on the `on_apply` method to maintain consistency.@@ -69,7 +70,7 @@ void ReplicationState::write(const http_req* request, http_res* response, googlebraft::Task task;task.data = &log;// This callback would be invoked when the task actually executes or fails- task.done = new ReplicationClosure(this, _http_server, request, response, done_guard.release());+ task.done = new ReplicationClosure(message_dispatcher, request, response);// To avoid ABA problemtask.expected_term = _leader_term.load(butil::memory_order_relaxed);@@ -104,13 +105,8 @@ void ReplicationState::on_apply(braft::Iterator& iter) {// A batch of tasks are committed, which must be processed through// |iter|for (; iter.valid(); iter.next()) {- int64_t detal_value = 0;- http_res* response = NULL;- const http_req* request;- std::unique_ptr<http_req> remote_request(new http_req);-- // Guard helps to invoke iter.done()->Run() asynchronously to avoid the callback blocking the StateMachine- braft::AsyncClosureGuard closure_guard(iter.done());+ http_res* response;+ http_req* request;if (iter.done()) {// This task is applied by this node, get value from the closure to avoid additional parsing.@@ -119,16 +115,23 @@ void ReplicationState::on_apply(braft::Iterator& iter) {request = c->get_request();} else {// Parse request from the log+ response = new http_res;+ http_req* remote_request = new http_req;remote_request->deserialize(iter.data().to_string());- request = remote_request.get();+ remote_request->_req = nullptr; // indicates remote request++ request = remote_request;}// Now that the log has been parsed, perform the actual operation- // TODO: call http server thread for write-- if (response) {- // TODO: leader, so send response back to the client- }+ // Call http server thread for write and response back to client (if `response` is NOT null)+ //auto replication_arg = new ReplicationArg{request, response, iter.done()};+ //message_dispatcher->send_message(REPLICATION_MSG, replication_arg);++ LOG(INFO) << "*** on_apply thread id: " << std::this_thread::get_id();+ LOG(INFO) << request->body;+ LOG(INFO) << "Done";+ //iter.done()->Run();}}@@ -158,8 +161,6 @@ void* ReplicationState::save_snapshot(void* arg) {butil::FileEnumerator dir_enum(butil::FilePath(snapshot_path),false, butil::FileEnumerator::FILES);for (butil::FilePath name = dir_enum.Next(); !name.empty(); name = dir_enum.Next()) {std::string file_name = "rocksdb_snapshot/" + name.BaseName().value();- sa->writer->add_file(file_name);-if (sa->writer->add_file(file_name) != 0) {sa->done->status().set_error(EIO, "Fail to add file to writer.");return NULL;@@ -172,7 +173,7 @@ void* ReplicationState::save_snapshot(void* arg) {void ReplicationState::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done) {// Start a new bthread to avoid blocking StateMachine since it could be slow to write data to diskSnapshotArg* arg = new SnapshotArg;- arg->db = _db;+ arg->db = db;arg->writer = writer;arg->done = done;bthread_t tid;@@ -212,9 +213,9 @@ bool ReplicationState::copy_snapshot(const std::string& from_path, const std::stint ReplicationState::on_snapshot_load(braft::SnapshotReader* reader) {// reset rocksdb handle- if (_db != NULL) {- delete _db;- _db = NULL;+ if (db != NULL) {+ delete db;+ db = NULL;}CHECK(!is_leader()) << "Leader is not supposed to load snapshot";@@ -231,8 +232,8 @@ int ReplicationState::on_snapshot_load(braft::SnapshotReader* reader) {}LOG(TRACE) << "rm " << db_path << " success";- //TODO: try use link instead of copy+ // tries to use link if possible, or else copiesif (!copy_snapshot(snapshot_path, db_path)) {LOG(WARNING) << "copy snapshot " << snapshot_path << " to " << db_path << " failed";return -1;@@ -252,3 +253,30 @@ void ReplicationState::refresh_peers(const std::string & peers) {_node->change_peers(conf, refresh_peers_done);}}++bool ReplicationState::on_raft_replication(void *data) {+ ReplicationArg* replication_arg = static_cast<ReplicationArg*>(data);++ // Guard invokes replication_arg->done->Run() asynchronously to avoid the callback blocking the main thread+ braft::AsyncClosureGuard closure_guard(replication_arg->done);++ std::vector<std::string> path_parts;+ StringUtils::split(replication_arg->req->path_without_query, path_parts, "/");++ /*route_path* found_rpath = nullptr;+ bool found = replication_arg->res->server->find_route(path_parts, replication_arg->req->http_method, &found_rpath);++ if(found) {+ // call handler function -- this effectively does the write+ found_rpath->handler(*replication_arg->req, *replication_arg->res);++ if(replication_arg->req->_req != nullptr) {+ // local request, respond to request+ replication_arg->res->server->send_response(replication_arg->req, replication_arg->res);+ }+ }++ delete replication_arg;*/++ return true;+}diff --git a/src/replicator.cpp b/src/replicator.cppindex 65ae2447..3b275cee 100644--- a/src/replicator.cpp+++ b/src/replicator.cpp@@ -22,25 +22,25 @@ void IterateBatchHandler::Put(const rocksdb::Slice& key, const rocksdb::Slice& vif(parts.size() == 1 && parts[0] == CollectionManager::NEXT_COLLECTION_ID_KEY) {ReplicationEvent* replication_event = new ReplicationEvent("UPDATE_NEXT_COLLECTION_ID", 0,key.ToString(), value.ToString());- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}if(parts.size() >= 2 && parts[0] == Collection::COLLECTION_META_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("ADD_COLLECTION_META", 0,key.ToString(), value.ToString());- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}if(parts.size() == 3 && parts[1] == Collection::SEQ_ID_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("ADD_DOCUMENT", std::stoi(parts[0]),key.ToString(), value.ToString());- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}if(parts.size() >= 2 && parts[0] == CollectionManager::SYMLINK_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("ADD_SYMLINK", 0,key.ToString(), value.ToString());- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}}@@ -50,18 +50,18 @@ void IterateBatchHandler::Delete(const rocksdb::Slice& key) {if(parts.size() == 3 && parts[1] == Collection::DOC_ID_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("REMOVE_DOCUMENT", 0, key.ToString(), "");- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}if(parts.size() >= 2 && parts[0] == Collection::COLLECTION_META_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("DROP_COLLECTION", 0, key.ToString(), "");- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}if(parts.size() >= 2 && parts[0] == CollectionManager::SYMLINK_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("REMOVE_SYMLINK", 0,key.ToString(), "");- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}}@@ -73,16 +73,16 @@ void IterateBatchHandler::Merge(const rocksdb::Slice& key, const rocksdb::Slice&if(parts.size() >= 2 && parts[0] == Collection::COLLECTION_NEXT_SEQ_PREFIX) {ReplicationEvent* replication_event = new ReplicationEvent("INCR_COLLECTION_NEXT_SEQ", 0,key.ToString(), value.ToString());- server->send_message(REPLICATION_EVENT_MSG, replication_event);+ message_dispatcher->send_message(REPLICATION_EVENT_MSG, replication_event);}}-void Replicator::start(HttpServer* server, const std::string & master_host_port,+void Replicator::start(http_message_dispatcher* message_dispatcher, const std::string & master_host_port,const std::string & api_key, Store& store) {size_t total_runs = 0;while(true) {- IterateBatchHandler handler(server);+ IterateBatchHandler handler(message_dispatcher);uint64_t latest_seq_num = store.get_latest_seq_number();if(total_runs++ % 20 == 0) {@@ -132,7 +132,7 @@ void Replicator::start(HttpServer* server, const std::string & master_host_port,}}-void Replicator::on_replication_event(void *data) {+bool Replicator::on_replication_event(void *data) {ReplicationEvent* replication_event = static_cast<ReplicationEvent*>(data);if(replication_event->type == "UPDATE_NEXT_COLLECTION_ID") {@@ -209,4 +209,6 @@ void Replicator::on_replication_event(void *data) {}delete replication_event;++ return true;}diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex 79961971..46acf8d3 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -45,6 +45,49 @@ Option<std::string> fetch_file_contents(const std::string & file_path) {return Option<std::string>(content);}+void response_proceed(h2o_generator_t *generator, h2o_req_t *req) {+ h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t*>(generator);+ custom_generator->handler(custom_generator->req, custom_generator->res, custom_generator->data);++ h2o_iovec_t body = h2o_strdup(&req->pool, custom_generator->res->body.c_str(), SIZE_MAX);+ const h2o_send_state_t state = custom_generator->res->final ?+ H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;+ h2o_send(req, &body, 1, state);++ if(custom_generator->res->final) {+ h2o_dispose_request(req);+ delete custom_generator->req;+ delete custom_generator->res;+ delete custom_generator;+ }+}++void response_stop(h2o_generator_t *generator, h2o_req_t *req) {+ h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t*>(generator);++ h2o_dispose_request(req);+ delete custom_generator->req;+ delete custom_generator->res;+ delete custom_generator;+}++void stream_response(bool (*handler)(http_req* req, http_res* res, void* data),+ http_req & request, http_res & response, void* data) {+ h2o_req_t* req = request._req;+ h2o_custom_generator_t* custom_generator = new h2o_custom_generator_t {+ h2o_generator_t {response_proceed, response_stop}, handler, &request, &response, data+ };++ req->res.status = response.status_code;+ req->res.reason = http_res::get_status_reason(response.status_code);+ h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, response.content_type_header.c_str(),+ response.content_type_header.size());+ h2o_start_response(req, &custom_generator->super);++ h2o_iovec_t body = h2o_strdup(&req->pool, "", SIZE_MAX);+ h2o_send(req, &body, 1, H2O_SEND_STATE_IN_PROGRESS);+}+void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {options.set_program_name("./typesense-server");@@ -104,6 +147,14 @@ int init_logger(Config & config, const std::string & server_version, std::uniquereturn 0;}+bool on_send_response(void *data) {+ request_response* req_res = static_cast<request_response*>(data);+ server->send_response(req_res->req, req_res->res);+ delete req_res;++ return true;+}+int run_server(const Config & config, const std::string & version,void (*master_server_routes)(), void (*replica_server_routes)()) {@@ -147,6 +198,7 @@ int run_server(const Config & config, const std::string & version,server->on(SEND_RESPONSE_MSG, on_send_response);server->on(REPLICATION_EVENT_MSG, Replicator::on_replication_event);+ server->on(ReplicationState::REPLICATION_MSG, ReplicationState::on_raft_replication);if(config.get_master().empty()) {master_server_routes();@@ -165,13 +217,15 @@ int run_server(const Config & config, const std::string & version,LOG(INFO) << "Spawning replication thread...";std::thread replication_thread([&store, &config]() {- Replicator::start(::server, config.get_master(), config.get_api_key(), store);+ Replicator::start(server->get_message_dispatcher(), config.get_master(), config.get_api_key(), store);});replication_thread.detach();}- std::thread raft_thread([&store, &config]() {+ ReplicationState replication_state(server->get_message_dispatcher(), store._get_db_unsafe());++ std::thread raft_thread([&replication_state, &store, &config]() {std::string path_to_peers = config.get_raft_peers();if(path_to_peers.empty()) {@@ -193,7 +247,6 @@ int run_server(const Config & config, const std::string & version,// start raft serverbrpc::Server raft_server;- ReplicationState replication_state;uint32_t raft_port = config.get_raft_port();@@ -211,7 +264,7 @@ int run_server(const Config & config, const std::string & version,StringUtils::split(peer_ips_string, peer_ips, ",");std::string peers = StringUtils::join(peer_ips, ":0,");- if (replication_state.start(raft_port, 1000, 3600, config.get_raft_dir(), peers) != 0) {+ if (replication_state.start(raft_port, 1000, 60, config.get_raft_dir(), peers) != 0) {LOG(ERR) << "Failed to start raft state";return -1;}@@ -250,7 +303,7 @@ int run_server(const Config & config, const std::string & version,raft_thread.detach();- int ret_code = server->run();+ int ret_code = server->run(&replication_state);// we are out of the event loop here@@ -260,4 +313,4 @@ int run_server(const Config & config, const std::string & version,CollectionManager::get_instance().dispose();return ret_code;-}\ No newline at end of file+}