Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Dec 24, 2024
1 parent 19e747a commit 5d01426
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/persist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ TDiskState::TDiskState(const std::string& name, uint32_t id)
State.read((char*)&LastLogIndex, sizeof(LastLogIndex));
State.read((char*)&CurrentTerm, sizeof(CurrentTerm));
State.read((char*)&VotedFor, sizeof(VotedFor));
} else {
Commit();
}
if (LastLogIndex > 0) {
LastLogTerm = Get(LastLogIndex-1)->Term;
}
State.clear();
State.seekg(0);
}

std::fstream TDiskState::Open(const std::string& name)
Expand Down Expand Up @@ -62,8 +65,7 @@ void TDiskState::RemoveLast()
{
if (LastLogIndex > 0) {
LastLogIndex--;
State.seekg(0);
State.write((char*)&LastLogIndex, sizeof(LastLogIndex));
Commit();
}
}

Expand All @@ -77,9 +79,9 @@ void TDiskState::Append(TMessageHolder<TLogEntry> entry)
Write(entry);
Index.seekg(LastLogIndex * sizeof(offset));
Index.write((char*)&offset, sizeof(offset));
LastLogTerm = entry->Term;
LastLogIndex ++;
State.seekg(0);
State.write((char*)&LastLogIndex, sizeof(LastLogIndex));
Commit();
}

TMessageHolder<TLogEntry> TDiskState::Get(int64_t index) const
Expand All @@ -105,6 +107,8 @@ void TDiskState::Commit()
if (!State.write((char*)&LastLogIndex, sizeof(LastLogIndex))) { abort(); }
if (!State.write((char*)&CurrentTerm, sizeof(CurrentTerm))) { abort(); }
if (!State.write((char*)&VotedFor, sizeof(VotedFor))) { abort(); }
State.clear();
State.flush();
Index.flush();
Entries.flush();
}

32 changes: 32 additions & 0 deletions test/test_raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,37 @@ void test_disk_state(void**) {
assert_terms(log2, {1,1,1,4,4,5,5,6,6});
}

void test_disk_state_restore1(void**) {
remove("test_disk_state_restore1.entries.1");
remove("test_disk_state_restore1.index.1");
remove("test_disk_state_restore1.state.1");

std::unique_ptr<TDiskState> state = std::make_unique<TDiskState>("test_disk_state_restore1", 1);
assert_int_equal(state->LastLogIndex, 0);
assert_int_equal(state->CurrentTerm, 1);
assert_int_equal(state->VotedFor, 0);
auto log = MakeLog<TLogEntry>({1});
for (auto entry : log) {
state->Append(entry);
}
assert_int_equal(state->LastLogIndex, 1);

state.reset();
state = std::make_unique<TDiskState>("test_disk_state_restore1", 1);
assert_int_equal(state->CurrentTerm, 1);
assert_int_equal(state->VotedFor, 0);
assert_int_equal(state->LastLogIndex, 1);

std::vector<TMessageHolder<TLogEntry>> log2;
for (int i = 0; i < state->LastLogIndex; i++) {
auto entry = state->Get(i);
log2.emplace_back(entry);
}

assert_terms(log2, {1});
}


void test_disk_state_restore(void**) {
remove("test_disk_state_restore.entries.1");
remove("test_disk_state_restore.index.1");
Expand Down Expand Up @@ -759,6 +790,7 @@ int main() {
cmocka_unit_test(test_commit_advance_wrong_term),
cmocka_unit_test(test_leader_heartbeat),
cmocka_unit_test(test_disk_state),
cmocka_unit_test(test_disk_state_restore1),
cmocka_unit_test(test_disk_state_restore),
};
return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down

0 comments on commit 5d01426

Please sign in to comment.