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 replication init and improve logging.

kishorenc0a6f8947f3ef
diff --git a/include/config.h b/include/config.h
index c0c1b643..80e1cc2b 100644
--- a/include/config.h
+++ b/include/config.h
@@ -85,7 +85,7 @@ public:
// getters
- std::string get_data_dir() {
+ std::string get_data_dir() const {
return this->data_dir;
}
@@ -93,31 +93,31 @@ public:
return this->log_dir;
}
- std::string get_api_key() {
+ std::string get_api_key() const {
return this->api_key;
}
- std::string get_search_only_api_key() {
+ std::string get_search_only_api_key() const {
return this->search_only_api_key;
}
- std::string get_listen_address() {
+ std::string get_listen_address() const {
return this->listen_address;
}
- int get_listen_port() {
+ int get_listen_port() const {
return this->listen_port;
}
- std::string get_master() {
+ std::string get_master() const {
return this->master;
}
- std::string get_ssl_cert() {
+ std::string get_ssl_cert() const {
return this->ssl_certificate;
}
- std::string get_ssl_cert_key() {
+ std::string get_ssl_cert_key() const {
return this->ssl_certificate_key;
}
@@ -125,11 +125,11 @@ public:
return config_file;
}
- bool get_enable_cors() {
+ bool get_enable_cors() const {
return this->enable_cors;
}
- size_t get_indices_per_collection() {
+ size_t get_indices_per_collection() const {
return indices_per_collection;
}
diff --git a/include/http_client.h b/include/http_client.h
index 818fc50f..e7b0bb1a 100644
--- a/include/http_client.h
+++ b/include/http_client.h
@@ -12,9 +12,33 @@ private:
std::string url;
std::string api_key;
+ std::string ca_cert_path;
+
+ inline bool file_exists (const std::string & name) {
+ struct stat buffer;
+ return (stat (name.c_str(), &buffer) == 0);
+ }
+
public:
HttpClient(std::string url, std::string api_key): url(url), api_key(api_key) {
+ // try to locate ca cert file (from: https://serverfault.com/a/722646/117601)
+ std::vector<std::string> locations = {
+ "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
+ "/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6
+ "/etc/ssl/ca-bundle.pem", // OpenSUSE
+ "/etc/pki/tls/cacert.pem", // OpenELEC
+ "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
+ "/usr/local/etc/openssl/cert.pem", // OSX
+ };
+ ca_cert_path = "";
+
+ for(const std::string & location: locations) {
+ if(file_exists(location)) {
+ ca_cert_path = location;
+ break;
+ }
+ }
}
static size_t curl_write (void *contents, size_t size, size_t nmemb, std::string *s) {
@@ -24,6 +48,14 @@ public:
long get_reponse(std::string & response) {
CURL *curl = curl_easy_init();
+
+ if(!ca_cert_path.empty()) {
+ curl_easy_setopt(curl, CURLOPT_CAINFO, ca_cert_path.c_str());
+ } else {
+ LOG(ERR) << "Unable to locate system SSL certificates.";
+ return 0;
+ }
+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // to allow self-signed certs
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpClient::curl_write);
diff --git a/include/replicator.h b/include/replicator.h
index 7b6f27bf..020167f3 100644
--- a/include/replicator.h
+++ b/include/replicator.h
@@ -34,7 +34,7 @@ public:
class Replicator {
public:
- static void start(HttpServer* server, const std::string master_host_port, const std::string api_key, Store& store);
+ static void start(HttpServer* server, const std::string & master_host_port, const std::string & api_key, Store& store);
static void on_replication_event(void *data);
};
diff --git a/include/typesense_server_utils.h b/include/typesense_server_utils.h
index 44a645eb..2e40c518 100644
--- a/include/typesense_server_utils.h
+++ b/include/typesense_server_utils.h
@@ -23,4 +23,5 @@ void init_cmdline_options(cmdline::parser & options, int argc, char **argv);
int init_logger(Config & config, const std::string & server_version, std::unique_ptr<g3::LogWorker> & log_worker);
-int run_server(Config & config, const std::string & version, void (*master_server_routes)(), void (*replica_server_routes)());
\ No newline at end of file
+int run_server(const Config & config, const std::string & version,
+ void (*master_server_routes)(), void (*replica_server_routes)());
\ No newline at end of file
diff --git a/src/replicator.cpp b/src/replicator.cpp
index 69ff52cc..65ae2447 100644
--- a/src/replicator.cpp
+++ b/src/replicator.cpp
@@ -77,7 +77,8 @@ void IterateBatchHandler::Merge(const rocksdb::Slice& key, const rocksdb::Slice&
}
}
-void Replicator::start(HttpServer* server, const std::string master_host_port, const std::string api_key, Store& store) {
+void Replicator::start(HttpServer* server, const std::string & master_host_port,
+ const std::string & api_key, Store& store) {
size_t total_runs = 0;
while(true) {
@@ -89,9 +90,8 @@ void Replicator::start(HttpServer* server, const std::string master_host_port, c
LOG(INFO) << "Replica's latest sequence number: " << latest_seq_num;
}
- HttpClient client(
- master_host_port+"/replication/updates?seq_number="+std::to_string(latest_seq_num+1), api_key
- );
+ std::string url = master_host_port+"/replication/updates?seq_number="+std::to_string(latest_seq_num+1);
+ HttpClient client(url, api_key);
std::string json_response;
long status_code = client.get_reponse(json_response);
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 61ebc02c..9aab8252 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -70,7 +70,7 @@ int init_logger(Config & config, const std::string & server_version, std::unique
return 0;
}
-int run_server(Config & config, const std::string & version,
+int run_server(const Config & config, const std::string & version,
void (*master_server_routes)(), void (*replica_server_routes)()) {
LOG(INFO) << "Starting Typesense " << version << std::flush;
@@ -127,9 +127,11 @@ int run_server(Config & config, const std::string & version,
return 1;
}
- LOG(INFO) << "Typesense is starting as a read-only replica... Spawning replication thread...";
- std::thread replication_thread([&master_host_port, &store, &config]() {
- Replicator::start(::server, master_host_port, config.get_api_key(), store);
+ LOG(INFO) << "Typesense is starting as a read-only replica... Master URL is: " << master_host_port;
+ LOG(INFO) << "Spawning replication thread...";
+
+ std::thread replication_thread([&store, &config]() {
+ Replicator::start(::server, config.get_master(), config.get_api_key(), store);
});
replication_thread.detach();