-
Notifications
You must be signed in to change notification settings - Fork 125
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
GH-768 Store config uniqueness enforced by StoreManager #1555
base: main
Are you sure you want to change the base?
Changes from all commits
324df36
9f67019
ccca3a6
6bb7cc2
01f23a7
4a4d11a
d361abf
91a3557
9adeef3
0022bdf
aff36b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mkeen@lab.245673:1735391239 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ use nativelink_proto::google::devtools::build::v1::{ | |
PublishBuildToolEventStreamRequest, PublishLifecycleEventRequest, StreamId, | ||
}; | ||
use nativelink_service::bep_server::BepServer; | ||
use nativelink_store::default_store_factory::store_factory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems heavy at first read but maybe it is an improvement to both the naming and the functionality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
use nativelink_store::default_store_factory::make_and_add_store_to_manager; | ||
use nativelink_store::store_manager::StoreManager; | ||
use nativelink_util::buf_channel::make_buf_channel_pair; | ||
use nativelink_util::channel_body_for_tests::ChannelBody; | ||
|
@@ -52,15 +52,14 @@ const BEP_STORE_NAME: &str = "main_bep"; | |
/// Utility function to construct a [`StoreManager`] | ||
async fn make_store_manager() -> Result<Arc<StoreManager>, Error> { | ||
let store_manager = Arc::new(StoreManager::new()); | ||
store_manager.add_store( | ||
make_and_add_store_to_manager( | ||
BEP_STORE_NAME, | ||
store_factory( | ||
&StoreSpec::memory(MemorySpec::default()), | ||
&store_manager, | ||
None, | ||
) | ||
.await?, | ||
); | ||
&StoreSpec::memory(MemorySpec::default()), | ||
&store_manager, | ||
None, | ||
) | ||
.await?; | ||
|
||
Ok(store_manager) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 The NativeLink Authors. All rights reserved. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PERFECT! |
||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use nativelink_config::stores::{MemorySpec, StoreSpec}; | ||
use nativelink_error::Error; | ||
use nativelink_macro::nativelink_test; | ||
use nativelink_store::default_store_factory::make_and_add_store_to_manager; | ||
use nativelink_store::store_manager::StoreManager; | ||
|
||
#[nativelink_test] | ||
async fn same_datasource_disallowed_simple() -> Result<(), Error> { | ||
let store_manager = Arc::new(StoreManager::new()); | ||
assert!(make_and_add_store_to_manager( | ||
"main_cas", | ||
&StoreSpec::memory(MemorySpec::default()), | ||
&store_manager, | ||
None, | ||
) | ||
.await | ||
.is_ok()); | ||
|
||
assert!(make_and_add_store_to_manager( | ||
"main_ac", | ||
&StoreSpec::memory(MemorySpec::default()), | ||
&store_manager, | ||
None, | ||
) | ||
.await | ||
.is_ok()); | ||
|
||
let existing_cas = store_manager.get_store("main_cas").unwrap(); | ||
store_manager.add_store("different_store", existing_cas)?; | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
// limitations under the License. | ||
|
||
use std::collections::HashMap; | ||
use std::ptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need this.. will remove |
||
|
||
use nativelink_error::{make_err, Code, Error}; | ||
use nativelink_metric::{MetricsComponent, RootMetricsComponent}; | ||
use nativelink_util::store_trait::Store; | ||
use parking_lot::RwLock; | ||
|
@@ -22,18 +24,56 @@ use parking_lot::RwLock; | |
pub struct StoreManager { | ||
#[metric] | ||
stores: RwLock<HashMap<String, Store>>, | ||
store_config_anti_collision_digests: RwLock<Vec<String>>, | ||
} | ||
|
||
impl StoreManager { | ||
pub fn new() -> StoreManager { | ||
StoreManager { | ||
stores: RwLock::new(HashMap::new()), | ||
store_config_anti_collision_digests: RwLock::new(vec![]), | ||
} | ||
} | ||
|
||
pub fn add_store(&self, name: &str, store: Store) { | ||
pub fn add_store(&self, name: &str, store: Store) -> Result<(), Error> { | ||
let mut stores = self.stores.write(); | ||
stores.insert(name.to_string(), store); | ||
|
||
if stores.contains_key(name) { | ||
return Err(make_err!( | ||
Code::AlreadyExists, | ||
"a store with the name '{}' already exists", | ||
name | ||
)); | ||
} | ||
|
||
for existing_store in stores.values().into_iter() { | ||
if ptr::eq(&store, existing_store) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove this check. Thanks to rust's memory model and the fact that in this design, ownership of the struct instance is passed to the StoreManager on line 58. We don't have Store instance pointer being passed around with a risk of passing the same pointer N times. We have a single instance of which ownership is transferred exactly once in guaranteed fashion (thanks Rust). So instance protection is no longer a goal or needed. But I do think the config overlap detection is valuable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Rust is an underrated comment. |
||
return Err(make_err!( | ||
Code::AlreadyExists, | ||
"an instance of this store is already managed" | ||
)); | ||
} | ||
} | ||
|
||
stores.insert(name.into(), store); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn digest_not_already_present(&self, digest: &str) -> Result<(), Error> { | ||
let digests = self.store_config_anti_collision_digests.read(); | ||
match digests.contains(&String::from(digest)) { | ||
true => Err(make_err!( | ||
Code::AlreadyExists, | ||
"the provided config is already being used by another store" | ||
)), | ||
_ => Ok(()), | ||
} | ||
} | ||
|
||
pub fn config_digest_add(&self, digest: String) { | ||
let mut digests = self.store_config_anti_collision_digests.write(); | ||
digests.push(digest); | ||
} | ||
|
||
pub fn get_store(&self, name: &str) -> Option<Store> { | ||
|
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.
For my understanding, what is the reason why you moved away from
store_factory
rather than modifyingstore_factory
?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.
Still using
store_factory
to build the Store, but wrapping up inmake_and_add_store_to_manager
because it makes building and adding a Store to a StoreManager a single step from a public-facing perspective.