Skip to content

Commit

Permalink
Log in-flight search queries during a crash.
Browse files Browse the repository at this point in the history
kishorenc committed May 20, 2024
1 parent 468f071 commit aab8188
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/core_api.h
Original file line number Diff line number Diff line change
@@ -178,6 +178,8 @@ void get_collections_for_auth(std::map<std::string, std::string>& req_params, co
std::vector<collection_key_t>& collections,
std::vector<nlohmann::json>& embedded_params_vec);

void log_running_queries();

bool is_doc_import_route(uint64_t route_hash);

bool is_coll_create_route(uint64_t route_hash);
50 changes: 49 additions & 1 deletion src/core_api.cpp
Original file line number Diff line number Diff line change
@@ -27,6 +27,27 @@ LRU::Cache<uint64_t, cached_res_t> res_cache;

std::atomic<bool> alter_in_progress = false;

// used to log the queries that were in-flight during a crash
std::mutex ifq_mutex;
std::unordered_map<uint64_t, std::shared_ptr<http_req>> in_flight_queries;

class in_flight_req_guard_t {
uint64_t req_id;
public:
in_flight_req_guard_t(const std::shared_ptr<http_req>& req) {
std::unique_lock ifq_lock(ifq_mutex);
in_flight_queries.emplace(req->start_ts, req);
req_id = req->start_ts;
ifq_lock.unlock();
}

~in_flight_req_guard_t() {
std::unique_lock ifq_lock(ifq_mutex);
in_flight_queries.erase(req_id);
ifq_lock.unlock();
}
};

void init_api(uint32_t cache_num_entries) {
res_cache.capacity(cache_num_entries);
}
@@ -39,6 +60,29 @@ bool get_alter_in_progress() {
return alter_in_progress;
}

void log_running_queries() {
std::unique_lock ifq_lock(ifq_mutex);
if(in_flight_queries.empty()) {
LOG(ERROR) << "No in-flight search queries were found.";
return ;
}

LOG(ERROR) << "Dump of in-flight search queries:";

for(const auto& kv: in_flight_queries) {
std::string query_string = "?";
std::string search_payload = kv.second->body;
StringUtils::erase_char(search_payload, '\n');
for(const auto& param_kv: kv.second->params) {
if(param_kv.first != http_req::AUTH_HEADER && param_kv.first != http_req::USER_HEADER) {
query_string += param_kv.first + "=" + param_kv.second + "&";
}
}

LOG(ERROR) << "id=" << kv.first << ", qs=" << query_string << ", body=" << search_payload;
}
}

bool handle_authentication(std::map<std::string, std::string>& req_params,
std::vector<nlohmann::json>& embedded_params_vec,
const std::string& body,
@@ -145,7 +189,7 @@ void get_collections_for_auth(std::map<std::string, std::string>& req_params,
}
}
} else {
LOG(ERROR) << "Multi search request body is malformed, body: " << body;
//LOG(ERROR) << "Multi search request body is malformed, body: " << body;
}
} else {
if(rpath.handler == post_create_collection) {
@@ -440,6 +484,8 @@ bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
bool use_cache = (use_cache_it != req->params.end()) && (use_cache_it->second == "1" || use_cache_it->second == "true");
uint64_t req_hash = 0;

in_flight_req_guard_t in_flight_req_guard(req);

if(use_cache) {
// cache enabled, let's check if request is already in the cache
req_hash = hash_request(req);
@@ -511,6 +557,8 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
bool use_cache = (use_cache_it != req->params.end()) && (use_cache_it->second == "1" || use_cache_it->second == "true");
uint64_t req_hash = 0;

in_flight_req_guard_t in_flight_req_guard(req);

if(use_cache) {
// cache enabled, let's check if request is already in the cache
req_hash = hash_request(req);
1 change: 1 addition & 0 deletions src/main/typesense_server.cpp
Original file line number Diff line number Diff line change
@@ -135,6 +135,7 @@ void crash_callback(int sig, backward::StackTrace& st) {
}
}

log_running_queries();
LOG(ERROR) << "Typesense " << TYPESENSE_VERSION << " is terminating abruptly.";
}

0 comments on commit aab8188

Please sign in to comment.