-
Notifications
You must be signed in to change notification settings - Fork 16
feat(kvp): add store trait and Hyper-V KVP storage crate #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peytonr18
wants to merge
5
commits into
Azure:main
Choose a base branch
from
peytonr18:probertson/kvp-store-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f435f70
feat(kvp): add store trait and Hyper-V KVP storage crate
peytonr18 5226e32
add Azure autodetect limits selection for HyperVKvpStore
peytonr18 c1440c9
Improving libazurekvp.md clarity
peytonr18 d63dd42
feat(kvp): harden Hyper-V record decode and refactor stale truncate t…
peytonr18 62a52fe
Refactor libazureinit-kvp: KvpStore trait, KvpError, HyperV/Azure spl…
peytonr18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "libazureinit-kvp" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| rust-version = "1.88" | ||
| repository = "https://github.com/Azure/azure-init/" | ||
| homepage = "https://github.com/Azure/azure-init/" | ||
| license = "MIT" | ||
| description = "Hyper-V KVP (Key-Value Pair) storage library for azure-init." | ||
|
|
||
| [dependencies] | ||
| fs2 = "0.4" | ||
| sysinfo = "0.38" | ||
|
|
||
| [dev-dependencies] | ||
| tempfile = "3" | ||
|
|
||
| [lib] | ||
| name = "libazureinit_kvp" | ||
| path = "src/lib.rs" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Azure-specific KVP store. | ||
| //! | ||
| //! Wraps [`HyperVKvpStore`] with the stricter value-size limit imposed | ||
| //! by the Azure host (1,022 bytes). All other behavior — record | ||
| //! format, file locking, append-only writes — is inherited from the | ||
| //! underlying Hyper-V pool file implementation. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use crate::hyperv::HyperVKvpStore; | ||
| use crate::{KvpError, KvpStore}; | ||
|
|
||
| /// Azure host-side value limit (values beyond this are truncated). | ||
| const AZURE_MAX_VALUE_BYTES: usize = 1022; | ||
|
|
||
| /// Azure KVP store backed by a Hyper-V pool file. | ||
| /// | ||
| /// Identical to [`HyperVKvpStore`] except that | ||
| /// [`MAX_VALUE_SIZE`](KvpStore::MAX_VALUE_SIZE) is set to 1,022 bytes, | ||
| /// matching the Azure host's truncation behavior. | ||
| #[derive(Clone, Debug)] | ||
| pub struct AzureKvpStore { | ||
| inner: HyperVKvpStore, | ||
| } | ||
|
|
||
| impl AzureKvpStore { | ||
| /// Create a new Azure KVP store backed by the pool file at `path`. | ||
| /// | ||
| /// When `truncate_on_stale` is `true` the constructor checks | ||
| /// whether the pool file predates the current boot and, if so, | ||
| /// truncates it before returning. | ||
| pub fn new( | ||
| path: impl Into<PathBuf>, | ||
| truncate_on_stale: bool, | ||
| ) -> Result<Self, KvpError> { | ||
| Ok(Self { | ||
| inner: HyperVKvpStore::new(path, truncate_on_stale)?, | ||
| }) | ||
| } | ||
|
|
||
| /// Return a reference to the pool file path. | ||
| pub fn path(&self) -> &Path { | ||
| self.inner.path() | ||
| } | ||
| } | ||
|
|
||
| impl KvpStore for AzureKvpStore { | ||
| const MAX_KEY_SIZE: usize = HyperVKvpStore::MAX_KEY_SIZE; | ||
| const MAX_VALUE_SIZE: usize = AZURE_MAX_VALUE_BYTES; | ||
|
|
||
| fn backend_read(&self, key: &str) -> Result<Option<String>, KvpError> { | ||
| self.inner.backend_read(key) | ||
| } | ||
|
|
||
| fn backend_write(&self, key: &str, value: &str) -> Result<(), KvpError> { | ||
| self.inner.backend_write(key, value) | ||
| } | ||
|
|
||
| fn entries(&self) -> Result<HashMap<String, String>, KvpError> { | ||
| self.inner.entries() | ||
| } | ||
|
|
||
| fn entries_raw(&self) -> Result<Vec<(String, String)>, KvpError> { | ||
| self.inner.entries_raw() | ||
| } | ||
|
|
||
| fn delete(&self, key: &str) -> Result<bool, KvpError> { | ||
| self.inner.delete(key) | ||
| } | ||
|
|
||
| fn backend_clear(&self) -> Result<(), KvpError> { | ||
| self.inner.backend_clear() | ||
| } | ||
|
|
||
| fn is_stale(&self) -> Result<bool, KvpError> { | ||
| self.inner.is_stale() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use tempfile::NamedTempFile; | ||
|
|
||
| fn azure_store(path: &Path) -> AzureKvpStore { | ||
| AzureKvpStore::new(path, false).unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_azure_rejects_value_over_1022() { | ||
| let tmp = NamedTempFile::new().unwrap(); | ||
| let store = azure_store(tmp.path()); | ||
|
|
||
| let value = "V".repeat(AZURE_MAX_VALUE_BYTES + 1); | ||
| let err = store.write("k", &value).unwrap_err(); | ||
| assert!( | ||
| matches!(err, KvpError::ValueTooLarge { .. }), | ||
| "expected ValueTooLarge, got: {err}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_azure_accepts_value_at_1022() { | ||
| let tmp = NamedTempFile::new().unwrap(); | ||
| let store = azure_store(tmp.path()); | ||
|
|
||
| let value = "V".repeat(AZURE_MAX_VALUE_BYTES); | ||
| store.write("k", &value).unwrap(); | ||
| assert_eq!(store.read("k").unwrap(), Some(value)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_azure_write_and_read() { | ||
| let tmp = NamedTempFile::new().unwrap(); | ||
| let store = azure_store(tmp.path()); | ||
|
|
||
| store.write("key", "value").unwrap(); | ||
| assert_eq!(store.read("key").unwrap(), Some("value".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_azure_clear() { | ||
| let tmp = NamedTempFile::new().unwrap(); | ||
| let store = azure_store(tmp.path()); | ||
|
|
||
| store.write("key", "value").unwrap(); | ||
| store.clear().unwrap(); | ||
| assert_eq!(store.read("key").unwrap(), None); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I think this composition setup works super well! This is the intended replacement for inheritance for Rust. My only request is that I would recommend updating
innerto be a more descriptive term, maybekvp_store, since I do not see many official references to using theinnername. This could get confusing if we ever extend this further.https://trpl.rantai.dev/docs/part-iii/chapter-20/#2043-implementing-composition-in-rust
For example, if AzureKvpStore were ever put into a separate composition, the pattern of calling the composed object
innercould remain and would result in aExtendedAzureKvpStore.inner.inner.pathcalling, instead ofExtendedAzureKvpStore.azure_kvp_store.kvp_store.pathwhich strikes me as more verbose.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big fan of this - will make this change!