Skip to content

Commit

Permalink
split storage operation structures #32
Browse files Browse the repository at this point in the history
  • Loading branch information
generall committed Nov 21, 2021
1 parent 77bc689 commit 12f19f9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 44 deletions.
69 changes: 44 additions & 25 deletions lib/storage/src/content_manager/storage_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,51 @@ pub enum AliasOperations {
},
}

/// Operation for creating new collection and (optionally) specify index params
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct CreateCollectionOperation {
pub name: String,
pub vector_size: usize,
pub distance: Distance,
/// Custom params for HNSW index. If none - values from service configuration file are used.
pub hnsw_config: Option<HnswConfigDiff>,
/// Custom params for WAL. If none - values from service configuration file are used.
pub wal_config: Option<WalConfigDiff>,
/// Custom params for Optimizers. If none - values from service configuration file are used.
pub optimizers_config: Option<OptimizersConfigDiff>,
}

/// Operation for updating parameters of the existing collection
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct UpdateCollectionOperation {
pub name: String,
/// Custom params for Optimizers. If none - values from service configuration file are used.
/// This operation is blocking, it will only proceed ones all current optimizations are complete
pub optimizers_config: Option<OptimizersConfigDiff>, // ToDo: Allow updates for other configuration params as well
}

/// Operation for performing changes of collection aliases.
/// Alias changes are atomic, meaning that no collection modifications can happen between
/// alias operations.
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ChangeAliasesOperation {
pub actions: Vec<AliasOperations>
}

/// Operation for deleting collection with given name
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct DeleteCollectionOperation(pub String);

/// Enumeration of all possible collection update operations
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum StorageOperations {
/// Create new collection and (optionally) specify index params
CreateCollection {
name: String,
vector_size: usize,
distance: Distance,
/// Custom params for HNSW index. If none - values from service configuration file are used.
hnsw_config: Option<HnswConfigDiff>,
/// Custom params for WAL. If none - values from service configuration file are used.
wal_config: Option<WalConfigDiff>,
/// Custom params for Optimizers. If none - values from service configuration file are used.
optimizers_config: Option<OptimizersConfigDiff>,
},
/// Update parameters of the existing collection
UpdateCollection {
name: String,
/// Custom params for Optimizers. If none - values from service configuration file are used.
/// This operation is blocking, it will only proceed ones all current optimizations are complete
optimizers_config: Option<OptimizersConfigDiff>, // ToDo: Allow updates for other configuration params as well
},
/// Delete collection with given name
DeleteCollection(String),
/// Perform changes of collection aliases.
/// Alias changes are atomic, meaning that no collection modifications can happen between
/// alias operations.
ChangeAliases { actions: Vec<AliasOperations> },
CreateCollection(CreateCollectionOperation),
UpdateCollection(UpdateCollectionOperation),
DeleteCollection(DeleteCollectionOperation),
ChangeAliases(ChangeAliasesOperation),
}
14 changes: 7 additions & 7 deletions lib/storage/src/content_manager/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use segment::types::{PointIdType, ScoredPoint, WithPayload};

