-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathopendal.rs
333 lines (298 loc) · 10 KB
/
opendal.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
/// `OpenDAL` backend for rustic.
use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::OnceLock};
use anyhow::{anyhow, Error, Result};
use bytes::Bytes;
use bytesize::ByteSize;
use log::trace;
use opendal::{
layers::{BlockingLayer, ConcurrentLimitLayer, LoggingLayer, RetryLayer, ThrottleLayer},
BlockingOperator, ErrorKind, Metakey, Operator, Scheme,
};
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use tokio::runtime::Runtime;
use rustic_core::{FileType, Id, ReadBackend, WriteBackend, ALL_FILE_TYPES};
mod constants {
/// Default number of retries
pub(super) const DEFAULT_RETRY: usize = 5;
}
/// `OpenDALBackend` contains a wrapper around an blocking operator of the `OpenDAL` library.
#[derive(Clone, Debug)]
pub struct OpenDALBackend {
operator: BlockingOperator,
}
fn runtime() -> &'static Runtime {
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
RUNTIME.get_or_init(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
})
}
/// Throttling parameters
///
/// Note: Throttle implements [`FromStr`] to read it from something like "10kiB,10MB"
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Throttle {
bandwidth: u32,
burst: u32,
}
impl FromStr for Throttle {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let mut values = s
.split(',')
.map(|s| ByteSize::from_str(s.trim()).map_err(|err| anyhow!("Error: {err}")))
.map(|b| -> Result<u32> { Ok(b?.as_u64().try_into()?) });
let bandwidth = values
.next()
.ok_or_else(|| anyhow!("no bandwidth given"))??;
let burst = values.next().ok_or_else(|| anyhow!("no burst given"))??;
Ok(Self { bandwidth, burst })
}
}
impl OpenDALBackend {
/// Create a new openDAL backend.
///
/// # Arguments
///
/// * `path` - The path to the `OpenDAL` backend.
/// * `options` - Additional options for the `OpenDAL` backend.
///
/// # Errors
///
/// If the path is not a valid `OpenDAL` path, an error is returned.
///
/// # Returns
///
/// A new `OpenDAL` backend.
pub fn new(path: impl AsRef<str>, options: HashMap<String, String>) -> Result<Self> {
let max_retries = match options.get("retry").map(String::as_str) {
Some("false" | "off") => 0,
None | Some("default") => constants::DEFAULT_RETRY,
Some(value) => usize::from_str(value)?,
};
let connections = options
.get("connections")
.map(|c| usize::from_str(c))
.transpose()?;
let throttle = options
.get("throttle")
.map(|t| Throttle::from_str(t))
.transpose()?;
let schema = Scheme::from_str(path.as_ref())?;
let mut operator = Operator::via_iter(schema, options)?
.layer(RetryLayer::new().with_max_times(max_retries).with_jitter());
if let Some(Throttle { bandwidth, burst }) = throttle {
operator = operator.layer(ThrottleLayer::new(bandwidth, burst));
}
if let Some(connections) = connections {
operator = operator.layer(ConcurrentLimitLayer::new(connections));
}
let _guard = runtime().enter();
let operator = operator
.layer(LoggingLayer::default())
.layer(BlockingLayer::create()?)
.blocking();
Ok(Self { operator })
}
/// Return a path for the given file type and id.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
///
/// # Returns
///
/// The path for the given file type and id.
// Let's keep this for now, as it's being used in the trait implementations.
#[allow(clippy::unused_self)]
fn path(&self, tpe: FileType, id: &Id) -> String {
let hex_id = id.to_hex();
match tpe {
FileType::Config => PathBuf::from("config"),
FileType::Pack => PathBuf::from("data").join(&hex_id[0..2]).join(hex_id),
_ => PathBuf::from(tpe.dirname()).join(hex_id),
}
.to_string_lossy()
.to_string()
}
}
impl ReadBackend for OpenDALBackend {
/// Returns the location of the backend.
///
/// This is `local:<path>`.
fn location(&self) -> String {
let mut location = "opendal:".to_string();
location.push_str(self.operator.info().name());
location
}
/// Lists all files of the given type.
///
/// # Arguments
///
/// * `tpe` - The type of the files to list.
///
/// # Notes
///
/// If the file type is `FileType::Config`, this will return a list with a single default id.
fn list(&self, tpe: FileType) -> Result<Vec<Id>> {
trace!("listing tpe: {tpe:?}");
if tpe == FileType::Config {
return Ok(if self.operator.is_exist("config")? {
vec![Id::default()]
} else {
Vec::new()
});
}
Ok(self
.operator
.list_with(&(tpe.dirname().to_string() + "/"))
.recursive(true)
.call()?
.into_iter()
.filter(|e| e.metadata().is_file())
.map(|e| Id::from_hex(e.name()))
.filter_map(Result::ok)
.collect())
}
/// Lists all files with their size of the given type.
///
/// # Arguments
///
/// * `tpe` - The type of the files to list.
///
fn list_with_size(&self, tpe: FileType) -> Result<Vec<(Id, u32)>> {
trace!("listing tpe: {tpe:?}");
if tpe == FileType::Config {
return match self.operator.stat("config") {
Ok(entry) => Ok(vec![(Id::default(), entry.content_length().try_into()?)]),
Err(err) if err.kind() == ErrorKind::NotFound => Ok(Vec::new()),
Err(err) => Err(err.into()),
};
}
Ok(self
.operator
.list_with(&(tpe.dirname().to_string() + "/"))
.recursive(true)
.metakey(Metakey::ContentLength)
.call()?
.into_iter()
.filter(|e| e.metadata().is_file())
.map(|e| -> Result<(Id, u32)> {
Ok((
Id::from_hex(e.name())?,
e.metadata().content_length().try_into()?,
))
})
.filter_map(Result::ok)
.collect())
}
fn read_full(&self, tpe: FileType, id: &Id) -> Result<Bytes> {
trace!("reading tpe: {tpe:?}, id: {id}");
Ok(self.operator.read(&self.path(tpe, id))?.to_bytes())
}
fn read_partial(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
offset: u32,
length: u32,
) -> Result<Bytes> {
trace!("reading tpe: {tpe:?}, id: {id}, offset: {offset}, length: {length}");
let range = u64::from(offset)..u64::from(offset + length);
Ok(self
.operator
.read_with(&self.path(tpe, id))
.range(range)
.call()?
.to_bytes())
}
}
impl WriteBackend for OpenDALBackend {
/// Create a repository on the backend.
fn create(&self) -> Result<()> {
trace!("creating repo at {:?}", self.location());
for tpe in ALL_FILE_TYPES {
self.operator
.create_dir(&(tpe.dirname().to_string() + "/"))?;
}
// creating 256 dirs can be slow on remote backends, hence we parallelize it.
(0u8..=255).into_par_iter().try_for_each(|i| {
self.operator.create_dir(
&(PathBuf::from("data")
.join(hex::encode([i]))
.to_string_lossy()
.to_string()
+ "/"),
)
})?;
Ok(())
}
/// Write the given bytes to the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `cacheable` - Whether the file is cacheable.
/// * `buf` - The bytes to write.
fn write_bytes(&self, tpe: FileType, id: &Id, _cacheable: bool, buf: Bytes) -> Result<()> {
trace!("writing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);
self.operator.write(&filename, buf)?;
Ok(())
}
/// Remove the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `cacheable` - Whether the file is cacheable.
fn remove(&self, tpe: FileType, id: &Id, _cacheable: bool) -> Result<()> {
trace!("removing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);
self.operator.delete(&filename)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::fs;
use super::*;
use rstest::rstest;
use serde::Deserialize;
#[rstest]
#[case("10kB,10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
#[case("10 kB,10 MB", Throttle{bandwidth:10_000, burst:10_000_000})]
#[case("10kB, 10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
#[case(" 10kB, 10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
#[case("10kiB,10MiB", Throttle{bandwidth:10_240, burst:10_485_760})]
fn correct_throttle(#[case] input: &str, #[case] expected: Throttle) {
assert_eq!(Throttle::from_str(input).unwrap(), expected);
}
#[rstest]
#[case("")]
#[case("10kiB")]
#[case("no_number,10MiB")]
#[case("10kB;10MB")]
fn invalid_throttle(#[case] input: &str) {
assert!(Throttle::from_str(input).is_err());
}
#[rstest]
fn new_opendal_backend(
#[files("tests/fixtures/opendal/*.toml")] test_case: PathBuf,
) -> Result<()> {
#[derive(Deserialize)]
struct TestCase {
path: String,
options: HashMap<String, String>,
}
let test: TestCase = toml::from_str(&fs::read_to_string(test_case)?)?;
_ = OpenDALBackend::new(test.path, test.options)?;
Ok(())
}
}