Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] RFC: keyspace level GC #113

Open
wants to merge 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
57aef25
ks_level_gc init commit
ystaticy Apr 24, 2024
b2e017a
in check consistency and trigger compaction
ystaticy Apr 24, 2024
3e5f711
fix words
ystaticy Apr 24, 2024
9196bce
fix words
ystaticy Apr 24, 2024
9e72539
fix words
ystaticy Apr 24, 2024
e90dd88
fix words
ystaticy Apr 24, 2024
0900f46
fix words
ystaticy Apr 24, 2024
2dce1cc
add link
ystaticy Apr 25, 2024
09181ad
format
ystaticy Apr 26, 2024
b97f612
add details for optimize trigger timing and assert judgment
ystaticy May 6, 2024
3689a4d
add details
ystaticy May 6, 2024
a964053
fix comments
ystaticy May 9, 2024
342a08d
fix comments
ystaticy May 9, 2024
9c5d786
fix comments
ystaticy May 9, 2024
24b2ff8
fix comments
ystaticy May 9, 2024
73afd99
fix comments
ystaticy May 9, 2024
1b4af39
add Usage and Compatibility
ystaticy May 9, 2024
3f2582a
fix comments
ystaticy May 9, 2024
0a63d88
fix
ystaticy May 10, 2024
fb72c0c
fix pick
ystaticy May 10, 2024
e09dd1e
add upgrade,remove Other non-GC logic desc
ystaticy May 10, 2024
d68bf14
update img
ystaticy May 10, 2024
9607e07
update img
ystaticy May 10, 2024
57ad936
fix lint
baiyuqing May 10, 2024
bd4f336
add Data import and export
ystaticy May 10, 2024
f40a3c9
add desc about global br
ystaticy May 10, 2024
98812bc
add desc about global br
ystaticy May 10, 2024
44a3b5e
update
baiyuqing May 11, 2024
af1fb55
update
baiyuqing May 11, 2024
66fbb9b
update formula
baiyuqing May 11, 2024
c142552
update formula
baiyuqing May 13, 2024
a5dffa3
update
baiyuqing May 16, 2024
b1bf526
update
baiyuqing May 16, 2024
bf00fec
fix
baiyuqing May 16, 2024
575fbbe
update Motivation
ystaticy Jun 5, 2024
7cafe00
add
ystaticy Jun 5, 2024
02c37c8
add link for keyspace meta
ystaticy Jun 5, 2024
d53aa00
Modify the flow chart
ystaticy Jun 12, 2024
d10dbec
Modify the flow chart
ystaticy Jun 12, 2024
8ac31a2
fix comments
ystaticy Jun 12, 2024
db0b6b2
fix comments
ystaticy Jun 12, 2024
db34775
fix comments
ystaticy Jun 12, 2024
0a9b122
fix comments
ystaticy Jun 12, 2024
1d329a9
fix comments
ystaticy Jun 12, 2024
1eb7445
fix comments
ystaticy Jun 12, 2024
38df077
fix comments
ystaticy Jun 12, 2024
1478202
fix comments
ystaticy Jun 12, 2024
6f70d7d
fix comments
ystaticy Jun 12, 2024
3da02a3
fix code format
ystaticy Jun 25, 2024
a83f127
remove upgrade part
ystaticy Jun 25, 2024
1b86eae
correct grammatical and spelling errors
ystaticy Sep 20, 2024
a0818df
small fix: Grammar and Expression
ystaticy Sep 23, 2024
5eb460b
small fix: Grammar and Expression
ystaticy Sep 23, 2024
47f606a
update words
ystaticy Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added media/keyspace-level-gc-get-gc-safe-point.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions text/0113-keyspace-level-gc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Keyspace level GC

- RFC PR: https://github.com/tikv/rfcs/pull/113
- Tracking Issue: https://github.com/tikv/tikv/issues/16896

## Summary

TiKV support [keyspace][1] level GC (garbage collection).

## Concepts of GC management type

1. Global GC:
- It represents the previous default GC logic, All TiDB clusters (including those without keyspace configuration and those with keyspace configuration) use the GC safe point calculated by the GC worker owner without keyspace configuration.
- The default GC management type for keyspaces is the Global GC.
2. Keyspace Level GC:
- This indicates that each keyspace will advance its own GC safe point.
- Keyspace GC-related data includes the minimum start timestamp, GC safe point, service safe point, which are stored in the respective etcd paths of each keyspace in PD.

## Motivation

In order to facilitate multi-tenant deployment of TiDB in a shared TiKV cluster, TiDB now supports the keyspace feature. Currently, a TiDB cluster with keyspace can only utilize the GC safe point calculated by the global GC worker. A global TiDB GC worker (a TiDB server without keyspace configuration) shared by all TiDB instances is responsible for calculating the global GC safe point and resolving locks, while each keyspace's TiDB instance has its own GC worker. The GC worker for each keyspace performs only one operation: utilizing the global GC safe point to execute "delete-range" operations within its keyspace ranges.

However, in this implementation, the calculation of the global GC safe point depends on the oldest timestamp (minStartTS, service safe point, etc.) used to calculate the GC safe point for all TiDB clusters (including those with keyspace configured and those without keyspace configured). When any factor affecting the GC safe point is blocked, the GC of all TiDB clusters will also be blocked.

Therefore, we propose the **Keyspace Level GC**:

TiDB Side:
Isolate the GC safe point calculations and the storage of related data between keyspaces (this concept is referred to as `keyspace level GC`). This will not affect the GC safe point calculations of other keyspaces. Keyspaces can be created by setting `gc_management_type = keyspace_level_gc` to enable keyspace level GC. Then, this keyspace can calculate its own GC safe point.

TiKV Side:
In the GC process, it parses the keyspace ID from the data key, uses the keyspace meta configuration and the keyspace level GC safe point corresponding to the keyspace ID to determine the GC safe point value of the data key and then executes the GC logic.

## Implementation in TiKV

1. Get [keyspace metadata](https://github.com/pingcap/kvproto/blob/d9297553c9009f569eaf4350f68a908f7811ee55/proto/keyspacepb.proto#L26C1-L33C2) and keyspace-level GC safe points from PD:
- Create a keyspace metadata watch service: Watch the keyspace metadata etcd path to get the keyspace metadata and store it in the mapping from keyspace ID to keyspace metadata.
- Create a keyspace level GC watch service: Watch the corresponding etcd path to get the GC safe point for the keyspace and store it in the mapping from keyspace ID to keyspace level GC safe point.

2. How to get the GC safe point after getting the MVCC key in the Compaction Filter:
![img.png](../media/keyspace-level-gc-get-gc-safe-point.png)

3. How to determine if a keyspace uses a keyspace level GC safe point:
![img.png](../media/keyspace-level-gc-is-enable-keyspace-level-gc.png)

4. Use GC safe point to optimize trigger timing and assert judgment:
1. In the global GC, it will skip GC when GC safe point is `0` in `create_compaction_filter`.
After supporting keyspace level GC, GC is skipped if global GC safe point is 0 or if no keyspace level GC is initialized.
2. In the global GC, `check_need_gc` will skip GC in the compaction filter when `props.min_ts` is greater than all GC safe points.
After implementing keyspace level GC, if `props.min_ts` is greater than the global GC safe point and `props.min_ts` is greater than all keyspace level GC safe points, it will return false.
3. In the global GC, assert( safe_point > 0 ) when new compaction filter.
After supporting keyspace level GC, the assert condition is whether global GC safe point > 0 or any keyspace level GC safe point has been initialized.

5. Support the use of keyspace level GC for data import and export:
- When using `BR`, `CDC`, `Lightning`, or `Dumpling` to import or export keyspace data, you need to update the service safe point for the specified keyspace. When the task starts, it needs to get the keyspace metadata first to determine whether global or keyspace GC is being used, and then execute the relevant GC logic on the corresponding GC safe point and service safe point.

## Global Backup

Specify ts to backup data for non-keyspaces and all keyspaces in the entire cluster. It will be introduced in another RFC.

[1]: https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#new-key-value-codec