In what format will the keys written by tikv be stored in Rocksdb-kv?

In what format will the keys written by tikv be stored in Rocksdb-kv?
For example:
prewritekey: ‘a’

rocksdb-defaultcf: ?
rocksdb-lockcf: ?
rocksdb-writecf: ?

If you want to write a key like “a” to the TiKV, it will be transformed multiple times before being written into the RocksDB. Since you mentioned different CFs, so I assume that we are talking about the TxnKV here.

We call the human-readable key like “a” the raw key, before TiKV processing it internally, we will first encode it to the memcomparable format, then what we get here is called an encoded key.

After encoding, depending on the different CFs, the key will be appended to a different timestamp, i.e, the MVCC version, or not.

  • For Default CF, it will be like encoded_key.append_ts(start_ts).
  • For Write CF, it will be like encoded_key.append_ts(commit_ts).
  • For Lock CF, it won’t be appended any timestamp.

And before writing a key into the RocksDB, we will prefix it with DATA_PREFIX_KEY to separate it from some metadata keys.

Reference:

1 Like

Can you point out the code to add DATA_PREFIX ?

You can refer to the call to the keys::data_key function: https://github.com/tikv/tikv/blob/90c4a0602040102fabf45ea8e8bfac33f4472d07/components/keys/src/lib.rs#L205

In wich stage does the prefix added ? Before raft or after raft?

It’s added right before we apply the data, so it’s after the Raft: https://github.com/tikv/tikv/blob/68f99ae034376f5629d80fa7712796a61dc5d50e/components/raftstore/src/store/fsm/apply.rs#L1601

1 Like