Skip to content

Commit

Permalink
optimize get term
Browse files Browse the repository at this point in the history
  • Loading branch information
CkTD authored and yangzining.yzn committed Feb 27, 2024
1 parent b37c610 commit 5e84f22
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/braft/log_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void LogManager::clear_memory_logs(const LogId& id) {
while (!_logs_in_memory.empty()
&& nentries < ARRAY_SIZE(entries_to_clear)) {
LogEntry* entry = _logs_in_memory.front();
if (entry->id > id) {
if (entry->id >= id) {
break;
}
entries_to_clear[nentries++] = entry;
Expand Down
2 changes: 1 addition & 1 deletion src/braft/log_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ friend class AppendBatcher;

void unsafe_truncate_suffix(const int64_t last_index_kept);

// Clear the logs in memory whose id <= the given |id|
// Clear the logs in memory whose id < the given |id|
void clear_memory_logs(const LogId& id);

int64_t unsafe_get_term(const int64_t index);
Expand Down
15 changes: 11 additions & 4 deletions test/test_log_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ TEST_F(LogManagerTest, get_should_be_ok_when_disk_thread_stucks) {
lm->clear_memory_logs(braft::LogId(N, 1));
// After clear all the memory logs, all the saved entries should have no
// other reference
for (size_t i = 0; i < N; ++i) {
for (size_t i = 0; i < N - 1; ++i) {
ASSERT_EQ(1u, saved_entries[i]->ref_count_);
saved_entries[i]->Release();
}
ASSERT_EQ(2u, saved_entries[N-1]->ref_count_);
saved_entries[N - 1]->Release();
}

TEST_F(LogManagerTest, configuration_changes) {
Expand Down Expand Up @@ -165,10 +167,12 @@ TEST_F(LogManagerTest, configuration_changes) {
lm->clear_memory_logs(braft::LogId(N, 1));
// After clear all the memory logs, all the saved entries should have no
// other reference
for (size_t i = 0; i < N; ++i) {
for (size_t i = 0; i < N - 1; ++i) {
ASSERT_EQ(1u, saved_entries[i]->ref_count_) << "i=" << i;
saved_entries[i]->Release();
}
ASSERT_EQ(2u, saved_entries[N-1]->ref_count_);
saved_entries[N - 1]->Release();
}

TEST_F(LogManagerTest, truncate_suffix_also_revert_configuration) {
Expand Down Expand Up @@ -321,8 +325,11 @@ TEST_F(LogManagerTest, append_with_the_same_index) {
for (size_t i = 0; i < N; ++i) {
ASSERT_EQ(1u, saved_entries0[i]->ref_count_);
ASSERT_EQ(1u, saved_entries1[i]->ref_count_);
ASSERT_EQ(1u, saved_entries2[i]->ref_count_);
if (i != N - 1) {
ASSERT_EQ(1u, saved_entries2[i]->ref_count_);
}
}
ASSERT_EQ(2u, saved_entries2[N-1]->ref_count_);

for (size_t i = 0; i < N; ++i) {
braft::LogEntry* entry = lm->get_entry(i + 1);
Expand Down Expand Up @@ -465,7 +472,7 @@ TEST_F(LogManagerTest, pipelined_append) {
ASSERT_EQ(N * 2, lm->_logs_in_memory.size());

lm->set_applied_id(braft::LogId(N * 2, 2));
ASSERT_EQ(0u, lm->_logs_in_memory.size())
ASSERT_EQ(1u, lm->_logs_in_memory.size())
<< "last_log_id=" << lm->last_log_id(true);

// We can still get the right data from storage
Expand Down

0 comments on commit 5e84f22

Please sign in to comment.