-
Notifications
You must be signed in to change notification settings - Fork 595
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
feat(common): LocalSystemParamManager
for worker node
#8153
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2467e4b
introduce local system param manager
Gun9niR 3fb1695
add comment
Gun9niR 66d9874
switch to watch
Gun9niR faf9dce
rename
Gun9niR bb000e5
fix
05b92b1
Merge branch 'main' into zhidong/local-param-manager
mergify[bot] c0538f1
Merge branch 'main' into zhidong/local-param-manager
Gun9niR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// 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::ops::Deref; | ||
use std::sync::Arc; | ||
|
||
use arc_swap::ArcSwap; | ||
use risingwave_pb::meta::SystemParams; | ||
use tokio::sync::watch::{channel, Receiver, Sender}; | ||
|
||
use super::reader::SystemParamsReader; | ||
|
||
pub type SystemParamsReaderRef = Arc<ArcSwap<SystemParamsReader>>; | ||
|
||
/// The system parameter manager on worker nodes. It provides two methods for other components to | ||
/// read the latest system parameters: | ||
/// - `get_params` returns a reference to the latest parameters that is atomically updated. | ||
/// - `watch_params` returns a channel on which calling `recv` will get the latest parameters. | ||
/// Compared with `get_params`, the caller can be explicitly notified of parameter change. | ||
pub struct LocalSystemParamManager { | ||
/// The latest parameters. | ||
params: SystemParamsReaderRef, | ||
|
||
/// Sender of the latest parameters. | ||
tx: Sender<SystemParamsReaderRef>, | ||
} | ||
|
||
impl LocalSystemParamManager { | ||
pub fn new(params: SystemParamsReader) -> Self { | ||
let params = Arc::new(ArcSwap::from_pointee(params)); | ||
let (tx, _) = channel(params.clone()); | ||
Self { params, tx } | ||
} | ||
|
||
pub fn get_params(&self) -> SystemParamsReaderRef { | ||
self.params.clone() | ||
} | ||
|
||
pub fn try_set_params(&self, new_params: SystemParams) { | ||
let new_params_reader = SystemParamsReader::from(new_params); | ||
if self.params.load().deref().deref() != &new_params_reader { | ||
self.params.store(Arc::new(new_params_reader)); | ||
// Ignore no active receiver. | ||
let _ = self.tx.send(self.params.clone()); | ||
} | ||
} | ||
|
||
pub fn watch_parmams(&self) -> Receiver<SystemParamsReaderRef> { | ||
self.tx.subscribe() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_manager() { | ||
let p = SystemParams::default().into(); | ||
let manager = LocalSystemParamManager::new(p); | ||
let shared_params = manager.get_params(); | ||
|
||
let new_params = SystemParams { | ||
sstable_size_mb: Some(1), | ||
..Default::default() | ||
}; | ||
|
||
let mut params_rx = manager.watch_parmams(); | ||
|
||
manager.try_set_params(new_params.clone()); | ||
params_rx.changed().await.unwrap(); | ||
assert_eq!(**params_rx.borrow().load(), new_params.clone().into()); | ||
assert_eq!(**shared_params.load(), new_params.into()); | ||
} | ||
} |
This file contains 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
This file contains 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
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.
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.
Using
Arc
alone should be enough here?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.
When the latest params arrive at the worker though notification service, the local manager needs a thread-safe way to update params, but Arc does not provide that functionality 🤔
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.
How will the subscribers get the param updates, through the
watch
channel to get the updates or simply hold aArc<ArcSwap>
? For the latter way, this sounds reasonable to me.Oh, the PR body has elaborated on this. 😄