-
Notifications
You must be signed in to change notification settings - Fork 296
/
valid_set_rule.rs
348 lines (320 loc) · 13.4 KB
/
valid_set_rule.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Including this clippy allow to circumvent clippy errors spawned by MockAll
// internal expansion. Should be removed when DFN-860 is resolved.
// Specifically relevant to the Vec<> parameter.
#![allow(clippy::ptr_arg)]
use ic_base_types::NumBytes;
use ic_constants::{INGRESS_HISTORY_MAX_MESSAGES, SMALL_APP_SUBNET_MAX_SIZE};
use ic_cycles_account_manager::{CyclesAccountManager, IngressInductionCost};
use ic_error_types::{ErrorCode, UserError};
use ic_ic00_types::CanisterStatusType;
use ic_interfaces::execution_environment::IngressHistoryWriter;
use ic_logger::{debug, error, trace, ReplicaLogger};
use ic_metrics::{buckets::decimal_buckets, buckets::linear_buckets, MetricsRegistry};
use ic_registry_subnet_type::SubnetType;
use ic_replicated_state::{
replicated_state::{
LABEL_VALUE_CANISTER_NOT_FOUND, LABEL_VALUE_CANISTER_OUT_OF_CYCLES,
LABEL_VALUE_CANISTER_STOPPED, LABEL_VALUE_CANISTER_STOPPING,
LABEL_VALUE_INGRESS_HISTORY_FULL, LABEL_VALUE_INVALID_SUBNET_PAYLOAD,
LABEL_VALUE_UNKNOWN_SUBNET_METHOD,
},
ReplicatedState, StateError,
};
use ic_types::{
ingress::{IngressState, IngressStatus},
messages::{
extract_effective_canister_id, HttpRequestContent, Ingress, ParseIngressError,
SignedIngressContent,
},
time::expiry_time_from_now,
SubnetId, Time,
};
use prometheus::{Histogram, HistogramVec, IntCounterVec, IntGauge};
use std::sync::Arc;
struct VsrMetrics {
/// Counts of ingress message induction attempts, by status.
inducted_ingress_messages: IntCounterVec,
/// Successfully inducted ingress message payload sizes.
inducted_ingress_payload_sizes: Histogram,
/// Latency of inducting an ingress message, by induction status.
/// The latency metric is unreliable because we assume expiry time
/// was set by 'expiry_time'.
unreliable_induct_ingress_message_duration: HistogramVec,
/// Memory currently used by payloads of statuses in the ingress
/// history.
ingress_history_size: IntGauge,
}
const METRIC_INDUCTED_INGRESS_MESSAGES: &str = "mr_inducted_ingress_message_count";
const METRIC_INDUCTED_INGRESS_PAYLOAD_SIZES: &str = "mr_inducted_ingress_payload_size_bytes";
const METRIC_UNRELIABLE_INDUCT_INGRESS_MESSAGE_DURATION: &str =
"mr_unreliable_induct_ingress_message_duration_seconds";
const METRIC_INGRESS_HISTORY_SIZE: &str = "mr_ingress_history_size_bytes";
const LABEL_STATUS: &str = "status";
const LABEL_VALUE_SUCCESS: &str = "success";
const LABEL_VALUE_DUPLICATE: &str = "duplicate";
impl VsrMetrics {
fn new(metrics_registry: &MetricsRegistry) -> Self {
let inducted_ingress_messages = metrics_registry.int_counter_vec(
METRIC_INDUCTED_INGRESS_MESSAGES,
"Counts of ingress message induction attempts, by status.",
&[LABEL_STATUS],
);
let inducted_ingress_payload_sizes = metrics_registry.histogram(
METRIC_INDUCTED_INGRESS_PAYLOAD_SIZES,
"Successfully inducted ingress message payload sizes.",
// 10 B - 5 MB
decimal_buckets(1, 6),
);
let unreliable_induct_ingress_message_duration = metrics_registry.histogram_vec(
METRIC_UNRELIABLE_INDUCT_INGRESS_MESSAGE_DURATION,
"Latency of inducting an ingress message, by induction status.",
linear_buckets(0.0, 0.5, 20),
&[LABEL_STATUS],
);
let ingress_history_size = metrics_registry.int_gauge(
METRIC_INGRESS_HISTORY_SIZE,
"Memory currently used by payloads of statuses in the ingress history",
);
// Initialize all `inducted_ingress_messages` counters with zero, so they are
// all exported from process start (`IntCounterVec` is really a map).
for status in &[
LABEL_VALUE_SUCCESS,
LABEL_VALUE_DUPLICATE,
LABEL_VALUE_CANISTER_NOT_FOUND,
LABEL_VALUE_INGRESS_HISTORY_FULL,
LABEL_VALUE_CANISTER_STOPPED,
LABEL_VALUE_CANISTER_STOPPING,
LABEL_VALUE_CANISTER_OUT_OF_CYCLES,
LABEL_VALUE_UNKNOWN_SUBNET_METHOD,
LABEL_VALUE_INVALID_SUBNET_PAYLOAD,
] {
inducted_ingress_messages.with_label_values(&[status]);
}
Self {
inducted_ingress_messages,
inducted_ingress_payload_sizes,
unreliable_induct_ingress_message_duration,
ingress_history_size,
}
}
}
pub(crate) trait ValidSetRule: Send {
/// Inducts the provided messages into the ReplicatedState.
fn induct_messages(&self, state: &mut ReplicatedState, msgs: Vec<SignedIngressContent>);
}
pub(crate) struct ValidSetRuleImpl {
ingress_history_writer: Arc<dyn IngressHistoryWriter<State = ReplicatedState>>,
ingress_history_max_messages: usize,
cycles_account_manager: Arc<CyclesAccountManager>,
own_subnet_id: SubnetId,
metrics: VsrMetrics,
log: ReplicaLogger,
}
impl ValidSetRuleImpl {
pub(crate) fn new(
ingress_history_writer: Arc<dyn IngressHistoryWriter<State = ReplicatedState>>,
cycles_account_manager: Arc<CyclesAccountManager>,
metrics_registry: &MetricsRegistry,
own_subnet_id: SubnetId,
log: ReplicaLogger,
) -> Self {
Self {
ingress_history_writer,
ingress_history_max_messages: INGRESS_HISTORY_MAX_MESSAGES,
metrics: VsrMetrics::new(metrics_registry),
own_subnet_id,
cycles_account_manager,
log,
}
}
/// Tries to induct a single ingress message and sets the message status in
/// `state` accordingly (to `Received` if successful; or to `Failed` with
/// the relevant error code on failure).
fn induct_message(
&self,
state: &mut ReplicatedState,
msg: SignedIngressContent,
subnet_size: usize,
) {
trace!(self.log, "induct_message");
let message_id = msg.id();
let source = msg.sender();
let receiver = msg.canister_id();
let payload_bytes = msg.arg().len();
let time = state.time();
let ingress_expiry = msg.ingress_expiry();
let status = match self.enqueue(state, msg, subnet_size) {
Ok(()) => {
self.observe_inducted_ingress_payload_size(payload_bytes);
self.ingress_history_writer.set_status(
state,
message_id,
IngressStatus::Known {
receiver: receiver.get(),
user_id: source,
time,
state: IngressState::Received,
},
);
LABEL_VALUE_SUCCESS
}
Err(err) => {
if let StateError::CanisterNotFound(canister_id) = &err {
error!(
self.log,
"Failed to induct message: canister does not exist";
messaging.message_id => format!("{}", message_id),
messaging.canister_id => format!("{}", canister_id),
);
}
let error_code = ErrorCode::from(&err);
self.ingress_history_writer.set_status(
state,
message_id,
IngressStatus::Known {
receiver: receiver.get(),
user_id: source,
time,
state: IngressState::Failed(UserError::new(error_code, err.to_string())),
},
);
err.to_label_value()
}
};
self.observe_inducted_ingress_status(status);
self.observe_unreliable_induct_ingress_message_duration(status, ingress_expiry);
}
/// Checks whether the given message has already been inducted.
fn is_duplicate(&self, state: &ReplicatedState, msg: &SignedIngressContent) -> bool {
state.get_ingress_status(&msg.id()) != IngressStatus::Unknown
}
/// Records the result of inducting an ingress message.
fn observe_inducted_ingress_status(&self, status: &str) {
self.metrics
.inducted_ingress_messages
.with_label_values(&[status])
.inc();
}
/// Records the size of a successfully inducted ingress message payload.
fn observe_inducted_ingress_payload_size(&self, bytes: usize) {
self.metrics
.inducted_ingress_payload_sizes
.observe(bytes as f64);
}
/// Records the (unreliably) estimated duration to induct one ingress
/// message.
fn observe_unreliable_induct_ingress_message_duration(
&self,
status: &str,
ingress_expiry: Time,
) {
let delta_in_nanos = expiry_time_from_now().saturating_sub(ingress_expiry);
self.metrics
.unreliable_induct_ingress_message_duration
.with_label_values(&[status])
.observe(delta_in_nanos.as_secs_f64());
}
/// Records the memory currently used for the ingress history.
fn observe_ingress_history_size(&self, bytes: NumBytes) {
self.metrics.ingress_history_size.set(bytes.get() as i64);
}
// Enqueues an ingress message into input queues.
fn enqueue(
&self,
state: &mut ReplicatedState,
msg: SignedIngressContent,
subnet_size: usize,
) -> Result<(), StateError> {
if state.metadata.own_subnet_type != SubnetType::System
&& state.metadata.ingress_history.len() >= self.ingress_history_max_messages
{
return Err(StateError::IngressHistoryFull {
capacity: self.ingress_history_max_messages,
});
}
let effective_canister_id =
match extract_effective_canister_id(&msg, state.metadata.own_subnet_id) {
Ok(effective_canister_id) => effective_canister_id,
Err(
ParseIngressError::UnknownSubnetMethod
| ParseIngressError::SubnetMethodNotAllowed,
) => {
return Err(StateError::UnknownSubnetMethod(
msg.method_name().to_string(),
))
}
Err(ParseIngressError::InvalidSubnetPayload(_)) => {
return Err(StateError::InvalidSubnetPayload)
}
};
// Compute the cost of induction.
let induction_cost = self.cycles_account_manager.ingress_induction_cost(
&msg,
effective_canister_id,
subnet_size,
);
let ingress = Ingress::from((msg, effective_canister_id));
match induction_cost {
IngressInductionCost::Free => {
// Only subnet methods can be free. These are enqueued directly.
assert!(ingress.is_addressed_to_subnet(self.own_subnet_id));
state.push_ingress(ingress)
}
IngressInductionCost::Fee { payer, cost } => {
// Get the paying canister from the state.
let canister = match state.canister_states.get_mut(&payer) {
Some(canister) => canister,
None => return Err(StateError::CanisterNotFound(payer)),
};
// Withdraw cost of inducting the message.
let memory_usage = canister.memory_usage();
let message_memory_usage = canister.message_memory_usage();
let compute_allocation = canister.scheduler_state.compute_allocation;
if let Err(err) = self.cycles_account_manager.charge_ingress_induction_cost(
canister,
memory_usage,
message_memory_usage,
compute_allocation,
cost,
subnet_size,
) {
return Err(StateError::CanisterOutOfCycles(err));
}
// Ensure the canister is running if the message isn't to a subnet.
if !ingress.is_addressed_to_subnet(self.own_subnet_id) {
match canister.status() {
CanisterStatusType::Running => {}
CanisterStatusType::Stopping => {
return Err(StateError::CanisterStopping(canister.canister_id()))
}
CanisterStatusType::Stopped => {
return Err(StateError::CanisterStopped(canister.canister_id()))
}
}
}
state.push_ingress(ingress)
}
}
}
}
impl ValidSetRule for ValidSetRuleImpl {
fn induct_messages(&self, state: &mut ReplicatedState, msgs: Vec<SignedIngressContent>) {
let subnet_size = state
.metadata
.network_topology
.get_subnet_size(&state.metadata.own_subnet_id)
.unwrap_or(SMALL_APP_SUBNET_MAX_SIZE);
for msg in msgs {
let message_id = msg.id();
if !self.is_duplicate(state, &msg) {
self.induct_message(state, msg, subnet_size);
} else {
self.observe_inducted_ingress_status(LABEL_VALUE_DUPLICATE);
debug!(self.log, "Didn't induct duplicate message {}", message_id);
}
}
self.observe_ingress_history_size(state.total_ingress_memory_taken());
}
}
#[cfg(test)]
mod test;