Skip to content
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 7 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ normal = ["workspace-hack"]

[dependencies]
anyhow = "1"
arc-swap = "1"
arrow-array = "33"
arrow-schema = "33"
async-trait = "0.1"
Expand Down
86 changes: 86 additions & 0 deletions src/common/src/system_param/local_manager.rs
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));
Copy link
Member

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?

Copy link
Contributor Author

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 🤔

Copy link
Member

@BugenZhao BugenZhao Feb 24, 2023

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 a Arc<ArcSwap>? For the latter way, this sounds reasonable to me.

Oh, the PR body has elaborated on this. 😄

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());
}
}
1 change: 1 addition & 0 deletions src/common/src/system_param/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod local_manager;
pub mod reader;

use std::collections::HashSet;
Expand Down
2 changes: 1 addition & 1 deletion src/common/src/system_param/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::system_params_to_kv;
/// - Avoid misuse of deprecated fields by hiding their getters.
/// - Abstract fallback logic for fields that might not be provided by meta service due to backward
/// compatibility.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct SystemParamsReader {
prost: ProstSystemParams,
}
Expand Down