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
use std::io::{self, Write};
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;
#[cfg(feature = "cloud-aws")]
pub use aws::{Config as S3Config, S3Storage};
#[cfg(feature = "cloud-gcp")]
pub use gcp::{Config as GCSConfig, GCSStorage};
#[cfg(feature = "prost-codec")]
pub use kvproto::backup::storage_backend::Backend;
use kvproto::backup::CloudDynamic;
#[cfg(feature = "protobuf-codec")]
pub use kvproto::backup::StorageBackend_oneof_backend as Backend;
#[cfg(any(feature = "cloud-gcp", feature = "cloud-aws"))]
use kvproto::backup::{Gcs, S3};
#[cfg(feature = "cloud-storage-dylib")]
use crate::dylib;
#[cfg(any(feature = "cloud-storage-dylib", feature = "cloud-storage-grpc"))]
use cloud::blob::BlobConfig;
use cloud::blob::BlobStorage;
use encryption::DataKeyManager;
#[cfg(feature = "cloud-storage-dylib")]
use external_storage::dylib_client;
#[cfg(feature = "cloud-storage-grpc")]
use external_storage::grpc_client;
use external_storage::record_storage_create;
pub use external_storage::{
read_external_storage_into_file, ExternalStorage, LocalStorage, NoopStorage,
};
use futures_io::AsyncRead;
use kvproto::backup::{Noop, StorageBackend};
use tikv_util::stream::block_on_external_io;
use tikv_util::time::Limiter;
#[cfg(feature = "cloud-storage-dylib")]
use tikv_util::warn;
pub fn create_storage(storage_backend: &StorageBackend) -> io::Result<Box<dyn ExternalStorage>> {
if let Some(backend) = &storage_backend.backend {
create_backend(backend)
} else {
Err(bad_storage_backend(storage_backend))
}
}
pub fn create_storage_no_client(
storage_backend: &StorageBackend,
) -> io::Result<Box<dyn ExternalStorage>> {
if let Some(backend) = &storage_backend.backend {
create_backend_inner(backend)
} else {
Err(bad_storage_backend(storage_backend))
}
}
fn bad_storage_backend(storage_backend: &StorageBackend) -> io::Error {
io::Error::new(
io::ErrorKind::NotFound,
format!("bad storage backend {:?}", storage_backend),
)
}
fn bad_backend(backend: Backend) -> io::Error {
let storage_backend = StorageBackend {
backend: Some(backend),
..Default::default()
};
bad_storage_backend(&storage_backend)
}
#[cfg(any(feature = "cloud-gcp", feature = "cloud-aws"))]
fn blob_store<Blob: BlobStorage>(store: Blob) -> Box<dyn ExternalStorage> {
Box::new(BlobStore::new(store)) as Box<dyn ExternalStorage>
}
#[cfg(feature = "cloud-storage-grpc")]
pub fn create_backend(backend: &Backend) -> io::Result<Box<dyn ExternalStorage>> {
match create_config(backend) {
Some(config) => {
let conf = config?;
grpc_client::new_client(backend.clone(), conf.name(), conf.url()?)
}
None => Err(bad_backend(backend.clone())),
}
}
#[cfg(feature = "cloud-storage-dylib")]
pub fn create_backend(backend: &Backend) -> io::Result<Box<dyn ExternalStorage>> {
match create_config(backend) {
Some(config) => {
let conf = config?;
let r = dylib_client::new_client(backend.clone(), conf.name(), conf.url()?);
match r {
Err(e) if e.kind() == io::ErrorKind::AddrNotAvailable => {
warn!("could not open dll for external_storage_export");
dylib::staticlib::new_client(backend.clone(), conf.name(), conf.url()?)
}
_ => r,
}
}
None => Err(bad_backend(backend.clone())),
}
}
#[cfg(all(
not(feature = "cloud-storage-grpc"),
not(feature = "cloud-storage-dylib")
))]
pub fn create_backend(backend: &Backend) -> io::Result<Box<dyn ExternalStorage>> {
create_backend_inner(backend)
}
#[cfg(any(feature = "cloud-storage-dylib", feature = "cloud-storage-grpc"))]
fn create_config(backend: &Backend) -> Option<io::Result<Box<dyn BlobConfig>>> {
match backend {
#[cfg(feature = "cloud-aws")]
Backend::S3(config) => {
let conf = S3Config::from_input(config.clone());
Some(conf.map(|c| Box::new(c) as Box<dyn BlobConfig>))
}
#[cfg(feature = "cloud-gcp")]
Backend::Gcs(config) => {
let conf = GCSConfig::from_input(config.clone());
Some(conf.map(|c| Box::new(c) as Box<dyn BlobConfig>))
}
Backend::CloudDynamic(dyn_backend) => match dyn_backend.provider_name.as_str() {
#[cfg(feature = "cloud-aws")]
"aws" | "s3" => {
let conf = S3Config::from_cloud_dynamic(&dyn_backend);
Some(conf.map(|c| Box::new(c) as Box<dyn BlobConfig>))
}
#[cfg(feature = "cloud-gcp")]
"gcp" | "gcs" => {
let conf = GCSConfig::from_cloud_dynamic(&dyn_backend);
Some(conf.map(|c| Box::new(c) as Box<dyn BlobConfig>))
}
_ => None,
},
_ => None,
}
}
fn create_backend_inner(backend: &Backend) -> io::Result<Box<dyn ExternalStorage>> {
let start = Instant::now();
let storage: Box<dyn ExternalStorage> = match backend {
Backend::Local(local) => {
let p = Path::new(&local.path);
Box::new(LocalStorage::new(p)?) as Box<dyn ExternalStorage>
}
Backend::Noop(_) => Box::new(NoopStorage::default()) as Box<dyn ExternalStorage>,
#[cfg(feature = "cloud-aws")]
Backend::S3(config) => blob_store(S3Storage::from_input(config.clone())?),
#[cfg(feature = "cloud-gcp")]
Backend::Gcs(config) => blob_store(GCSStorage::from_input(config.clone())?),
Backend::CloudDynamic(dyn_backend) => match dyn_backend.provider_name.as_str() {
#[cfg(feature = "cloud-aws")]
"aws" | "s3" => blob_store(S3Storage::from_cloud_dynamic(&dyn_backend)?),
#[cfg(feature = "cloud-gcp")]
"gcp" | "gcs" => blob_store(GCSStorage::from_cloud_dynamic(&dyn_backend)?),
_ => {
return Err(bad_backend(Backend::CloudDynamic(dyn_backend.clone())));
}
},
#[cfg(not(any(feature = "cloud-gcp", feature = "cloud-aws")))]
_ => return Err(bad_backend(backend.clone())),
};
record_storage_create(start, &*storage);
Ok(storage)
}
#[cfg(feature = "cloud-aws")]
pub fn make_s3_backend(config: S3) -> StorageBackend {
#[cfg(feature = "prost-codec")]
{
StorageBackend {
backend: Some(Backend::S3(config)),
}
}
#[cfg(feature = "protobuf-codec")]
{
let mut backend = StorageBackend::default();
backend.set_s3(config);
backend
}
}
pub fn make_local_backend(path: &Path) -> StorageBackend {
let path = path.display().to_string();
#[cfg(feature = "prost-codec")]
{
StorageBackend {
backend: Some(Backend::Local(Local { path })),
}
}
#[cfg(feature = "protobuf-codec")]
{
let mut backend = StorageBackend::default();
backend.mut_local().set_path(path);
backend
}
}
pub fn make_noop_backend() -> StorageBackend {
let noop = Noop::default();
#[cfg(feature = "prost-codec")]
{
StorageBackend {
backend: Some(Backend::Noop(noop)),
}
}
#[cfg(feature = "protobuf-codec")]
{
let mut backend = StorageBackend::default();
backend.set_noop(noop);
backend
}
}
#[cfg(feature = "cloud-gcp")]
pub fn make_gcs_backend(config: Gcs) -> StorageBackend {
#[cfg(feature = "prost-codec")]
{
StorageBackend {
backend: Some(Backend::Gcs(config)),
}
}
#[cfg(feature = "protobuf-codec")]
{
let mut backend = StorageBackend::default();
backend.set_gcs(config);
backend
}
}
pub fn make_cloud_backend(config: CloudDynamic) -> StorageBackend {
#[cfg(feature = "prost-codec")]
{
StorageBackend {
backend: Some(Backend::CloudDynamic(config)),
}
}
#[cfg(feature = "protobuf-codec")]
{
let mut backend = StorageBackend::default();
backend.set_cloud_dynamic(config);
backend
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::Builder;
#[test]
fn test_create_storage() {
let temp_dir = Builder::new().tempdir().unwrap();
let path = temp_dir.path();
let backend = make_local_backend(&path.join("not_exist"));
match create_storage(&backend) {
Ok(_) => panic!("must be NotFound error"),
Err(e) => {
assert_eq!(e.kind(), io::ErrorKind::NotFound);
}
}
let backend = make_local_backend(path);
create_storage(&backend).unwrap();
let backend = make_noop_backend();
create_storage(&backend).unwrap();
let backend = StorageBackend::default();
assert!(create_storage(&backend).is_err());
}
}
pub struct BlobStore<Blob: BlobStorage>(Blob);
impl<Blob: BlobStorage> BlobStore<Blob> {
pub fn new(inner: Blob) -> Self {
BlobStore(inner)
}
}
impl<Blob: BlobStorage> std::ops::Deref for BlobStore<Blob> {
type Target = Blob;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct EncryptedExternalStorage {
pub key_manager: Arc<DataKeyManager>,
pub storage: Box<dyn ExternalStorage>,
}
impl ExternalStorage for EncryptedExternalStorage {
fn name(&self) -> &'static str {
self.storage.name()
}
fn url(&self) -> io::Result<url::Url> {
self.storage.url()
}
fn write(
&self,
name: &str,
reader: Box<dyn AsyncRead + Send + Unpin>,
content_length: u64,
) -> io::Result<()> {
self.storage.write(name, reader, content_length)
}
fn read(&self, name: &str) -> Box<dyn AsyncRead + Unpin + '_> {
self.storage.read(name)
}
fn restore(
&self,
storage_name: &str,
restore_name: std::path::PathBuf,
expected_length: u64,
speed_limiter: &Limiter,
) -> io::Result<()> {
let mut input = self.read(storage_name);
let file_writer: &mut dyn Write = &mut self.key_manager.create_file(&restore_name)?;
let min_read_speed: usize = 8192;
block_on_external_io(read_external_storage_into_file(
&mut input,
file_writer,
&speed_limiter,
expected_length,
min_read_speed,
))
}
}
impl<Blob: BlobStorage> ExternalStorage for BlobStore<Blob> {
fn name(&self) -> &'static str {
(**self).config().name()
}
fn url(&self) -> io::Result<url::Url> {
(**self).config().url()
}
fn write(
&self,
name: &str,
reader: Box<dyn AsyncRead + Send + Unpin>,
content_length: u64,
) -> io::Result<()> {
(**self).put(name, reader, content_length)
}
fn read(&self, name: &str) -> Box<dyn AsyncRead + Unpin + '_> {
(**self).get(name)
}
}