Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Nov 28, 2023
1 parent dfeaa44 commit e773b38
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TRaft::TRaft(int node, const TNodeDict& nodes, const std::shared_ptr<ITimeSource
, VolatileState(std::make_unique<TVolatileState>())
, StateName(EState::FOLLOWER)
, LastTime(TimeSource->Now())
{
{
for (auto [id, _] : Nodes) {
VolatileState->NextIndex[id] = 1;
}
Expand Down Expand Up @@ -102,7 +102,8 @@ std::unique_ptr<TResult> TRaft::OnRequestVote(TMessageHolder<TRequestVoteRequest
return std::make_unique<TResult>(TResult {
.NextState = std::make_unique<TState>(TState{
.CurrentTerm = message->Term,
.VotedFor = message->CandidateId
.VotedFor = message->CandidateId,
.Log = State->Log
}),
.Message = reply,
});
Expand All @@ -114,10 +115,6 @@ std::unique_ptr<TResult> TRaft::OnRequestVote(TMessageHolder<TRequestVoteRequest
std::unique_ptr<TResult> TRaft::OnRequestVote(TMessageHolder<TRequestVoteResponse> message) {
if (message->Term > State->CurrentTerm) {
return std::make_unique<TResult>(TResult {
.NextState = std::make_unique<TState>(TState{
.CurrentTerm = State->CurrentTerm,
.VotedFor = State->VotedFor,
}),
.NextStateName = EState::FOLLOWER,
.UpdateLastTime = true
});
Expand All @@ -136,10 +133,6 @@ std::unique_ptr<TResult> TRaft::OnRequestVote(TMessageHolder<TRequestVoteRespons
nextIndex.emplace(id, value);
}
return std::make_unique<TResult>(TResult {
.NextState = std::make_unique<TState>(TState{
.CurrentTerm = State->CurrentTerm,
.VotedFor = State->VotedFor,
}),
.NextVolatileState = std::make_unique<TVolatileState>(TVolatileState {
.CommitIndex = VolatileState->CommitIndex,
.LastApplied = VolatileState->LastApplied,
Expand All @@ -153,10 +146,6 @@ std::unique_ptr<TResult> TRaft::OnRequestVote(TMessageHolder<TRequestVoteRespons
auto nextVolatileState = *VolatileState;
nextVolatileState.SetVotes(votes);
return std::make_unique<TResult>(TResult {
.NextState = std::make_unique<TState>(TState{
.CurrentTerm = State->CurrentTerm,
.VotedFor = State->VotedFor,
}),
.NextVolatileState = std::make_unique<TVolatileState>(
nextVolatileState
),
Expand Down Expand Up @@ -322,6 +311,7 @@ std::unique_ptr<TResult> TRaft::Candidate(ITimeSource::Time now, TMessageHolder<
.NextState = std::make_unique<TState>(TState {
.CurrentTerm = State->CurrentTerm+1,
.VotedFor = Id,
.Log = State->Log
}),
.NextVolatileState = std::move(nextVolatileState),
.UpdateLastTime = true,
Expand Down

0 comments on commit e773b38

Please sign in to comment.