-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathdownsampling.rs
166 lines (148 loc) · 5.15 KB
/
downsampling.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
//! ⚠️ WARNING ⚠️
//!
//! This module is intended for Zenoh's internal use.
//!
//! [Click here for Zenoh's documentation](../zenoh/index.html)
use crate::net::routing::interceptor::*;
use std::sync::{Arc, Mutex};
use zenoh_config::{DownsamplingFlow, DownsamplingItemConf, DownsamplingRuleConf};
use zenoh_core::zlock;
use zenoh_keyexpr::keyexpr_tree::impls::KeyedSetProvider;
use zenoh_keyexpr::keyexpr_tree::IKeyExprTreeMut;
use zenoh_keyexpr::keyexpr_tree::{support::UnknownWildness, KeBoxTree};
use zenoh_protocol::network::NetworkBody;
use zenoh_result::ZResult;
pub(crate) fn downsampling_interceptor_factories(
config: &Vec<DownsamplingItemConf>,
) -> ZResult<Vec<InterceptorFactory>> {
let mut res: Vec<InterceptorFactory> = vec![];
for ds in config {
res.push(Box::new(DownsamplingInterceptorFactory::new(ds.clone())));
}
Ok(res)
}
pub struct DownsamplingInterceptorFactory {
interfaces: Option<Vec<String>>,
rules: Vec<DownsamplingRuleConf>,
flow: DownsamplingFlow,
}
impl DownsamplingInterceptorFactory {
pub fn new(conf: DownsamplingItemConf) -> Self {
Self {
interfaces: conf.interfaces,
rules: conf.rules,
flow: conf.flow,
}
}
}
impl InterceptorFactoryTrait for DownsamplingInterceptorFactory {
fn new_transport_unicast(
&self,
transport: &TransportUnicast,
) -> (Option<IngressInterceptor>, Option<EgressInterceptor>) {
log::debug!("New downsampler transport unicast {:?}", transport);
if let Some(interfaces) = &self.interfaces {
log::debug!(
"New downsampler transport unicast config interfaces: {:?}",
interfaces
);
if let Ok(links) = transport.get_links() {
for link in links {
log::debug!(
"New downsampler transport unicast link interfaces: {:?}",
link.interfaces
);
if !link.interfaces.iter().any(|x| interfaces.contains(x)) {
return (None, None);
}
}
}
};
match self.flow {
DownsamplingFlow::Ingress => (
Some(Box::new(DownsamplingInterceptor::new(self.rules.clone()))),
None,
),
DownsamplingFlow::Egress => (
None,
Some(Box::new(DownsamplingInterceptor::new(self.rules.clone()))),
),
}
}
fn new_transport_multicast(
&self,
_transport: &TransportMulticast,
) -> Option<EgressInterceptor> {
None
}
fn new_peer_multicast(&self, _transport: &TransportMulticast) -> Option<IngressInterceptor> {
None
}
}
struct Timestate {
pub threshold: std::time::Duration,
pub latest_message_timestamp: std::time::Instant,
}
pub(crate) struct DownsamplingInterceptor {
ke_state: Arc<Mutex<KeBoxTree<Timestate, UnknownWildness, KeyedSetProvider>>>,
}
impl InterceptorTrait for DownsamplingInterceptor {
fn intercept(
&self,
ctx: RoutingContext<NetworkMessage>,
) -> Option<RoutingContext<NetworkMessage>> {
if matches!(ctx.msg.body, NetworkBody::Push(_)) {
if let Some(key_expr) = ctx.full_key_expr() {
let mut ke_state = zlock!(self.ke_state);
if let Some(state) = ke_state.weight_at_mut(&key_expr.clone()) {
let timestamp = std::time::Instant::now();
if timestamp - state.latest_message_timestamp >= state.threshold {
state.latest_message_timestamp = timestamp;
return Some(ctx);
} else {
return None;
}
}
}
}
Some(ctx)
}
}
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
impl DownsamplingInterceptor {
pub fn new(rules: Vec<DownsamplingRuleConf>) -> Self {
let mut ke_state = KeBoxTree::default();
for rule in rules {
let mut threshold = std::time::Duration::MAX;
let mut latest_message_timestamp = std::time::Instant::now();
if rule.rate != 0.0 {
threshold =
std::time::Duration::from_nanos((1. / rule.rate * NANOS_PER_SEC) as u64);
latest_message_timestamp -= threshold;
}
ke_state.insert(
&rule.key_expr,
Timestate {
threshold,
latest_message_timestamp,
},
);
}
Self {
ke_state: Arc::new(Mutex::new(ke_state)),
}
}
}