Skip to content

Commit 8392342

Browse files
vlademVlad Volodkin
andauthored
Add versioning of the configuration format in mount_from_config example (#1545)
Example binary `mount_from_config` now accepts `config_version` parameter. This may be used to ensure that user is aware of updates to the configuration format and prevent from silent failures. ### Does this change impact existing behavior? No. ### Does this change need a changelog entry? Does it require a version change? No. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and I agree to the terms of the [Developer Certificate of Origin (DCO)](https://developercertificate.org/). --------- Signed-off-by: Vlad Volodkin <[email protected]> Co-authored-by: Vlad Volodkin <[email protected]>
1 parent 315db60 commit 8392342

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

mountpoint-s3-fs/examples/config.json.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"config_version": "0.0.1",
23
"mountpoint": "/exmpl/mountpoint",
34
"metadata_store_dir": "/exmpl/tmpdir",
45
"event_log_dir": "/exmpl/event_log_dir",

mountpoint-s3-fs/examples/mount_from_config.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs::File, io::BufReader, path::Path, time::Instant};
1+
use std::{fs::File, io::Read, path::Path, time::Instant};
22

33
use anyhow::{Context, Result, anyhow};
44
use clap::Parser;
@@ -24,6 +24,8 @@ use serde::Deserialize;
2424
use tempfile::tempdir_in;
2525
use tracing::info;
2626

27+
const CONFIG_VERSION: &str = "0.0.1";
28+
2729
#[derive(Debug, Deserialize)]
2830
#[serde(tag = "type")]
2931
enum ThroughputConfig {
@@ -36,6 +38,9 @@ enum ThroughputConfig {
3638
#[derive(Debug, Deserialize)]
3739
#[serde(deny_unknown_fields)]
3840
struct ConfigOptions {
41+
/// Version of the configuration format
42+
#[allow(dead_code)]
43+
config_version: String,
3944
/// Directory to mount the bucket at
4045
mountpoint: String,
4146
/// AWS region of the bucket, e.g. "us-east-2"
@@ -167,10 +172,31 @@ impl ConfigOptions {
167172
}
168173
}
169174

175+
/// Reads the config_version field from a JSON config file and validates it against CONFIG_VERSION
176+
fn validate_config_version(json_str: &str) -> Result<()> {
177+
#[derive(Deserialize)]
178+
struct VersionOnly {
179+
config_version: String,
180+
}
181+
182+
let version_info: VersionOnly = serde_json::from_str(json_str)?;
183+
184+
if version_info.config_version != CONFIG_VERSION {
185+
return Err(anyhow!(
186+
"Unsupported version of the configuration format, supported version is {}",
187+
CONFIG_VERSION
188+
));
189+
}
190+
191+
Ok(())
192+
}
193+
170194
fn load_config<P: AsRef<Path>>(path: P) -> Result<ConfigOptions> {
171-
let file = File::open(path)?;
172-
let reader = BufReader::new(file);
173-
let config: ConfigOptions = serde_json::from_reader(reader)?;
195+
let mut file = File::open(path)?;
196+
let mut json_str = String::new();
197+
file.read_to_string(&mut json_str)?;
198+
validate_config_version(&json_str)?;
199+
let config: ConfigOptions = serde_json::from_str(&json_str)?;
174200
Ok(config)
175201
}
176202

0 commit comments

Comments
 (0)