-
Notifications
You must be signed in to change notification settings - Fork 124
/
size_partitioning_store.rs
178 lines (166 loc) · 5.95 KB
/
size_partitioning_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
// 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::pin::Pin;
use std::sync::Arc;
use async_trait::async_trait;
use nativelink_error::{make_input_err, Error, ResultExt};
use nativelink_metric::MetricsComponent;
use nativelink_util::buf_channel::{DropCloserReadHalf, DropCloserWriteHalf};
use nativelink_util::health_utils::{default_health_status_indicator, HealthStatusIndicator};
use nativelink_util::store_trait::{Store, StoreDriver, StoreKey, StoreLike, UploadSizeInfo};
use tokio::join;
#[derive(MetricsComponent)]
pub struct SizePartitioningStore {
#[metric(help = "Size to partition our data")]
partition_size: i64,
#[metric(group = "lower_store")]
lower_store: Store,
#[metric(group = "upper_store")]
upper_store: Store,
}
impl SizePartitioningStore {
pub fn new(
config: &nativelink_config::stores::SizePartitioningStore,
lower_store: Store,
upper_store: Store,
) -> Arc<Self> {
Arc::new(SizePartitioningStore {
partition_size: config.size as i64,
lower_store,
upper_store,
})
}
}
#[async_trait]
impl StoreDriver for SizePartitioningStore {
async fn has_with_results(
self: Pin<&Self>,
keys: &[StoreKey<'_>],
results: &mut [Option<usize>],
) -> Result<(), Error> {
let mut non_digest_sample = None;
let (lower_digests, upper_digests): (Vec<_>, Vec<_>) =
keys.iter().map(|v| v.borrow()).partition(|k| {
let StoreKey::Digest(digest) = k else {
non_digest_sample = Some(k.borrow().into_owned());
return false;
};
digest.size_bytes < self.partition_size
});
if let Some(non_digest) = non_digest_sample {
return Err(make_input_err!(
"SizePartitioningStore only supports Digest keys, got {non_digest:?}"
));
}
let (lower_results, upper_results) = join!(
self.lower_store.has_many(&lower_digests),
self.upper_store.has_many(&upper_digests),
);
let mut lower_results = match lower_results {
Ok(lower_results) => lower_results.into_iter(),
Err(err) => match upper_results {
Ok(_) => return Err(err),
Err(upper_err) => return Err(err.merge(upper_err)),
},
};
let mut upper_digests = upper_digests.into_iter().peekable();
let mut upper_results = upper_results?.into_iter();
for (digest, result) in keys.iter().zip(results.iter_mut()) {
if Some(digest) == upper_digests.peek() {
upper_digests.next();
*result = upper_results
.next()
.err_tip(|| "upper_results out of sync with upper_digests")?;
} else {
*result = lower_results
.next()
.err_tip(|| "lower_results out of sync with lower_digests")?;
}
}
Ok(())
}
async fn update(
self: Pin<&Self>,
key: StoreKey<'_>,
reader: DropCloserReadHalf,
size_info: UploadSizeInfo,
) -> Result<(), Error> {
let digest = match key {
StoreKey::Digest(digest) => digest,
other => {
return Err(make_input_err!(
"SizePartitioningStore only supports Digest keys, got {other:?}"
))
}
};
if digest.size_bytes < self.partition_size {
return self.lower_store.update(digest, reader, size_info).await;
}
self.upper_store.update(digest, reader, size_info).await
}
async fn get_part(
self: Pin<&Self>,
key: StoreKey<'_>,
writer: &mut DropCloserWriteHalf,
offset: usize,
length: Option<usize>,
) -> Result<(), Error> {
let digest = match key {
StoreKey::Digest(digest) => digest,
other => {
return Err(make_input_err!(
"SizePartitioningStore only supports Digest keys, got {other:?}"
))
}
};
if digest.size_bytes < self.partition_size {
return self
.lower_store
.get_part(digest, writer, offset, length)
.await;
}
self.upper_store
.get_part(digest, writer, offset, length)
.await
}
fn inner_store(&self, key: Option<StoreKey>) -> &'_ dyn StoreDriver {
let Some(key) = key else {
return self;
};
let digest = match key {
StoreKey::Digest(digest) => digest,
_ => return self,
};
if digest.size_bytes < self.partition_size {
return self.lower_store.inner_store(Some(digest));
}
self.upper_store.inner_store(Some(digest))
}
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
}
}
// impl MetricsComponent for SizePartitioningStore {
// fn gather_metrics(&self, c: &mut CollectorState) {
// c.publish(
// "partition_size",
// &self.partition_size,
// "Size to partition our data",
// );
// }
// }
default_health_status_indicator!(SizePartitioningStore);