-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathstore.rs
420 lines (356 loc) · 15.9 KB
/
store.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use arrow2::datatypes::DataType as ArrowDataType;
use nohash_hasher::IntMap;
use re_chunk::{Chunk, ChunkId, RowId};
use re_log_types::{EntityPath, StoreId, TimeInt, Timeline};
use re_types_core::ComponentName;
use crate::{ChunkStoreChunkStats, ChunkStoreError, ChunkStoreResult};
// ---
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChunkStoreConfig {
/// If `true` (the default), the store will emit events when its contents are modified in
/// any way (insertion, GC), that can be subscribed to.
///
/// Leaving this disabled can lead to major performance improvements on the ingestion path
/// in some workloads, provided that the subscribers aren't needed (e.g. headless mode).
pub enable_changelog: bool,
/// What is the threshold, in bytes, after which a [`Chunk`] cannot be compacted any further?
///
/// This is a multi-dimensional trade-off:
/// * Larger chunks lead to less fixed overhead introduced by metadata, indices and such. Good.
/// * Larger chunks lead to slower query execution on some unhappy paths. Bad.
/// * Larger chunks lead to slower and slower compaction as chunks grow larger. Bad.
/// * Larger chunks lead to coarser garbage collection. Good or bad depending on use case.
/// * Larger chunks lead to less precision in e.g. the time panel. Bad.
///
/// Empirical testing shows that the space overhead gains rapidly diminish beyond ~1000 rows,
/// which is the default row threshold.
/// The default byte threshold is set to 8MiB, which is a reasonable unit of work when e.g.
/// sending chunks over the network.
pub chunk_max_bytes: u64,
/// What is the threshold, in rows, after which a [`Chunk`] cannot be compacted any further?
///
/// This specifically applies to time-sorted chunks.
/// See also [`ChunkStoreConfig::chunk_max_rows_if_unsorted`].
///
/// This is a multi-dimensional trade-off:
/// * Larger chunks lead to less fixed overhead caused to metadata, indices and such. Good.
/// * Larger chunks lead to slower query execution in unhappy paths. Bad.
/// * Larger chunks lead to slower and slower compaction as chunks grow larger. Bad.
/// * Larger chunks lead to coarser garbage collection. Bad.
/// * Larger chunks lead to less precision in e.g. the time panel. Bad.
///
/// Empirical testing shows that the space overhead gains rapidly diminish beyond ~1000 rows,
/// which is the default row threshold.
/// The default byte threshold is set to 8MiB, which is a reasonable unit of work when e.g.
/// sending chunks over the network.
pub chunk_max_rows: u64,
/// What is the threshold, in rows, after which a [`Chunk`] cannot be compacted any further?
///
/// This specifically applies to _non_ time-sorted chunks.
/// See also [`ChunkStoreConfig::chunk_max_rows`].
///
/// This is a multi-dimensional trade-off:
/// * Larger chunks lead to less fixed overhead caused to metadata, indices and such. Good.
/// * Larger chunks lead to slower query execution in unhappy paths. Bad.
/// * Larger chunks lead to slower and slower compaction as chunks grow larger. Bad.
/// * Larger chunks lead to coarser garbage collection. Bad.
/// * Larger chunks lead to less precision in e.g. the time panel. Bad.
///
/// Empirical testing shows that the space overhead gains rapidly diminish beyond ~1000 rows,
/// which is the default row threshold.
/// The default byte threshold is set to 8MiB, which is a reasonable unit of work when e.g.
/// sending chunks over the network.
pub chunk_max_rows_if_unsorted: u64,
//
// TODO(cmc): It could make sense to have time-range-based thresholds in here, since the time
// range covered by a chunk has direct effects on A) the complexity of backward walks and
// B) in downstream subscribers (e.g. the precision of the time panel).
//
// In practice this is highly recording-dependent, and would require either to make it
// user-configurable per-recording, or use heuristics to compute it on the fly.
//
// The added complexity just isn't worth it at the moment.
// Maybe at some point.
}
impl Default for ChunkStoreConfig {
#[inline]
fn default() -> Self {
Self::DEFAULT
}
}
impl ChunkStoreConfig {
/// Default configuration, applicable to most use cases, according to empirical testing.
pub const DEFAULT: Self = Self {
enable_changelog: true,
chunk_max_bytes: 8 * 1024 * 1024,
chunk_max_rows: 1024,
chunk_max_rows_if_unsorted: 256,
};
/// Environment variable to configure [`Self::enable_changelog`].
pub const ENV_STORE_ENABLE_CHANGELOG: &'static str = "RERUN_STORE_ENABLE_CHANGELOG";
/// Environment variable to configure [`Self::chunk_max_bytes`].
pub const ENV_CHUNK_MAX_BYTES: &'static str = "RERUN_CHUNK_MAX_BYTES";
/// Environment variable to configure [`Self::chunk_max_rows`].
pub const ENV_CHUNK_MAX_ROWS: &'static str = "RERUN_CHUNK_MAX_ROWS";
/// Environment variable to configure [`Self::chunk_max_rows_if_unsorted`].
//
// NOTE: Shared with the same env-var on the batcher side, for consistency.
pub const ENV_CHUNK_MAX_ROWS_IF_UNSORTED: &'static str = "RERUN_CHUNK_MAX_ROWS_IF_UNSORTED";
/// Creates a new `ChunkStoreConfig` using the default values, optionally overridden
/// through the environment.
///
/// See [`Self::apply_env`].
#[inline]
pub fn from_env() -> ChunkStoreResult<Self> {
Self::default().apply_env()
}
/// Returns a copy of `self`, overriding existing fields with values from the environment if
/// they are present.
///
/// See [`Self::ENV_STORE_ENABLE_CHANGELOG`], [`Self::ENV_CHUNK_MAX_BYTES`], [`Self::ENV_CHUNK_MAX_ROWS`]
/// and [`Self::ENV_CHUNK_MAX_ROWS_IF_UNSORTED`].
pub fn apply_env(&self) -> ChunkStoreResult<Self> {
let mut new = self.clone();
if let Ok(s) = std::env::var(Self::ENV_STORE_ENABLE_CHANGELOG) {
new.enable_changelog = s.parse().map_err(|err| ChunkStoreError::ParseConfig {
name: Self::ENV_STORE_ENABLE_CHANGELOG,
value: s.clone(),
err: Box::new(err),
})?;
}
if let Ok(s) = std::env::var(Self::ENV_CHUNK_MAX_BYTES) {
new.chunk_max_bytes = s.parse().map_err(|err| ChunkStoreError::ParseConfig {
name: Self::ENV_CHUNK_MAX_BYTES,
value: s.clone(),
err: Box::new(err),
})?;
}
if let Ok(s) = std::env::var(Self::ENV_CHUNK_MAX_ROWS) {
new.chunk_max_rows = s.parse().map_err(|err| ChunkStoreError::ParseConfig {
name: Self::ENV_CHUNK_MAX_ROWS,
value: s.clone(),
err: Box::new(err),
})?;
}
if let Ok(s) = std::env::var(Self::ENV_CHUNK_MAX_ROWS_IF_UNSORTED) {
new.chunk_max_rows_if_unsorted =
s.parse().map_err(|err| ChunkStoreError::ParseConfig {
name: Self::ENV_CHUNK_MAX_ROWS_IF_UNSORTED,
value: s.clone(),
err: Box::new(err),
})?;
}
Ok(new)
}
}
#[test]
fn chunk_store_config() {
// Detect breaking changes in our environment variables.
std::env::set_var("RERUN_STORE_ENABLE_CHANGELOG", "false");
std::env::set_var("RERUN_CHUNK_MAX_BYTES", "42");
std::env::set_var("RERUN_CHUNK_MAX_ROWS", "666");
std::env::set_var("RERUN_CHUNK_MAX_ROWS_IF_UNSORTED", "999");
let config = ChunkStoreConfig::from_env().unwrap();
let expected = ChunkStoreConfig {
enable_changelog: false,
chunk_max_bytes: 42,
chunk_max_rows: 666,
chunk_max_rows_if_unsorted: 999,
};
assert_eq!(expected, config);
}
// ---
pub type ChunkIdSet = BTreeSet<ChunkId>;
#[derive(Default, Debug, Clone)]
pub struct ChunkIdSetPerTime {
/// Keeps track of the longest interval being currently stored in the two maps below.
///
/// This is used to bound the backwards linear walk when looking for overlapping chunks in
/// latest-at queries.
///
/// See [`ChunkStore::latest_at_relevant_chunks`] implementation comments for more details.
pub(crate) max_interval_length: u64,
pub(crate) per_start_time: BTreeMap<TimeInt, ChunkIdSet>,
pub(crate) per_end_time: BTreeMap<TimeInt, ChunkIdSet>,
}
pub type ChunkIdSetPerTimePerComponent = BTreeMap<ComponentName, ChunkIdSetPerTime>;
pub type ChunkIdSetPerTimePerComponentPerTimeline =
BTreeMap<Timeline, ChunkIdSetPerTimePerComponent>;
pub type ChunkIdSetPerTimePerComponentPerTimelinePerEntity =
BTreeMap<EntityPath, ChunkIdSetPerTimePerComponentPerTimeline>;
pub type ChunkIdPerComponent = BTreeMap<ComponentName, ChunkId>;
pub type ChunkIdPerComponentPerEntity = BTreeMap<EntityPath, ChunkIdPerComponent>;
// ---
/// Incremented on each edit.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ChunkStoreGeneration {
insert_id: u64,
gc_id: u64,
}
/// A complete chunk store: covers all timelines, all entities, everything.
///
/// The chunk store _always_ works at the chunk level, whether it is for write & read queries or
/// garbage collection. It is completely oblivious to individual rows.
///
/// Use the `Display` implementation for a detailed view of the internals.
#[derive(Debug)]
pub struct ChunkStore {
pub(crate) id: StoreId,
/// The configuration of the chunk store (e.g. compaction settings).
pub(crate) config: ChunkStoreConfig,
/// Keeps track of the _latest_ datatype information for all component types that have been written
/// to the store so far.
///
/// See also [`Self::lookup_datatype`].
//
// TODO(#1809): replace this with a centralized Arrow registry.
// TODO(cmc): this would become fairly problematic in a world where each chunk can use a
// different datatype for a given component.
pub(crate) type_registry: IntMap<ComponentName, ArrowDataType>,
pub(crate) chunks_per_chunk_id: BTreeMap<ChunkId, Arc<Chunk>>,
/// All [`ChunkId`]s currently in the store, indexed by the smallest [`RowId`] in each of them.
///
/// This is effectively all chunks in global data order. Used for garbage collection.
///
/// This is a map of vecs instead of individual [`ChunkId`] in order to better support
/// duplicated [`RowId`]s.
pub(crate) chunk_ids_per_min_row_id: BTreeMap<RowId, Vec<ChunkId>>,
/// All temporal [`ChunkId`]s for all entities on all timelines.
///
/// See also [`Self::static_chunk_ids_per_entity`].
pub(crate) temporal_chunk_ids_per_entity: ChunkIdSetPerTimePerComponentPerTimelinePerEntity,
/// Accumulated size statitistics for all temporal [`Chunk`]s currently present in the store.
///
/// This is too costly to be computed from scratch every frame, and is required by e.g. the GC.
pub(crate) temporal_chunks_stats: ChunkStoreChunkStats,
/// Static data. Never garbage collected.
///
/// Static data unconditionally shadows temporal data at query time.
///
/// Existing temporal will not be removed. Events won't be fired.
pub(crate) static_chunk_ids_per_entity: ChunkIdPerComponentPerEntity,
/// Accumulated size statitistics for all static [`Chunk`]s currently present in the store.
///
/// This is too costly to be computed from scratch every frame, and is required by e.g. the GC.
pub(crate) static_chunks_stats: ChunkStoreChunkStats,
// pub(crate) static_tables: BTreeMap<EntityPathHash, StaticTable>,
/// Monotonically increasing ID for insertions.
pub(crate) insert_id: u64,
/// Monotonically increasing ID for queries.
pub(crate) query_id: AtomicU64,
/// Monotonically increasing ID for GCs.
pub(crate) gc_id: u64,
/// Monotonically increasing ID for store events.
pub(crate) event_id: AtomicU64,
}
impl Clone for ChunkStore {
#[inline]
fn clone(&self) -> Self {
Self {
id: self.id.clone(),
config: self.config.clone(),
type_registry: self.type_registry.clone(),
chunks_per_chunk_id: self.chunks_per_chunk_id.clone(),
chunk_ids_per_min_row_id: self.chunk_ids_per_min_row_id.clone(),
temporal_chunk_ids_per_entity: self.temporal_chunk_ids_per_entity.clone(),
temporal_chunks_stats: self.temporal_chunks_stats,
static_chunk_ids_per_entity: self.static_chunk_ids_per_entity.clone(),
static_chunks_stats: self.static_chunks_stats,
insert_id: Default::default(),
query_id: Default::default(),
gc_id: Default::default(),
event_id: Default::default(),
}
}
}
impl std::fmt::Display for ChunkStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
id,
config,
type_registry: _,
chunks_per_chunk_id,
chunk_ids_per_min_row_id: chunk_id_per_min_row_id,
temporal_chunk_ids_per_entity: _,
temporal_chunks_stats,
static_chunk_ids_per_entity: _,
static_chunks_stats,
insert_id: _,
query_id: _,
gc_id: _,
event_id: _,
} = self;
f.write_str("ChunkStore {\n")?;
f.write_str(&indent::indent_all_by(4, format!("id: {id}\n")))?;
f.write_str(&indent::indent_all_by(4, format!("config: {config:?}\n")))?;
f.write_str(&indent::indent_all_by(4, "stats: {\n"))?;
f.write_str(&indent::indent_all_by(
8,
format!("{}", *static_chunks_stats + *temporal_chunks_stats),
))?;
f.write_str(&indent::indent_all_by(4, "}\n"))?;
f.write_str(&indent::indent_all_by(4, "chunks: [\n"))?;
for chunk_id in chunk_id_per_min_row_id.values().flatten() {
if let Some(chunk) = chunks_per_chunk_id.get(chunk_id) {
f.write_str(&indent::indent_all_by(8, format!("{chunk}\n")))?;
} else {
f.write_str(&indent::indent_all_by(8, "<not_found>\n"))?;
}
}
f.write_str(&indent::indent_all_by(4, "]\n"))?;
f.write_str("}")?;
Ok(())
}
}
// ---
impl ChunkStore {
#[inline]
pub fn new(id: StoreId, config: ChunkStoreConfig) -> Self {
Self {
id,
config,
type_registry: Default::default(),
chunk_ids_per_min_row_id: Default::default(),
chunks_per_chunk_id: Default::default(),
temporal_chunk_ids_per_entity: Default::default(),
temporal_chunks_stats: Default::default(),
static_chunk_ids_per_entity: Default::default(),
static_chunks_stats: Default::default(),
insert_id: 0,
query_id: AtomicU64::new(0),
gc_id: 0,
event_id: AtomicU64::new(0),
}
}
#[inline]
pub fn id(&self) -> &StoreId {
&self.id
}
/// Return the current [`ChunkStoreGeneration`]. This can be used to determine whether the
/// database has been modified since the last time it was queried.
#[inline]
pub fn generation(&self) -> ChunkStoreGeneration {
ChunkStoreGeneration {
insert_id: self.insert_id,
gc_id: self.gc_id,
}
}
/// See [`ChunkStoreConfig`] for more information about configuration.
#[inline]
pub fn config(&self) -> &ChunkStoreConfig {
&self.config
}
/// Iterate over all chunks in the store, in ascending [`ChunkId`] order.
#[inline]
pub fn iter_chunks(&self) -> impl Iterator<Item = &Arc<Chunk>> + '_ {
self.chunks_per_chunk_id.values()
}
/// Lookup the _latest_ arrow [`ArrowDataType`] used by a specific [`re_types_core::Component`].
#[inline]
pub fn lookup_datatype(&self, component_name: &ComponentName) -> Option<&ArrowDataType> {
self.type_registry.get(component_name)
}
}