use crate::content_manager::collections_ops::{Checker, Collections};
use crate::content_manager::errors::StorageError;
use crate::content_manager::storage_ops::{AliasOperations, StorageOperations};
use crate::content_manager::storage_ops::{AliasOperations, ChangeAliasesOperation, CreateCollectionOperation, DeleteCollectionOperation, StorageOperations, UpdateCollectionOperation};
use crate::types::StorageConfig;
use collection::collection_manager::collection_managers::CollectionSearcher;
use collection::collection_manager::simple_collection_searcher::SimpleCollectionSearcher;
Expand Down Expand Up @@ -125,14 +125,14 @@ impl TableOfContent {
operation: StorageOperations,
) -> Result<bool, StorageError> {
match operation {
StorageOperations::CreateCollection {
StorageOperations::CreateCollection(CreateCollectionOperation {
name: collection_name,
vector_size,
distance,
hnsw_config: hnsw_config_diff,
wal_config: wal_config_diff,
optimizers_config: optimizers_config_diff,
} => {
}) => {
self.collections
.read()
.await
Expand Down Expand Up @@ -174,10 +174,10 @@ impl TableOfContent {
write_collections.insert(collection_name, Arc::new(collection));
Ok(true)
}
StorageOperations::UpdateCollection {
StorageOperations::UpdateCollection(UpdateCollectionOperation {
name,
optimizers_config,
} => {
}) => {
match optimizers_config {
None => {}
Some(new_optimizers_config) => {
Expand All @@ -189,7 +189,7 @@ impl TableOfContent {
}
Ok(true)
}
StorageOperations::DeleteCollection(collection_name) => {
StorageOperations::DeleteCollection(DeleteCollectionOperation(collection_name)) => {
if let Some(removed) = self.collections.write().await.remove(&collection_name) {
removed.stop().await?;
{
Expand All @@ -209,7 +209,7 @@ impl TableOfContent {
Ok(false)
}
}
StorageOperations::ChangeAliases { actions } => {
StorageOperations::ChangeAliases(ChangeAliasesOperation { actions }) => {
// Lock all collections for alias changes
// Prevent search on partially switched collections
let collection_lock = self.collections.write().await;
Expand Down
14 changes: 7 additions & 7 deletions lib/storage/tests/alias_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::runtime::Runtime;
mod tests {
use super::*;
use segment::types::Distance;
use storage::content_manager::storage_ops::{AliasOperations, StorageOperations};
use storage::content_manager::storage_ops::{AliasOperations, ChangeAliasesOperation, CreateCollectionOperation, StorageOperations};

#[test]
fn test_alias_operation() {
Expand Down Expand Up @@ -39,31 +39,31 @@ mod tests {

handle
.block_on(
toc.perform_collection_operation(StorageOperations::CreateCollection {
toc.perform_collection_operation(StorageOperations::CreateCollection(CreateCollectionOperation {
name: "test".to_string(),
vector_size: 10,
distance: Distance::Cosine,
hnsw_config: None,
wal_config: None,
optimizers_config: None,
}),
})),
)
.unwrap();

handle
.block_on(
toc.perform_collection_operation(StorageOperations::ChangeAliases {
toc.perform_collection_operation(StorageOperations::ChangeAliases(ChangeAliasesOperation {
actions: vec![AliasOperations::CreateAlias {
collection_name: "test".to_string(),
alias_name: "test_alias".to_string(),
}],
}),
})),
)
.unwrap();

handle
.block_on(
toc.perform_collection_operation(StorageOperations::ChangeAliases {
toc.perform_collection_operation(StorageOperations::ChangeAliases(ChangeAliasesOperation {
actions: vec![
AliasOperations::CreateAlias {
collection_name: "test".to_string(),
Expand All @@ -77,7 +77,7 @@ mod tests {
new_alias_name: "test_alias3".to_string(),
},
],
}),
})),
)
.unwrap();

Expand Down
11 changes: 6 additions & 5 deletions src/tonic/api/collections_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::convert::TryFrom;
use std::sync::Arc;
use std::time::Instant;
use storage::content_manager::errors::StorageError;
use storage::content_manager::storage_ops::{CreateCollectionOperation, DeleteCollectionOperation, UpdateCollectionOperation};
use storage::content_manager::toc::TableOfContent;

pub struct CollectionsService {
Expand Down Expand Up @@ -99,14 +100,14 @@ impl TryFrom<CreateCollection> for storage::content_manager::storage_ops::Storag

fn try_from(value: CreateCollection) -> Result<Self, Self::Error> {
if let Some(distance) = FromPrimitive::from_i32(value.distance) {
Ok(Self::CreateCollection {
Ok(Self::CreateCollection(CreateCollectionOperation {
name: value.name,
vector_size: value.vector_size as usize,
distance,
hnsw_config: value.hnsw_config.map(|v| v.into()),
wal_config: value.wal_config.map(|v| v.into()),
optimizers_config: value.optimizers_config.map(|v| v.into()),
})
}))
} else {
Err(Status::failed_precondition("Bad value of distance field!"))
}
Expand Down Expand Up @@ -174,15 +175,15 @@ impl From<(Instant, Result<bool, StorageError>)> for CollectionOperationResponse

impl From<UpdateCollection> for storage::content_manager::storage_ops::StorageOperations {
fn from(value: UpdateCollection) -> Self {
Self::UpdateCollection {
Self::UpdateCollection(UpdateCollectionOperation {
name: value.name,
optimizers_config: value.optimizers_config.map(|v| v.into()),
}
})
}
}

impl From<DeleteCollection> for storage::content_manager::storage_ops::StorageOperations {
fn from(value: DeleteCollection) -> Self {
Self::DeleteCollection(value.name)
Self::DeleteCollection(DeleteCollectionOperation(value.name))
}
}

0 comments on commit 12f19f9

Please sign in to comment.