-
Notifications
You must be signed in to change notification settings - Fork 98
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
[WIP]feat: query config listeners info #134
Open
l-7-l
wants to merge
4
commits into
nacos-group:master
Choose a base branch
from
l-7-l:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ use std::sync::Arc; | |
use std::sync::Weak; | ||
use std::time::Duration; | ||
|
||
use crate::console::model::paginate::PaginateQuery; | ||
use crate::raft::store::ClientRequest; | ||
use crate::raft::NacosRaft; | ||
use crate::utils::get_md5; | ||
|
@@ -21,6 +22,8 @@ use actix::prelude::*; | |
|
||
use super::config_subscribe::Subscriber; | ||
use super::dal::ConfigHistoryParam; | ||
use super::dal::ConfigListenerDo; | ||
use super::dal::QueryListeners; | ||
use crate::config::config_index::{ConfigQueryParam, TenantIndex}; | ||
use crate::config::config_type::ConfigType; | ||
use crate::config::model::{ | ||
|
@@ -297,14 +300,48 @@ pub enum ListenerResult { | |
DATA(Vec<ConfigKey>), | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct AppName(String); | ||
|
||
impl AppName { | ||
pub fn as_str(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Default for AppName { | ||
fn default() -> Self { | ||
Self("unknown".to_owned()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize)] | ||
pub struct ConfigListenerInfo { | ||
/// 客户端设置的应用名字,默认值为 unknown | ||
/// appName set by the client, the default value is unknown | ||
pub name: AppName, | ||
/// 客户端 ip 地址 | ||
/// client remote ip address | ||
pub ip: String, | ||
/// 客户端订阅的版本,当前取值为请求 User-Agent | ||
/// The client version. The current value is user agent | ||
pub version: String, | ||
} | ||
|
||
type ListenerSenderType = tokio::sync::oneshot::Sender<ListenerResult>; | ||
//type ListenerReceiverType = tokio::sync::oneshot::Receiver<ListenerResult>; | ||
|
||
pub struct ConfigListenerChannel { | ||
sender: ListenerSenderType, | ||
info: ConfigListenerInfo, | ||
} | ||
|
||
pub(crate) struct ConfigListener { | ||
version: u64, | ||
listener: HashMap<ConfigKey, Vec<u64>>, | ||
time_listener: BTreeMap<i64, Vec<OnceListener>>, | ||
sender_map: HashMap<u64, ListenerSenderType>, | ||
channels: HashMap<u64, ConfigListenerChannel>, | ||
} | ||
|
||
impl ConfigListener { | ||
|
@@ -313,11 +350,11 @@ impl ConfigListener { | |
version: 0, | ||
listener: Default::default(), | ||
time_listener: Default::default(), | ||
sender_map: Default::default(), | ||
channels: Default::default(), | ||
} | ||
} | ||
|
||
fn add(&mut self, items: Vec<ListenerItem>, sender: ListenerSenderType, time: i64) { | ||
fn add(&mut self, items: Vec<ListenerItem>, channel: ConfigListenerChannel, time: i64) { | ||
self.version += 1; | ||
for item in &items { | ||
let key = item.key.clone(); | ||
|
@@ -330,7 +367,7 @@ impl ConfigListener { | |
} | ||
}; | ||
} | ||
self.sender_map.insert(self.version, sender); | ||
self.channels.insert(self.version, channel); | ||
let once_listener = OnceListener { | ||
version: self.version, | ||
//time, | ||
|
@@ -349,8 +386,11 @@ impl ConfigListener { | |
fn notify(&mut self, key: ConfigKey) { | ||
if let Some(list) = self.listener.remove(&key) { | ||
for v in list { | ||
if let Some(sender) = self.sender_map.remove(&v) { | ||
sender.send(ListenerResult::DATA(vec![key.clone()])).ok(); | ||
if let Some(sender) = self.channels.remove(&v) { | ||
sender | ||
.sender | ||
.send(ListenerResult::DATA(vec![key.clone()])) | ||
.ok(); | ||
} | ||
} | ||
} | ||
|
@@ -364,8 +404,8 @@ impl ConfigListener { | |
keys.push(*key); | ||
for item in list { | ||
let v = item.version; | ||
if let Some(sender) = self.sender_map.remove(&v) { | ||
sender.send(ListenerResult::NULL).ok(); | ||
if let Some(channel) = self.channels.remove(&v) { | ||
channel.sender.send(ListenerResult::NULL).ok(); | ||
} | ||
} | ||
} else { | ||
|
@@ -378,7 +418,7 @@ impl ConfigListener { | |
} | ||
|
||
pub(crate) fn get_listener_client_size(&self) -> usize { | ||
self.sender_map.len() | ||
self.channels.len() | ||
} | ||
|
||
pub(crate) fn get_listener_key_size(&self) -> usize { | ||
|
@@ -531,6 +571,31 @@ impl ConfigActor { | |
Ok(()) | ||
} | ||
|
||
pub fn get_config_listeners( | ||
&self, | ||
config_key: &ConfigKey, | ||
pagiante: &PaginateQuery, | ||
) -> (usize, Vec<ConfigListenerDo>) { | ||
let ids = self.listener.listener.get(config_key).unwrap(); | ||
let cursors = &ids[pagiante.page_no - 1..pagiante.page_size]; | ||
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. page_no 的最小值是1 |
||
|
||
let subscribers = cursors | ||
.iter() | ||
.copied() | ||
.filter_map(|id| { | ||
self.listener | ||
.channels | ||
.get(&id) | ||
.map(|channel| ConfigListenerDo { | ||
id, | ||
info: channel.info.clone(), | ||
}) | ||
}) | ||
.collect::<Vec<ConfigListenerDo>>(); | ||
|
||
(ids.len(), subscribers) | ||
} | ||
|
||
pub fn get_config_info_page(&self, param: &ConfigQueryParam) -> (usize, Vec<ConfigInfoDto>) { | ||
let (size, list) = self.tenant_index.query_config_page(param); | ||
|
||
|
@@ -653,7 +718,13 @@ pub enum ConfigCmd { | |
GET(ConfigKey), | ||
QueryPageInfo(Box<ConfigQueryParam>), | ||
QueryHistoryPageInfo(Box<ConfigHistoryParam>), | ||
LISTENER(Vec<ListenerItem>, ListenerSenderType, i64), | ||
Listener( | ||
Vec<ListenerItem>, | ||
ListenerSenderType, | ||
ConfigListenerInfo, | ||
i64, | ||
), | ||
QueryListeners(QueryListeners), | ||
Subscribe(Vec<ListenerItem>, Arc<String>), | ||
RemoveSubscribe(Vec<ListenerItem>, Arc<String>), | ||
RemoveSubscribeClient(Arc<String>), | ||
|
@@ -685,6 +756,7 @@ pub enum ConfigResult { | |
ChangeKey(Vec<ConfigKey>), | ||
ConfigInfoPage(usize, Vec<ConfigInfoDto>), | ||
ConfigHistoryInfoPage(usize, Vec<ConfigHistoryInfoDto>), | ||
ConfigListenerInfoPage(usize, Vec<ConfigListenerDo>), | ||
} | ||
|
||
impl Actor for ConfigActor { | ||
|
@@ -727,7 +799,13 @@ impl Handler<ConfigCmd> for ConfigActor { | |
}); | ||
} | ||
} | ||
ConfigCmd::LISTENER(items, sender, time) => { | ||
ConfigCmd::QueryListeners(cmd) => { | ||
let (total, subscribers) = | ||
self.get_config_listeners(&cmd.config_key, &cmd.paginate); | ||
|
||
return Ok(ConfigResult::ConfigListenerInfoPage(total, subscribers)); | ||
} | ||
ConfigCmd::Listener(items, sender, subscribe_info, time) => { | ||
let mut changes = vec![]; | ||
for item in &items { | ||
if let Some(v) = self.cache.get(&item.key) { | ||
|
@@ -742,7 +820,14 @@ impl Handler<ConfigCmd> for ConfigActor { | |
sender.send(ListenerResult::DATA(changes)).ok(); | ||
return Ok(ConfigResult::NULL); | ||
} else { | ||
self.listener.add(items, sender, time); | ||
self.listener.add( | ||
items, | ||
ConfigListenerChannel { | ||
sender, | ||
info: subscribe_info, | ||
}, | ||
time, | ||
); | ||
return Ok(ConfigResult::NULL); | ||
} | ||
} | ||
|
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
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
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,59 @@ | ||
use std::ops::Sub; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Copy, Deserialize)] | ||
#[serde(from = "String")] | ||
pub struct Page(usize); | ||
|
||
impl Page { | ||
pub fn new(page: usize) -> Self { | ||
Self(page.max(1)) | ||
} | ||
pub fn as_usize(&self) -> usize { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Default for Page { | ||
fn default() -> Self { | ||
Self(1) | ||
} | ||
} | ||
|
||
impl Sub<usize> for Page { | ||
type Output = usize; | ||
|
||
fn sub(self, rhs: usize) -> Self::Output { | ||
self.0 - rhs | ||
} | ||
} | ||
|
||
impl From<String> for Page { | ||
fn from(page: String) -> Self { | ||
Self(page.parse::<usize>().unwrap_or(1)) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct PaginateQuery { | ||
pub page_no: Page, | ||
pub page_size: usize, | ||
} | ||
|
||
impl Default for PaginateQuery { | ||
fn default() -> Self { | ||
Self { | ||
page_no: Page::default(), | ||
page_size: 0xffff_ffff, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct PaginateResponse<T> { | ||
pub count: usize, | ||
pub list: Vec<T>, | ||
} |
Oops, something went wrong.
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.
AppName 位置需要改动吗? 需要添加校验规则吗?
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.
AppName后面注册中心应该也会用,位置可以考虑放在common/model中;
校验规则应该不需要,取值上可以把考虑只取符合规则的值,如果不合规就当做unknow。
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.
好的