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
//! Locating stable row ids in the physical files that currently hold them.
use std::collections::{HashMap, HashSet};
use futures::{StreamExt, TryStreamExt, stream};
use object_store::path::Path;
use tracing::{debug, warn};
use super::{ReadOnlyCatalog, SUMMARY_READ_CONCURRENCY, WARM_TABLE_CONCURRENCY};
use crate::{
catalog::{CatalogSnapshot, DataFileId, DataFileInfo, FileRowCandidate, TableId},
data_file::{self, DataStore, FileSummary},
error::Result,
};
/// What one warming pass did.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RowSummaryWarmth {
/// Current data files the pass looked at.
pub files_considered: u64,
/// Files whose row-id column it read and cached. The rest were already
/// resident or answer from their dense range.
pub summaries_built: u64,
/// Files it could not summarize. They stay correct but cold: a later
/// lookup leaves every requested row a candidate for them.
pub files_failed: u64,
}
impl ReadOnlyCatalog {
/// Resolves each of the table's current data files to its summary,
/// pairing every result with the file it came from.
async fn file_summaries(
&self,
store: &DataStore,
data_prefix: &str,
table_prefix: &str,
table: TableId,
files: Vec<DataFileInfo>,
) -> Vec<(DataFileId, Result<FileSummary>)> {
stream::iter(files.into_iter().map(|file| {
let relative = match (file.path_is_relative, data_prefix.is_empty()) {
(false, _) => file.path,
(true, true) => format!("{table_prefix}{}", file.path),
(true, false) => format!("{data_prefix}/{table_prefix}{}", file.path),
};
let path = Path::from(relative.as_str());
let store = store.clone();
let metrics = self.data_read_metrics();
async move {
let summary = data_file::file_summary(
data_file::ParquetFile::new(
store,
path,
file.file_size_bytes,
file.footer_size,
)
.with_metrics(metrics),
table.get(),
file.id.get(),
file.row_id_start,
file.record_count,
)
.await;
(file.id, summary)
}
}))
.buffer_unordered(SUMMARY_READ_CONCURRENCY)
.collect::<Vec<_>>()
.await
}
/// Locates `row_ids` among the table's current data files.
///
/// Head-only, exactly as an equality lookup is. Every degraded path
/// broadens the answer: a file that cannot be summarized leaves each
/// requested id a candidate for it, and an id found in neither a
/// current file nor current inline data comes back with no file. The
/// result can therefore name a file that does not hold a row, but
/// never omits one that does.
///
/// `data_store` is the `DATA_PATH` object store; a table whose rows are
/// entirely inlined needs none.
///
/// # Errors
///
/// Returns [`Error::NotFound`](crate::Error::NotFound) if the table does
/// not exist, or a store error if the head view cannot be read. A file
/// that cannot be read is not an error: it broadens the answer instead.
pub async fn locate_row_ids(
&self,
data_store: Option<DataStore>,
data_prefix: &str,
table: TableId,
row_ids: Vec<u64>,
) -> Result<Vec<FileRowCandidate>> {
if row_ids.is_empty() {
return Ok(Vec::new());
}
let snapshot = self.snapshot().await?;
let file_placements = async {
let Some(store) = &data_store else {
return Ok(HashMap::<u64, Vec<DataFileId>>::new());
};
let table_prefix = snapshot.table_data_prefix(table)?;
let files = snapshot.data_files_of(table);
let mut placements = HashMap::<u64, Vec<DataFileId>>::new();
for (data_file_id, summary) in self
.file_summaries(store, data_prefix, &table_prefix, table, files)
.await
{
let matched = match summary {
Ok(summary) => summary.matching(&row_ids),
Err(error) => {
warn!(
table_id = table.get(),
data_file_id = data_file_id.get(),
%error,
"row location fell back to every requested row for this file"
);
row_ids.clone()
}
};
for row_id in matched {
placements.entry(row_id).or_default().push(data_file_id);
}
}
Ok(placements)
};
// A row can be inlined and still hold an expired physical copy in a
// current file, so the inlined copy is its own candidate. Only the
// ids are needed, so the bodies stay unread.
let requested: HashSet<u64> = row_ids.iter().copied().collect();
let inlined = async {
Ok::<_, crate::Error>(
self.live_inline_row_ids(table)
.await?
.into_iter()
.filter(|row_id| requested.contains(row_id))
.collect::<HashSet<u64>>(),
)
};
let (mut placements, inlined) = futures::try_join!(file_placements, inlined)?;
// Emit in the caller's order; locating must not reorder.
let mut seen = HashSet::new();
let located = row_ids
.iter()
.copied()
.filter(|row_id| seen.insert(*row_id))
.flat_map(|row_id| {
// A live inlined row, and a row located nowhere at all, are both
// reported without a file rather than dropped.
let inline_placements =
if inlined.contains(&row_id) || !placements.contains_key(&row_id) {
Some(FileRowCandidate {
row_id,
data_file_id: None,
})
} else {
None
};
let file_placements = if let Some(files) = placements.get_mut(&row_id) {
// Summaries resolve concurrently, so this arrives in
// completion order; candidates must not vary run to run.
files.sort_unstable();
files.dedup();
files
.iter()
.map(|data_file_id| FileRowCandidate {
row_id,
data_file_id: Some(*data_file_id),
})
.collect()
} else {
Vec::new()
};
file_placements.into_iter().chain(inline_placements)
})
.collect();
Ok(located)
}
/// Builds and caches the summaries a lookup would otherwise build cold.
///
/// Best-effort and idempotent: a file already resident, or answering
/// from its dense range, costs nothing, and a file that cannot be read
/// is counted rather than raised.
///
/// # Errors
///
/// Returns a store error if the head view cannot be read. Per-file
/// failures are reported in [`RowSummaryWarmth::files_failed`].
pub async fn warm_row_summaries(
&self,
data_store: DataStore,
data_prefix: &str,
table: TableId,
) -> Result<RowSummaryWarmth> {
let snapshot = self.snapshot().await?;
self.warm_table(&snapshot, &data_store, data_prefix, table)
.await
}
/// Builds and caches the summaries a lookup would otherwise build cold,
/// for every table the catalog currently holds.
///
/// # Errors
///
/// Returns a store error if the head view cannot be read. Per-file
/// failures are reported in [`RowSummaryWarmth::files_failed`].
pub async fn warm_all_row_summaries(
&self,
data_store: DataStore,
data_prefix: &str,
) -> Result<RowSummaryWarmth> {
let snapshot = self.snapshot().await?;
let tables = snapshot
.schemas()
.into_iter()
.flat_map(|schema| snapshot.tables_in(schema.id))
.map(|table| table.id)
.collect::<Vec<_>>();
self.warm_selected_row_summaries(data_store, data_prefix, tables)
.await
}
/// Builds and caches the summaries a lookup would otherwise build cold,
/// for the given tables.
///
/// # Errors
///
/// Returns a store error if the head view cannot be read. Per-file
/// failures are reported in [`RowSummaryWarmth::files_failed`].
pub async fn warm_selected_row_summaries(
&self,
data_store: DataStore,
data_prefix: &str,
tables: Vec<TableId>,
) -> Result<RowSummaryWarmth> {
let snapshot = self.snapshot().await?;
let total = stream::iter(tables)
.map(|table| self.warm_table(&snapshot, &data_store, data_prefix, table))
.buffer_unordered(WARM_TABLE_CONCURRENCY)
.try_collect::<Vec<_>>()
.await?
.into_iter()
.fold(RowSummaryWarmth::default(), |acc, table| RowSummaryWarmth {
files_considered: acc.files_considered.saturating_add(table.files_considered),
summaries_built: acc.summaries_built.saturating_add(table.summaries_built),
files_failed: acc.files_failed.saturating_add(table.files_failed),
});
Ok(total)
}
/// One table's warming pass against an already-resolved head view.
async fn warm_table(
&self,
snapshot: &CatalogSnapshot,
data_store: &DataStore,
data_prefix: &str,
table: TableId,
) -> Result<RowSummaryWarmth> {
let table_prefix = snapshot.table_data_prefix(table)?;
let files = snapshot.data_files_of(table);
let mut warmth = RowSummaryWarmth {
files_considered: u64::try_from(files.len()).unwrap_or(u64::MAX),
..RowSummaryWarmth::default()
};
for (data_file_id, summary) in self
.file_summaries(data_store, data_prefix, &table_prefix, table, files)
.await
{
match summary {
Ok(summary) if summary.built => {
warmth.summaries_built = warmth.summaries_built.saturating_add(1);
}
Ok(_) => {}
Err(error) => {
warmth.files_failed = warmth.files_failed.saturating_add(1);
warn!(
table_id = table.get(),
data_file_id = data_file_id.get(),
%error,
"row summary warm skipped a file it could not read"
);
}
}
}
debug!(
table_id = table.get(),
files_considered = warmth.files_considered,
summaries_built = warmth.summaries_built,
files_failed = warmth.files_failed,
"row summaries warmed"
);
Ok(warmth)
}
}