-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
localfs.rs
576 lines (498 loc) · 18.5 KB
/
localfs.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use std::{
collections::{BTreeMap, HashMap},
path::{Path, PathBuf},
sync::Arc,
time::Instant,
};
use async_trait::async_trait;
use bytes::Bytes;
use datafusion::{datasource::listing::ListingTableUrl, execution::runtime_env::RuntimeConfig};
use fs_extra::file::CopyOptions;
use futures::{stream::FuturesUnordered, TryStreamExt};
use relative_path::{RelativePath, RelativePathBuf};
use tokio::fs::{self, DirEntry};
use tokio_stream::wrappers::ReadDirStream;
use crate::option::validation;
use crate::{
handlers::http::users::USERS_ROOT_DIR,
metrics::storage::{localfs::REQUEST_RESPONSE_TIME, StorageMetrics},
};
use super::{
LogStream, ObjectStorage, ObjectStorageError, ObjectStorageProvider, PARSEABLE_ROOT_DIRECTORY,
SCHEMA_FILE_NAME, STREAM_METADATA_FILE_NAME, STREAM_ROOT_DIRECTORY,
};
#[derive(Debug, Clone, clap::Args)]
#[command(
name = "Local filesystem config",
about = "Start Parseable with a drive as storage",
help_template = "\
{about-section}
{all-args}
"
)]
pub struct FSConfig {
#[arg(
env = "P_FS_DIR",
value_name = "filesystem path",
default_value = "./data",
value_parser = validation::canonicalize_path
)]
pub root: PathBuf,
}
impl ObjectStorageProvider for FSConfig {
fn get_datafusion_runtime(&self) -> RuntimeConfig {
RuntimeConfig::new()
}
fn get_object_store(&self) -> Arc<dyn ObjectStorage + Send> {
Arc::new(LocalFS::new(self.root.clone()))
}
fn get_endpoint(&self) -> String {
self.root.to_str().unwrap().to_string()
}
fn register_store_metrics(&self, handler: &actix_web_prometheus::PrometheusMetrics) {
self.register_metrics(handler);
}
}
pub struct LocalFS {
// absolute path of the data directory
root: PathBuf,
}
impl LocalFS {
pub fn new(root: PathBuf) -> Self {
Self { root }
}
pub fn path_in_root(&self, path: &RelativePath) -> PathBuf {
path.to_path(&self.root)
}
}
#[async_trait]
impl ObjectStorage for LocalFS {
async fn get_object(&self, path: &RelativePath) -> Result<Bytes, ObjectStorageError> {
let time = Instant::now();
let file_path = self.path_in_root(path);
let res: Result<Bytes, ObjectStorageError> = match fs::read(file_path).await {
Ok(x) => Ok(x.into()),
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => {
Err(ObjectStorageError::NoSuchKey(path.to_string()))
}
_ => Err(ObjectStorageError::UnhandledError(Box::new(e))),
},
};
let status = if res.is_ok() { "200" } else { "400" };
let time = time.elapsed().as_secs_f64();
REQUEST_RESPONSE_TIME
.with_label_values(&["GET", status])
.observe(time);
res
}
async fn get_ingestor_meta_file_paths(
&self,
) -> Result<Vec<RelativePathBuf>, ObjectStorageError> {
let time = Instant::now();
let mut path_arr = vec![];
let mut entries = fs::read_dir(&self.root).await?;
while let Some(entry) = entries.next_entry().await? {
let flag = entry
.path()
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.contains("ingestor");
if flag {
path_arr.push(
RelativePathBuf::from_path(entry.path().file_name().unwrap())
.map_err(ObjectStorageError::PathError)?,
);
}
}
let time = time.elapsed().as_secs_f64();
REQUEST_RESPONSE_TIME
.with_label_values(&["GET", "200"]) // this might not be the right status code
.observe(time);
Ok(path_arr)
}
async fn get_stream_file_paths(
&self,
stream_name: &str,
) -> Result<Vec<RelativePathBuf>, ObjectStorageError> {
let time = Instant::now();
let mut path_arr = vec![];
// = data/stream_name
let stream_dir_path = self.path_in_root(&RelativePathBuf::from(stream_name));
let mut entries = fs::read_dir(&stream_dir_path).await?;
while let Some(entry) = entries.next_entry().await? {
let flag = entry
.path()
.file_name()
.ok_or(ObjectStorageError::NoSuchKey(
"Dir Entry Suggests no file present".to_string(),
))?
.to_str()
.expect("file name is parseable to str")
.contains("ingestor");
if flag {
path_arr.push(RelativePathBuf::from_iter([
stream_name,
entry.path().file_name().unwrap().to_str().unwrap(), // checking the error before hand
]));
}
}
path_arr.push(RelativePathBuf::from_iter([
stream_name,
STREAM_METADATA_FILE_NAME,
]));
path_arr.push(RelativePathBuf::from_iter([stream_name, SCHEMA_FILE_NAME]));
let time = time.elapsed().as_secs_f64();
REQUEST_RESPONSE_TIME
.with_label_values(&["GET", "200"]) // this might not be the right status code
.observe(time);
Ok(path_arr)
}
/// currently it is not using the starts_with_pattern
async fn get_objects(
&self,
base_path: Option<&RelativePath>,
filter_func: Box<(dyn Fn(String) -> bool + std::marker::Send + 'static)>,
) -> Result<Vec<Bytes>, ObjectStorageError> {
let time = Instant::now();
let prefix = if let Some(path) = base_path {
path.to_path(&self.root)
} else {
self.root.clone()
};
let mut entries = fs::read_dir(&prefix).await?;
let mut res = Vec::new();
while let Some(entry) = entries.next_entry().await? {
let path = entry
.path()
.file_name()
.ok_or(ObjectStorageError::NoSuchKey(
"Dir Entry suggests no file present".to_string(),
))?
.to_str()
.expect("file name is parseable to str")
.to_owned();
let ingestor_file = filter_func(path);
if !ingestor_file {
continue;
}
let file = fs::read(entry.path()).await?;
res.push(file.into());
}
// maybe change the return code
let status = if res.is_empty() { "200" } else { "400" };
let time = time.elapsed().as_secs_f64();
REQUEST_RESPONSE_TIME
.with_label_values(&["GET", status])
.observe(time);
Ok(res)
}
async fn put_object(
&self,
path: &RelativePath,
resource: Bytes,
) -> Result<(), ObjectStorageError> {
let time = Instant::now();
let path = self.path_in_root(path);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}
let res = fs::write(path, resource).await;
let status = if res.is_ok() { "200" } else { "400" };
let time = time.elapsed().as_secs_f64();
REQUEST_RESPONSE_TIME
.with_label_values(&["PUT", status])
.observe(time);
res.map_err(Into::into)
}
async fn delete_prefix(&self, path: &RelativePath) -> Result<(), ObjectStorageError> {
let path = self.path_in_root(path);
tokio::fs::remove_dir_all(path).await?;
Ok(())
}
async fn delete_object(&self, path: &RelativePath) -> Result<(), ObjectStorageError> {
let path = self.path_in_root(path);
tokio::fs::remove_file(path).await?;
Ok(())
}
async fn check(&self) -> Result<(), ObjectStorageError> {
fs::create_dir_all(&self.root)
.await
.map_err(|e| ObjectStorageError::UnhandledError(e.into()))
}
async fn delete_stream(&self, stream_name: &str) -> Result<(), ObjectStorageError> {
let path = self.root.join(stream_name);
Ok(fs::remove_dir_all(path).await?)
}
async fn try_delete_ingestor_meta(
&self,
ingestor_filename: String,
) -> Result<(), ObjectStorageError> {
let path = self.root.join(ingestor_filename);
Ok(fs::remove_file(path).await?)
}
async fn list_streams(&self) -> Result<Vec<LogStream>, ObjectStorageError> {
let ignore_dir = &["lost+found", PARSEABLE_ROOT_DIRECTORY, USERS_ROOT_DIR];
let directories = ReadDirStream::new(fs::read_dir(&self.root).await?);
let entries: Vec<DirEntry> = directories.try_collect().await?;
let entries = entries
.into_iter()
.map(|entry| dir_with_stream(entry, ignore_dir));
let logstream_dirs: Vec<Option<String>> =
FuturesUnordered::from_iter(entries).try_collect().await?;
let logstreams = logstream_dirs
.into_iter()
.flatten()
.map(|name| LogStream { name })
.collect();
Ok(logstreams)
}
async fn list_old_streams(&self) -> Result<Vec<LogStream>, ObjectStorageError> {
let ignore_dir = &["lost+found", PARSEABLE_ROOT_DIRECTORY];
let directories = ReadDirStream::new(fs::read_dir(&self.root).await?);
let entries: Vec<DirEntry> = directories.try_collect().await?;
let entries = entries
.into_iter()
.map(|entry| dir_with_old_stream(entry, ignore_dir));
let logstream_dirs: Vec<Option<String>> =
FuturesUnordered::from_iter(entries).try_collect().await?;
let logstreams = logstream_dirs
.into_iter()
.flatten()
.map(|name| LogStream { name })
.collect();
Ok(logstreams)
}
async fn list_dirs(&self) -> Result<Vec<String>, ObjectStorageError> {
let dirs = ReadDirStream::new(fs::read_dir(&self.root).await?)
.try_collect::<Vec<DirEntry>>()
.await?
.into_iter()
.map(dir_name);
let dirs = FuturesUnordered::from_iter(dirs)
.try_collect::<Vec<_>>()
.await?
.into_iter()
.flatten()
.collect::<Vec<_>>();
Ok(dirs)
}
async fn get_all_dashboards(
&self,
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
let mut dashboards: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
let users_root_path = self.root.join(USERS_ROOT_DIR);
let directories = ReadDirStream::new(fs::read_dir(&users_root_path).await?);
let users: Vec<DirEntry> = directories.try_collect().await?;
for user in users {
if !user.path().is_dir() {
continue;
}
let dashboards_path = users_root_path.join(user.path()).join("dashboards");
let directories = ReadDirStream::new(fs::read_dir(&dashboards_path).await?);
let dashboards_files: Vec<DirEntry> = directories.try_collect().await?;
for dashboard in dashboards_files {
let dashboard_absolute_path = dashboard.path();
let file = fs::read(dashboard_absolute_path.clone()).await?;
let dashboard_relative_path = dashboard_absolute_path
.strip_prefix(self.root.as_path())
.unwrap();
dashboards
.entry(RelativePathBuf::from_path(dashboard_relative_path).unwrap())
.or_default()
.push(file.into());
}
}
Ok(dashboards)
}
async fn get_all_saved_filters(
&self,
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
let mut filters: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
let users_root_path = self.root.join(USERS_ROOT_DIR);
let directories = ReadDirStream::new(fs::read_dir(&users_root_path).await?);
let users: Vec<DirEntry> = directories.try_collect().await?;
for user in users {
if !user.path().is_dir() {
continue;
}
let stream_root_path = users_root_path.join(user.path()).join("filters");
let directories = ReadDirStream::new(fs::read_dir(&stream_root_path).await?);
let streams: Vec<DirEntry> = directories.try_collect().await?;
for stream in streams {
if !stream.path().is_dir() {
continue;
}
let filters_path = users_root_path
.join(user.path())
.join("filters")
.join(stream.path());
let directories = ReadDirStream::new(fs::read_dir(&filters_path).await?);
let filters_files: Vec<DirEntry> = directories.try_collect().await?;
for filter in filters_files {
let filter_absolute_path = filter.path();
let file = fs::read(filter_absolute_path.clone()).await?;
let filter_relative_path = filter_absolute_path
.strip_prefix(self.root.as_path())
.unwrap();
filters
.entry(RelativePathBuf::from_path(filter_relative_path).unwrap())
.or_default()
.push(file.into());
}
}
}
Ok(filters)
}
async fn list_dates(&self, stream_name: &str) -> Result<Vec<String>, ObjectStorageError> {
let path = self.root.join(stream_name);
let directories = ReadDirStream::new(fs::read_dir(&path).await?);
let entries: Vec<DirEntry> = directories.try_collect().await?;
let entries = entries.into_iter().map(dir_name);
let dates: Vec<_> = FuturesUnordered::from_iter(entries).try_collect().await?;
Ok(dates.into_iter().flatten().collect())
}
async fn list_manifest_files(
&self,
_stream_name: &str,
) -> Result<BTreeMap<String, Vec<String>>, ObjectStorageError> {
//unimplemented
Ok(BTreeMap::new())
}
async fn upload_file(&self, key: &str, path: &Path) -> Result<(), ObjectStorageError> {
let op = CopyOptions {
overwrite: true,
skip_exist: true,
..CopyOptions::default()
};
let to_path = self.root.join(key);
if let Some(path) = to_path.parent() {
fs::create_dir_all(path).await?;
}
let _ = fs_extra::file::copy(path, to_path, &op)?;
Ok(())
}
fn absolute_url(&self, prefix: &RelativePath) -> object_store::path::Path {
object_store::path::Path::parse(
format!("{}", self.root.join(prefix.as_str()).display())
.trim_start_matches(std::path::MAIN_SEPARATOR),
)
.unwrap()
}
fn query_prefixes(&self, prefixes: Vec<String>) -> Vec<ListingTableUrl> {
prefixes
.into_iter()
.filter_map(|prefix| ListingTableUrl::parse(format!("/{}", prefix)).ok())
.collect()
}
fn store_url(&self) -> url::Url {
url::Url::parse("file:///").unwrap()
}
fn get_bucket_name(&self) -> String {
self.root
.iter()
.last()
.expect("can be unwrapped without checking as the path is absolute")
.to_str()
.expect("valid unicode")
.to_string()
}
}
async fn dir_with_old_stream(
entry: DirEntry,
ignore_dirs: &[&str],
) -> Result<Option<String>, ObjectStorageError> {
let dir_name = entry
.path()
.file_name()
.expect("valid path")
.to_str()
.expect("valid unicode")
.to_owned();
if ignore_dirs.contains(&dir_name.as_str()) {
return Ok(None);
}
if entry.file_type().await?.is_dir() {
let path = entry.path();
// even in ingest mode, we should only look for the global stream metadata file
let stream_json_path = path.join(STREAM_METADATA_FILE_NAME);
if stream_json_path.exists() {
Ok(Some(dir_name))
} else {
let err: Box<dyn std::error::Error + Send + Sync + 'static> =
format!("found {}", entry.path().display()).into();
Err(ObjectStorageError::UnhandledError(err))
}
} else {
Ok(None)
}
}
async fn dir_with_stream(
entry: DirEntry,
ignore_dirs: &[&str],
) -> Result<Option<String>, ObjectStorageError> {
let dir_name = entry
.path()
.file_name()
.expect("valid path")
.to_str()
.expect("valid unicode")
.to_owned();
if ignore_dirs.contains(&dir_name.as_str()) {
return Ok(None);
}
if entry.file_type().await?.is_dir() {
let path = entry.path();
// even in ingest mode, we should only look for the global stream metadata file
let stream_json_path = path
.join(STREAM_ROOT_DIRECTORY)
.join(STREAM_METADATA_FILE_NAME);
if stream_json_path.exists() {
Ok(Some(dir_name))
} else {
let err: Box<dyn std::error::Error + Send + Sync + 'static> =
format!("found {}", entry.path().display()).into();
Err(ObjectStorageError::UnhandledError(err))
}
} else {
Ok(None)
}
}
async fn dir_name(entry: DirEntry) -> Result<Option<String>, ObjectStorageError> {
if entry.file_type().await?.is_dir() {
let dir_name = entry
.path()
.file_name()
.expect("valid path")
.to_str()
.expect("valid unicode")
.to_owned();
Ok(Some(dir_name))
} else {
Ok(None)
}
}
impl From<fs_extra::error::Error> for ObjectStorageError {
fn from(e: fs_extra::error::Error) -> Self {
ObjectStorageError::UnhandledError(Box::new(e))
}
}