-
Notifications
You must be signed in to change notification settings - Fork 124
/
fast_slow_store.rs
429 lines (393 loc) · 15.9 KB
/
fast_slow_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
421
422
423
424
425
426
427
428
429
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::borrow::BorrowMut;
use std::cmp::{max, min};
use std::ops::Range;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Weak};
use async_trait::async_trait;
use futures::{join, FutureExt};
use nativelink_error::{make_err, Code, Error, ResultExt};
use nativelink_metric::MetricsComponent;
use nativelink_util::buf_channel::{
make_buf_channel_pair, DropCloserReadHalf, DropCloserWriteHalf,
};
use nativelink_util::fs;
use nativelink_util::health_utils::{default_health_status_indicator, HealthStatusIndicator};
use nativelink_util::store_trait::{
slow_update_store_with_file, Store, StoreDriver, StoreKey, StoreLike, StoreOptimizations,
UploadSizeInfo,
};
// TODO(blaise.bruer) This store needs to be evaluated for more efficient memory usage,
// there are many copies happening internally.
// TODO(blaise.bruer) We should consider copying the data in the background to allow the
// client to hang up while the data is buffered. An alternative is to possibly make a
// "BufferedStore" that could be placed on the "slow" store that would hang up early
// if data is in the buffer.
#[derive(MetricsComponent)]
pub struct FastSlowStore {
#[metric(group = "fast_store")]
fast_store: Store,
#[metric(group = "slow_store")]
slow_store: Store,
weak_self: Weak<Self>,
#[metric]
metrics: FastSlowStoreMetrics,
}
impl FastSlowStore {
pub fn new(
_config: &nativelink_config::stores::FastSlowStore,
fast_store: Store,
slow_store: Store,
) -> Arc<Self> {
Arc::new_cyclic(|weak_self| Self {
fast_store,
slow_store,
weak_self: weak_self.clone(),
metrics: FastSlowStoreMetrics::default(),
})
}
pub fn fast_store(&self) -> &Store {
&self.fast_store
}
pub fn slow_store(&self) -> &Store {
&self.slow_store
}
pub fn get_arc(&self) -> Option<Arc<Self>> {
self.weak_self.upgrade()
}
/// Ensure our fast store is populated. This should be kept as a low
/// cost function. Since the data itself is shared and not copied it should be fairly
/// low cost to just discard the data, but does cost a few mutex locks while
/// streaming.
pub async fn populate_fast_store(&self, key: StoreKey<'_>) -> Result<(), Error> {
let maybe_size_info = self
.fast_store
.has(key.borrow())
.await
.err_tip(|| "While querying in populate_fast_store")?;
if maybe_size_info.is_some() {
return Ok(());
}
// TODO(blaise.bruer) This is extremely inefficient, since we are just trying
// to send the stream to /dev/null. Maybe we could instead make a version of
// the stream that can send to the drain more efficiently?
let (tx, mut rx) = make_buf_channel_pair();
let drain_fut = async move {
while !rx.recv().await?.is_empty() {}
Ok(())
};
let (drain_res, get_res) = join!(drain_fut, StoreDriver::get(Pin::new(self), key, tx));
get_res.err_tip(|| "Failed to populate()").merge(drain_res)
}
/// Returns the range of bytes that should be sent given a slice bounds
/// offset so the output range maps the received_range.start to 0.
// TODO(allada) This should be put into utils, as this logic is used
// elsewhere in the code.
pub fn calculate_range(
received_range: &Range<usize>,
send_range: &Range<usize>,
) -> Option<Range<usize>> {
// Protect against subtraction overflow.
if received_range.start >= received_range.end {
return None;
}
let start = max(received_range.start, send_range.start);
let end = min(received_range.end, send_range.end);
if received_range.contains(&start) && received_range.contains(&(end - 1)) {
// Offset both to the start of the received_range.
Some(start - received_range.start..end - received_range.start)
} else {
None
}
}
}
#[async_trait]
impl StoreDriver for FastSlowStore {
async fn has_with_results(
self: Pin<&Self>,
key: &[StoreKey<'_>],
results: &mut [Option<usize>],
) -> Result<(), Error> {
// If our slow store is a noop store, it'll always return a 404,
// so only check the fast store in such case.
let slow_store = self.slow_store.inner_store::<StoreKey<'_>>(None);
if slow_store.optimized_for(StoreOptimizations::NoopDownloads) {
return self.fast_store.has_with_results(key, results).await;
}
// Only check the slow store because if it's not there, then something
// down stream might be unable to get it. This should not affect
// workers as they only use get() and a CAS can use an
// ExistenceCacheStore to avoid the bottleneck.
self.slow_store.has_with_results(key, results).await
}
async fn update(
self: Pin<&Self>,
key: StoreKey<'_>,
mut reader: DropCloserReadHalf,
size_info: UploadSizeInfo,
) -> Result<(), Error> {
// If either one of our stores is a noop store, bypass the multiplexing
// and just use the store that is not a noop store.
let slow_store = self.slow_store.inner_store(Some(key.borrow()));
if slow_store.optimized_for(StoreOptimizations::NoopUpdates) {
return self.fast_store.update(key, reader, size_info).await;
}
let fast_store = self.fast_store.inner_store(Some(key.borrow()));
if fast_store.optimized_for(StoreOptimizations::NoopUpdates) {
return self.slow_store.update(key, reader, size_info).await;
}
let (mut fast_tx, fast_rx) = make_buf_channel_pair();
let (mut slow_tx, slow_rx) = make_buf_channel_pair();
let data_stream_fut = async move {
loop {
let buffer = reader
.recv()
.await
.err_tip(|| "Failed to read buffer in fastslow store")?;
if buffer.is_empty() {
// EOF received.
fast_tx.send_eof().err_tip(|| {
"Failed to write eof to fast store in fast_slow store update"
})?;
slow_tx
.send_eof()
.err_tip(|| "Failed to write eof to writer in fast_slow store update")?;
return Result::<(), Error>::Ok(());
}
let (fast_result, slow_result) =
join!(fast_tx.send(buffer.clone()), slow_tx.send(buffer));
fast_result
.map_err(|e| {
make_err!(
Code::Internal,
"Failed to send message to fast_store in fast_slow_store {:?}",
e
)
})
.merge(slow_result.map_err(|e| {
make_err!(
Code::Internal,
"Failed to send message to slow_store in fast_slow store {:?}",
e
)
}))?;
}
};
let fast_store_fut = self.fast_store.update(key.borrow(), fast_rx, size_info);
let slow_store_fut = self.slow_store.update(key.borrow(), slow_rx, size_info);
let (data_stream_res, fast_res, slow_res) =
join!(data_stream_fut, fast_store_fut, slow_store_fut);
data_stream_res.merge(fast_res).merge(slow_res)?;
Ok(())
}
/// FastSlowStore has optimiations for dealing with files.
fn optimized_for(&self, optimization: StoreOptimizations) -> bool {
optimization == StoreOptimizations::FileUpdates
}
/// Optimized variation to consume the file if one of the stores is a
/// filesystem store. This makes the operation a move instead of a copy
/// dramatically increasing performance for large files.
async fn update_with_whole_file(
self: Pin<&Self>,
key: StoreKey<'_>,
mut file: fs::ResumeableFileSlot,
upload_size: UploadSizeInfo,
) -> Result<Option<fs::ResumeableFileSlot>, Error> {
if self
.fast_store
.optimized_for(StoreOptimizations::FileUpdates)
{
if !self
.slow_store
.optimized_for(StoreOptimizations::NoopUpdates)
{
slow_update_store_with_file(
self.slow_store.as_store_driver_pin(),
key.borrow(),
&mut file,
upload_size,
)
.await
.err_tip(|| "In FastSlowStore::update_with_whole_file slow_store")?;
}
return self
.fast_store
.update_with_whole_file(key, file, upload_size)
.await;
}
if self
.slow_store
.optimized_for(StoreOptimizations::FileUpdates)
{
if !self
.fast_store
.optimized_for(StoreOptimizations::NoopUpdates)
{
slow_update_store_with_file(
self.fast_store.as_store_driver_pin(),
key.borrow(),
&mut file,
upload_size,
)
.await
.err_tip(|| "In FastSlowStore::update_with_whole_file fast_store")?;
}
return self
.slow_store
.update_with_whole_file(key, file, upload_size)
.await;
}
slow_update_store_with_file(self, key, &mut file, upload_size)
.await
.err_tip(|| "In FastSlowStore::update_with_whole_file")?;
Ok(Some(file))
}
async fn get_part(
self: Pin<&Self>,
key: StoreKey<'_>,
writer: &mut DropCloserWriteHalf,
offset: usize,
length: Option<usize>,
) -> Result<(), Error> {
// TODO(blaise.bruer) Investigate if we should maybe ignore errors here instead of
// forwarding the up.
if self.fast_store.has(key.borrow()).await?.is_some() {
self.metrics
.fast_store_hit_count
.fetch_add(1, Ordering::Acquire);
self.fast_store
.get_part(key, writer.borrow_mut(), offset, length)
.await?;
self.metrics
.fast_store_downloaded_bytes
.fetch_add(writer.get_bytes_written(), Ordering::Acquire);
return Ok(());
}
let sz = self
.slow_store
.has(key.borrow())
.await
.err_tip(|| "Failed to run has() on slow store")?
.ok_or_else(|| {
make_err!(
Code::NotFound,
"Object {} not found in either fast or slow store",
key.as_str()
)
})?;
self.metrics
.slow_store_hit_count
.fetch_add(1, Ordering::Acquire);
let send_range = offset..length.map_or(usize::MAX, |length| length + offset);
let mut bytes_received: usize = 0;
let (mut fast_tx, fast_rx) = make_buf_channel_pair();
let (slow_tx, mut slow_rx) = make_buf_channel_pair();
let data_stream_fut = async move {
let mut writer_pin = Pin::new(writer);
loop {
let output_buf = slow_rx
.recv()
.await
.err_tip(|| "Failed to read data data buffer from slow store")?;
if output_buf.is_empty() {
// Write out our EOF.
// We are dropped as soon as we send_eof to writer_pin, so
// we wait until we've finished all of our joins to do that.
let fast_res = fast_tx.send_eof();
return Ok::<_, Error>((fast_res, writer_pin));
}
self.metrics
.slow_store_downloaded_bytes
.fetch_add(output_buf.len() as u64, Ordering::Acquire);
let writer_fut = if let Some(range) = Self::calculate_range(
&(bytes_received..bytes_received + output_buf.len()),
&send_range,
) {
writer_pin.send(output_buf.slice(range)).right_future()
} else {
futures::future::ready(Ok(())).left_future()
};
bytes_received += output_buf.len();
let (fast_tx_res, writer_res) = join!(fast_tx.send(output_buf), writer_fut);
fast_tx_res.err_tip(|| "Failed to write to fast store in fast_slow store")?;
writer_res.err_tip(|| "Failed to write result to writer in fast_slow store")?;
}
};
let slow_store_fut = self.slow_store.get(key.borrow(), slow_tx);
let fast_store_fut =
self.fast_store
.update(key.borrow(), fast_rx, UploadSizeInfo::ExactSize(sz));
let (data_stream_res, slow_res, fast_res) =
join!(data_stream_fut, slow_store_fut, fast_store_fut);
match data_stream_res {
Ok((fast_eof_res, mut writer_pin)) =>
// Sending the EOF will drop us almost immediately in bytestream_server
// so we perform it as the very last action in this method.
{
fast_eof_res
.merge(fast_res)
.merge(slow_res)
.merge(writer_pin.send_eof())
}
Err(err) => fast_res.merge(slow_res).merge(Err(err)),
}
}
fn inner_store(&self, _key: Option<StoreKey>) -> &dyn StoreDriver {
self
}
fn as_any<'a>(&'a self) -> &'a (dyn std::any::Any + Sync + Send + 'static) {
self
}
fn as_any_arc(self: Arc<Self>) -> Arc<dyn std::any::Any + Sync + Send + 'static> {
self
}
}
#[derive(Default, MetricsComponent)]
struct FastSlowStoreMetrics {
#[metric(help = "Hit count for the fast store")]
fast_store_hit_count: AtomicU64,
#[metric(help = "Downloaded bytes from the fast store")]
fast_store_downloaded_bytes: AtomicU64,
#[metric(help = "Hit count for the slow store")]
slow_store_hit_count: AtomicU64,
#[metric(help = "Downloaded bytes from the slow store")]
slow_store_downloaded_bytes: AtomicU64,
}
// impl MetricsComponent for FastSlowStoreMetrics {
// fn gather_metrics(&self, c: &mut CollectorState) {
// c.publish(
// "fast_store_hit_count",
// &self.fast_store_hit_count,
// "Hit count for the fast store",
// );
// c.publish(
// "fast_store_downloaded_bytes",
// &self.fast_store_downloaded_bytes,
// "Downloaded bytes from the fast store",
// );
// c.publish(
// "slow_store_hit_count",
// &self.slow_store_hit_count,
// "Hit count for the slow store",
// );
// c.publish(
// "slow_store_downloaded_bytes",
// &self.slow_store_downloaded_bytes,
// "Downloaded bytes from the slow store",
// );
// }
// }
default_health_status_indicator!(FastSlowStore);