Skip to content

Commit

Permalink
fix bug when leader restart
Browse files Browse the repository at this point in the history
  • Loading branch information
LLiuJJ committed Aug 22, 2023
1 parent 6091791 commit 35fa9f6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ rm-net:
docker network rm mytestnetwork

run-demo:
docker run --name kvserver-node1 --network mytestnetwork --ip 172.18.0.10 -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftkv 0 /tmp/kv_db0 /tmp/log_db0 /tmp/snap_db0 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090 /eraft/logs/eraftkv-1.log
docker run --name kvserver-node1 --network mytestnetwork --ip 172.18.0.10 --privileged=true -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftkv 0 /tmp/kv_db0 /tmp/log_db0 /tmp/snap_db0 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090 /eraft/logs/eraftkv-1.log
sleep 2
docker run --name kvserver-node2 --network mytestnetwork --ip 172.18.0.11 -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftkv 1 /tmp/kv_db1 /tmp/log_db1 /tmp/snap_db1 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090 /eraft/logs/eraftkv-2.log
docker run --name kvserver-node3 --network mytestnetwork --ip 172.18.0.12 -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftkv 2 /tmp/kv_db2 /tmp/log_db2 /tmp/snap_db2 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090 /eraft/logs/eraftkv-3.log
docker run --name kvserver-node2 --network mytestnetwork --ip 172.18.0.11 --privileged=true -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftkv 1 /tmp/kv_db1 /tmp/log_db1 /tmp/snap_db1 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090 /eraft/logs/eraftkv-2.log
docker run --name kvserver-node3 --network mytestnetwork --ip 172.18.0.12 --privileged=true -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftkv 2 /tmp/kv_db2 /tmp/log_db2 /tmp/snap_db2 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090 /eraft/logs/eraftkv-3.log
sleep 1
docker run --name metaserver-node1 --network mytestnetwork --ip 172.18.0.2 -d --rm -v $(realpath .):/eraft eraft/eraftkv:v0.0.6 /eraft/build/eraftmeta 0 /tmp/meta_db0 /tmp/log_db0 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090
sleep 3
Expand Down
5 changes: 2 additions & 3 deletions src/raft_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ RaftServer::RaftServer(RaftConfig raft_config,
, max_entries_per_append_req_(100)
, tick_interval_(1000)
, granted_votes_(0)
, snap_threshold_log_count_(21)
, snap_threshold_log_count_(20000)
, open_auto_apply_(true)
, is_snapshoting_(false)
, snap_db_path_(raft_config.snap_path)
Expand Down Expand Up @@ -383,8 +383,6 @@ EStatus RaftServer::HandleRequestVoteResp(RaftNode* from_node,
this->id_,
this->current_term_);
this->BecomeLeader();
this->SendHeartBeat();
this->SendAppendEntries();
this->granted_votes_ = 0;
}
} else {
Expand Down Expand Up @@ -800,6 +798,7 @@ EStatus RaftServer::BecomeLeader() {
node->next_log_index = this->log_store_->LastIndex() + 1;
node->match_log_index = 0;
}
this->SendHeartBeat();
election_running_ = false;
return EStatus::kOk;
}
Expand Down
14 changes: 10 additions & 4 deletions src/rocksdb_storage_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ EStatus RocksDBStorageImpl::ApplyLog(RaftServer* raft,
{
std::lock_guard<std::mutex> lg(ERaftKvServer::ready_mutex_);
ERaftKvServer::is_ok_ = true;
ERaftKvServer::ready_cond_vars_[op_pair->op_count()]
->notify_one();
if (ERaftKvServer::ready_cond_vars_[op_pair->op_count()] !=
nullptr) {
ERaftKvServer::ready_cond_vars_[op_pair->op_count()]
->notify_one();
}
}
}
}
Expand All @@ -114,8 +117,11 @@ EStatus RocksDBStorageImpl::ApplyLog(RaftServer* raft,
{
std::lock_guard<std::mutex> lg(ERaftKvServer::ready_mutex_);
ERaftKvServer::is_ok_ = true;
ERaftKvServer::ready_cond_vars_[op_pair->op_count()]
->notify_one();
if (ERaftKvServer::ready_cond_vars_[op_pair->op_count()] !=
nullptr) {
ERaftKvServer::ready_cond_vars_[op_pair->op_count()]
->notify_one();
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/rocksdb_storage_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ class RocksDBSingleLogStorageImpl : public LogStore {
*/
EStatus Append(eraftkv::Entry* ety);

/**
* @brief
*
* @param new_idx
*/
void ResetFirstIndex(int64_t new_idx);

/**
Expand Down
1 change: 0 additions & 1 deletion src/shardgroup_command_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ EStatus ShardGroupCommandHandler::Execute(
return_bufs.push_back(server.address());
}
}
// example: *2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n
std::string reply_buf;
reply_buf += "*";
reply_buf += std::to_string(return_bufs.size());
Expand Down

0 comments on commit 35fa9f6

Please sign in to comment.