Skip to content

Commit ddafdb4

Browse files
committed
feat(dumper): config.path can include {class}
1 parent 3705968 commit ddafdb4

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
.idea
33
Cargo.lock
44

5-
example.dump
5+
*.dump

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- errors: add `TrySendError::map` and `RequestError::map`.
1212
- dumping: write `ts` firstly to support the `sort` utility.
1313
- dumper: extract a dump's name if it's not specified.
14+
- dumper: support the `{class}` variable in config's `path` param.
1415

1516
### Fixed
1617
- init: do not start termination if the memory tracker fails to read files.

elfo-dumper/src/actor.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use fxhash::FxHashSet;
1111
use metrics::counter;
1212
use parking_lot::Mutex;
1313
use tokio::{task, time::Duration};
14-
use tracing::{debug, error, info, warn};
14+
use tracing::{error, info, warn};
1515

1616
use elfo_core as elfo;
1717
use elfo_macros::{message, msg_raw as msg};
@@ -59,7 +59,7 @@ impl Dumper {
5959
}
6060

6161
async fn main(mut self) -> Result<()> {
62-
let mut file = open_file(self.ctx.config()).await;
62+
let mut file = open_file(self.ctx.config(), self.ctx.key()).await;
6363

6464
let class = Box::leak(self.ctx.key().clone().into_boxed_str());
6565
let registry = { self.storage.lock().registry(class) };
@@ -74,15 +74,17 @@ impl Dumper {
7474

7575
while let Some(envelope) = ctx.recv().await {
7676
msg!(match envelope {
77+
// TODO: open on `ValidateConfig`
7778
ReopenDumpFile | ConfigUpdated => {
7879
let config = ctx.config();
7980
interval.set_period(config.interval);
80-
file = open_file(config).await;
81+
file = open_file(config, self.ctx.key()).await;
8182
}
8283
DumpingTick => {
8384
let registry = registry.clone();
8485
let timeout = ctx.config().interval;
8586

87+
// TODO: run inside scope?
8688
let report = task::spawn_blocking(move || -> Result<Report> {
8789
let mut errors = Vec::new();
8890
let mut written = 0;
@@ -214,13 +216,13 @@ pub(crate) fn new(storage: Arc<Mutex<Storage>>) -> Schema {
214216
.exec(move |ctx| Dumper::new(ctx, storage_1.clone()).main())
215217
}
216218

217-
async fn open_file(config: &Config) -> BufWriter<File> {
219+
async fn open_file(config: &Config, class: &str) -> BufWriter<File> {
218220
use tokio::fs::OpenOptions;
219221

220222
let file = OpenOptions::new()
221223
.create(true)
222224
.append(true)
223-
.open(&config.path)
225+
.open(&config.path(class))
224226
.await
225227
.expect("cannot open the dump file")
226228
.into_std()

elfo-dumper/src/config.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
use std::{path::PathBuf, time::Duration};
1+
use std::time::Duration;
22

33
use serde::Deserialize;
44

55
#[derive(Debug, Deserialize)]
66
pub(crate) struct Config {
7-
pub(crate) path: PathBuf,
7+
/// A path to a dump file or template:
8+
/// * `dumps/all.dump` - one file.
9+
/// * `dumps/{class}.dump` - file per class.
10+
path: String,
811
#[serde(with = "humantime_serde", default = "default_interval")]
912
pub(crate) interval: Duration,
1013
#[serde(default = "default_registry_capacity")]
1114
pub(crate) registry_capacity: usize,
1215
}
1316

17+
impl Config {
18+
pub(crate) fn path(&self, class: &str) -> String {
19+
self.path.replace("{class}", class)
20+
}
21+
}
22+
1423
fn default_interval() -> Duration {
1524
Duration::from_millis(500)
1625
}

elfo/examples/config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ address = "0.0.0.0:9042"
3434
#quantiles = [0.75, 0.9, 0.95, 0.99]
3535

3636
[system.dumpers]
37-
path = "example.dump"
37+
path = "example.{class}.dump"
3838

3939
[producers]
4040
group_count = 3

0 commit comments

Comments
 (0)