mirror of
https://github.com/typesense/typesense.git
synced 2025-05-16 19:55:21 +08:00
Exit cleanly from raft service on validation errors.
This commit is contained in:
parent
123888d4d3
commit
df08df5d96
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user