Skip to content

raftstore-v2: compact and gc raft logs#13846

Merged
ti-chi-bot merged 30 commits intotikv:masterfrom
tabokie:221125-raftlog-gc
Dec 27, 2022
Merged

raftstore-v2: compact and gc raft logs#13846
ti-chi-bot merged 30 commits intotikv:masterfrom
tabokie:221125-raftlog-gc

Conversation

@tabokie
Copy link
Member

@tabokie tabokie commented Nov 25, 2022

Signed-off-by: tabokie [email protected]

What is changed and how it works?

Issue Number: Ref #12842

What's Changed:

Related changes

Check List

Tests

  • Unit test

Release note

None

Signed-off-by: tabokie <[email protected]>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Nov 25, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • BusyJay
  • Connor1996

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Details

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 25, 2022
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
@BusyJay
Copy link
Member

BusyJay commented Dec 6, 2022

Logs can't be gced until its data are all applied and flushed. So log gc depends on removing WAL. I suggest to continue the work after removing wal is supported.

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 7, 2022
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 20, 2022
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 21, 2022
RAFT_LOG_GC_DELETED_KEYS_HISTOGRAM.observe(n as f64);
}
}
if let Err(e) = self.engines.raft.consume(&mut batch, false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can keep the most of original flush by:

fn gc_raft_log() {
   ...
   consume(batch)
   ...
}

fn flush() {
    ...
    for _ in _ {
        engine.gc(batch);
    }
    ...
}

So most code is unchanged and easier to understand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's the failpoint bothering you, the old code is simply wrong. It skips writing log batch but doesn't skip consuming callbacks. It is not the intended behavior as per

// Simulate raft log gc are pending in queue.
let fp = "worker_gc_raft_log";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually the expected behavior. Because we just ignore any write errors and only prints a log. The callbacks are used to ensure no more background writes to the same region.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless of the behavior it is not what the test comment says, because tasks are lost forever. For RocksDB GC it means some entries will never be compacted again. So we will change the failpoint comment if that's what you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return action is to simulate the case that the node is stopped before completing GC. It's more appropriate to use "pause", but "pause" will stop the case from restarting node.

On the other hand, writing raft engine can fail. If that error happens, we just ignore it for now. I think they are two different things.

self.fsm.peer.mut_store().evict_entry_cache(true);
}
let mut _usage = 0;
if memory_usage_reaches_high_water(&mut _usage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next tick it will call needs_evict_entry_cache again, which calls memory_usage_reaches_high_water inside. I think checking later again makes more sense than checking right after a compaction.

Also, this further hides the function memory_usage_reaches_high_water and keeps the pattern consistent with

if needs_evict_entry_cache(self.ctx.cfg.evict_cache_on_memory_ratio) {
self.fsm.peer.mut_store().evict_entry_cache(true);
if !self.fsm.peer.get_store().is_entry_cache_empty() {
self.register_entry_cache_evict_tick();
}
}

self.entry_storage().truncated_index(),
self.storage().apply_trace().persisted_apply_index(),
);
if compact_index > self.last_engine_compact_log_index() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last_engine_compact_log_index may not be necessary. For on_ready_res, it should compact log if truncated_index < persisted_applied before applying the updates, For record_apply_trace, it should compact log if persisted_applied < truncated_index.

Copy link
Member Author

@tabokie tabokie Dec 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. But it makes the code a little more complex, and it introduces an extra dependency between caller and callee.

It wouldn't work if one index advances across another index,

Copy link
Member

@BusyJay BusyJay Dec 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? For example, truncated_index is advanced from 3 to 10 in on_ready_res, while persisted_applied is 5. Because 3 < 5, so it should gc logs using cmp::min(10, 5) = 5.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It is still very complex.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just two if check to get rid of a field (and two if check).

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 24, 2022
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 26, 2022
if found_flush_state[cf_id as usize] {
batch.0.delete(raft_group_id, key.to_vec());
} else {
found_flush_state[cf_id as usize] = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will IndexOutOfBound if cf_id == MAX_CF_ID.

.and_then(|_| {
store_ctx
.engine
.delete_all_but_one_states_before(region_id, compact_index, lb)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it. It should delete those states whenever persisted_applied is advanced. For example, even the truncated index is 3, but it can delete the states at 6 when applied index is 10 and there is another state at 7.

RAFT_LOG_GC_DELETED_KEYS_HISTOGRAM.observe(n as f64);
}
}
if let Err(e) = self.engines.raft.consume(&mut batch, false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually the expected behavior. Because we just ignore any write errors and only prints a log. The callbacks are used to ensure no more background writes to the same region.

Signed-off-by: tabokie <[email protected]>
Signed-off-by: tabokie <[email protected]>
Copy link
Member

@BusyJay BusyJay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM

}

let data = req.write_to_bytes().unwrap();
self.propose_with_ctx(store_ctx, data, vec![])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.propose_with_ctx(store_ctx, data, vec![])
self.propose(store_ctx, data)

}
// TODO: check is_merging
// compact failure is safe to be omitted, no need to assert.
if res.compact_index <= self.entry_storage().truncated_index() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is covered by checking first_index.

.apply_state()
.get_truncated_state()
.get_index();
self.entry_storage_mut()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save apply_state_mut as a temporary can reduce code.

self.set_has_extra_write();
}
}
self.maybe_compact_log_from_engine(store_ctx, Either::Left(old_persisted));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be included in if above.

Either::Right(old_truncated) if old_truncated >= persisted => return,
_ => {}
}
let compact_index = std::cmp::min(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean let compact_index = std::cmp::min(truncated, persisted)?

Comment on lines +463 to +469
if let Some(last_heartbeat) = self.peer_heartbeats.get(&peer_id)
&& *last_heartbeat >= *deadline
{
return true;
}
false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean matches!(self.peer_heartbeats.get(&peer_id), Some(last_heartbat) if *last_heartbeat >= *deadline)?

}
}
RAFT_LOG_GC_WRITE_DURATION_HISTOGRAM.observe(start.saturating_elapsed_secs());
for cb in cbs {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callback are not collected and invoked.

Signed-off-by: tabokie <[email protected]>
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Dec 26, 2022
Copy link
Member

@Connor1996 Connor1996 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Dec 26, 2022
@tabokie
Copy link
Member Author

tabokie commented Dec 27, 2022

Latest commit needs review. @bufferflies @BusyJay

@tabokie
Copy link
Member Author

tabokie commented Dec 27, 2022

/merge

@ti-chi-bot
Copy link
Member

@tabokie: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

DetailsCommit hash: 97f9300

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Dec 27, 2022
@ti-chi-bot ti-chi-bot merged commit f21361d into tikv:master Dec 27, 2022
@ti-chi-bot ti-chi-bot added this to the Pool milestone Dec 27, 2022
@tabokie tabokie deleted the 221125-raftlog-gc branch December 27, 2022 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants