Skip to content

Commit 83f040b

Browse files
authored
docs: RFC for keyspace (#39685)
ref #40425
1 parent 790c6c0 commit 83f040b

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

docs/design/2022-12-07-keyspace.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# Proposal: Keyspace
2+
* Authors: [ystaticy](https://github.com/ystaticy), [iosmanthus](https://github.com/iosmanthus), [AmoebaProtozoa](https://github.com/AmoebaProtozoa),[zeminzhou](https://github.com/zeminzhou)
3+
4+
5+
## Motivation
6+
7+
We aim to store data from multiple applications in a TiKV cluster. For instance, in a multi tenant scenario, a common solution is to add a prefix to the key to differentiate the data of various applications. Therefore, we introduce a new concept referred to as `Keyspace` to describe the logical isolation among different business scenarios within a TiKV cluster.
8+
9+
## Architecture
10+
11+
In this section, we will outline the architecture when utilizing keyspaces.
12+
13+
![keyspace-arch.png](imgs/keyspace-arch.png)
14+
15+
### PD
16+
17+
PD is responsible for managing `Keyspace` metadata. It provides HTTP and RPC interfaces to create, load, and modify `Keyspace` metadata. All other components retrieve the keyspace metadata from PD.
18+
19+
### TiDB
20+
21+
After the keyspace is created on PD, it can be configured on multiple TiDB instances, forming a TiDB cluster for that keyspace. However, a TiDB instance can serve only one keyspace at a specific moment, unless it is restarted and configured to use a different keyspace.
22+
23+
### Client-go
24+
25+
In TiDB, client-go utilizes [codec V2](https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#new-key-value-codec) to add a keyspace prefix when the keyspace is set. A client configured with a keyspace can only read and write data from the specified keyspace.
26+
27+
### TiKV
28+
29+
In TiKV, different keyspaces are isolated by different data key prefixes.
30+
31+
### TiFlash
32+
33+
In TiFlash, requests carry the keyspace prefix, and the data structures and storage paths are isolated by the keyspace ID.
34+
35+
## Usage
36+
37+
To use TiDB with `Keyspace`, the following configuration is required:
38+
1. TiKV needs to [enable API V2](https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#tikv-server).
39+
2. The TiDB configuration should set `keyspace-name` to specify which keyspace is currently served by the TiDB instance.
40+
3. BR, Lightning, Dumpling, and TiCDC specify the keyspace by setting `keyspace-name` on the command line.
41+
42+
## Implementation
43+
44+
### PD
45+
46+
#### PD Functionality
47+
48+
PD provides an HTTP interface and an RPC interface to create, load, change the state, and configure keyspace metadata, and is responsible for interacting with the keyspace metadata store. Users can request the `create keyspace` interface by specifying the keyspace name, which generates the keyspace metadata.
49+
* [PD HTTP interface](https://github.com/tikv/pd/blob/098b802fcda75b5ae0527b0b50b95ba8b31403cf/server/apiv2/handlers/keyspace.go)
50+
* [PD RPC interface](https://github.com/tikv/pd/blob/b95dddaa7e9decaad7c24becb401c67cea83fd3a/server/keyspace_service.go)
51+
52+
#### Keyspace Name
53+
54+
**Keyspace Name** is specified by the creator during the creation of the keyspace, and once the keyspace is created, the keyspace name cannot be bound to another keyspace.
55+
56+
#### Keyspace ID Allocation
57+
58+
**Keyspace ID** is allocated by PD createKeyspace interface. The keyspace id is assigned by auto-incrementing the id, so keyspace ids are not re-assigned. The max keyspace id is 16777216, no new keyspaces can be created after the maximum keyspace id has been reached.
59+
60+
#### Keyspace Metadata
61+
62+
[**Keyspace Meta**](https://github.com/pingcap/kvproto/blob/d9297553c9009f569eaf4350f68a908f7811ee55/proto/keyspacepb.proto#L26C1-L33C2) is built when a keyspace is created, and the keyspace management interface operates on this metadata. The keyspace state in the metadata changes according to the keyspace state machine. The state change logic is as follows:
63+
![keyspace-state-machine.png](imgs%2Fkeyspace-state-machine.png)
64+
65+
### TiDB
66+
67+
#### Using API V2 and etcd namespace to isolate TiKV data and etcd path by Keyspace.
68+
69+
1. Configure the keyspace name by setting `keyspace-name` in the configuration file.
70+
* When the TiDB server starts, it will open a TiKV driver. The driver has a TxnKV client object called `tikv.KVStore`. If `keyspace-name` is set, it will use `tikv.KVStore` via API V2 to access TiKV.
71+
2. The etcd path should have a different prefix based on the etcd namespace.
72+
* An etcd client is created during domain initialization. If `keyspace-name` is set, it will add an etcd namespace when creating the etcd client.
73+
* The format of the etcd namespace is `/keyspaces/tidb/$keyspaceId`.
74+
75+
#### TiDB BR
76+
77+
If "keyspace-name" is set when executing the BR command, it supports restore and backup for the specified keyspace data. The BR task will parse the old keyspace prefix from the source data, and get target keyspace from KVStore.codec. The old keyspace prefix and new keyspace prefix are used to update the old key prefix and new key prefix of rewrite rules. Restore checksum also requires the old and new keyspace prefixes. The backup task needs to add a target keyspace prefix when building ranges in `BackupRequest` to scan for the data of the specified keyspace in TiKV.
78+
79+
#### TiDB Lightning
80+
81+
If `keyspace-name` is set when executing the `Lightning` command, it will encode the key to add the keyspace prefix before appending rows to the SST file, and decode the key before recording conflict data into the TiDB table.
82+
83+
#### TiCDC
84+
85+
If `keyspace-name` is set when creating a changefeed to the TiCDC server, the server will only capture the table data within that keyspace for the changefeed. For changefeeds configured with a keyspace, the TiCDC server will encode the table range with the keyspace prefix to subscribe to the table data from the TiKV server, and it will also decode the keyspace prefix from the key received from the TiKV server to retrieve the table data. If `keyspace-name` is not set, the TiCDC server will not capture the table data from [API V2](https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#tikv-server).
86+
87+
#### TiDB GC
88+
89+
##### Concepts of GC management type:
90+
91+
1. Global GC:
92+
- Represents the previous default GC logic. There is a TiDB GC Worker that is responsible for calculating the global GC safe point for the entire cluster.
93+
- The default GC management type for a keyspace is global GC.
94+
2. Keyspace Level GC:
95+
- Indicates that the keyspace will advance its own GC safe point.
96+
- `Keyspace` GC related data includes: minimum start timestamp, GC safe point, and service safe point, which are stored in the dedicated path of each keyspace in PD.
97+
98+
Previously, there was only one GCWorker leader for GC in the entire PD and TiKV cluster. After supporting keyspace level GC, the default GC management type for a keyspace is global GC. Alternatively, you can specify the GC management type as global_gc or keyspace_level_gc via the keyspace meta's configuration `gc_management_type`.
99+
100+
* When a specified keyspace uses global GC, this keyspace will have its own GCWorker leader. However, this GCWorker leader is not responsible for calculating the GC safe point or resolving locks. This keyspace will rely on the GC safe point produced by the GCWorker leader that is not configured with a keyspace in the cluster for GC, and will use its own keyspace specific GCWorker leader for deleting ranges.
101+
102+
* When a specified keyspace uses keyspace level GC, this keyspace will have its own GCWorker leader that computes its own GC safe point, resolves locks, and deletes ranges. The keyspace level safe point information is stored in PD, isolated by the keyspace prefix, and only GC the data within its keyspace range, without affecting other keyspaces.
103+
104+
#### Observability
105+
106+
Keyspace observability is essential for understanding and managing performance effectively.
107+
108+
1. Metric Filtering: TiDB metrics can be filtered by the `keyspace_id` label for precise monitoring.
109+
2. Grafana Setup: Add a query box for `keyspace_id` in Grafana dashboards to visualize data specific to each keyspace.
110+
3. Log Enhancement: Include the `keyspace_name` label in TiDB logs for better tracking and context in log analysis.
111+
112+
### TiKV
113+
114+
`Keyspace` can only be used when TiKV [enabled API V2](https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#tikv-server).
115+
For more details about keyspace in TiKV, please refer to the [API V2 RFC](https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md).
116+
117+
#### Keyspace Key encoding:
118+
119+
We use [`Keyspace` key encoding](https://github.com/tikv/rfcs/blob/master/text/0069-api-v2.md#key-encoding) as the key prefix for keyspace to isolate data between different keyspaces. When TiDB enables `Keyspace`, it operates in `TxnKV` mode. The prefix 'x' indicates the `TxnKV` key mode, followed directly by a 3-byte keyspace ID identifier, and then comes the user key.
120+
121+
#### Client-go
122+
123+
Client-go also supports API V2 and `Keyspace`. This will make all the codec logic transparent to applications. We currently maintain these implementations in tikv/client-go/api-v2. The new version of client-go unifies all the codec logic by introducing a new interface called `Codec`:
124+
```go
125+
// Codec is responsible for encode/decode requests.
126+
type Codec interface {
127+
// GetAPIVersion returns the api version of the codec.
128+
GetAPIVersion() kvrpcpb.APIVersion
129+
// GetKeyspace return the keyspace id of the codec in bytes.
130+
GetKeyspace() []byte
131+
// GetKeyspaceID return the keyspace id of the codec.
132+
GetKeyspaceID() KeyspaceID
133+
// EncodeRequest encodes with the given Codec.
134+
// NOTE: req is reused on retry. MUST encode on cloned request, other than overwrite the original.
135+
EncodeRequest(req *tikvrpc.Request) (*tikvrpc.Request, error)
136+
// DecodeResponse decode the resp with the given codec.
137+
DecodeResponse(req *tikvrpc.Request, resp *tikvrpc.Response) (*tikvrpc.Response, error)
138+
// EncodeRegionKey encode region's key.
139+
EncodeRegionKey(key []byte) []byte
140+
// DecodeRegionKey decode region's key
141+
DecodeRegionKey(encodedKey []byte) ([]byte, error)
142+
// DecodeBucketKeys decode region bucket's key
143+
DecodeBucketKeys(keys [][]byte) ([][]byte, error)
144+
// EncodeRegionRange encode region's start and end.
145+
EncodeRegionRange(start, end []byte) ([]byte, []byte)
146+
// DecodeRegionRange decode region's start and end.
147+
DecodeRegionRange(encodedStart, encodedEnd []byte) ([]byte, []byte, error)
148+
// EncodeRange encode a key range.
149+
EncodeRange(start, end []byte) ([]byte, []byte)
150+
// DecodeRange decode a key range.
151+
DecodeRange(encodedStart, encodedEnd []byte) ([]byte, []byte, error)
152+
// EncodeKey encode a key.
153+
EncodeKey(key []byte) []byte
154+
// DecodeKey decode a key.
155+
DecodeKey(encoded []byte) ([]byte, error)
156+
}
157+
```
158+
159+
This interface will encode the key-related request right before it is sent to the TiKV/TiFlash/PD server. It will decode the response immediately after it is received. This design aims to be neat enough to make encoding and decoding transparent to applications.
160+
161+
Typically, [`codecV2`](https://github.com/tikv/client-go/blob/239ac1b2b7fc67921b00e1d51d47f3716c2c2f0c/internal/apicodec/codec_v2.go#L41) implements the `Codec` interface and the codec logic related to `Keyspace`.
162+
The encoding logic is trivial, but the decoding logic is somewhat complicated.
163+
To achieve transparency, `codecV2` will map the region range to `[0, +inf)`, if the range is not overlapped with the current keyspace, it will return an error.
164+
For instance, if the keyspace is `[x001, x002)`, then the region range `[x003, x004)` is invalid, and `[x000, x002)` is mapped to `[0, +inf)`.
165+
166+
client-go provides a function called `NewCodecPDClientWithKeyspace` to create a PD client with `Keyspace` support. It tries to fetch the keyspace id with the given keyspace name from PD, and create a `codecV2` with the keyspace id.
167+
You could get the `Codec` within the `CodecPDClient` and use it to construct a TiKV gRPC client.
168+
169+
#### Coprocessor
170+
171+
TiKV Coprocessor uses the range specified in the request to determine which range to scan. After scanning, it transforms the key/value pairs into a columnar format and begins executing the DAG request. However, the original code cannot recognize the new format of the key. Therefore, the `RangeScanner` should be modified to support the new format of the key by ignoring the first 4 bytes if the request uses API V2.
172+
173+
### TiFlash
174+
175+
TiFlash is a columnar storage engine that acts as a coprocessor for TiKV. When TiDB is configured with a keyspace, TiFlash requests also include the keyspace prefix. TiFlash's data structures and storage paths are isolated by the keyspace ID.
176+
177+
#### Storage
178+
179+
Without a keyspace, TiFlash stores each table's columnar/schema data in a separate directory identified by the table ID. However, in a keyspace scenario, multiple keyspaces may have the same table ID, which means that the table ID alone cannot uniquely identify a table.
180+
181+
The storage path structure of TiFlash will be as follows:
182+
```
183+
.
184+
├── data
185+
│ ├── ks_1_t_92
186+
│ │ ├── data
187+
├── metadata
188+
│ ├── ks_1_db_1
189+
│ │ └── ks_1_t_92.sql
190+
│ ├── ks_1_db_2
191+
│ └── system
192+
```
193+
194+
Each table directory will be identified by both the keyspace ID and the table ID.
195+
196+
#### Cache
197+
198+
When decoding row data into a columnar format, TiFlash ignores the keyspace prefix. Table-related cache structures will be refactored into a map of `<KeyspaceID, TableID> -> <ValueType>` instead of `<TableID> -> <ValueType>`.
199+
200+
#### Schema sync
201+
202+
When syncing the schema from TiDB to TiFlash, TiFlash adds the keyspace prefix to requests sent to TiKV for specific table schemas.
203+
204+
#### Request handling
205+
206+
TiFlash requests carry the keyspace prefix. TiFlash parses `<KeyspaceID, TableID>` from the encoded key range. It then uses this information to route requests to the relevant table storage.
207+
208+
### Placement Rules
209+
210+
Placement rules are isolated by keyspace. TiDB must name placement rules using the keyspace prefix, such as `keyspace-<keyspace_id>-xxxx`. Additionally, the key range of some rules must be encoded.

docs/design/imgs/keyspace-arch.png

173 KB
Loading
66.5 KB
Loading

0 commit comments

Comments
 (0)