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

Exit cleanly from raft service on validation errors.

kishorencdf08df5d96e7
diff --git a/include/string_utils.h b/include/string_utils.h
index 8472f42a..4c0641dd 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -48,7 +48,7 @@ struct StringUtils {
static std::string join(std::vector<std::string> vec, std::string delimiter, size_t start_index = 0) {
std::stringstream ss;
- for(size_t i = start_index; i <= vec.size()-1; i++) {
+ for(size_t i = start_index; i < vec.size(); i++) {
if(i != start_index) {
ss << delimiter;
}
diff --git a/src/file_utils.cpp b/src/file_utils.cpp
index 82035c08..eec1c9db 100644
--- a/src/file_utils.cpp
+++ b/src/file_utils.cpp
@@ -12,7 +12,7 @@ bool directory_exists(const std::string & dir_path) {
bool file_exists(const std::string & file_path) {
struct stat info;
- return stat(file_path.c_str(), &info) == 0;
+ return stat(file_path.c_str(), &info) == 0 && !(info.st_mode & S_IFDIR);
}
// tries to hard link first
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index a960b434..385258b6 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -31,7 +31,7 @@ int ReplicationState::start(int port, int election_timeout_ms, int snapshot_inte
std::string actual_peers = peers;
- if(actual_peers == "::") {
+ if(actual_peers.empty()) {
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(addr.ip.s_addr), str, INET_ADDRSTRLEN);
actual_peers = std::string(str) + ":" + std::to_string(port) + ":0";
@@ -80,7 +80,7 @@ int ReplicationState::start(int port, int election_timeout_ms, int snapshot_inte
}
std::vector<std::string> peer_vec;
- StringUtils::split(peers, peer_vec, ",");
+ StringUtils::split(actual_peers, peer_vec, ",");
if(peer_vec.size() == 1) {
// NOTE: `reset_peers` is NOT safe to run on a cluster of nodes, but okay for standalone
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index e49367ef..296f0c36 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -144,25 +144,23 @@ bool on_send_response(void *data) {
int start_raft_server(ReplicationState& replication_state, const std::string& state_dir,
const std::string& path_to_peers, uint32_t raft_port) {
- // TODO: early returns must quit parent process as well
- const Option<std::string> & peers_op = fetch_file_contents(path_to_peers);
- std::string peer_ips_string = "::";
+ std::string peer_ips_string;
- if(!peers_op.ok()) {
- if(peers_op.code() == 404) {
- LOG(INFO) << "Since no --peers argument is provided, starting a single node Typesense cluster.";
- } else {
- LOG(ERR) << peers_op.error();
- return -1;
- }
+ if(path_to_peers.empty()) {
+ LOG(INFO) << "Since no --peers argument is provided, starting a single node Typesense cluster.";
} else {
- peer_ips_string = peers_op.get();
- }
+ const Option<std::string> & peers_op = fetch_file_contents(path_to_peers);
- if(peer_ips_string.empty()) {
- LOG(ERR) << "File containing raft peers is empty.";
- return -1;
+ if(!peers_op.ok()) {
+ LOG(ERR) << peers_op.error();
+ exit(-1);
+ } else if(peer_ips_string.empty()) {
+ LOG(ERR) << "File containing raft peers is empty.";
+ exit(-1);
+ } else {
+ peer_ips_string = peers_op.get();
+ }
}
// start raft server
@@ -170,12 +168,12 @@ int start_raft_server(ReplicationState& replication_state, const std::string& st
if (braft::add_service(&raft_server, raft_port) != 0) {
LOG(ERR) << "Failed to add raft service";
- return -1;
+ exit(-1);
}
if (raft_server.Start(raft_port, nullptr) != 0) {
LOG(ERR) << "Failed to start raft server";
- return -1;
+ exit(-1);
}
std::vector<std::string> peer_ips;
@@ -184,7 +182,7 @@ int start_raft_server(ReplicationState& replication_state, const std::string& st
if (replication_state.start(raft_port, 1000, 600, state_dir, peers) != 0) {
LOG(ERR) << "Failed to start raft state";
- return -1;
+ exit(-1);
}
LOG(INFO) << "Typesense raft service is running on " << raft_server.listen_address();
@@ -195,7 +193,7 @@ int start_raft_server(ReplicationState& replication_state, const std::string& st
if(++raft_counter % 10 == 0 && !path_to_peers.empty()) {
// reset peer configuration periodically to identify change in cluster membership
const Option<std::string> & refreshed_peers_op = fetch_file_contents(path_to_peers);
- if(!peers_op.ok()) {
+ if(!refreshed_peers_op.ok()) {
LOG(ERR) << "Error while refreshing peer configuration: " << refreshed_peers_op.error();
continue;
}
diff --git a/test/string_utils_test.cpp b/test/string_utils_test.cpp
index de8d9fb8..4aa63c61 100644
--- a/test/string_utils_test.cpp
+++ b/test/string_utils_test.cpp
@@ -45,4 +45,7 @@ TEST(StringUtilsTest, ShouldJoinString) {
const std::string & joined_str2 = StringUtils::join(parts, "/", 2);
ASSERT_STREQ("baz/bazinga", joined_str2.c_str());
+
+ const std::string & joined_str3 = StringUtils::join({}, "/");
+ ASSERT_STREQ("", joined_str3.c_str());
}