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

Use condition variable for checking on queue.

Kishore Nallan02c63b443c71
diff --git a/include/batched_indexer.h b/include/batched_indexer.h
index 890bcd12..0aa5530e 100644
--- a/include/batched_indexer.h
+++ b/include/batched_indexer.h
@@ -32,12 +32,17 @@ private:
next_chunk_index(0), is_complete(false) {};
};
+ struct await_t {
+ std::mutex mcv;
+ std::condition_variable cv;
+ };
+
HttpServer* server;
Store* store;
const size_t num_threads;
- std::mutex* qmutuxes;
+ await_t* qmutuxes;
std::vector<std::deque<uint64_t>> queues;
/* Variables to be serialized on snapshot /
diff --git a/src/batched_indexer.cpp b/src/batched_indexer.cpp
index 89be93b9..af766113 100644
--- a/src/batched_indexer.cpp
+++ b/src/batched_indexer.cpp
@@ -6,7 +6,7 @@ BatchedIndexer::BatchedIndexer(HttpServer* server, Store* store, const size_t nu
server(server), store(store), num_threads(num_threads),
last_gc_run(std::chrono::high_resolution_clock::now()), quit(false) {
queues.resize(num_threads);
- qmutuxes = new std::mutex[num_threads];
+ qmutuxes = new await_t[num_threads];
}
void BatchedIndexer::enqueue(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
@@ -53,8 +53,13 @@ void BatchedIndexer::enqueue(const std::shared_ptr<http_req>& req, const std::sh
{
const std::string& coll_name = get_collection_name(req);
uint64_t queue_id = StringUtils::hash_wy(coll_name.c_str(), coll_name.size()) % num_threads;
- std::unique_lock lk1(qmutuxes[queue_id]);
- queues[queue_id].emplace_back(req->start_ts);
+
+ {
+ std::unique_lock lk1(qmutuxes[queue_id].mcv);
+ queues[queue_id].emplace_back(req->start_ts);
+ }
+
+ qmutuxes[queue_id].cv.notify_one();
std::unique_lock lk2(mutex);
req_res_map[req->start_ts].is_complete = true;
@@ -113,101 +118,100 @@ void BatchedIndexer::run() {
for(size_t i = 0; i < num_threads; i++) {
std::deque<uint64_t>& queue = queues[i];
- std::mutex& queue_mutex = qmutuxes[i];
+ await_t& queue_mutex = qmutuxes[i];
thread_pool->enqueue([&queue, &queue_mutex, this, i]() {
while(!quit) {
- std::unique_lock<std::mutex> qlk(queue_mutex);
-
- if(queue.empty()) {
- qlk.unlock();
- } else {
- uint64_t req_id = queue.front();
- queue.pop_front();
- qlk.unlock();
-
- uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(
- std::chrono::system_clock::now().time_since_epoch()).count();
-
- std::unique_lock mlk(mutex);
- auto req_res_map_it = req_res_map.find(req_id);
- if(req_res_map_it == req_res_map.end()) {
- LOG(ERROR) << "Req ID " << req_id << " not found in req_res_map.";
- continue;
- }
+ std::unique_lock<std::mutex> qlk(queue_mutex.mcv);
+ queue_mutex.cv.wait(qlk, [&] { return quit || !queue.empty(); });
- req_res_map_it->second.last_updated = now;
- req_res_t orig_req_res = req_res_map_it->second; // copy for ref counting req + res
- mlk.unlock();
+ if(quit) {
+ break;
+ }
- // scan db for all logs associated with request
- const std::string& req_key_prefix = get_req_prefix_key(req_id);
+ uint64_t req_id = queue.front();
+ queue.pop_front();
+ qlk.unlock();
- const std::string& req_key_start_prefix = get_req_prefix_key(req_id) +
- StringUtils::serialize_uint32_t(orig_req_res.next_chunk_index);
+ uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
- rocksdb::Iterator* iter = store->scan(req_key_start_prefix);
+ std::unique_lock mlk(mutex);
+ auto req_res_map_it = req_res_map.find(req_id);
+ if(req_res_map_it == req_res_map.end()) {
+ LOG(ERROR) << "Req ID " << req_id << " not found in req_res_map.";
+ continue;
+ }
- // used to handle partial JSON documents caused by chunking
- std::string& prev_body = orig_req_res.prev_req_body;
+ req_res_map_it->second.last_updated = now;
+ req_res_t orig_req_res = req_res_map_it->second; // copy for ref counting req + res
+ mlk.unlock();
- const std::shared_ptr<http_req>& orig_req = orig_req_res.req;
- const std::shared_ptr<http_res>& orig_res = orig_req_res.res;
- bool is_live_req = orig_res->is_alive;
+ // scan db for all logs associated with request
+ const std::string& req_key_prefix = get_req_prefix_key(req_id);
- route_path* found_rpath = nullptr;
- bool route_found = server->get_route(orig_req->route_hash, &found_rpath);
- bool async_res = false;
+ const std::string& req_key_start_prefix = get_req_prefix_key(req_id) +
+ StringUtils::serialize_uint32_t(orig_req_res.next_chunk_index);
- while(iter->Valid() && iter->key().starts_with(req_key_prefix)) {
- std::shared_lock slk(pause_mutex); // used for snapshot
- orig_req->body = prev_body;
- orig_req->load_from_json(iter->value().ToString());
+ rocksdb::Iterator* iter = store->scan(req_key_start_prefix);
- // update thread local for reference during a crash
- write_log_index = orig_req->log_index;
+ // used to handle partial JSON documents caused by chunking
+ std::string& prev_body = orig_req_res.prev_req_body;
- //LOG(INFO) << "original request: " << orig_req_res.req << ", req: " << orig_req_res.req->req;
+ const std::shared_ptr<http_req>& orig_req = orig_req_res.req;
+ const std::shared_ptr<http_res>& orig_res = orig_req_res.res;
+ bool is_live_req = orig_res->is_alive;
- if(route_found) {
- async_res = found_rpath->async_res;
- found_rpath->handler(orig_req, orig_res);
- prev_body = orig_req->body;
- } else {
- orig_res->set_404();
- }
+ route_path* found_rpath = nullptr;
+ bool route_found = server->get_route(orig_req->route_hash, &found_rpath);
+ bool async_res = false;
- if(is_live_req && (!route_found ||!async_res)) {
- // sync request get a response immediately
- async_req_res_t* async_req_res = new async_req_res_t(orig_req, orig_res, true);
- server->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, async_req_res);
- }
+ while(iter->Valid() && iter->key().starts_with(req_key_prefix)) {
+ std::shared_lock slk(pause_mutex); // used for snapshot
+ orig_req->body = prev_body;
+ orig_req->load_from_json(iter->value().ToString());
- if(!route_found) {
- break;
- }
+ // update thread local for reference during a crash
+ write_log_index = orig_req->log_index;
- queued_writes--;
- orig_req_res.next_chunk_index++;
- iter->Next();
+ //LOG(INFO) << "original request: " << orig_req_res.req << ", req: " << orig_req_res.req->req;
- if(quit) {
- break;
- }
+ if(route_found) {
+ async_res = found_rpath->async_res;
+ found_rpath->handler(orig_req, orig_res);
+ prev_body = orig_req->body;
+ } else {
+ orig_res->set_404();
}
- delete iter;
+ if(is_live_req && (!route_found ||!async_res)) {
+ // sync request get a response immediately
+ async_req_res_t* async_req_res = new async_req_res_t(orig_req, orig_res, true);
+ server->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, async_req_res);
+ }
- //LOG(INFO) << "Erasing request data from disk and memory for request " << orig_req_res.req->start_ts;
+ if(!route_found) {
+ break;
+ }
- // we can delete the buffered request content
- store->delete_range(req_key_prefix, req_key_prefix + StringUtils::serialize_uint32_t(UINT32_MAX));
+ queued_writes--;
+ orig_req_res.next_chunk_index++;
+ iter->Next();
- std::unique_lock lk(mutex);
- req_res_map.erase(req_id);
+ if(quit) {
+ break;
+ }
}
- std::this_thread::sleep_for(std::chrono::milliseconds (10));
+ delete iter;
+
+ //LOG(INFO) << "Erasing request data from disk and memory for request " << orig_req_res.req->start_ts;
+
+ // we can delete the buffered request content
+ store->delete_range(req_key_prefix, req_key_prefix + StringUtils::serialize_uint32_t(UINT32_MAX));
+
+ std::unique_lock lk(mutex);
+ req_res_map.erase(req_id);
}
});
}
@@ -250,6 +254,14 @@ void BatchedIndexer::run() {
}
}
+ LOG(INFO) << "Notifying batch indexer threads about shutdown...";
+ for(size_t i = 0; i < num_threads; i++) {
+ await_t& queue_mutex = qmutuxes[i];
+ std::lock_guard<std::mutex> lk(queue_mutex.mcv);
+ queue_mutex.cv.notify_one();
+ }
+
+
LOG(INFO) << "Batched indexer threadpool shutdown...";
thread_pool->shutdown();
delete thread_pool;
@@ -332,7 +344,7 @@ void BatchedIndexer::load_state(const nlohmann::json& state) {
const std::string& coll_name = get_collection_name(req);
uint64_t queue_id = StringUtils::hash_wy(coll_name.c_str(), coll_name.size()) % num_threads;
queue_ids.push_back(queue_id);
- std::unique_lock qlk(qmutuxes[queue_id]);
+ std::unique_lock qlk(qmutuxes[queue_id].mcv);
queues[queue_id].emplace_back(req->start_ts);
}
@@ -341,7 +353,7 @@ void BatchedIndexer::load_state(const nlohmann::json& state) {
// need to sort on `start_ts` to preserve original order
for(auto queue_id: queue_ids) {
- std::unique_lock lk(qmutuxes[queue_id]);
+ std::unique_lock lk(qmutuxes[queue_id].mcv);
std::sort(queues[queue_id].begin(), queues[queue_id].end());
}