-
Notifications
You must be signed in to change notification settings - Fork 9
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
可用区内资源数量监控 #391
可用区内资源数量监控 #391
Changes from all commits
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{Builder, Endpoint, Single, Topology}; | ||
use discovery::TopologyWrite; | ||
use discovery::{distance, TopologyWrite}; | ||
use protocol::{Protocol, RedisFlager, Request, Resource}; | ||
use sharding::distribution::Distribute; | ||
use sharding::hash::{Hash, HashKey, Hasher}; | ||
|
@@ -262,6 +262,7 @@ where | |
for (master_addr, slaves) in addrs { | ||
assert_ne!(master_addr.len(), 0); | ||
assert_ne!(slaves.len(), 0); | ||
let port = master_addr.port().to_string(); | ||
let master = self.take_or_build(&mut old, &master_addr, self.cfg.timeout_master()); | ||
master.enable_single(); | ||
|
||
|
@@ -281,6 +282,20 @@ where | |
replicas, | ||
self.cfg.basic.region_enabled, | ||
); | ||
|
||
// 生成端口副本数量监控数据 | ||
let n: u16 = if self.cfg.basic.region_enabled { | ||
shard.len_region() + 10000 // snapshot时>0才有输出,len_region这里加10000作为基准值,保证监控有数据; | ||
} else { | ||
0 // region功能关闭时,len_region为0,也就是不输出监控数据;可用区从打开到关闭场景,0也要生成监控数据,覆盖已有的同path旧数据 | ||
}; | ||
metrics::resource_num_metric( | ||
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. 增加的指标数量会不会太多,内存总量有上升吗?指标动态创建会有瞬时的较大内存申请 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. 从tcp copy环境看没有肉眼可见的增加 |
||
self.cfg.resource_type(), | ||
self.cfg.service.as_str(), | ||
(host_region() + ":" + port.as_str()).as_str(), | ||
n, | ||
); | ||
|
||
self.shards.push(shard); | ||
} | ||
assert_eq!(self.shards.len(), self.cfg.shards_url.len()); | ||
|
@@ -292,6 +307,19 @@ where | |
true | ||
} | ||
} | ||
|
||
// 本机的region,优先级: | ||
// 1. 启动参数/环境变量的region | ||
// 2. 通过本机IP计算出来的region | ||
// TODO 目前仅本文件内被使用,后续可以考虑放在一个公共的地方 | ||
fn host_region() -> String { | ||
if let Some(region) = context::get().region() { | ||
return region.to_string(); | ||
} | ||
|
||
distance::host_region() | ||
} | ||
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. 为啥返回string呢? 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. 与distance::host_region()保持一致;distance::host_region()返回string的原因是返回str会报错; |
||
|
||
#[derive(Clone)] | ||
struct Shard<E> { | ||
master: (String, E), | ||
|
@@ -327,6 +355,9 @@ impl<E> Shard<E> { | |
fn next(&self, idx: usize, runs: usize) -> (usize, &(String, E)) { | ||
unsafe { self.slaves.unsafe_next(idx, runs) } | ||
} | ||
pub fn len_region(&self) -> u16 { | ||
self.slaves.len_region() | ||
} | ||
} | ||
impl<E: discovery::Inited> Shard<E> { | ||
// 1. 主已经初始化 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ use std::sync::Arc; | |
pub trait MetricData: Sized { | ||
fn incr_to(self, data: &ItemData); | ||
fn incr_to_cache(self, _id: &Arc<Id>) {} | ||
fn zero(self, _data: &ItemData) {} | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
|
@@ -30,6 +31,13 @@ impl ItemData { | |
assert!(self.id.t.is_num()); | ||
self.inner.num.incr(num); | ||
} | ||
#[inline] | ||
pub fn zero_num(&self) { | ||
assert!(self.id.t.is_num()); | ||
unsafe { | ||
self.inner.num.zero(); | ||
} | ||
} | ||
} | ||
use crate::ToNumber; | ||
impl<T: ToNumber> MetricData for T { | ||
|
@@ -41,6 +49,10 @@ impl<T: ToNumber> MetricData for T { | |
fn incr_to_cache(self, id: &Arc<Id>) { | ||
crate::register_cache(id, self.int()); | ||
} | ||
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. 增加个set方法更合适吧 |
||
#[inline] | ||
fn zero(self, data: &ItemData) { | ||
data.zero_num(); | ||
} | ||
} | ||
use ds::time::Duration; | ||
impl MetricData for Duration { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,14 @@ pub fn decr_task() { | |
pub fn set_sockfile_failed(failed_count: usize) { | ||
SOCKFILE_FAILED.store(failed_count as i64, Relaxed); | ||
} | ||
|
||
// fn unchange_number_metric(region_enable:bool,len_region: u16, port: &str, rsname: &str, region: &str) { | ||
pub fn resource_num_metric(source: &str, namespace: &str, bip: &str, n: u16) { | ||
let path = crate::Path::new(vec![source, namespace, bip]); | ||
let mut metric = path.num("region_resource"); | ||
if metric.inited() { | ||
metric.zero_num() | ||
}; | ||
|
||
metric += n; | ||
} | ||
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. 可用区指标相关不应放在host下吧 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. 没合适的位置;新增一个文件似乎也不值当。有建议的地方吗? |
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.
这也可以不用string吧
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.
返回&str有编译报错