Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft arbiter support #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/braft/fsm_caller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ void FSMCaller::do_committed(int64_t committed_index) {
continue;
}
Iterator iter(&iter_impl);
_fsm->on_apply(iter);
if (!_node->arbiter()) {
_fsm->on_apply(iter);
} else {
for (;iter.valid();iter.next()) {
}
}
LOG_IF(ERROR, iter.valid())
<< "Node " << _node->node_id()
<< " Iterator is still valid, did you return before iterator "
Expand Down Expand Up @@ -353,7 +358,11 @@ void FSMCaller::do_snapshot_save(SaveSnapshotClosure* done) {
return;
}

_fsm->on_snapshot_save(writer, done);
if (!_node->arbiter()) {
_fsm->on_snapshot_save(writer, done);
} else {
done->Run();
}
return;
}

Expand Down Expand Up @@ -402,7 +411,9 @@ void FSMCaller::do_snapshot_load(LoadSnapshotClosure* done) {
return done->Run();
}

ret = _fsm->on_snapshot_load(reader);
if (!_node->arbiter()) {
ret = _fsm->on_snapshot_load(reader);
}
if (ret != 0) {
done->status().set_error(ret, "StateMachine on_snapshot_load failed");
done->Run();
Expand Down
13 changes: 12 additions & 1 deletion src/braft/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,11 @@ void NodeImpl::handle_election_timeout() {

return;
}

if (arbiter()) {
return;
}

bool triggered = _vote_triggered;
_vote_triggered = false;

Expand Down Expand Up @@ -1086,6 +1091,12 @@ void NodeImpl::handle_timeout_now_request(brpc::Controller* controller,
<< state2str(saved_state) << " at term=" << saved_term;
return;
}
if (arbiter()) {
response->set_term(_current_term);
response->set_success(false);
lck.unlock();
return;
}
const butil::EndPoint remote_side = controller->remote_side();
const int64_t saved_term = _current_term;
if (FLAGS_raft_enable_leader_lease) {
Expand Down Expand Up @@ -2698,7 +2709,7 @@ void NodeImpl::describe(std::ostream& os, bool use_html) {
lck.unlock();
const char *newline = use_html ? "<br>" : "\r\n";
os << "peer_id: " << _server_id << newline;
os << "state: " << state2str(st) << newline;
os << "state: " << state2str(st) << (arbiter() ? ", ARBITER" : "") << newline;
os << "readonly: " << readonly << newline;
os << "term: " << term << newline;
os << "conf_index: " << conf_index << newline;
Expand Down
2 changes: 2 additions & 0 deletions src/braft/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ friend class VoteBallotCtx;

bool disable_cli() const { return _options.disable_cli; }

bool arbiter() const { return _options.arbiter; }

private:
friend class butil::RefCountedThreadSafe<NodeImpl>;

Expand Down
3 changes: 3 additions & 0 deletions src/braft/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ struct NodeOptions {
// Default: false
bool disable_cli;

bool arbiter;

// Construct a default instance
NodeOptions();

Expand All @@ -609,6 +611,7 @@ inline NodeOptions::NodeOptions()
, snapshot_file_system_adaptor(NULL)
, snapshot_throttle(NULL)
, disable_cli(false)
, arbiter(false)
{}

inline int NodeOptions::get_catchup_timeout_ms() {
Expand Down
36 changes: 19 additions & 17 deletions src/braft/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,23 +976,25 @@ void LocalSnapshotCopier::copy_file(const std::string& filename) {
set_error(ECANCELED, "%s", berror(ECANCELED));
return;
}
scoped_refptr<RemoteFileCopier::Session> session
= _copier.start_to_copy_to_file(filename, file_path, NULL);
if (session == NULL) {
LOG(WARNING) << "Fail to copy " << filename
<< " path: " << _writer->get_path();
set_error(-1, "Fail to copy %s", filename.c_str());
return;
}
_cur_session = session.get();
lck.unlock();
session->join();
lck.lock();
_cur_session = NULL;
lck.unlock();
if (!session->status().ok()) {
set_error(session->status().error_code(), session->status().error_cstr());
return;
if (!_storage->dummy) {
scoped_refptr<RemoteFileCopier::Session> session
= _copier.start_to_copy_to_file(filename, file_path, NULL);
if (session == NULL) {
LOG(WARNING) << "Fail to copy " << filename
<< " path: " << _writer->get_path();
set_error(-1, "Fail to copy %s", filename.c_str());
return;
}
_cur_session = session.get();
lck.unlock();
session->join();
lck.lock();
_cur_session = NULL;
lck.unlock();
if (!session->status().ok()) {
set_error(session->status().error_code(), session->status().error_cstr());
return;
}
}
if (_writer->add_file(filename, &meta) != 0) {
set_error(EIO, "Fail to add file to writer");
Expand Down
3 changes: 3 additions & 0 deletions src/braft/snapshot_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ int SnapshotExecutor::init(const SnapshotExecutorOptions& options) {
_snapshot_throttle = options.snapshot_throttle;
_snapshot_storage->set_snapshot_throttle(options.snapshot_throttle);
}
if (_node->arbiter()) {
_snapshot_storage->dummy = true;
}
if (_snapshot_storage->init() != 0) {
LOG(ERROR) << "node " << _node->node_id()
<< " fail to init snapshot storage, uri " << options.uri;
Expand Down
1 change: 1 addition & 0 deletions src/braft/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class SnapshotStorage {
}

static butil::Status destroy(const std::string& uri);
bool dummy = false;
};

inline brpc::Extension<const LogStorage>* log_storage_extension() {
Expand Down