Skip to content

Enable resetting max_optimization_threads to automatic#5634

Merged
coszio merged 10 commits intodevfrom
max-optimization-thread-nullable
Dec 18, 2024
Merged

Enable resetting max_optimization_threads to automatic#5634
coszio merged 10 commits intodevfrom
max-optimization-thread-nullable

Conversation

@coszio
Copy link
Copy Markdown
Contributor

@coszio coszio commented Dec 10, 2024

Fixes #5243

Continues the work from @gulshan-rs (#5520) to enable setting max_optimization_threads to automatic mode.

The main problem is that serde has little distinction between setting to null and 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

@coszio coszio requested a review from timvisee December 10, 2024 10:56
@coszio coszio requested a review from generall December 17, 2024 12:43
}
}

impl TryFrom<MaxOptimizationThreads> for rest::MaxOptimizationThreads {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)),
        }
    }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Comment thread lib/api/src/grpc/proto/collections.proto
Comment thread lib/common/common/Cargo.toml Outdated
Comment thread Cargo.lock Outdated
@coszio coszio merged commit daef713 into dev Dec 18, 2024
@coszio coszio deleted the max-optimization-thread-nullable branch December 18, 2024 20:17
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants