Enable resetting max_optimization_threads to automatic#5634
Merged
Conversation
ghost
reviewed
Dec 18, 2024
| } | ||
| } | ||
|
|
||
| impl TryFrom<MaxOptimizationThreads> for rest::MaxOptimizationThreads { |
There was a problem hiding this comment.
nit: we can just make it more rust aesthetic :)
#[derive(thiserror::Error, Debug)]
enum MaxOptimizationThreadsError {
#[error("Malformed MaxOptimizationThreads")]
Malformed,
#[error("Invalid MaxOptimizationThreads setting: {0}")]
InvalidSetting(#[from] std::num::TryFromIntError),
}
impl From<MaxOptimizationThreadsError> for Status {
fn from(error: MaxOptimizationThreadsError) -> Self {
Status::invalid_argument(error.to_string())
}
}
fn parse_variant(value: &MaxOptimizationThreads) -> Result<&Variant, MaxOptimizationThreadsError> {
value.variant.as_ref().ok_or(MaxOptimizationThreadsError::Malformed)
}
impl TryFrom<MaxOptimizationThreads> for rest::MaxOptimizationThreads {
type Error = Status;
fn try_from(value: MaxOptimizationThreads) -> Result<Self, Self::Error> {
let variant = parse_variant(&value).map_err(Status::from)?;
match variant {
Variant::Setting(setting_int) => {
let setting = Setting::try_from(*setting_int)
.map_err(MaxOptimizationThreadsError::from)
.map_err(Status::from)?;
match setting {
Setting::Auto => Ok(Self::Setting(rest::MaxOptimizationThreadsSetting::Auto)),
}
}
Variant::Value(num_threads) => Ok(Self::Threads(*num_threads as usize)),
}
}
}
impl TryFrom<MaxOptimizationThreads> for Option<usize> {
type Error = Status;
fn try_from(value: MaxOptimizationThreads) -> Result<Self, Self::Error> {
let variant = parse_variant(&value).map_err(Status::from)?;
match variant {
Variant::Setting(setting_int) => {
let setting = Setting::try_from(*setting_int)
.map_err(MaxOptimizationThreadsError::from)
.map_err(Status::from)?;
match setting {
Setting::Auto => Ok(None),
}
}
Variant::Value(num_threads) => Ok(Some(*num_threads as usize)),
}
}
}
Contributor
Author
There was a problem hiding this comment.
That's actually a nice idea. Since it is a common pattern in all the conversions, it might be a good idea to generalize it into a conversion error which takes the type it failed to convert from, and has the same error message for all of them.
It would be best to do it in a separate PR, though :)
generall
reviewed
Dec 18, 2024
generall
reviewed
Dec 18, 2024
generall
approved these changes
Dec 18, 2024
timvisee
pushed a commit
that referenced
this pull request
Jan 8, 2025
* Allow max-optimization-thread config to be set to null on update * add new field. Support both, but prepare for deprecation. * better openapi * only introduce change in OptimizersConfigDiff * move to `api::rest::schema` * update openapi * improve test, fix diff to config conversion * upd grpc docs * clippy * remove schemars from common common --------- Co-authored-by: Gulshan Kumar <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #5243
Continues the work from @gulshan-rs (#5520) to enable setting
max_optimization_threadsto automatic mode.The main problem is that serde has little distinction between setting to
nulland not including the field. So, to make it explicit, this PR switches the config diff to be an enum of["auto" | usize ], but only on the API level