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
Use shared_ptr to handle req/res life cycles.
kishorenc95ee14350a65
diff --git a/include/core_api.h b/include/core_api.hindex 6f0cc04b..59e8f596 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -7,95 +7,95 @@ bool handle_authentication(std::map<std::string, std::string>& req_params, const// Collections-bool get_collections(http_req& req, http_res& res);+bool get_collections(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_create_collection(http_req& req, http_res& res);+bool post_create_collection(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_drop_collection(http_req& req, http_res& res);+bool del_drop_collection(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_collection_summary(http_req& req, http_res& res);+bool get_collection_summary(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Documents-bool get_search(http_req& req, http_res& res);+bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_multi_search(http_req& req, http_res& res);+bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_export_documents(http_req& req, http_res& res);+bool get_export_documents(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_add_document(http_req& req, http_res& res);+bool post_add_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool patch_update_document(http_req& req, http_res& res);+bool patch_update_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_import_documents(http_req& req, http_res& res);+bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_fetch_document(http_req& req, http_res& res);+bool get_fetch_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_remove_document(http_req& req, http_res& res);+bool del_remove_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_remove_documents(http_req& req, http_res& res);+bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Alias-bool get_alias(http_req& req, http_res& res);+bool get_alias(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_aliases(http_req& req, http_res& res);+bool get_aliases(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool put_upsert_alias(http_req& req, http_res& res);+bool put_upsert_alias(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_alias(http_req& req, http_res& res);+bool del_alias(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Overrides-bool get_overrides(http_req& req, http_res& res);+bool get_overrides(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_override(http_req& req, http_res& res);+bool get_override(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool put_override(http_req& req, http_res& res);+bool put_override(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_override(http_req& req, http_res& res);+bool del_override(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Synonyms-bool get_synonyms(http_req& req, http_res& res);+bool get_synonyms(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_synonym(http_req& req, http_res& res);+bool get_synonym(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool put_synonym(http_req& req, http_res& res);+bool put_synonym(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_synonym(http_req& req, http_res& res);+bool del_synonym(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Keys-bool get_keys(http_req& req, http_res& res);+bool get_keys(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_create_key(http_req& req, http_res& res);+bool post_create_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_key(http_req& req, http_res& res);+bool get_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool del_key(http_req& req, http_res& res);+bool del_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Health + Metrics-bool get_debug(http_req& req, http_res& res);+bool get_debug(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_health(http_req& req, http_res& res);+bool get_health(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_health(http_req& req, http_res& res);+bool post_health(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_metrics_json(http_req& req, http_res& res);+bool get_metrics_json(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_stats_json(http_req& req, http_res& res);+bool get_stats_json(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_log_sequence(http_req& req, http_res& res);+bool get_log_sequence(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// operations-bool post_snapshot(http_req& req, http_res& res);+bool post_snapshot(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_vote(http_req& req, http_res& res);+bool post_vote(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool post_config(http_req& req, http_res& res);+bool post_config(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);// Misc helpersdiff --git a/include/http_client.h b/include/http_client.hindex 972aa8e6..777d1c91 100644--- a/include/http_client.h+++ b/include/http_client.h@@ -52,7 +52,8 @@ public:static long post_response(const std::string & url, const std::string & body, std::string & response,std::map<std::string, std::string>& res_headers, long timeout_ms=4000);- static long post_response_async(const std::string &url, http_req* request, http_res* response,+ static long post_response_async(const std::string &url, const std::shared_ptr<http_req> request,+ const std::shared_ptr<http_res> response,HttpServer* server);static long put_response(const std::string & url, const std::string & body, std::string & response,diff --git a/include/http_data.h b/include/http_data.hindex f7ccda9f..2b0235f7 100644--- a/include/http_data.h+++ b/include/http_data.h@@ -66,20 +66,36 @@ struct http_res {std::string body;std::atomic<bool> final;- // fulfilled by an async response handler to pass control back for further writes- // use `mark_proceed` and `wait_proceed` instead of accessing this directly- await_t await;-- h2o_generator_t* generator = nullptr;+ void* generator = nullptr;// indicates whether follower is proxying this response stream from leaderbool proxied_stream = false;- // indicates whether this object is eligible for disposal at the end of req/res cycle- bool auto_dispose = true;+ std::mutex mcv;+ std::condition_variable cv;+ bool ready;++ http_res(): status_code(0), content_type_header("application/json; charset=utf-8"), final(true), ready(false) {++ }- http_res(): status_code(0), content_type_header("application/json; charset=utf-8"), final(true) {+ ~http_res() {+ //LOG(INFO) << "http_res " << this;+ }+ void wait() {+ auto lk = std::unique_lock<std::mutex>(mcv);+ cv.wait(lk, [&] { return ready; });+ ready = false;+ }++ void notify() {+ // Ideally we don't need lock over notify but it is needed here because+ // the parent object could be deleted after lock on mutex is released but+ // before notify can be called on condition variable.+ std::lock_guard<std::mutex> lk(mcv);+ ready = true;+ cv.notify_all();}static const char* get_status_reason(uint32_t status_code) {@@ -183,19 +199,20 @@ struct http_req {void* data;- // used during forwarding of requests from follower to leader- // use `mark_proceed` and `wait_proceed` instead of accessing this directly- await_t await;-// for deffered processing of async handlersh2o_custom_timer_t defer_timer;uint64_t start_ts;bool deserialized_request;+ std::mutex mcv;+ std::condition_variable cv;+ bool ready;++http_req(): _req(nullptr), route_hash(1),first_chunk_aggregate(true), last_chunk_aggregate(false),- chunk_len(0), body_index(0), data(nullptr), deserialized_request(true) {+ chunk_len(0), body_index(0), data(nullptr), deserialized_request(true), ready(false) {start_ts = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();@@ -206,7 +223,7 @@ struct http_req {const std::map<std::string, std::string> & params, const std::string& body):_req(_req), http_method(http_method), path_without_query(path_without_query), route_hash(route_hash),params(params), first_chunk_aggregate(true), last_chunk_aggregate(false),- chunk_len(0), body(body), body_index(0), data(nullptr), deserialized_request(false) {+ chunk_len(0), body(body), body_index(0), data(nullptr), deserialized_request(false), ready(false) {start_ts = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();@@ -240,6 +257,21 @@ struct http_req {}}+ void wait() {+ auto lk = std::unique_lock<std::mutex>(mcv);+ cv.wait(lk, [&] { return ready; });+ ready = false;+ }++ void notify() {+ // Ideally we don't need lock over notify but it is needed here because+ // the parent object could be deleted after lock on mutex is released but+ // before notify can be called on condition variable.+ std::lock_guard<std::mutex> lk(mcv);+ ready = true;+ cv.notify_all();+ }+// NOTE: we don't ser/de all fields, only ones needed for write forwarding// Take care to check for existence of key to ensure backward compatibility during upgrade@@ -278,20 +310,20 @@ struct http_req {};struct request_response {- http_req* req;- http_res* res;+ const std::shared_ptr<http_req>& req;+ const std::shared_ptr<http_res>& res;};struct route_path {std::string http_method;std::vector<std::string> path_parts;- bool (*handler)(http_req &, http_res &);+ bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&);bool async_req;bool async_res;std::string action;route_path(const std::string &httpMethod, const std::vector<std::string> &pathParts,- bool (*handler)(http_req &, http_res &), bool async_req, bool async_res) :+ bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&), bool async_req, bool async_res) :http_method(httpMethod), path_parts(pathParts), handler(handler),async_req(async_req), async_res(async_res) {action = _get_action();@@ -428,6 +460,10 @@ struct http_message_dispatcher {};struct AsyncIndexArg {- http_req* req;- http_res* res;+ std::shared_ptr<http_req> req;+ std::shared_ptr<http_res> res;++ AsyncIndexArg(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.hindex 0f9d25ed..cd7c8b65 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -28,13 +28,21 @@ struct h2o_custom_generator_t {h2o_generator_t super;h2o_custom_req_handler_t* h2o_handler;route_path* rpath;- http_req* request;- http_res* response;+ std::shared_ptr<http_req> request;+ std::shared_ptr<http_res> response;++ std::shared_ptr<http_req>& req() {+ return request;+ }++ std::shared_ptr<http_res>& res() {+ return response;+ }};struct deferred_req_res_t {- http_req* req;- http_res* res;+ const std::shared_ptr<http_req> req;+ const std::shared_ptr<http_res> res;HttpServer* server;};@@ -128,25 +136,26 @@ public:void set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const std::string& body,const route_path & rpath, const std::string & auth_key));- void get(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);+ void get(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res), bool async_req=false, bool async_res=false);- void post(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);+ void post(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res), bool async_req=false, bool async_res=false);- void put(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);+ void put(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res), bool async_req=false, bool async_res=false);- void patch(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);+ void patch(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res), bool async_req=false, bool async_res=false);- void del(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);+ void del(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res), bool async_req=false, bool async_res=false);void on(const std::string & message, bool (*handler)(void*));void send_message(const std::string & type, void* data);- void send_response(http_req* request, http_res* response);+ void send_response(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response);- static void destroy_request_response(http_req* request, http_res* response);+ static void destroy_request_response(const std::shared_ptr<http_req>& request,+ const std::shared_ptr<http_res>& response);- static void stream_response(http_req& request, http_res& response);+ static void stream_response(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response);uint64_t find_route(const std::vector<std::string> & path_parts, const std::string & http_method,route_path** found_rpath);@@ -175,14 +184,14 @@ public:static constexpr const char* STREAM_RESPONSE_MESSAGE = "STREAM_RESPONSE";static constexpr const char* REQUEST_PROCEED_MESSAGE = "REQUEST_PROCEED";- static int process_request(http_req* request, http_res* response, route_path *rpath,+ 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 void on_deferred_process_request(h2o_timer_t *entry);- void defer_processing(http_req& req, http_res& res, size_t timeout_ms);+ void defer_processing(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res, size_t timeout_ms);- void do_snapshot(const std::string& snapshot_path, http_req& req, http_res& res);+ void do_snapshot(const std::string& snapshot_path, const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);bool trigger_vote();};diff --git a/include/raft_server.h b/include/raft_server.hindex 9beab1cd..41460a80 100644--- a/include/raft_server.h+++ b/include/raft_server.h@@ -18,21 +18,21 @@ class ReplicationState;// Implements the callback for the state machineclass ReplicationClosure : public braft::Closure {private:- http_req* request;- http_res* response;+ const std::shared_ptr<http_req> request;+ const std::shared_ptr<http_res> response;public:- ReplicationClosure(http_req* request, http_res* response): request(request), response(response) {+ ReplicationClosure(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response): request(request), response(response) {}~ReplicationClosure() {}- http_req* get_request() const {+ const std::shared_ptr<http_req> get_request() const {return request;}- http_res* get_response() const {+ const std::shared_ptr<http_res> get_response() const {return response;}@@ -76,12 +76,12 @@ public:class OnDemandSnapshotClosure : public braft::Closure {private:ReplicationState* replication_state;- http_req* req;- http_res* res;+ const std::shared_ptr<http_req>& req;+ const std::shared_ptr<http_res>& res;public:- OnDemandSnapshotClosure(ReplicationState* replication_state, http_req* req, http_res* res):+ OnDemandSnapshotClosure(ReplicationState* replication_state, const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res):replication_state(replication_state), req(req), res(res) {}~OnDemandSnapshotClosure() {}@@ -135,10 +135,10 @@ public:const std::string & raft_dir, const std::string & nodes);// Generic write method for synchronizing all writes- void write(http_req* request, http_res* response);+ void write(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response);// Generic read method for consistent reads, not used for now- void read(http_res* response);+ void read(const std::shared_ptr<http_res>& response);// updates cluster membershipvoid refresh_nodes(const std::string & nodes);@@ -179,7 +179,7 @@ public:Store* get_store();- void do_snapshot(const std::string& snapshot_path, http_req& req, http_res& res);+ void do_snapshot(const std::string& snapshot_path, const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);static std::string to_nodes_config(const butil::EndPoint &peering_endpoint, const int api_port,const std::string &nodes_config);@@ -220,8 +220,10 @@ private:// have to do a dummy write, otherwise snapshot will not triggerif(create_init_db_snapshot) {- http_req* request = new http_req(nullptr, "POST", "/INIT_SNAPSHOT", 0, {}, "INIT_SNAPSHOT");- http_res* response = new http_res();+ std::map<std::string, std::string> params;+ std::shared_ptr<http_req> request = std::make_shared<http_req>(nullptr, "POST", "/INIT_SNAPSHOT", 0, params,+ "INIT_SNAPSHOT");+ std::shared_ptr<http_res> response = std::make_shared<http_res>();write(request, response);}@@ -254,7 +256,7 @@ private:LOG(INFO) << "Node stops following " << ctx;}- void write_to_leader(http_req *request, http_res *response) const;+ void write_to_leader(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response) const;void do_dummy_write();diff --git a/src/core_api.cpp b/src/core_api.cppindex b3bacb08..0760e5c5 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -66,23 +66,23 @@ index_operation_t get_index_operation(const std::string& action) {return CREATE;}-bool get_collections(http_req & req, http_res & res) {+bool get_collections(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();nlohmann::json json_response = collectionManager.get_collection_summaries();- res.set_200(json_response.dump());+ res->set_200(json_response.dump());return true;}-bool post_create_collection(http_req & req, http_res & res) {+bool post_create_collection(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {const char* NUM_MEMORY_SHARDS = "num_memory_shards";nlohmann::json req_json;try {- req_json = nlohmann::json::parse(req.body);+ req_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {//LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}@@ -91,7 +91,7 @@ bool post_create_collection(http_req & req, http_res & res) {// validate presence of mandatory fieldsif(req_json.count("name") == 0) {- res.set_400("Parameter `name` is required.");+ res->set_400("Parameter `name` is required.");return false;}@@ -100,7 +100,7 @@ bool post_create_collection(http_req & req, http_res & res) {}if(req_json.count("fields") == 0) {- res.set_400("Parameter `fields` is required.");+ res->set_400("Parameter `fields` is required.");return false;}@@ -111,31 +111,31 @@ bool post_create_collection(http_req & req, http_res & res) {}if(!req_json[DEFAULT_SORTING_FIELD].is_string()) {- res.set_400(std::string("`") + DEFAULT_SORTING_FIELD ++ res->set_400(std::string("`") + DEFAULT_SORTING_FIELD +"` should be a string. It should be the name of an int32/float field.");return false;}if(!req_json[NUM_MEMORY_SHARDS].is_number_unsigned()) {- res.set_400(std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");+ res->set_400(std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");return false;}size_t num_memory_shards = req_json[NUM_MEMORY_SHARDS].get<size_t>();if(num_memory_shards == 0) {- res.set_400(std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");+ res->set_400(std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");return false;}if(collectionManager.get_collection(req_json["name"]) != nullptr) {- res.set_409("Collection with name `" + req_json["name"].get<std::string>() + "` already exists.");+ res->set_409("Collection with name `" + req_json["name"].get<std::string>() + "` already exists.");return false;}// field specific validationif(!req_json["fields"].is_array() || req_json["fields"].empty()) {- res.set_400("The `fields` value should be an array of objects containing "+ res->set_400("The `fields` value should be an array of objects containing ""`name`, `type` and optionally, `facet` properties.");return false;}@@ -145,7 +145,7 @@ bool post_create_collection(http_req & req, http_res & res) {auto parse_op = field::json_fields_to_fields(req_json["fields"], fallback_field_type, fields);if(!parse_op.ok()) {- res.set(parse_op.code(), parse_op.error());+ res->set(parse_op.code(), parse_op.error());return false;}@@ -155,68 +155,68 @@ bool post_create_collection(http_req & req, http_res & res) {if(collection_op.ok()) {nlohmann::json json_response = collection_op.get()->get_summary_json();- res.set_201(json_response.dump());+ res->set_201(json_response.dump());return true;}- res.set(collection_op.code(), collection_op.error());+ res->set(collection_op.code(), collection_op.error());return false;}-bool del_drop_collection(http_req & req, http_res & res) {- std::string doc_id = req.params["id"];+bool del_drop_collection(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ std::string doc_id = req->params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();- Option<nlohmann::json> drop_op = collectionManager.drop_collection(req.params["collection"]);+ Option<nlohmann::json> drop_op = collectionManager.drop_collection(req->params["collection"]);if(!drop_op.ok()) {- res.set(drop_op.code(), drop_op.error());+ res->set(drop_op.code(), drop_op.error());return false;}- res.set_200(drop_op.get().dump());+ res->set_200(drop_op.get().dump());return true;}-bool get_debug(http_req & req, http_res & res) {+bool get_debug(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json result;result["version"] = server->get_version();uint64_t state = server->node_state();result["state"] = state;- res.set_200(result.dump());+ res->set_200(result.dump());return true;}-bool get_health(http_req & req, http_res & res) {+bool get_health(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json result;bool alive = server->is_alive();result["ok"] = alive;if(alive) {- res.set_body(200, result.dump());+ res->set_body(200, result.dump());} else {- res.set_body(503, result.dump());+ res->set_body(503, result.dump());}return alive;}-bool post_health(http_req & req, http_res & res) {+bool post_health(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json result;bool alive = server->is_alive();result["ok"] = alive;if(alive) {- res.set_body(200, result.dump());+ res->set_body(200, result.dump());} else {- res.set_body(503, result.dump());+ res->set_body(503, result.dump());}return alive;}-bool get_metrics_json(http_req &req, http_res &res) {+bool get_metrics_json(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json result;CollectionManager & collectionManager = CollectionManager::get_instance();@@ -225,74 +225,74 @@ bool get_metrics_json(http_req &req, http_res &res) {SystemMetrics sys_metrics;sys_metrics.get(data_dir_path, result);- res.set_body(200, result.dump(2));+ res->set_body(200, result.dump(2));return true;}-bool get_stats_json(http_req &req, http_res &res) {+bool get_stats_json(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json result;AppMetrics::get_instance().get("requests_per_second", "latency_ms", result);- res.set_body(200, result.dump(2));+ res->set_body(200, result.dump(2));return true;}-bool get_log_sequence(http_req &req, http_res &res) {+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));+ res->content_type_header = "text/plain; charset=utf8";+ res->set_body(200, std::to_string(seq_num));return true;}-bool get_search(http_req & req, http_res & res) {+bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {std::string results_json_str;- Option<bool> search_op = CollectionManager::do_search(req.params, results_json_str);+ Option<bool> search_op = CollectionManager::do_search(req->params, results_json_str);if(!search_op.ok()) {- res.set(search_op.code(), search_op.error());+ res->set(search_op.code(), search_op.error());return false;}- res.set_200(results_json_str);+ res->set_200(results_json_str);return true;}-bool post_multi_search(http_req& req, http_res& res) {+bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json req_json;try {- req_json = nlohmann::json::parse(req.body);+ req_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}if(req_json.count("searches") == 0) {- res.set_400("Missing `searches` array.");+ res->set_400("Missing `searches` array.");return false;}if(!req_json["searches"].is_array()) {- res.set_400("Missing `searches` array.");+ res->set_400("Missing `searches` array.");return false;}const char* LIMIT_MULTI_SEARCHES = "limit_multi_searches";size_t limit_multi_searches = 50;- if(req.params.count(LIMIT_MULTI_SEARCHES) != 0 && StringUtils::is_uint32_t(req.params[LIMIT_MULTI_SEARCHES])) {- limit_multi_searches = std::stoi(req.params[LIMIT_MULTI_SEARCHES]);+ if(req->params.count(LIMIT_MULTI_SEARCHES) != 0 && StringUtils::is_uint32_t(req->params[LIMIT_MULTI_SEARCHES])) {+ limit_multi_searches = std::stoi(req->params[LIMIT_MULTI_SEARCHES]);}if(req_json["searches"].size() > limit_multi_searches) {- res.set_400(std::string("Number of multi searches exceeds `") + LIMIT_MULTI_SEARCHES + "` parameter.");+ res->set_400(std::string("Number of multi searches exceeds `") + LIMIT_MULTI_SEARCHES + "` parameter.");return false;}- auto orig_req_params = req.params;+ auto orig_req_params = req->params;nlohmann::json response;response["results"] = nlohmann::json::array();@@ -300,22 +300,22 @@ bool post_multi_search(http_req& req, http_res& res) {// we have to ensure that `req_json` is a flat <string, string> mapfor(auto& search: req_json["searches"]) {if(!search.is_object()) {- res.set_400("The value of `searches` must be an array of objects.");+ res->set_400("The value of `searches` must be an array of objects.");return false;}- req.params = orig_req_params;+ req->params = orig_req_params;for(auto& item: search.items()) {- bool populated = AuthManager::populate_req_params(req.params, item);+ bool populated = AuthManager::populate_req_params(req->params, item);if(!populated) {- res.set_400("One or more search parameters are malformed.");+ res->set_400("One or more search parameters are malformed.");return false;}}std::string results_json_str;- Option<bool> search_op = CollectionManager::do_search(req.params, results_json_str);+ Option<bool> search_op = CollectionManager::do_search(req->params, results_json_str);if(search_op.ok()) {response["results"].push_back(nlohmann::json::parse(results_json_str));@@ -327,34 +327,34 @@ bool post_multi_search(http_req& req, http_res& res) {}}- res.set_200(response.dump());+ res->set_200(response.dump());return true;}-bool get_collection_summary(http_req & req, http_res & res) {+bool get_collection_summary(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager& collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}nlohmann::json json_response = collection->get_summary_json();- res.set_200(json_response.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));+ res->set_200(json_response.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));return true;}-bool get_export_documents(http_req & req, http_res & res) {+bool get_export_documents(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {// NOTE: this is a streaming response end-point so this handler will be called multiple timesCollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- req.last_chunk_aggregate = true;- res.final = true;- res.set_404();+ req->last_chunk_aggregate = true;+ res->final = true;+ res->set_404();HttpServer::stream_response(req, res);return false;}@@ -362,118 +362,119 @@ bool get_export_documents(http_req & req, http_res & res) {const std::string seq_id_prefix = collection->get_seq_id_collection_prefix();rocksdb::Iterator* it = nullptr;- if(req.data == nullptr) {+ if(req->data == nullptr) {it = collectionManager.get_store()->get_iterator();it->Seek(seq_id_prefix);- req.data = it;+ req->data = it;} else {- it = static_cast<rocksdb::Iterator*>(req.data);+ it = static_cast<rocksdb::Iterator*>(req->data);}if(it->Valid() && it->key().ToString().compare(0, seq_id_prefix.size(), seq_id_prefix) == 0) {- res.body = it->value().ToString();+ // FIXME: remove meta key from doc+ res->body = it->value().ToString();it->Next();// append a new line character if there is going to be one more record to sendif(it->Valid() && it->key().ToString().compare(0, seq_id_prefix.size(), seq_id_prefix) == 0) {- res.body += "\n";- req.last_chunk_aggregate = false;- res.final = false;+ res->body += "\n";+ req->last_chunk_aggregate = false;+ res->final = false;} else {- req.last_chunk_aggregate = true;- res.final = true;+ req->last_chunk_aggregate = true;+ res->final = true;delete it;- req.data = nullptr;+ req->data = nullptr;}}- res.content_type_header = "application/octet-stream";- res.status_code = 200;+ res->content_type_header = "application/octet-stream";+ res->status_code = 200;HttpServer::stream_response(req, res);return true;}-bool post_import_documents(http_req& req, http_res& res) {+bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {//LOG(INFO) << "post_import_documents";- //LOG(INFO) << "req.first_chunk=" << req.first_chunk_aggregate << ", last_chunk=" << req.last_chunk_aggregate;+ //LOG(INFO) << "req->first_chunk=" << req->first_chunk_aggregate << ", last_chunk=" << req->last_chunk_aggregate;const char *BATCH_SIZE = "batch_size";const char *ACTION = "action";const char *DIRTY_VALUES = "dirty_values";- if(req.params.count(BATCH_SIZE) == 0) {- req.params[BATCH_SIZE] = "40";+ if(req->params.count(BATCH_SIZE) == 0) {+ req->params[BATCH_SIZE] = "40";}- if(req.params.count(ACTION) == 0) {- req.params[ACTION] = "create";+ if(req->params.count(ACTION) == 0) {+ req->params[ACTION] = "create";}- if(req.params.count(DIRTY_VALUES) == 0) {- req.params[DIRTY_VALUES] = ""; // set it empty as default will depend on `index_all_fields`+ if(req->params.count(DIRTY_VALUES) == 0) {+ req->params[DIRTY_VALUES] = ""; // set it empty as default will depend on `index_all_fields`}- 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.");+ 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);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.");+ 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);return false;}- const size_t IMPORT_BATCH_SIZE = std::stoi(req.params[BATCH_SIZE]);+ const size_t IMPORT_BATCH_SIZE = std::stoi(req->params[BATCH_SIZE]);if(IMPORT_BATCH_SIZE == 0) {- res.final = true;- res.set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");+ res->final = true;+ res->set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");HttpServer::stream_response(req, res);return false;}- if(req.body_index == 0) {+ if(req->body_index == 0) {// will log for every major chunk of request body- //LOG(INFO) << "Import, req.body.size=" << req.body.size() << ", batch_size=" << IMPORT_BATCH_SIZE;- //int nminusten_pos = std::max(0, int(req.body.size())-10);- //LOG(INFO) << "Last 10 chars: " << req.body.substr(nminusten_pos);+ //LOG(INFO) << "Import, req->body.size=" << req->body.size() << ", batch_size=" << IMPORT_BATCH_SIZE;+ //int nminusten_pos = std::max(0, int(req->body.size())-10);+ //LOG(INFO) << "Last 10 chars: " << req->body.substr(nminusten_pos);}CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.final = true;- res.set_404();+ res->final = true;+ res->set_404();HttpServer::stream_response(req, res);return false;}- //LOG(INFO) << "Import, " << "req.body_index=" << req.body_index << ", req.body.size: " << req.body.size();- //LOG(INFO) << "req body %: " << (float(req.body_index)/req.body.size())*100;+ //LOG(INFO) << "Import, " << "req->body_index=" << req->body_index << ", req->body.size: " << req->body.size();+ //LOG(INFO) << "req body %: " << (float(req->body_index)/req->body.size())*100;std::vector<std::string> json_lines;- req.body_index = StringUtils::split(req.body, json_lines, "\n", false, req.body_index, IMPORT_BATCH_SIZE);+ req->body_index = StringUtils::split(req->body, json_lines, "\n", false, req->body_index, IMPORT_BATCH_SIZE);- //LOG(INFO) << "json_lines.size before: " << json_lines.size() << ", req.body_index: " << req.body_index;+ //LOG(INFO) << "json_lines.size before: " << json_lines.size() << ", req->body_index: " << req->body_index;bool stream_proceed = false; // default state- if(req.body_index == req.body.size()) {+ if(req->body_index == req->body.size()) {// body has been consumed fully, see whether we can fetch more request body- req.body_index = 0;+ req->body_index = 0;stream_proceed = true;- if(req.last_chunk_aggregate) {- //LOG(INFO) << "req.last_chunk_aggregate is true";- req.body = "";+ if(req->last_chunk_aggregate) {+ //LOG(INFO) << "req->last_chunk_aggregate is true";+ req->body = "";} else {if(!json_lines.empty()) {- // check if req.body had complete last record+ // check if req->body had complete last recordbool complete_document;try {@@ -485,37 +486,37 @@ bool post_import_documents(http_req& req, http_res& res) {if(!complete_document) {// eject partial record- req.body = json_lines.back();+ req->body = json_lines.back();json_lines.pop_back();} else {- req.body = "";+ req->body = "";}}}}//LOG(INFO) << "json_lines.size after: " << json_lines.size() << ", stream_proceed: " << stream_proceed;- //LOG(INFO) << "json_lines.size: " << json_lines.size() << ", req.stream_state: " << req.stream_state;+ //LOG(INFO) << "json_lines.size: " << json_lines.size() << ", req->stream_state: " << req->stream_state;// When only one partial record arrives as a chunk, an empty body is pushed to response stream- bool single_partial_record_body = (json_lines.empty() && !req.body.empty());+ bool single_partial_record_body = (json_lines.empty() && !req->body.empty());std::stringstream response_stream;//LOG(INFO) << "single_partial_record_body: " << single_partial_record_body;- const index_operation_t operation = get_index_operation(req.params[ACTION]);+ const index_operation_t operation = get_index_operation(req->params[ACTION]);if(!single_partial_record_body) {nlohmann::json document;- const auto& dirty_values = collection->parse_dirty_values_option(req.params[DIRTY_VALUES]);+ const auto& dirty_values = collection->parse_dirty_values_option(req->params[DIRTY_VALUES]);nlohmann::json json_res = collection->add_many(json_lines, document, operation, "",dirty_values);- //const std::string& import_summary_json = json_res.dump();+ //const std::string& import_summary_json = json_res->dump();//response_stream << import_summary_json << "\n";for (size_t i = 0; i < json_lines.size(); i++) {- if(i == json_lines.size()-1 && req.body_index == req.body.size() && req.last_chunk_aggregate) {+ if(i == json_lines.size()-1 && req->body_index == req->body.size() && req->last_chunk_aggregate) {// indicates last record of last batchresponse_stream << json_lines[i];} else {@@ -524,12 +525,12 @@ bool post_import_documents(http_req& req, http_res& res) {}}- res.content_type_header = "text/plain; charset=utf8";- res.status_code = 200;- res.body += response_stream.str();+ res->content_type_header = "text/plain; charset=utf8";+ res->status_code = 200;+ res->body += response_stream.str();if(stream_proceed) {- res.final = req.last_chunk_aggregate;+ res->final = req->last_chunk_aggregate;HttpServer::stream_response(req, res);} else {// push handler back onto the event loop: we must process the next batch without blocking the event loop@@ -539,137 +540,137 @@ bool post_import_documents(http_req& req, http_res& res) {return true;}-bool post_add_document(http_req & req, http_res & res) {+bool post_add_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {const char *ACTION = "action";const char *DIRTY_VALUES_PARAM = "dirty_values";- if(req.params.count(ACTION) == 0) {- req.params[ACTION] = "create";+ if(req->params.count(ACTION) == 0) {+ req->params[ACTION] = "create";}- if(req.params[ACTION] != "create" && req.params[ACTION] != "update" && req.params[ACTION] != "upsert") {- res.set_400("Parameter `" + std::string(ACTION) + "` must be a create|update|upsert.");+ if(req->params[ACTION] != "create" && req->params[ACTION] != "update" && req->params[ACTION] != "upsert") {+ res->set_400("Parameter `" + std::string(ACTION) + "` must be a create|update|upsert.");return false;}- if(req.params.count(DIRTY_VALUES_PARAM) == 0) {- req.params[DIRTY_VALUES_PARAM] = ""; // set it empty as default will depend on whether schema is enabled+ if(req->params.count(DIRTY_VALUES_PARAM) == 0) {+ req->params[DIRTY_VALUES_PARAM] = ""; // set it empty as default will depend on whether schema is enabled}CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}- const index_operation_t operation = get_index_operation(req.params[ACTION]);- const auto& dirty_values = collection->parse_dirty_values_option(req.params[DIRTY_VALUES_PARAM]);+ const index_operation_t operation = get_index_operation(req->params[ACTION]);+ const auto& dirty_values = collection->parse_dirty_values_option(req->params[DIRTY_VALUES_PARAM]);- Option<nlohmann::json> inserted_doc_op = collection->add(req.body, operation, "", dirty_values);+ Option<nlohmann::json> inserted_doc_op = collection->add(req->body, operation, "", dirty_values);if(!inserted_doc_op.ok()) {- res.set(inserted_doc_op.code(), inserted_doc_op.error());+ res->set(inserted_doc_op.code(), inserted_doc_op.error());return false;}- res.set_201(inserted_doc_op.get().dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));+ res->set_201(inserted_doc_op.get().dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));return true;}-bool patch_update_document(http_req & req, http_res & res) {- std::string doc_id = req.params["id"];+bool patch_update_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ std::string doc_id = req->params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}const char* DIRTY_VALUES_PARAM = "dirty_values";- if(req.params.count(DIRTY_VALUES_PARAM) == 0) {- req.params[DIRTY_VALUES_PARAM] = ""; // set it empty as default will depend on whether schema is enabled+ if(req->params.count(DIRTY_VALUES_PARAM) == 0) {+ req->params[DIRTY_VALUES_PARAM] = ""; // set it empty as default will depend on whether schema is enabled}- const auto& dirty_values = collection->parse_dirty_values_option(req.params[DIRTY_VALUES_PARAM]);- Option<nlohmann::json> upserted_doc_op = collection->add(req.body, index_operation_t::UPDATE, doc_id, dirty_values);+ const auto& dirty_values = collection->parse_dirty_values_option(req->params[DIRTY_VALUES_PARAM]);+ Option<nlohmann::json> upserted_doc_op = collection->add(req->body, index_operation_t::UPDATE, doc_id, dirty_values);if(!upserted_doc_op.ok()) {- res.set(upserted_doc_op.code(), upserted_doc_op.error());+ res->set(upserted_doc_op.code(), upserted_doc_op.error());return false;}- res.set_201(upserted_doc_op.get().dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));+ res->set_201(upserted_doc_op.get().dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));return true;}-bool get_fetch_document(http_req & req, http_res & res) {- std::string doc_id = req.params["id"];+bool get_fetch_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ std::string doc_id = req->params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}Option<nlohmann::json> doc_option = collection->get(doc_id);if(!doc_option.ok()) {- res.set(doc_option.code(), doc_option.error());+ res->set(doc_option.code(), doc_option.error());return false;}- res.set_200(doc_option.get().dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));+ res->set_200(doc_option.get().dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));return true;}-bool del_remove_document(http_req & req, http_res & res) {- std::string doc_id = req.params["id"];+bool del_remove_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ std::string doc_id = req->params["id"];CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}Option<nlohmann::json> doc_option = collection->get(doc_id);if(!doc_option.ok()) {- res.set(doc_option.code(), doc_option.error());+ res->set(doc_option.code(), doc_option.error());return false;}Option<std::string> deleted_id_op = collection->remove(doc_id);if(!deleted_id_op.ok()) {- res.set(deleted_id_op.code(), deleted_id_op.error());+ res->set(deleted_id_op.code(), deleted_id_op.error());return false;}nlohmann::json doc = doc_option.get();- res.set_200(doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));+ res->set_200(doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));return true;}-bool del_remove_documents(http_req & req, http_res & res) {+bool del_remove_documents(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {// defaults: will get overridden later if needed- res.content_type_header = "application/json";- res.status_code = 200;+ res->content_type_header = "application/json";+ res->status_code = 200;// NOTE: this is a streaming response end-point so this handler will be called multiple timesCollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- req.last_chunk_aggregate = true;- res.final = true;- res.set_404();+ req->last_chunk_aggregate = true;+ res->final = true;+ res->set_404();HttpServer::stream_response(req, res);return false;}@@ -677,52 +678,52 @@ bool del_remove_documents(http_req & req, http_res & res) {const char *BATCH_SIZE = "batch_size";const char *FILTER_BY = "filter_by";- if(req.params.count(BATCH_SIZE) == 0) {- req.params[BATCH_SIZE] = "40";+ if(req->params.count(BATCH_SIZE) == 0) {+ req->params[BATCH_SIZE] = "40";}- if(req.params.count(FILTER_BY) == 0) {- req.last_chunk_aggregate = true;- res.final = true;- res.set_400("Parameter `" + std::string(FILTER_BY) + "` must be provided.");+ if(req->params.count(FILTER_BY) == 0) {+ req->last_chunk_aggregate = true;+ res->final = true;+ res->set_400("Parameter `" + std::string(FILTER_BY) + "` must be provided.");HttpServer::stream_response(req, res);return false;}- if(!StringUtils::is_uint32_t(req.params[BATCH_SIZE])) {- req.last_chunk_aggregate = true;- res.final = true;- res.set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");+ if(!StringUtils::is_uint32_t(req->params[BATCH_SIZE])) {+ 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);return false;}- const size_t DELETE_BATCH_SIZE = std::stoi(req.params[BATCH_SIZE]);+ const size_t DELETE_BATCH_SIZE = std::stoi(req->params[BATCH_SIZE]);if(DELETE_BATCH_SIZE == 0) {- req.last_chunk_aggregate = true;- res.final = true;- res.set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");+ 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);return false;}std::string simple_filter_query;- if(req.params.count(FILTER_BY) != 0) {- simple_filter_query = req.params[FILTER_BY];+ if(req->params.count(FILTER_BY) != 0) {+ simple_filter_query = req->params[FILTER_BY];}deletion_state_t* deletion_state = nullptr;- if(req.data == nullptr) {+ if(req->data == nullptr) {deletion_state = new deletion_state_t{};auto filter_ids_op = collection->get_filter_ids(simple_filter_query, deletion_state->index_ids);if(!filter_ids_op.ok()) {- res.set(filter_ids_op.code(), filter_ids_op.error());- req.last_chunk_aggregate = true;- res.final = true;+ res->set(filter_ids_op.code(), filter_ids_op.error());+ req->last_chunk_aggregate = true;+ res->final = true;HttpServer::stream_response(req, res);return false;}@@ -732,30 +733,30 @@ bool del_remove_documents(http_req & req, http_res & res) {}deletion_state->collection = collection.get();deletion_state->num_removed = 0;- req.data = deletion_state;+ req->data = deletion_state;} else {- deletion_state = static_cast<deletion_state_t*>(req.data);+ deletion_state = static_cast<deletion_state_t*>(req->data);}bool done = true;Option<bool> remove_op = stateful_remove_docs(deletion_state, DELETE_BATCH_SIZE, done);if(!remove_op.ok()) {- res.set(remove_op.code(), remove_op.error());- req.last_chunk_aggregate = true;- res.final = true;+ res->set(remove_op.code(), remove_op.error());+ req->last_chunk_aggregate = true;+ res->final = true;} else {if(!done) {- req.last_chunk_aggregate = false;- res.final = false;+ req->last_chunk_aggregate = false;+ res->final = false;} else {nlohmann::json response;response["num_deleted"] = deletion_state->num_removed;- req.last_chunk_aggregate = true;- req.data = nullptr;- res.body = response.dump();- res.final = true;+ req->last_chunk_aggregate = true;+ req->data = nullptr;+ res->body = response.dump();+ res->final = true;delete deletion_state;}}@@ -764,7 +765,7 @@ bool del_remove_documents(http_req & req, http_res & res) {return true;}-bool get_aliases(http_req & req, http_res & res) {+bool get_aliases(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();const spp::sparse_hash_map<std::string, std::string> & symlinks = collectionManager.get_symlinks();nlohmann::json res_json = nlohmann::json::object();@@ -777,17 +778,17 @@ bool get_aliases(http_req & req, http_res & res) {res_json["aliases"].push_back(symlink);}- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool get_alias(http_req & req, http_res & res) {- const std::string & alias = req.params["alias"];+bool get_alias(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ const std::string & alias = req->params["alias"];CollectionManager & collectionManager = CollectionManager::get_instance();Option<std::string> collection_name_op = collectionManager.resolve_symlink(alias);if(!collection_name_op.ok()) {- res.set_404();+ res->set_404();return false;}@@ -795,72 +796,72 @@ bool get_alias(http_req & req, http_res & res) {res_json["name"] = alias;res_json["collection_name"] = collection_name_op.get();- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool put_upsert_alias(http_req & req, http_res & res) {+bool put_upsert_alias(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json req_json;try {- req_json = nlohmann::json::parse(req.body);+ req_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}CollectionManager & collectionManager = CollectionManager::get_instance();- const std::string & alias = req.params["alias"];+ const std::string & alias = req->params["alias"];const char* COLLECTION_NAME = "collection_name";if(req_json.count(COLLECTION_NAME) == 0) {- res.set_400(std::string("Parameter `") + COLLECTION_NAME + "` is required.");+ res->set_400(std::string("Parameter `") + COLLECTION_NAME + "` is required.");return false;}Option<bool> success_op = collectionManager.upsert_symlink(alias, req_json[COLLECTION_NAME]);if(!success_op.ok()) {- res.set_500(success_op.error());+ res->set_500(success_op.error());return false;}req_json["name"] = alias;- res.set_200(req_json.dump());+ res->set_200(req_json.dump());return true;}-bool del_alias(http_req & req, http_res & res) {- const std::string & alias = req.params["alias"];+bool del_alias(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ const std::string & alias = req->params["alias"];CollectionManager & collectionManager = CollectionManager::get_instance();Option<std::string> collection_name_op = collectionManager.resolve_symlink(alias);if(!collection_name_op.ok()) {- res.set_404();+ res->set_404();return false;}Option<bool> delete_op = collectionManager.delete_symlink(alias);if(!delete_op.ok()) {- res.set_500(delete_op.error());+ res->set_500(delete_op.error());return false;}nlohmann::json res_json;res_json["name"] = alias;res_json["collection_name"] = collection_name_op.get();- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool get_overrides(http_req &req, http_res &res) {+bool get_overrides(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}@@ -873,103 +874,103 @@ bool get_overrides(http_req &req, http_res &res) {res_json["overrides"].push_back(override);}- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool get_override(http_req &req, http_res &res) {+bool get_override(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}- std::string override_id = req.params["id"];+ std::string override_id = req->params["id"];const std::map<std::string, override_t>& overrides = collection->get_overrides();if(overrides.count(override_id) != 0) {nlohmann::json override = overrides.at(override_id).to_json();- res.set_200(override.dump());+ res->set_200(override.dump());return true;}- res.set_404();+ res->set_404();return false;}-bool put_override(http_req &req, http_res &res) {+bool put_override(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);- std::string override_id = req.params["id"];+ std::string override_id = req->params["id"];if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}nlohmann::json req_json;try {- req_json = nlohmann::json::parse(req.body);+ req_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}override_t override;Option<bool> parse_op = override_t::parse(req_json, override_id, override);if(!parse_op.ok()) {- res.set(parse_op.code(), parse_op.error());+ res->set(parse_op.code(), parse_op.error());return false;}Option<uint32_t> add_op = collection->add_override(override);if(!add_op.ok()) {- res.set(add_op.code(), add_op.error());+ res->set(add_op.code(), add_op.error());return false;}req_json["id"] = override.id;- res.set_200(req_json.dump());+ res->set_200(req_json.dump());return true;}-bool del_override(http_req &req, http_res &res) {+bool del_override(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}- Option<uint32_t> rem_op = collection->remove_override(req.params["id"]);+ Option<uint32_t> rem_op = collection->remove_override(req->params["id"]);if(!rem_op.ok()) {- res.set(rem_op.code(), rem_op.error());+ res->set(rem_op.code(), rem_op.error());return false;}nlohmann::json res_json;- res_json["id"] = req.params["id"];+ res_json["id"] = req->params["id"];- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool get_keys(http_req &req, http_res &res) {+bool get_keys(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();AuthManager &auth_manager = collectionManager.getAuthManager();const Option<std::vector<api_key_t>>& keys_op = auth_manager.list_keys();if(!keys_op.ok()) {- res.set(keys_op.code(), keys_op.error());+ res->set(keys_op.code(), keys_op.error());return false;}@@ -984,11 +985,11 @@ bool get_keys(http_req &req, http_res &res) {res_json["keys"].push_back(key_obj);}- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool post_create_key(http_req &req, http_res &res) {+bool post_create_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {//LOG(INFO) << "post_create_key";CollectionManager & collectionManager = CollectionManager::get_instance();@@ -997,21 +998,21 @@ bool post_create_key(http_req &req, http_res &res) {nlohmann::json req_json;try {- req_json = nlohmann::json::parse(req.body);+ req_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}const Option<uint32_t>& validate_op = api_key_t::validate(req_json);if(!validate_op.ok()) {- res.set(validate_op.code(), validate_op.error());+ res->set(validate_op.code(), validate_op.error());return false;}- const std::string &rand_key = req.metadata;+ const std::string &rand_key = req->metadata;if(req_json.count("expires_at") == 0) {req_json["expires_at"] = api_key_t::FAR_FUTURE_TIMESTAMP;@@ -1027,25 +1028,25 @@ bool post_create_key(http_req &req, http_res &res) {const Option<api_key_t>& api_key_op = auth_manager.create_key(api_key);if(!api_key_op.ok()) {- res.set(api_key_op.code(), api_key_op.error());+ res->set(api_key_op.code(), api_key_op.error());return false;}- res.set_201(api_key_op.get().to_json().dump());+ res->set_201(api_key_op.get().to_json().dump());return true;}-bool get_key(http_req &req, http_res &res) {+bool get_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();AuthManager &auth_manager = collectionManager.getAuthManager();- const std::string& key_id_str = req.params["id"];+ const std::string& key_id_str = req->params["id"];uint32_t key_id = (uint32_t) std::stoul(key_id_str);const Option<api_key_t>& key_op = auth_manager.get_key(key_id);if(!key_op.ok()) {- res.set(key_op.code(), key_op.error());+ res->set(key_op.code(), key_op.error());return false;}@@ -1053,28 +1054,28 @@ bool get_key(http_req &req, http_res &res) {key_obj["value_prefix"] = key_obj["value"];key_obj.erase("value");- res.set_200(key_obj.dump());+ res->set_200(key_obj.dump());return true;}-bool del_key(http_req &req, http_res &res) {+bool del_key(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();AuthManager &auth_manager = collectionManager.getAuthManager();- const std::string& key_id_str = req.params["id"];+ const std::string& key_id_str = req->params["id"];uint32_t key_id = (uint32_t) std::stoul(key_id_str);const Option<api_key_t> &del_op = auth_manager.remove_key(key_id);if(!del_op.ok()) {- res.set(del_op.code(), del_op.error());+ res->set(del_op.code(), del_op.error());return false;}nlohmann::json res_json;res_json["id"] = del_op.get().id;- res.set_200(res_json.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));+ res->set_200(res_json.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));return true;}@@ -1093,7 +1094,7 @@ bool raft_write_send_response(void *data) {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);+ found_rpath->handler(index_arg->req, index_arg->res);} else {index_arg->res->set_404();}@@ -1106,55 +1107,56 @@ bool raft_write_send_response(void *data) {// 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(http_req& req, http_res& res) {+bool post_snapshot(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {const std::string SNAPSHOT_PATH = "snapshot_path";- res.status_code = 201;- res.content_type_header = "application/json";+ res->status_code = 201;+ res->content_type_header = "application/json";- if(req.params.count(SNAPSHOT_PATH) == 0) {- req.last_chunk_aggregate = true;- res.final = true;- res.set_400(std::string("Parameter `") + SNAPSHOT_PATH + "` is required.");+ if(req->params.count(SNAPSHOT_PATH) == 0) {+ req->last_chunk_aggregate = true;+ res->final = true;+ res->set_400(std::string("Parameter `") + SNAPSHOT_PATH + "` is required.");HttpServer::stream_response(req, res);return false;}- server->do_snapshot(req.params[SNAPSHOT_PATH], req, res);+ server->do_snapshot(req->params[SNAPSHOT_PATH], req, res);return true;}-bool post_vote(http_req& req, http_res& res) {- res.status_code = 200;- res.content_type_header = "application/json";+bool post_vote(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ res->status_code = 200;+ res->content_type_header = "application/json";nlohmann::json response;response["success"] = server->trigger_vote();- res.body = response.dump();+ res->body = response.dump();return true;}-bool post_config(http_req &req, http_res &res) {+bool post_config(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json req_json;try {- req_json = nlohmann::json::parse(req.body);+ req_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}if(req_json.count("log-slow-requests-time-ms") != 0) {if(!req_json["log-slow-requests-time-ms"].is_number_integer()) {- res.set_400("Configuration `log-slow-requests-time-ms` must be an integer.");+ res->set_400("Configuration `log-slow-requests-time-ms` must be an integer.");return false;}@@ -1162,20 +1164,20 @@ bool post_config(http_req &req, http_res &res) {nlohmann::json response;response["success"] = true;- res.set_201(response.dump());+ res->set_201(response.dump());} else {- res.set_400("Invalid configuration.");+ res->set_400("Invalid configuration.");}return true;}-bool get_synonyms(http_req &req, http_res &res) {+bool get_synonyms(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}@@ -1188,52 +1190,52 @@ bool get_synonyms(http_req &req, http_res &res) {res_json["synonyms"].push_back(synonym);}- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}-bool get_synonym(http_req &req, http_res &res) {+bool get_synonym(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}- std::string synonym_id = req.params["id"];+ std::string synonym_id = req->params["id"];synonym_t synonym;bool found = collection->get_synonym(synonym_id, synonym);if(found) {nlohmann::json synonym_json = synonym.to_view_json();- res.set_200(synonym_json.dump());+ res->set_200(synonym_json.dump());return true;}- res.set_404();+ res->set_404();return false;}-bool put_synonym(http_req &req, http_res &res) {+bool put_synonym(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);- std::string synonym_id = req.params["id"];+ std::string synonym_id = req->params["id"];if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}nlohmann::json syn_json;try {- syn_json = nlohmann::json::parse(req.body);+ syn_json = nlohmann::json::parse(req->body);} catch(const std::exception& e) {LOG(ERROR) << "JSON error: " << e.what();- res.set_400("Bad JSON.");+ res->set_400("Bad JSON.");return false;}@@ -1243,39 +1245,39 @@ bool put_synonym(http_req &req, http_res &res) {Option<bool> syn_op = synonym_t::parse(syn_json, synonym);if(!syn_op.ok()) {- res.set(syn_op.code(), syn_op.error());+ res->set(syn_op.code(), syn_op.error());return false;}Option<bool> upsert_op = collection->add_synonym(synonym);if(!upsert_op.ok()) {- res.set(upsert_op.code(), upsert_op.error());+ res->set(upsert_op.code(), upsert_op.error());return false;}- res.set_200(syn_json.dump());+ res->set_200(syn_json.dump());return true;}-bool del_synonym(http_req &req, http_res &res) {+bool del_synonym(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {CollectionManager & collectionManager = CollectionManager::get_instance();- auto collection = collectionManager.get_collection(req.params["collection"]);+ auto collection = collectionManager.get_collection(req->params["collection"]);if(collection == nullptr) {- res.set_404();+ res->set_404();return false;}- Option<bool> rem_op = collection->remove_synonym(req.params["id"]);+ Option<bool> rem_op = collection->remove_synonym(req->params["id"]);if(!rem_op.ok()) {- res.set(rem_op.code(), rem_op.error());+ res->set(rem_op.code(), rem_op.error());return false;}nlohmann::json res_json;- res_json["id"] = req.params["id"];+ res_json["id"] = req->params["id"];- res.set_200(res_json.dump());+ res->set_200(res_json.dump());return true;}diff --git a/src/http_client.cpp b/src/http_client.cppindex 8908ecc0..bc550fa4 100644--- a/src/http_client.cpp+++ b/src/http_client.cpp@@ -18,7 +18,8 @@ long HttpClient::post_response(const std::string &url, const std::string &body,return perform_curl(curl, res_headers);}-long HttpClient::post_response_async(const std::string &url, http_req* request, http_res* response, HttpServer* server) {+long HttpClient::post_response_async(const std::string &url, const std::shared_ptr<http_req> request,+ const std::shared_ptr<http_res> response, HttpServer* server) {deferred_req_res_t* req_res = new deferred_req_res_t{request, response, server};std::unique_ptr<deferred_req_res_t> req_res_guard(req_res);struct curl_slist *chunk = nullptr;@@ -179,7 +180,7 @@ size_t HttpClient::curl_req_send_callback(char* buffer, size_t size, size_t niteserver->get_message_dispatcher()->send_message(HttpServer::REQUEST_PROCEED_MESSAGE, req_res);//LOG(INFO) << "Waiting for request body to be ready";- req_res->req->await.wait();+ req_res->req->wait();//LOG(INFO) << "Request body is ready";//LOG(INFO) << "Buffer refilled, unpausing request forwarding, body_size=" << req_res->req->body.size();}@@ -223,8 +224,8 @@ size_t HttpClient::curl_write_async(char *buffer, size_t size, size_t nmemb, voireq_res->server->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);// wait until response is sent- //LOG(INFO) << "Waiting for response to be sent";- req_res->res->await.wait();+ //LOG(INFO) << "Waiting on req_res " << req_res->res;+ req_res->res->wait();//LOG(INFO) << "Response sent";return res_size;@@ -245,7 +246,7 @@ size_t HttpClient::curl_write_async_done(void *context, curl_socket_t item) {req_res->server->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);// wait until final response is flushed or response object will be destroyed by caller- req_res->res->await.wait();+ req_res->res->wait();return 0;}diff --git a/src/http_server.cpp b/src/http_server.cppindex 345df6e2..73739c21 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -300,8 +300,15 @@ uint64_t HttpServer::find_route(const std::vector<std::string> & path_parts, convoid HttpServer::on_res_generator_dispose(void *self) {//LOG(INFO) << "on_res_generator_dispose fires";- h2o_custom_generator_t* res_generator = static_cast<h2o_custom_generator_t*>(self);- destroy_request_response(res_generator->request, res_generator->response);+ h2o_custom_generator_t* custom_generator = *static_cast<h2o_custom_generator_t**>(self);+ destroy_request_response(custom_generator->req(), custom_generator->res());++ /*LOG(INFO) << "Deleting custom_generator, res: " << custom_generator->res();+ << ", refcount: " << custom_generator->res().use_count();*/++ delete custom_generator;++ //LOG(INFO) << "Deleted custom_generator";}int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {@@ -407,19 +414,25 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {return send_response(req, 401, message);}- http_req* request = new http_req(req, rpath->http_method, path_without_query, route_hash, query_map, body);- http_res* response = new http_res();+ std::shared_ptr<http_req> request = std::make_shared<http_req>(req, rpath->http_method, path_without_query,+ route_hash, query_map, body);+ std::shared_ptr<http_res> response = std::make_shared<http_res>();// add custom generator with a dispose function for cleaning up resources- h2o_custom_generator_t* custom_generator = static_cast<h2o_custom_generator_t*>(- h2o_mem_alloc_shared(&req->pool, sizeof(*custom_generator), on_res_generator_dispose)+ h2o_custom_generator_t* custom_gen = new h2o_custom_generator_t;+ custom_gen->super = h2o_generator_t {response_proceed, response_abort};+ custom_gen->request = request;+ custom_gen->response = response;+ custom_gen->rpath = rpath;+ custom_gen->h2o_handler = h2o_handler;+ response->generator = &custom_gen->super;++ h2o_custom_generator_t** allocated_generator = static_cast<h2o_custom_generator_t**>(+ h2o_mem_alloc_shared(&req->pool, sizeof(*allocated_generator), on_res_generator_dispose));- custom_generator->super = h2o_generator_t {response_proceed, response_abort};- custom_generator->request = request;- custom_generator->response = response;- custom_generator->rpath = rpath;- custom_generator->h2o_handler = h2o_handler;- response->generator = &custom_generator->super;+ *allocated_generator = custom_gen;++ //LOG(INFO) << "Init res: " << custom_gen->response << ", ref count: " << custom_gen->response.use_count();// routes match and is an authenticated request// do any additional pre-request middleware operations here@@ -440,7 +453,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {//LOG(INFO) << "Partial request body length: " << req->entity.len;req->write_req.cb = async_req_cb;- req->write_req.ctx = custom_generator;+ req->write_req.ctx = custom_gen;req->proceed_req(req, req->entity.len, H2O_SEND_STATE_IN_PROGRESS);}@@ -453,8 +466,8 @@ int HttpServer::async_req_cb(void *ctx, h2o_iovec_t chunk, int is_end_stream) {// This quirk is because of the underlying buffer/window sizes. We will have to deal with both cases.h2o_custom_generator_t* custom_generator = static_cast<h2o_custom_generator_t*>(ctx);- http_req* request = custom_generator->request;- http_res* response = custom_generator->response;+ const std::shared_ptr<http_req>& request = custom_generator->req();+ const std::shared_ptr<http_res>& response = custom_generator->res();std::string chunk_str(chunk.base, chunk.len);@@ -526,7 +539,7 @@ int HttpServer::async_req_cb(void *ctx, h2o_iovec_t chunk, int is_end_stream) {return 0;}-int HttpServer::process_request(http_req* request, http_res* response, route_path *rpath,+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) {//LOG(INFO) << "process_request called";@@ -545,33 +558,23 @@ int HttpServer::process_request(http_req* request, http_res* response, route_patreturn 0;}+ auto http_server = handler->http_server;auto message_dispatcher = handler->http_server->get_message_dispatcher();auto api_handler = rpath->handler;- handler->http_server->get_thread_pool()->enqueue([api_handler, message_dispatcher, request, response]() {- (api_handler)(*request, *response);- request_response req_res{request, response};- response->auto_dispose = false;- message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, &req_res);+ // LOG(INFO) << "Before enqueue res: " << response+ handler->http_server->get_thread_pool()->enqueue([http_server, api_handler, message_dispatcher,+ request, response]() {+ // call the API handler+ (api_handler)(request, response);- // wait until response is sent- response->await.wait();+ deferred_req_res_t req_res{request, response, http_server};+ message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, &req_res);- delete request;- delete response;+ std::this_thread::sleep_for(std::chrono::milliseconds(100));+ response->wait();});- /*- // for reads, we will invoke the request handler and handle response as well- (rpath->handler)(*request, *response);-- if(!rpath->async_res) {- // If a handler is marked as async res, it's responsible for sending the response itself in an async fashion- // otherwise, we send the whole body response on behalf of the handler- handler->http_server->send_response(request, response);- }- */-return 0;}@@ -582,26 +585,29 @@ 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);+ found_rpath->handler(deferred_req_res->req, deferred_req_res->res);}}-void HttpServer::defer_processing(http_req& req, http_res& res, size_t timeout_ms) {- if(req.defer_timer.data == nullptr) {- auto deferred_req_res = new deferred_req_res_t{&req, &res, this};- req.defer_timer.data = deferred_req_res;- h2o_timer_init(&req.defer_timer.timer, on_deferred_process_request);+void HttpServer::defer_processing(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res,+ size_t timeout_ms) {+ if(req->defer_timer.data == nullptr) {+ auto deferred_req_res = new deferred_req_res_t{req, res, this};+ req->defer_timer.data = deferred_req_res;+ h2o_timer_init(&req->defer_timer.timer, on_deferred_process_request);} else {- h2o_timer_unlink(&req.defer_timer.timer);+ h2o_timer_unlink(&req->defer_timer.timer);}- h2o_timer_link(ctx.loop, timeout_ms, &req.defer_timer.timer);+ h2o_timer_link(ctx.loop, timeout_ms, &req->defer_timer.timer);- //LOG(INFO) << "defer_processing, exit_loop: " << exit_loop << ", res.await: " << res.await;+ //LOG(INFO) << "defer_processing, exit_loop: " << exit_loop << ", res->await: " << res->await;if(exit_loop) {// otherwise, replication thread could be stuck waiting on a future- res.await.notify();+ req->_req = nullptr;+ req->notify();+ res->notify();}}@@ -620,23 +626,23 @@ int HttpServer::send_response(h2o_req_t *req, int status_code, const std::stringreturn 0;}-void HttpServer::send_response(http_req* request, http_res* response) {+void HttpServer::send_response(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response) {//LOG(INFO) << "send_response, request->_req=" << request->_req;if(request->_req == nullptr) {- // indicates serialized request and response -- lifecycle must be managed here- return destroy_request_response(request, response);+ // indicates serialized request and response+ return ;}h2o_req_t* req = request->_req;- h2o_generator_t& generator = *response->generator;+ h2o_generator_t* generator = static_cast<h2o_generator_t*>(response->generator);h2o_iovec_t body = h2o_strdup(&req->pool, response->body.c_str(), SIZE_MAX);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,nullptr, response->content_type_header.c_str(), response->content_type_header.size());- h2o_start_response(req, &generator);+ h2o_start_response(req, generator);h2o_send(req, &body, 1, H2O_SEND_STATE_FINAL);}@@ -644,93 +650,99 @@ void HttpServer::response_abort(h2o_generator_t *generator, h2o_req_t *req) {LOG(INFO) << "response_abort called";h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t*>(generator);- custom_generator->request->_req = nullptr;- custom_generator->response->final = true;+ custom_generator->req()->_req = nullptr;+ custom_generator->res()->final = true;// returns control back to caller (raft replication or follower forward)- LOG(INFO) << "response_abort: fulfilling req & res proceed.";-- // order is important!- custom_generator->request->await.notify();- custom_generator->response->await.notify();+ //LOG(INFO) << "response_abort: fulfilling req & res proceed.";}void HttpServer::response_proceed(h2o_generator_t *generator, h2o_req_t *req) {//LOG(INFO) << "response_proceed called";h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t*>(generator);- custom_generator->response->await.notify();-//LOG(INFO) << "proxied_stream: " << custom_generator->response->proxied_stream;//LOG(INFO) << "response.final: " << custom_generator->response->final;- if(custom_generator->response->proxied_stream) {+ if(custom_generator->res()->proxied_stream) {// request progression should not be tied to response generation//LOG(INFO) << "Ignoring request proceed";+ custom_generator->res()->notify();return ;}+ // FIXME: should this be at the end of the function?+ custom_generator->res()->notify();+// if the request itself is async, we will proceed the request to fetch input contentif (custom_generator->rpath->async_req) {- auto stream_state = (custom_generator->response->final) ? H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;+ auto stream_state = (custom_generator->res()->final) ? H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;// `written` is ignored by http1.1 implementation, so meant only for http 2+- size_t written = custom_generator->request->chunk_len;- custom_generator->request->chunk_len = 0;+ size_t written = custom_generator->req()->chunk_len;+ custom_generator->req()->chunk_len = 0;- if(custom_generator->request->_req->proceed_req) {+ if(custom_generator->req()->_req->proceed_req) {//LOG(INFO) << "response_proceed: proceeding req";- custom_generator->request->_req->proceed_req(custom_generator->request->_req, written, stream_state);+ custom_generator->req()->_req->proceed_req(custom_generator->req()->_req, written,+ stream_state);}} else {// otherwise, call the handler since it will be the handler that will be producing content// (streaming response but not request)- custom_generator->h2o_handler->http_server->defer_processing(*custom_generator->request,- *custom_generator->response, 1);+ custom_generator->h2o_handler->http_server->defer_processing(custom_generator->req(),+ custom_generator->res(), 1);}}-void HttpServer::stream_response(http_req& request, http_res& response) {+void HttpServer::stream_response(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response) {//LOG(INFO) << "stream_response called";- if(request._req == nullptr) {++ if(request->_req == nullptr) {// raft log replay or when underlying request is aborted//LOG(INFO) << "stream_response, request._req == nullptr";- destroy_request_response(&request, &response);+ response->notify();return;}- h2o_req_t* req = request._req;- h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t *>(response.generator);+ h2o_req_t* req = request->_req;+ h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t *>(response->generator);if (req->res.status == 0) {//LOG(INFO) << "h2o_start_response, content_type=" << response.content_type_header// << ",response.status_code=" << response.status_code;- response.status_code = (response.status_code == 0) ? 503 : response.status_code; // just to be sure- req->res.status = response.status_code;- req->res.reason = http_res::get_status_reason(response.status_code);+ response->status_code = (response->status_code == 0) ? 503 : response->status_code; // just to be sure+ 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());+ response->content_type_header.c_str(),+ response->content_type_header.size());h2o_start_response(req, &custom_generator->super);}//LOG(INFO) << "stream_response, body_size: " << response.body.size() << ", response_final="// << custom_generator->response->final;- h2o_iovec_t body = h2o_strdup(&req->pool, response.body.c_str(), SIZE_MAX);- response.body = "";+ h2o_iovec_t body = h2o_strdup(&req->pool, response->body.c_str(), SIZE_MAX);+ response->body = "";- const h2o_send_state_t state = custom_generator->response->final ? H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;+ 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->rpath->async_req && custom_generator->response->final &&- !custom_generator->request->last_chunk_aggregate) {+ if(custom_generator->rpath->async_req && custom_generator->res()->final &&+ !custom_generator->req()->last_chunk_aggregate) {// premature termination of async request: handle this explicitly as otherwise, request is not being closed+ LOG(INFO) << "Premature termination of async request, disposing req object.";h2o_dispose_request(req);}++ // LOG(INFO) << "stream_response after send";}-void HttpServer::destroy_request_response(http_req* request, http_res* response) {+void HttpServer::destroy_request_response(const std::shared_ptr<http_req>& request,+ const std::shared_ptr<http_res>& response) {+ //LOG(INFO) << "destroy_request_response";+if(request->defer_timer.data != nullptr) {deferred_req_res_t* deferred_req_res = static_cast<deferred_req_res_t*>(request->defer_timer.data);h2o_timer_unlink(&request->defer_timer.timer);@@ -742,18 +754,11 @@ void HttpServer::destroy_request_response(http_req* request, http_res* response)//LOG(INFO) << "destroy_request_response, response: " << response << ", response->auto_dispose: " << response->auto_dispose;- if(response->auto_dispose) {- //LOG(INFO) << "destroy_request_response: deleting req/res";- delete request;- delete response;- } else {- // lifecycle of proxied/replicated resources are managed externally- // we will just nullify _req to indicate that original request is dead- //LOG(INFO) << "Ignoring request/response cleanup since auto_dispose is false.";- response->final = true;- request->_req = nullptr;- response->await.notify();- }+ request->_req = nullptr;+ response->final = true;++ request->notify();+ response->notify();}void HttpServer::set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const std::string& body,@@ -761,35 +766,35 @@ void HttpServer::set_auth_handler(bool (*handler)(std::map<std::string, std::strauth_handler = handler;}-void HttpServer::get(const std::string & path, bool (*handler)(http_req &, http_res &), bool async_req, bool async_res) {+void HttpServer::get(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&), bool async_req, bool async_res) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath("GET", path_parts, handler, async_req, async_res);routes.emplace_back(rpath.route_hash(), rpath);}-void HttpServer::post(const std::string & path, bool (*handler)(http_req &, http_res &), bool async_req, bool async_res) {+void HttpServer::post(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&), bool async_req, bool async_res) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath("POST", path_parts, handler, async_req, async_res);routes.emplace_back(rpath.route_hash(), rpath);}-void HttpServer::put(const std::string & path, bool (*handler)(http_req &, http_res &), bool async_req, bool async_res) {+void HttpServer::put(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&), bool async_req, bool async_res) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath("PUT", path_parts, handler, async_req, async_res);routes.emplace_back(rpath.route_hash(), rpath);}-void HttpServer::patch(const std::string & path, bool (*handler)(http_req &, http_res &), bool async_req, bool async_res) {+void HttpServer::patch(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&), bool async_req, bool async_res) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath("PATCH", path_parts, handler, async_req, async_res);routes.emplace_back(rpath.route_hash(), rpath);}-void HttpServer::del(const std::string & path, bool (*handler)(http_req &, http_res &), bool async_req, bool async_res) {+void HttpServer::del(const std::string & path, bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&), bool async_req, bool async_res) {std::vector<std::string> path_parts;StringUtils::split(path, path_parts, "/");route_path rpath("DELETE", path_parts, handler, async_req, async_res);@@ -859,7 +864,7 @@ uint64_t HttpServer::node_state() const {bool HttpServer::on_stream_response_message(void *data) {//LOG(INFO) << "on_stream_response_message";deferred_req_res_t* req_res = static_cast<deferred_req_res_t *>(data);- stream_response(*req_res->req, *req_res->res);+ stream_response(req_res->req, req_res->res);return true;}@@ -882,7 +887,7 @@ bool HttpServer::has_exited() const {return exit_loop;}-void HttpServer::do_snapshot(const std::string& snapshot_path, http_req& req, http_res& res) {+void HttpServer::do_snapshot(const std::string& snapshot_path, const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {return replication_state->do_snapshot(snapshot_path, req, res);}diff --git a/src/raft_server.cpp b/src/raft_server.cppindex f7eb3ee1..47d7ce67 100644--- a/src/raft_server.cpp+++ b/src/raft_server.cpp@@ -114,7 +114,7 @@ std::string ReplicationState::to_nodes_config(const butil::EndPoint& peering_endreturn actual_nodes_config;}-void ReplicationState::write(http_req* request, http_res* response) {+void ReplicationState::write(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response) {if(!node) {return ;}@@ -145,7 +145,7 @@ void ReplicationState::write(http_req* request, http_res* response) {return node->apply(task);}-void ReplicationState::write_to_leader(http_req *request, http_res *response) const {+void ReplicationState::write_to_leader(const std::shared_ptr<http_req>& request, const std::shared_ptr<http_res>& response) const {if(!node || node->leader_id().is_empty()) {// Handle no leader scenarioLOG(ERROR) << "Rejecting write: could not find a leader.";@@ -153,7 +153,8 @@ void ReplicationState::write_to_leader(http_req *request, http_res *response) coif(request->_req->proceed_req && response->proxied_stream) {// streaming in progress: ensure graceful termination (cannot start response again)LOG(ERROR) << "Terminating streaming request gracefully.";- request->await.notify();+ request->_req = nullptr;+ request->notify();return ;}@@ -166,7 +167,7 @@ void ReplicationState::write_to_leader(http_req *request, http_res *response) coif (request->_req->proceed_req && response->proxied_stream) {// indicates async request body of in-flight request//LOG(INFO) << "Inflight proxied request, returning control to caller, body_size=" << request->body.size();- request->await.notify();+ request->notify();return ;}@@ -191,13 +192,8 @@ void ReplicationState::write_to_leader(http_req *request, http_res *response) coif(path_parts.back().rfind("import", 0) == 0) {// imports are handled asynchronouslyresponse->proxied_stream = true;- response->auto_dispose = false;long status = HttpClient::post_response_async(url, request, response, server);- // must manage life cycle for forwarded requests- delete request;- delete response;-//LOG(INFO) << "Import call done.";if(status == 500) {@@ -255,57 +251,51 @@ void ReplicationState::on_apply(braft::Iterator& iter) {// A batch of tasks are committed, which must be processed through// |iter|for (; iter.valid(); iter.next()) {- http_res* response;- http_req* request;-// Guard invokes replication_arg->done->Run() asynchronously to avoid the callback blocking the main threadbraft::AsyncClosureGuard closure_guard(iter.done());+ std::shared_ptr<http_req> request_generated = std::make_shared<http_req>();+ std::shared_ptr<http_res> response_generated = std::make_shared<http_res>();if (iter.done()) {// This task is applied by this node, get value from the closure to avoid additional parsing.ReplicationClosure* c = dynamic_cast<ReplicationClosure*>(iter.done());- response = c->get_response();- request = c->get_request();-+ request_generated = c->get_request();+ response_generated = c->get_response();//LOG(INFO) << ":::" << "body size inside apply: " << request->body.size();} else {// Parse request from the log- response = new http_res;-- request = new http_req;- request->deserialize(iter.data().to_string());-+ const std::string &data_str = iter.data().to_string();+ request_generated->deserialize(iter.data().to_string());//LOG(INFO) << "Parsed request from the log, body_size: " << request->body.size();++ /* uint64_t hash = StringUtils::hash_wy(data_str.c_str(), data_str.size());+ if(hash == 12066143973577129900) {+ LOG(INFO) << "get stuck here...";+ }*/}- if(request->_req == nullptr && request->body == "INIT_SNAPSHOT") {+ if(request_generated->_req == nullptr && request_generated->body == "INIT_SNAPSHOT") {// We attempt to trigger a cold snapshot against an existing stand-alone DB for backward compatibilityInitSnapshotClosure* init_snapshot_closure = new InitSnapshotClosure(this);node->snapshot(init_snapshot_closure);- delete request;- delete response;continue ;}// Now that the log has been parsed, perform the actual operation// 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- response->auto_dispose = false;- auto replication_arg = new AsyncIndexArg{request, response};- message_dispatcher->send_message(REPLICATION_MSG, replication_arg);- //LOG(INFO) << "Raft write waiting to proceed";- response->await.wait();- //LOG(INFO) << "Raft write ready to proceed, response->final=" << response->final;+ // FIXME: AsyncIndexArg lifecycle should be managed by caller+ AsyncIndexArg* replication_arg = new AsyncIndexArg{request_generated, response_generated};+ message_dispatcher->send_message(REPLICATION_MSG, replication_arg);- if(response->final) {- delete request;- delete response;- }+ //LOG(INFO) << "Raft write waiting to proceed response->final=" << response_generated->final;+ response_generated->wait();+ //LOG(INFO) << "Raft write ready to proceed, response->final=" << response_generated->final;}}-void ReplicationState::read(http_res* response) {+void ReplicationState::read(const std::shared_ptr<http_res>& response) {// NOT USED:// For consistency, reads to followers could be rejected.// Currently, we don't do implement reads via raft.@@ -536,11 +526,12 @@ uint64_t ReplicationState::node_state() const {return node_status.state;}-void ReplicationState::do_snapshot(const std::string& snapshot_path, http_req& req, http_res& res) {+void ReplicationState::do_snapshot(const std::string& snapshot_path, const std::shared_ptr<http_req>& req,+ const std::shared_ptr<http_res>& res) {LOG(INFO) << "Triggerring an on demand snapshot...";- thread_pool->enqueue([&snapshot_path, &req, &res, this]() {- OnDemandSnapshotClosure* snapshot_closure = new OnDemandSnapshotClosure(this, &req, &res);+ thread_pool->enqueue([&snapshot_path, req, res, this]() {+ OnDemandSnapshotClosure* snapshot_closure = new OnDemandSnapshotClosure(this, req, res);ext_snapshot_path = snapshot_path;node->snapshot(snapshot_closure);});@@ -610,7 +601,6 @@ void OnDemandSnapshotClosure::Run() {req->last_chunk_aggregate = true;res->final = true;- res->auto_dispose = false;nlohmann::json response;uint32_t status_code;@@ -629,14 +619,9 @@ void OnDemandSnapshotClosure::Run() {res->status_code = status_code;res->body = response.dump();- deferred_req_res_t* req_res = new deferred_req_res_t{req, res, nullptr};- std::unique_ptr<deferred_req_res_t> req_res_guard(req_res);-- replication_state->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);+ deferred_req_res_t req_res{req, res, nullptr};+ replication_state->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, &req_res);// wait for response to be sent- req_res->res->await.wait();-- delete req;- delete res;+ res->wait();}diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex 4bccb5a1..ee405d83 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -263,7 +263,7 @@ int start_raft_server(ReplicationState& replication_state, const std::string& st// Wait until 'CTRL-C' is pressed. then Stop() and Join() the servicesize_t raft_counter = 0;- while (!quit_raft_service.load()) {+ while (!brpc::IsAskedToQuit() && !quit_raft_service.load()) {// post-increment to ensure that we refresh right away on a fresh bootif(raft_counter++ % 10 == 0) {// reset peer configuration periodically to identify change in cluster membership