Skip to content

Commit 18b04a4

Browse files
authored
feat(storage): flexible KeyPrefix encoding for Block (risingwavelabs#8379)
1 parent 0c53349 commit 18b04a4

File tree

5 files changed

+452
-91
lines changed

5 files changed

+452
-91
lines changed

src/storage/benches/bench_block_iter.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#![feature(once_cell)]
16+
use std::sync::LazyLock;
17+
1518
use bytes::{BufMut, Bytes, BytesMut};
1619
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
1720
use risingwave_hummock_sdk::key::FullKey;
@@ -23,6 +26,7 @@ const TABLES_PER_SSTABLE: u32 = 10;
2326
const KEYS_PER_TABLE: u64 = 100;
2427
const RESTART_INTERVAL: usize = 16;
2528
const BLOCK_CAPACITY: usize = TABLES_PER_SSTABLE as usize * KEYS_PER_TABLE as usize * 64;
29+
const EXCHANGE_INTERVAL: usize = RESTART_INTERVAL / 2;
2630

2731
fn block_iter_next(block: BlockHolder) {
2832
let mut iter = BlockIterator::new(block);
@@ -86,11 +90,22 @@ fn bench_block_iter(c: &mut Criterion) {
8690
let l = data.len();
8791
let block = BlockHolder::from_owned_block(Box::new(Block::decode(data, l).unwrap()));
8892
let mut iter = BlockIterator::new(block);
93+
let mut item_count = 0;
94+
let mut ext_index = 0;
95+
let (mut k_ext, mut v_ext) = (&DATA_LEN_SET[ext_index].0, &DATA_LEN_SET[ext_index].1);
96+
8997
iter.seek_to_first();
9098
for t in 1..=TABLES_PER_SSTABLE {
9199
for i in 1..=KEYS_PER_TABLE {
92-
assert_eq!(iter.key(), FullKey::decode(&key(t, i)));
93-
assert_eq!(iter.value(), value(i).to_vec());
100+
item_count += 1;
101+
102+
if item_count % EXCHANGE_INTERVAL == 0 {
103+
ext_index = (ext_index + 1) % DATA_LEN_SET.len();
104+
(k_ext, v_ext) = (&DATA_LEN_SET[ext_index].0, &DATA_LEN_SET[ext_index].1);
105+
}
106+
107+
assert_eq!(iter.key(), FullKey::decode(&key(t, i, k_ext)));
108+
assert_eq!(iter.value(), value(i, v_ext).to_vec());
94109
iter.next();
95110
}
96111
}
@@ -100,30 +115,57 @@ fn bench_block_iter(c: &mut Criterion) {
100115
criterion_group!(benches, bench_block_iter);
101116
criterion_main!(benches);
102117

118+
static DATA_LEN_SET: LazyLock<Vec<(Vec<u8>, Vec<u8>)>> = LazyLock::new(|| {
119+
vec![
120+
(vec![b'a'; 10], vec![b'a'; 10]), // U8U8
121+
(vec![b'a'; 10], vec![b'a'; 300]), // U8U16
122+
(vec![b'a'; 100], vec![b'a'; 65550]), // U8U32
123+
(vec![b'a'; 300], vec![b'a'; 100]), // U16U8
124+
(vec![b'a'; 300], vec![b'a'; 300]), // U16U16
125+
(vec![b'a'; 300], vec![b'a'; 65550]), // U16U32
126+
(vec![b'a'; 65550], vec![b'a'; 100]), // U32U8
127+
(vec![b'a'; 65550], vec![b'a'; 300]), // U32U16
128+
(vec![b'a'; 65550], vec![b'a'; 65550]), // U32U32
129+
]
130+
});
131+
103132
fn build_block_data(t: u32, i: u64) -> Bytes {
104133
let options = BlockBuilderOptions {
105134
capacity: BLOCK_CAPACITY,
106135
compression_algorithm: CompressionAlgorithm::None,
107136
restart_interval: RESTART_INTERVAL,
108137
};
109138
let mut builder = BlockBuilder::new(options);
139+
let mut item_count = 0;
140+
let mut ext_index = 0;
141+
let (mut k_ext, mut v_ext) = (&DATA_LEN_SET[ext_index].0, &DATA_LEN_SET[ext_index].1);
142+
110143
for tt in 1..=t {
111144
for ii in 1..=i {
112-
builder.add(FullKey::decode(&key(tt, ii)), &value(ii));
145+
item_count += 1;
146+
147+
if item_count % EXCHANGE_INTERVAL == 0 {
148+
ext_index = (ext_index + 1) % DATA_LEN_SET.len();
149+
(k_ext, v_ext) = (&DATA_LEN_SET[ext_index].0, &DATA_LEN_SET[ext_index].1);
150+
}
151+
152+
builder.add(FullKey::decode(&key(tt, ii, k_ext)), &value(ii, v_ext));
113153
}
114154
}
115155
Bytes::from(builder.build().to_vec())
116156
}
117157

118-
fn key(t: u32, i: u64) -> Bytes {
158+
fn key(t: u32, i: u64, ext: &[u8]) -> Bytes {
119159
let mut buf = BytesMut::new();
160+
buf.put_slice(ext);
120161
buf.put_u32(t);
121162
buf.put_u64(i);
122163
buf.freeze()
123164
}
124165

125-
fn value(i: u64) -> Bytes {
166+
fn value(i: u64, ext: &[u8]) -> Bytes {
126167
let mut buf = BytesMut::new();
127168
buf.put_u64(i);
169+
buf.put(ext);
128170
buf.freeze()
129171
}

src/storage/src/hummock/block_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum BlockEntry {
3838

3939
pub struct BlockHolder {
4040
_handle: BlockEntry,
41-
block: *const Block,
41+
pub block: *const Block,
4242
}
4343

4444
impl BlockHolder {

0 commit comments

Comments
 (0)