Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Refactor deferring of batched writes during import.

Kishore Nallan6b793afbae0e
diff --git a/include/http_data.h b/include/http_data.h
index 536e0976..4de27590 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -231,6 +231,8 @@ struct http_req {
~http_req() {
+ //LOG(INFO) << "~http_req " << this;
+
if(!deserialized_request) {
uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
diff --git a/include/raft_server.h b/include/raft_server.h
index 7a0cb7de..5220c86d 100644
--- a/include/raft_server.h
+++ b/include/raft_server.h
@@ -27,13 +27,15 @@ public:
}
- ~ReplicationClosure() {}
+ ~ReplicationClosure() {
+ //LOG(INFO) << "~ReplicationClosure req use count " << request.use_count();
+ }
- const std::shared_ptr<http_req> get_request() const {
+ const std::shared_ptr<http_req>& get_request() const {
return request;
}
- const std::shared_ptr<http_res> get_response() const {
+ const std::shared_ptr<http_res>& get_response() const {
return response;
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 7dbff5be..c032b61c 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -34,6 +34,7 @@ void stream_response(const std::shared_ptr<http_req>& req, const std::shared_ptr
void defer_processing(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res, size_t timeout_ms) {
defer_processing_t* defer = new defer_processing_t(req, res, timeout_ms, server);
+ //LOG(INFO) << "core_api req " << req.get() << ", use count: " << req.use_count();
server->get_message_dispatcher()->send_message(HttpServer::DEFER_PROCESSING_MESSAGE, defer);
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index c20d6b37..8c635711 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -241,7 +241,7 @@ uint64_t HttpServer::find_route(const std::vector<std::string> & path_parts, con
void HttpServer::on_res_generator_dispose(void *self) {
h2o_custom_generator_t* custom_generator = *static_cast<h2o_custom_generator_t**>(self);
- //LOG(INFO) << "on_res_generator_dispose fires " << custom_generator->res().get();
+ //LOG(INFO) << "on_res_generator_dispose fires, req use count " << custom_generator->req().use_count();
destroy_request_response(custom_generator->req(), custom_generator->res());
/*LOG(INFO) << "Deleting custom_generator, res: " << custom_generator->res();
@@ -524,27 +524,44 @@ int HttpServer::process_request(const std::shared_ptr<http_req>& request, const
void HttpServer::on_deferred_process_request(h2o_timer_t *entry) {
h2o_custom_timer_t* custom_timer = reinterpret_cast<h2o_custom_timer_t*>(entry);
deferred_req_res_t* deferred_req_res = static_cast<deferred_req_res_t*>(custom_timer->data);
- //LOG(INFO) << "on_deferred_process_request " << deferred_req_res->res.get();
+ //LOG(INFO) << "on_deferred_process_request " << deferred_req_res->req.get();
route_path* found_rpath = nullptr;
deferred_req_res->server->get_route(deferred_req_res->req->route_hash, &found_rpath);
+
+ const std::shared_ptr<http_req> request = deferred_req_res->req;
+ const std::shared_ptr<http_res> response = deferred_req_res->res;
+ HttpServer* server = deferred_req_res->server;
+
+ // done with timer, so we can clear timer and data
+ h2o_timer_unlink(&deferred_req_res->req->defer_timer.timer);
+ delete deferred_req_res;
+ request->defer_timer.data = nullptr;
+
if(found_rpath) {
// must be called on a separate thread so as not to block http thread
- deferred_req_res->server->thread_pool->enqueue([found_rpath, deferred_req_res]() {
- found_rpath->handler(deferred_req_res->req, deferred_req_res->res);
+ server->thread_pool->enqueue([found_rpath, request, response]() {
+ //LOG(INFO) << "Sleeping for 5s req count " << deferred_req_res->req.use_count();
+ //std::this_thread::sleep_for(std::chrono::seconds(5));
+ //LOG(INFO) << "on_deferred_process_request, calling handler, req use count " << request.use_count();
+ found_rpath->handler(request, response);
});
}
}
void HttpServer::defer_processing(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res,
size_t timeout_ms) {
- //LOG(INFO) << "defer_processing, exit_loop: " << exit_loop << ", res: " << res.get();
+ //LOG(INFO) << "defer_processing, exit_loop: " << exit_loop << ", req: " << req.get() << ", use count: " << req.use_count();
if(req->defer_timer.data == nullptr) {
+ //LOG(INFO) << "req->defer_timer.data is null";
auto deferred_req_res = new deferred_req_res_t(req, res, this);
+ //LOG(INFO) << "req use count " << req.use_count();
req->defer_timer.data = deferred_req_res;
h2o_timer_init(&req->defer_timer.timer, on_deferred_process_request);
} else {
+ // This should not happen as data is cleared when defer handler is run
+ LOG(ERROR) << "HttpServer::defer_processing, timer data is NOT null";
h2o_timer_unlink(&req->defer_timer.timer);
}
@@ -686,19 +703,15 @@ void HttpServer::stream_response(const std::shared_ptr<http_req>& request, const
void HttpServer::destroy_request_response(const std::shared_ptr<http_req>& request,
const std::shared_ptr<http_res>& response) {
- //LOG(INFO) << "destroy_request_response " << response.get();
-
- 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);
- delete deferred_req_res;
- }
+ //LOG(INFO) << "destroy_request_response, req use count: " << request.use_count() << " req " << request.get();
/*LOG(INFO) << "destroy_request_response, response->proxied_stream=" << response->proxied_stream
<< ", request->_req=" << request->_req << ", response->await=" << &response->await;*/
//LOG(INFO) << "destroy_request_response, response: " << response << ", response->auto_dispose: " << response->auto_dispose;
+ //LOG(INFO) << "after destroy_request_response, req use count: " << request.use_count() << " req " << request.get();
+
request->_req = nullptr;
response->final = true;
@@ -772,7 +785,9 @@ HttpServer::~HttpServer() {
ctx.globalconf->server_name.base = nullptr;
}
- h2o_evloop_destroy(ctx.loop);
+ // Flaky, sometimes assertion on timeouts occur, preventing a clean shutdown
+ //h2o_evloop_destroy(ctx.loop);
+
h2o_config_dispose(&config);
SSL_CTX_free(accept_ctx->ssl_ctx);
@@ -812,6 +827,7 @@ bool HttpServer::on_stream_response_message(void *data) {
stream_response(req_res->req, req_res->res);
if(req_res->destroy_after_stream_response) {
+ //LOG(INFO) << "delete req_res";
delete req_res;
}
@@ -836,7 +852,9 @@ bool HttpServer::on_request_proceed_message(void *data) {
bool HttpServer::on_deferred_processing_message(void *data) {
//LOG(INFO) << "on_deferred_processing_message";
defer_processing_t* defer = static_cast<defer_processing_t *>(data);
+ //LOG(INFO) << "defer req count: " << defer->req.use_count();
defer->server->defer_processing(defer->req, defer->res, defer->timeout_ms);
+ //LOG(INFO) << "req use count: " << defer->req.use_count() << ", req " << defer->req.get();
delete defer;
return true;
}
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 23b40a9a..e2a68172 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -139,6 +139,8 @@ void ReplicationState::write(const std::shared_ptr<http_req>& request, const std
butil::IOBufBuilder bufBuilder;
bufBuilder << request->serialize();
+ //LOG(INFO) << "write() pre request ref count " << request.use_count();
+
// Apply this log as a braft::Task
braft::Task task;
@@ -146,6 +148,8 @@ void ReplicationState::write(const std::shared_ptr<http_req>& request, const std
// This callback would be invoked when the task actually executes or fails
task.done = new ReplicationClosure(request, response);
+ //LOG(INFO) << "write() post request ref count " << request.use_count();
+
// To avoid ABA problem
task.expected_term = leader_term.load(butil::memory_order_relaxed);
@@ -258,33 +262,26 @@ std::string ReplicationState::get_leader_url_path(const std::string& leader_addr
void ReplicationState::on_apply(braft::Iterator& iter) {
//LOG(INFO) << "ReplicationState::on_apply";
-
// NOTE: this is executed on a different thread and runs concurrent to http thread
// A batch of tasks are committed, which must be processed through
// |iter|
for (; iter.valid(); iter.next()) {
// Guard invokes replication_arg->done->Run() asynchronously to avoid the callback blocking the main thread
braft::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());
- 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
+
+ //LOG(INFO) << "Init use count: " << dynamic_cast<ReplicationClosure*>(iter.done())->get_request().use_count();
+
+ const std::shared_ptr<http_req>& request_generated = iter.done() ?
+ dynamic_cast<ReplicationClosure*>(iter.done())->get_request() : std::make_shared<http_req>();
+
+ //LOG(INFO) << "Post assignment " << request_generated.get() << ", use count: " << request_generated.use_count();
+
+ const std::shared_ptr<http_res>& response_generated = iter.done() ?
+ dynamic_cast<ReplicationClosure*>(iter.done())->get_response() : std::make_shared<http_res>();
+
+ if(!iter.done()) {
+ // indicates log serialized request
request_generated->deserialize(iter.data().to_string());
- //LOG(INFO) << "Parsed request from the log, body_size: " << request->body.size();
-
- /*
- const std::string &data_str = iter.data().to_string();
- uint64_t hash = StringUtils::hash_wy(data_str.c_str(), data_str.size());
- if(hash == 12066143973577129900) {
- LOG(INFO) << "get stuck here...";
- }*/
}
// Now that the log has been parsed, perform the actual operation
@@ -296,6 +293,8 @@ void ReplicationState::on_apply(braft::Iterator& iter) {
route_path* found_rpath = nullptr;
bool route_found = server->get_route(request_generated->route_hash, &found_rpath);
+ //LOG(INFO) << "Pre handler " << request_generated.get() << ", use count: " << request_generated.use_count();
+
if(route_found) {
async_res = found_rpath->async_res;
found_rpath->handler(request_generated, response_generated);
@@ -303,14 +302,18 @@ void ReplicationState::on_apply(braft::Iterator& iter) {
response_generated->set_404();
}
+ //LOG(INFO) << "Pre dispatch " << request_generated.get() << ", use count: " << request_generated.use_count();
+
if(!async_res) {
deferred_req_res_t* req_res = new deferred_req_res_t(request_generated, response_generated, server, true);
message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
}
- //LOG(INFO) << "Raft write pre wait " << response_generated.get();
+ //LOG(INFO) << "Raft write pre wait " << request_generated.get() << ", use count: " << request_generated.use_count();
+
response_generated->wait();
- //LOG(INFO) << "Raft write post wait " << response_generated.get();
+
+ //LOG(INFO) << "Raft write post wait " << request_generated.get() << ", use count: " << request_generated.use_count();
if(iter.done()) {
pending_writes--;