Skip to content

Commit

Permalink
Add max optimization threads builder
Browse files Browse the repository at this point in the history
  • Loading branch information
timvisee committed Jan 15, 2025
1 parent a29cb7b commit f53b4f3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
72 changes: 72 additions & 0 deletions src/builders/max_optimization_threads_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
use crate::qdrant::*;

/// Max number of threads (jobs) for running optimizations per shard.
/// Each optimization job will also use `max_indexing_threads` threads by itself for index building.
///
/// - If `auto` - have no limit and choose dynamically to saturate CPU.
/// - If `disabled` or `0` - no optimization threads, optimizations will be disabled.
pub struct MaxOptimizationThreadsBuilder {
pub(crate) inner: MaxOptimizationThreads,
}

impl MaxOptimizationThreadsBuilder {
/// Use specific number of optimization threads.
///
/// - If `0` - no optimization threads, optimizations will be disabled.
#[allow(unused_mut)]
#[inline]
pub fn threads(threads: u64) -> Self {
Self {
inner: MaxOptimizationThreads::from(threads),
}
}

/// No optimization threads, optimizations will be disabled.
#[allow(unused_mut)]
#[inline]
pub fn disabled() -> Self {
Self::threads(0)
}

/// Have no limit and choose dynamically to saturate CPU.
#[allow(unused_mut)]
#[inline]
pub fn auto() -> Self {
Self {
inner: MaxOptimizationThreads::from(max_optimization_threads::Variant::Setting(
max_optimization_threads::Setting::Auto as i32,
)),
}
}
}

impl From<MaxOptimizationThreadsBuilder> for MaxOptimizationThreads {
fn from(value: MaxOptimizationThreadsBuilder) -> Self {
value.build()
}
}

impl MaxOptimizationThreadsBuilder {
pub fn build(self) -> MaxOptimizationThreads {
self.inner
}
}

impl Default for MaxOptimizationThreadsBuilder {
fn default() -> Self {
Self::auto()
}
}

impl From<u64> for MaxOptimizationThreads {
fn from(threads: u64) -> Self {
MaxOptimizationThreads {
Expand All @@ -8,8 +66,22 @@ impl From<u64> for MaxOptimizationThreads {
}
}

impl From<max_optimization_threads::Variant> for MaxOptimizationThreads {
fn from(setting: max_optimization_threads::Variant) -> Self {
MaxOptimizationThreads {
variant: Some(setting),
}
}
}

impl From<u64> for max_optimization_threads::Variant {
fn from(threads: u64) -> Self {
Self::Value(threads)
}
}

impl From<max_optimization_threads::Setting> for max_optimization_threads::Variant {
fn from(setting: max_optimization_threads::Setting) -> Self {
Self::Setting(setting as i32)
}
}
1 change: 1 addition & 0 deletions src/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod rename_alias_builder;
pub use rename_alias_builder::RenameAliasBuilder;

mod max_optimization_threads_builder;
pub use max_optimization_threads_builder::MaxOptimizationThreadsBuilder;

mod move_shard_builder;
pub use move_shard_builder::MoveShardBuilder;
Expand Down
37 changes: 31 additions & 6 deletions src/builders/optimizers_config_diff_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ pub struct OptimizersConfigDiffBuilder {
pub(crate) flush_interval_sec: Option<Option<u64>>,
///
/// Max number of threads (jobs) for running optimizations per shard.
/// Note: each optimization job will also use `max_indexing_threads` threads by itself for index building.
/// If null - have no limit and choose dynamically to saturate CPU.
/// If 0 - no optimization threads, optimizations will be disabled.
/// Each optimization job will also use `max_indexing_threads` threads by itself for index building.
///
/// - If `auto` - have no limit and choose dynamically to saturate CPU.
/// - If `disabled` or `0` - no optimization threads, optimizations will be disabled.
pub(crate) max_optimization_threads: Option<Option<MaxOptimizationThreads>>,
}

Expand Down Expand Up @@ -143,9 +144,33 @@ impl OptimizersConfigDiffBuilder {
}
///
/// Max number of threads (jobs) for running optimizations per shard.
/// Note: each optimization job will also use `max_indexing_threads` threads by itself for index building.
/// If null - have no limit and choose dynamically to saturate CPU.
/// If 0 - no optimization threads, optimizations will be disabled.
/// Each optimization job will also use `max_indexing_threads` threads by itself for index building.
///
/// - If `auto` - have no limit and choose dynamically to saturate CPU.
/// - If `disabled` or `0` - no optimization threads, optimizations will be disabled.
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
/// use qdrant_client::qdrant::{OptimizersConfigDiffBuilder, UpdateCollectionBuilder, MaxOptimizationThreadsBuilder};
///
///# async fn create_collection(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// let optimizers_config = OptimizersConfigDiffBuilder::default()
/// // Use exactly 8 threads
/// .max_optimization_threads(8)
/// // Or automatically choose
/// .max_optimization_threads(MaxOptimizationThreadsBuilder::auto())
/// // Or disable
/// .max_optimization_threads(MaxOptimizationThreadsBuilder::disabled());
///
/// client
/// .update_collection(
/// UpdateCollectionBuilder::new("my_collection").optimizers_config(optimizers_config),
/// )
/// .await?;
///# Ok(())
///# }
/// ```
#[allow(unused_mut)]
pub fn max_optimization_threads<VALUE: Into<MaxOptimizationThreads>>(
self,
Expand Down

0 comments on commit f53b4f3

Please sign in to comment.