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
Parameterize thread pool size and collection load configs.
kishorencbc0d01bb478d
diff --git a/TODO.md b/TODO.mdindex a4f13b61..fdbf335c 100644--- a/TODO.md+++ b/TODO.md@@ -1,13 +1,17 @@# Typesense: TODO-a) ~~Fix memory ratio (decreasing with indexing)~~-b) ~~Speed up wildcard searches further~~-c) ~~Allow int64 in default sorting field~~-d) ~~Use connection timeout for CURL rather than request timeout~~-e) Async import+- Writes on dedicated threads+- Single field aggregation short circuit+- Test for group + multiple fields**Search index**+- ~~Fix memory ratio (decreasing with indexing)~~+- ~~Speed up wildcard searches further~~+- ~~Allow int64 in default sorting field~~+- ~~Use connection timeout for CURL rather than request timeout~~+- ~~Async import~~+- ~~Highlight all matching fields~~- ~~Proper JSON as input~~- ~~Storing raw JSON input to RocksDB~~- ~~ART for every indexed field~~diff --git a/include/collection_manager.h b/include/collection_manager.hindex 99c4db4f..a1102d7d 100644--- a/include/collection_manager.h+++ b/include/collection_manager.h@@ -123,7 +123,7 @@ public:// only for tests!void init(Store *store, const float max_memory_ratio, const std::string & auth_key);- Option<bool> load(const size_t init_batch_size=1000);+ Option<bool> load(const size_t collection_batch_size, const size_t document_batch_size);// frees in-memory data structures when server is shutdown - helps us run a memory leak detector properlyvoid dispose();diff --git a/include/config.h b/include/config.hindex b9464060..3fdbf88c 100644--- a/include/config.h+++ b/include/config.h@@ -40,6 +40,11 @@ private:int log_slow_requests_time_ms;+ size_t num_collections_parallel_load;+ size_t num_documents_parallel_load;++ size_t thread_pool_size;+protected:Config() {@@ -52,6 +57,11 @@ protected:this->catch_up_min_sequence_diff = 3000;this->catch_up_threshold_percentage = 95;this->log_slow_requests_time_ms = -1;+ this->num_collections_parallel_load = 4;+ this->num_documents_parallel_load = 1000;++ // will be set dynamically if not overridden+ this->thread_pool_size = 0;}Config(Config const&) {@@ -193,6 +203,18 @@ public:return this->log_slow_requests_time_ms;}+ size_t get_num_collections_parallel_load() const {+ return this->num_collections_parallel_load;+ }++ size_t get_num_documents_parallel_load() const {+ return this->num_documents_parallel_load;+ }++ size_t get_thread_pool_size() const {+ return this->thread_pool_size;+ }+// loadersstd::string get_env(const char *name) {@@ -264,6 +286,18 @@ public:if(!get_env("TYPESENSE_LOG_SLOW_REQUESTS_TIME_MS").empty()) {this->log_slow_requests_time_ms = std::stoi(get_env("TYPESENSE_LOG_SLOW_REQUESTS_TIME_MS"));}++ if(!get_env("TYPESENSE_NUM_COLLECTIONS_PARALLEL_LOAD").empty()) {+ this->num_collections_parallel_load = std::stoi(get_env("TYPESENSE_NUM_COLLECTIONS_PARALLEL_LOAD"));+ }++ if(!get_env("TYPESENSE_NUM_DOCUMENTS_PARALLEL_LOAD").empty()) {+ this->num_documents_parallel_load = std::stoi(get_env("TYPESENSE_NUM_DOCUMENTS_PARALLEL_LOAD"));+ }++ if(!get_env("TYPESENSE_THREAD_POOL_SIZE").empty()) {+ this->thread_pool_size = std::stoi(get_env("TYPESENSE_THREAD_POOL_SIZE"));+ }}void load_config_file(cmdline::parser & options) {@@ -365,6 +399,18 @@ public:if(reader.Exists("server", "log-slow-requests-time-ms")) {this->log_slow_requests_time_ms = (int) reader.GetInteger("server", "log-slow-requests-time-ms", -1);}++ if(reader.Exists("server", "num-collections-parallel-load")) {+ this->num_collections_parallel_load = (int) reader.GetInteger("server", "num-collections-parallel-load", 4);+ }++ if(reader.Exists("server", "num-documents-parallel-load")) {+ this->num_documents_parallel_load = (int) reader.GetInteger("server", "num-documents-parallel-load", 1000);+ }++ if(reader.Exists("server", "thread-pool-size")) {+ this->thread_pool_size = (int) reader.GetInteger("server", "thread-pool-size", 0);+ }}void load_config_cmd_args(cmdline::parser & options) {@@ -448,6 +494,18 @@ public:if(options.exist("log-slow-requests-time-ms")) {this->log_slow_requests_time_ms = options.exist("log-slow-requests-time-ms");}++ if(options.exist("num-collections-parallel-load")) {+ this->num_collections_parallel_load = options.exist("num-collections-parallel-load");+ }++ if(options.exist("num-documents-parallel-load")) {+ this->num_documents_parallel_load = options.exist("num-documents-parallel-load");+ }++ if(options.exist("thread-pool-size")) {+ this->thread_pool_size = options.exist("thread-pool-size");+ }}// validationdiff --git a/include/raft_server.h b/include/raft_server.hindex c8ad0142..a6f43a91 100644--- a/include/raft_server.h+++ b/include/raft_server.h@@ -59,19 +59,6 @@ public:}};-// Closure that fires when initial snapshot operation finishes-class InitSnapshotClosure : public braft::Closure {-private:- ReplicationState* replication_state;-public:-- InitSnapshotClosure(ReplicationState* replication_state): replication_state(replication_state) {}-- ~InitSnapshotClosure() {}-- void Run();-};-// Closure that fires when requestedclass OnDemandSnapshotClosure : public braft::Closure {private:@@ -108,6 +95,10 @@ private:const size_t catchup_min_sequence_diff;const size_t catch_up_threshold_percentage;++ const size_t num_collections_parallel_load;+ const size_t num_documents_parallel_load;+std::atomic<bool> caught_up;const bool api_uses_ssl;@@ -132,7 +123,8 @@ public:static constexpr const char* snapshot_dir_name = "snapshot";ReplicationState(Store* store, ThreadPool* thread_pool, http_message_dispatcher* message_dispatcher,- bool api_uses_ssl, size_t catchup_min_sequence_diff, size_t catch_up_threshold_percentage);+ bool api_uses_ssl, size_t catchup_min_sequence_diff, size_t catch_up_threshold_percentage,+ size_t num_collections_parallel_load, size_t num_documents_parallel_load);// Starts this nodeint start(const butil::EndPoint & peering_endpoint, int api_port,diff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex ac3c2282..d5ae3675 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -94,7 +94,7 @@ void CollectionManager::init(Store *store, const float max_memory_ratio, const sinit(store, thread_pool, max_memory_ratio, auth_key);}-Option<bool> CollectionManager::load(const size_t init_batch_size) {+Option<bool> CollectionManager::load(const size_t collection_batch_size, const size_t document_batch_size) {// This function must be idempotent, i.e. when called multiple times, must produce the same state without leaksLOG(INFO) << "CollectionManager::load()";@@ -116,12 +116,14 @@ Option<bool> CollectionManager::load(const size_t init_batch_size) {next_collection_id = 0;}+ LOG(INFO) << "Loading " << collection_batch_size << " collections in parallel, "+ << document_batch_size << " documents at a time.";+std::vector<std::string> collection_meta_jsons;store->scan_fill(Collection::COLLECTION_META_PREFIX, collection_meta_jsons);LOG(INFO) << "Found " << collection_meta_jsons.size() << " collection(s) on disk.";- const size_t PROC_COUNT = std::max<size_t>(1, std::thread::hardware_concurrency());std::vector<nlohmann::json> coll_meta_buffer;for(size_t coll_index = 0; coll_index < collection_meta_jsons.size(); coll_index++) {@@ -134,7 +136,7 @@ Option<bool> CollectionManager::load(const size_t init_batch_size) {coll_meta_buffer.push_back(collection_meta);- if(coll_meta_buffer.size() % PROC_COUNT == 0 || coll_index == collection_meta_jsons.size() - 1) {+ if(coll_meta_buffer.size() % collection_batch_size == 0 || coll_index == collection_meta_jsons.size() - 1) {size_t num_processed = 0;std::mutex m_process;std::condition_variable cv_process;@@ -143,9 +145,9 @@ Option<bool> CollectionManager::load(const size_t init_batch_size) {LOG(INFO) << "coll_meta_buffer.size(): " << coll_meta_buffer.size();for(const auto& buff_coll_meta: coll_meta_buffer) {- thread_pool->enqueue([&buff_coll_meta, init_batch_size, &next_coll_id_status, &m_process, &cv_process,+ thread_pool->enqueue([&buff_coll_meta, document_batch_size, &next_coll_id_status, &m_process, &cv_process,&num_processed, &results]() {- Option<bool> res = load_collection(buff_coll_meta, init_batch_size, next_coll_id_status);+ Option<bool> res = load_collection(buff_coll_meta, document_batch_size, next_coll_id_status);std::unique_lock<std::mutex> lock(m_process);results.push_back(res);num_processed++;diff --git a/src/raft_server.cpp b/src/raft_server.cppindex cdd4fc82..fd06cf6d 100644--- a/src/raft_server.cpp+++ b/src/raft_server.cpp@@ -396,7 +396,9 @@ void ReplicationState::on_snapshot_save(braft::SnapshotWriter* writer, braft::Clint ReplicationState::init_db() {LOG(INFO) << "Loading collections from disk...";- Option<bool> init_op = CollectionManager::get_instance().load();+ Option<bool> init_op = CollectionManager::get_instance().load(+ num_collections_parallel_load, num_documents_parallel_load+ );if(init_op.ok()) {LOG(INFO) << "Finished loading collections from disk.";@@ -536,10 +538,13 @@ void ReplicationState::refresh_nodes(const std::string & nodes) {ReplicationState::ReplicationState(Store *store, ThreadPool* thread_pool, http_message_dispatcher *message_dispatcher,bool api_uses_ssl, size_t catchup_min_sequence_diff,- size_t catch_up_threshold_percentage):+ size_t catch_up_threshold_percentage,+ size_t num_collections_parallel_load, size_t num_documents_parallel_load):node(nullptr), leader_term(-1), store(store), thread_pool(thread_pool),message_dispatcher(message_dispatcher),catchup_min_sequence_diff(catchup_min_sequence_diff), catch_up_threshold_percentage(catch_up_threshold_percentage),+ num_collections_parallel_load(num_collections_parallel_load),+ num_documents_parallel_load(num_documents_parallel_load),api_uses_ssl(api_uses_ssl),ready(false), shutting_down(false), pending_writes(0) {@@ -656,19 +661,6 @@ void ReplicationState::shutdown() {}}-void InitSnapshotClosure::Run() {- // Auto delete this after Run()- std::unique_ptr<InitSnapshotClosure> self_guard(this);-- if(status().ok()) {- LOG(INFO) << "Init snapshot succeeded!";- replication_state->get_store()->reload(false, "");- replication_state->init_db();- } else {- LOG(ERROR) << "Init snapshot failed, error: " << status().error_str() << ", code: " << status().error_code();- }-}-void OnDemandSnapshotClosure::Run() {// Auto delete this after Done()std::unique_ptr<OnDemandSnapshotClosure> self_guard(this);diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex b5fdcc17..62603c1e 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -82,6 +82,11 @@ void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {options.add<int>("catch-up-threshold-percentage", '\0', "The threshold at which a follower is deemed to have caught up with leader.", false, 95);options.add<int>("log-slow-requests-time-ms", '\0', "When > 0, requests that take longer than this duration are logged.", false, -1);+ options.add<uint32_t>("num-collections-parallel-load", '\0', "Number of collections that are loaded in parallel during start up.", false, 4);+ options.add<uint32_t>("num-documents-parallel-load", '\0', "Number of documents per collection that are indexed in parallel during start up.", false, 1000);++ options.add<uint32_t>("thread-pool-size", '\0', "Number of threads used for handling concurrent requests.", false, 4);+options.add<std::string>("log-dir", '\0', "Path to the log directory.", false, "");options.add<std::string>("config", '\0', "Path to the configuration file.", false, "");@@ -339,9 +344,10 @@ int run_server(const Config & config, const std::string & version, void (*masterstd::string data_dir = config.get_data_dir();std::string db_dir = config.get_data_dir() + "/db";std::string state_dir = config.get_data_dir() + "/state";+ size_t thread_pool_size = config.get_thread_pool_size();const size_t proc_count = std::max<size_t>(1, std::thread::hardware_concurrency());- const size_t num_threads = std::max<size_t>(proc_count * 8, 16);+ const size_t num_threads = thread_pool_size == 0 ? std::max<size_t>(proc_count * 8, 16) : thread_pool_size;LOG(INFO) << "Thread pool size: " << num_threads;ThreadPool app_thread_pool(num_threads);@@ -378,7 +384,9 @@ int run_server(const Config & config, const std::string & version, void (*masterReplicationState replication_state(&store, &app_thread_pool, server->get_message_dispatcher(),ssl_enabled, config.get_catch_up_min_sequence_diff(),- config.get_catch_up_threshold_percentage());+ config.get_catch_up_threshold_percentage(),+ config.get_num_collections_parallel_load(),+ config.get_num_documents_parallel_load());std::thread raft_thread([&replication_state, &config, &state_dir, &app_thread_pool, &server_thread_pool]() {std::string path_to_nodes = config.get_nodes();diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cppindex 183c63f0..2e47d3e5 100644--- a/test/collection_all_fields_test.cpp+++ b/test/collection_all_fields_test.cpp@@ -21,7 +21,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);}virtual void SetUp() {diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cppindex 0204ea6d..ebf31ccf 100644--- a/test/collection_faceting_test.cpp+++ b/test/collection_faceting_test.cpp@@ -21,7 +21,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);}virtual void SetUp() {diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cppindex 2682c1aa..684a90c4 100644--- a/test/collection_filtering_test.cpp+++ b/test/collection_filtering_test.cpp@@ -22,7 +22,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);}virtual void SetUp() {diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cppindex c44f35d4..c226383d 100644--- a/test/collection_grouping_test.cpp+++ b/test/collection_grouping_test.cpp@@ -19,7 +19,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);std::vector<field> fields = {field("title", field_types::STRING, false),diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cppindex bec4bbcb..1669f137 100644--- a/test/collection_manager_test.cpp+++ b/test/collection_manager_test.cpp@@ -21,7 +21,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);search_fields = {field("title", field_types::STRING, false),@@ -239,7 +239,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {// create a new collection manager to ensure that it restores the records from the disk backed storeCollectionManager & collectionManager2 = CollectionManager::get_instance();collectionManager2.init(store, 1.0, "auth_key");- auto load_op = collectionManager2.load();+ auto load_op = collectionManager2.load(8, 1000);if(!load_op.ok()) {LOG(ERROR) << load_op.error();@@ -325,7 +325,7 @@ TEST_F(CollectionManagerTest, RestoreAutoSchemaDocsOnRestart) {// create a new collection manager to ensure that it restores the records from the disk backed storeCollectionManager & collectionManager2 = CollectionManager::get_instance();collectionManager2.init(store, 1.0, "auth_key");- auto load_op = collectionManager2.load();+ auto load_op = collectionManager2.load(8, 1000);if(!load_op.ok()) {LOG(ERROR) << load_op.error();@@ -434,7 +434,7 @@ TEST_F(CollectionManagerTest, Symlinking) {system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());Store *new_store = new Store(state_dir_path);cmanager.init(new_store, 1.0, "auth_key");- cmanager.load();+ cmanager.load(8, 1000);// try resolving on a blank slateOption<std::string> collection_option = cmanager.resolve_symlink("collection");@@ -509,7 +509,7 @@ TEST_F(CollectionManagerTest, Symlinking) {// should be able to restore state on initCollectionManager & cmanager2 = CollectionManager::get_instance();cmanager2.init(store, 1.0, "auth_key");- cmanager2.load();+ cmanager2.load(8, 1000);collection_option = cmanager2.resolve_symlink("company");ASSERT_TRUE(collection_option.ok());@@ -536,7 +536,7 @@ TEST_F(CollectionManagerTest, LoadMultipleCollections) {system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());Store *new_store = new Store(state_dir_path);cmanager.init(new_store, 1.0, "auth_key");- cmanager.load();+ cmanager.load(8, 1000);for(size_t i = 0; i < 100; i++) {auto schema = {@@ -558,7 +558,7 @@ TEST_F(CollectionManagerTest, LoadMultipleCollections) {new_store = new Store(state_dir_path);cmanager.init(new_store, 1.0, "auth_key");- cmanager.load();+ cmanager.load(8, 1000);ASSERT_EQ(100, cmanager.get_collections().size());diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cppindex 42c7b9d4..c7a556d4 100644--- a/test/collection_override_test.cpp+++ b/test/collection_override_test.cpp@@ -19,7 +19,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");std::vector<field> fields = {diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cppindex 99f4804c..b44b3771 100644--- a/test/collection_sorting_test.cpp+++ b/test/collection_sorting_test.cpp@@ -22,7 +22,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);}virtual void SetUp() {diff --git a/test/collection_synonyms_test.cpp b/test/collection_synonyms_test.cppindex 61b1c742..4b40cf6a 100644--- a/test/collection_synonyms_test.cpp+++ b/test/collection_synonyms_test.cpp@@ -19,7 +19,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");std::vector<field> fields = {diff --git a/test/collection_test.cpp b/test/collection_test.cppindex 1419bea3..38cb00d9 100644--- a/test/collection_test.cpp+++ b/test/collection_test.cpp@@ -24,7 +24,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);std::ifstream infile(std::string(ROOT_DIR)+"test/documents.jsonl");std::vector<field> search_fields = {diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cppindex ea1bfb42..6a2f863e 100644--- a/test/core_api_utils_test.cpp+++ b/test/core_api_utils_test.cpp@@ -19,7 +19,7 @@ protected:store = new Store(state_dir_path);collectionManager.init(store, 1.0, "auth_key");- collectionManager.load();+ collectionManager.load(8, 1000);}virtual void SetUp() {