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
Reject writes to followers.
kishorenc1cd9793d698a
diff --git a/include/http_data.h b/include/http_data.hindex b278b3d8..0b03a68e 100644--- a/include/http_data.h+++ b/include/http_data.h@@ -66,6 +66,11 @@ struct http_res {body = "{\"message\": \"Not Found\"}";}+ void send_405(const std::string & message) {+ status_code = 405;+ body = "{\"message\": \"" + message + "\"}";+ }+void send_409(const std::string & message) {status_code = 409;body = "{\"message\": \"" + message + "\"}";@@ -87,6 +92,11 @@ struct http_res {}};+enum class ROUTE_CODES {+ NOT_FOUND = -1,+ RETURN_EARLY = -2,+};+struct http_req {h2o_req_t* _req;std::string http_method;diff --git a/include/raft_server.h b/include/raft_server.hindex abdd3855..a9b8c895 100644--- a/include/raft_server.h+++ b/include/raft_server.h@@ -107,7 +107,7 @@ public:// Generic write method for synchronizing all writesvoid write(http_req* request, http_res* response);- // Generic read method for consistent reads+ // Generic read method for consistent reads, not used for nowvoid read(http_res* response);// updates cluster membership@@ -141,9 +141,6 @@ private:friend class ReplicationClosure;- // redirecting request to leader- void redirect(http_res* response);-// actual application of writes onto the WALvoid on_apply(braft::Iterator& iter);@@ -164,7 +161,7 @@ private:// have to do a dummy write, otherwise snapshot will not triggerif(create_init_db_snapshot) {- http_req* request = new http_req(nullptr, "PRIVATE", 0, {}, "INIT_SNAPSHOT");+ http_req* request = new http_req(nullptr, "", 0, {}, "INIT_SNAPSHOT");http_res* response = new http_res();write(request, response);}diff --git a/src/core_api.cpp b/src/core_api.cppindex c88c90a7..16ad9448 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -610,11 +610,17 @@ bool get_replication_updates(http_req & req, http_res & res) {bool async_write_request(void *data) {LOG(INFO) << "async_write_request called";+AsyncIndexArg* index_arg = static_cast<AsyncIndexArg*>(data);std::unique_ptr<AsyncIndexArg> index_arg_guard(index_arg);- if(index_arg->req->route_index == -1) {+ if(index_arg->req->route_index == static_cast<int>(ROUTE_CODES::NOT_FOUND)) {+ // route not foundreturn false;+ } else if(index_arg->req->route_index == static_cast<int>(ROUTE_CODES::RETURN_EARLY)) {+ // respond without calling internal route+ server->send_response(index_arg->req, index_arg->res);+ return true;}route_path* found_rpath;@@ -628,6 +634,9 @@ bool async_write_request(void *data) {server->send_response(index_arg->req, index_arg->res);}- index_arg->promise->set_value(true); // returns control back to caller+ if(index_arg->promise != nullptr) {+ index_arg->promise->set_value(true); // returns control back to caller+ }+return true;}diff --git a/src/http_server.cpp b/src/http_server.cppindex 9a9bd90f..01855e0d 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -232,7 +232,7 @@ int HttpServer::find_route(const std::vector<std::string> & path_parts, const st}}- return -1;+ return static_cast<int>(ROUTE_CODES::NOT_FOUND);}int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {diff --git a/src/raft_server.cpp b/src/raft_server.cppindex e3bac75b..36cfe45f 100644--- a/src/raft_server.cpp+++ b/src/raft_server.cpp@@ -9,16 +9,9 @@void ReplicationClosure::Run() {+ // nothing much to do here since responding to client is handled upstream// Auto delete `this` after Run()std::unique_ptr<ReplicationClosure> self_guard(this);-- // Respond to this RPC.- if (status().ok()) {- return;- }-- // Try redirect if this request failed.- // TODO: searchStore->redirect(response);}// State machine implementation@@ -96,8 +89,13 @@ int ReplicationState::start(int port, int election_timeout_ms, int snapshot_intevoid ReplicationState::write(http_req* request, http_res* response) {if (!is_leader()) {- LOG(INFO) << "Write called on a follower.";- // TODO: return redirect(response);+ LOG(INFO) << "Rejecting write sent to follower.";++ response->send_405("Not the leader.");+ auto replication_arg = new AsyncIndexArg{request, response, nullptr};+ replication_arg->req->route_index = static_cast<int>(ROUTE_CODES::RETURN_EARLY);+ message_dispatcher->send_message(REPLICATION_MSG, replication_arg);+ return ;}// Serialize request to replicated WAL so that all the peers in the group receive it as well.@@ -121,25 +119,9 @@ void ReplicationState::write(http_req* request, http_res* response) {}void ReplicationState::read(http_res* response) {- // For consistency, reads to follower must be rejected.- if (!is_leader()) {- // This node is a follower or it's not up-to-date. Redirect to- // the leader if possible.- return redirect(response);- }-- // This is the leader and is up-to-date. It's safe to respond client- // TODO:- // response->set_value(_value.load(butil::memory_order_relaxed));-}--void ReplicationState::redirect(http_res* response) {- if (node) {- braft::PeerId leader = node->leader_id();- if (!leader.is_empty()) {- // TODO: response->set_redirect(leader.to_string());- }- }+ // NOT USED:+ // For consistency, reads to followers could be rejected.+ // Currently, we don't do implement reads via raft.}void ReplicationState::on_apply(braft::Iterator& iter) {@@ -167,7 +149,7 @@ void ReplicationState::on_apply(braft::Iterator& iter) {request = remote_request;}- if(request->http_method == "PRIVATE" && request->body == "INIT_SNAPSHOT") {+ if(request->_req == nullptr && request->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);diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex 5a5c733d..54b8c884 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -44,7 +44,6 @@ Option<std::string> fetch_file_contents(const std::string & file_path) {return Option<std::string>(404, "Error reading file containing raft peers.");}- std::string contents;std::ifstream infile(file_path);std::string content((std::istreambuf_iterator<char>(infile)), (std::istreambuf_iterator<char>()));infile.close();@@ -182,11 +181,14 @@ int start_raft_server(ReplicationState& replication_state, const std::string& stif(!peers_op.ok()) {LOG(ERROR) << peers_op.error();exit(-1);- } else if(peer_ips_string.empty()) {- LOG(ERROR) << "File containing raft peers is empty.";- exit(-1);} else {peer_ips_string = peers_op.get();+ if(peer_ips_string.empty()) {+ LOG(ERROR) << "File containing raft peers is empty.";+ exit(-1);+ } else {+ peer_ips_string = peers_op.get();+ }}}