-
Notifications
You must be signed in to change notification settings - Fork 296
/
query_handler.rs
444 lines (407 loc) · 16.3 KB
/
query_handler.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! This module implements the `QueryHandler` trait which is used to execute
//! query methods via query calls.
mod query_cache;
mod query_call_graph;
mod query_context;
mod query_scheduler;
#[cfg(test)]
mod tests;
use crate::execution_environment::subnet_memory_capacity;
use crate::{
hypervisor::Hypervisor,
metrics::{MeasurementScope, QueryHandlerMetrics},
};
use candid::Encode;
use ic_btc_interface::NetworkInRequest as BitcoinNetwork;
use ic_config::execution_environment::Config;
use ic_config::flag_status::FlagStatus;
use ic_crypto_tree_hash::{flatmap, Label, LabeledTree, LabeledTree::SubTree};
use ic_cycles_account_manager::CyclesAccountManager;
use ic_error_types::{ErrorCode, RejectCode, UserError};
use ic_interfaces::execution_environment::{
QueryExecutionError, QueryExecutionResponse, QueryExecutionService, QueryHandler,
};
use ic_interfaces_state_manager::{Labeled, StateReader};
use ic_logger::ReplicaLogger;
use ic_metrics::MetricsRegistry;
use ic_query_stats::QueryStatsCollector;
use ic_registry_subnet_type::SubnetType;
use ic_replicated_state::ReplicatedState;
use ic_types::batch::QueryStats;
use ic_types::QueryStatsEpoch;
use ic_types::{
ingress::WasmResult,
messages::{
Blob, Certificate, CertificateDelegation, HttpQueryResponse, HttpQueryResponseReply,
UserQuery,
},
CanisterId, NumInstructions, PrincipalId,
};
use serde::Serialize;
use std::convert::Infallible;
use std::str::FromStr;
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::sync::oneshot;
use tower::{util::BoxCloneService, Service};
pub(crate) use self::query_scheduler::{QueryScheduler, QuerySchedulerFlag};
use ic_ic00_types::{
BitcoinGetBalanceArgs, BitcoinGetUtxosArgs, FetchCanisterLogsRequest,
FetchCanisterLogsResponse, LogVisibility, Payload, QueryMethod,
};
use ic_replicated_state::NetworkTopology;
/// Convert an object into CBOR binary.
fn into_cbor<R: Serialize>(r: &R) -> Vec<u8> {
let mut ser = serde_cbor::Serializer::new(Vec::new());
ser.self_describe().expect("Could not write magic tag.");
r.serialize(&mut ser).expect("Serialization failed.");
ser.into_inner()
}
fn get_latest_certified_state_and_data_certificate(
state_reader: Arc<dyn StateReader<State = ReplicatedState>>,
certificate_delegation: Option<CertificateDelegation>,
canister_id: CanisterId,
) -> Option<(Labeled<Arc<ReplicatedState>>, Vec<u8>)> {
// The path to fetch the data certificate for the canister.
let path = SubTree(flatmap! {
label("canister") => SubTree(
flatmap! {
label(canister_id.get_ref()) => SubTree(
flatmap!(label("certified_data") => LabeledTree::Leaf(()))
)
}),
// We must always add the time path to comply with the IC spec.
label("time") => LabeledTree::Leaf(())
});
state_reader
.read_certified_state(&path)
.map(|(state, tree, cert)| {
(
Labeled::new(cert.height, state),
into_cbor(&Certificate {
tree,
signature: Blob(cert.signed.signature.signature.get().0),
delegation: certificate_delegation,
}),
)
})
}
fn label<T: Into<Label>>(t: T) -> Label {
t.into()
}
pub struct InternalHttpQueryHandler {
log: ReplicaLogger,
hypervisor: Arc<Hypervisor>,
own_subnet_type: SubnetType,
config: Config,
metrics: QueryHandlerMetrics,
max_instructions_per_query: NumInstructions,
cycles_account_manager: Arc<CyclesAccountManager>,
local_query_execution_stats: QueryStatsCollector,
query_cache: query_cache::QueryCache,
}
#[derive(Clone)]
/// Struct that is responsible for handling queries sent by user.
pub(crate) struct HttpQueryHandler {
internal: Arc<dyn QueryHandler<State = ReplicatedState>>,
state_reader: Arc<dyn StateReader<State = ReplicatedState>>,
query_scheduler: QueryScheduler,
}
impl InternalHttpQueryHandler {
pub fn new(
log: ReplicaLogger,
hypervisor: Arc<Hypervisor>,
own_subnet_type: SubnetType,
config: Config,
metrics_registry: &MetricsRegistry,
max_instructions_per_query: NumInstructions,
cycles_account_manager: Arc<CyclesAccountManager>,
local_query_execution_stats: QueryStatsCollector,
) -> Self {
let query_cache_capacity = config.query_cache_capacity;
let query_cache_max_expiry_time = config.query_cache_max_expiry_time;
Self {
log,
hypervisor,
own_subnet_type,
config,
metrics: QueryHandlerMetrics::new(metrics_registry),
max_instructions_per_query,
cycles_account_manager,
local_query_execution_stats,
query_cache: query_cache::QueryCache::new(
metrics_registry,
query_cache_capacity,
query_cache_max_expiry_time,
),
}
}
/// Get query stas for given canister from query stats collector.
///
/// This is used in testing.
pub fn query_stats_for_testing(&self, canister_id: &CanisterId) -> Option<QueryStats> {
self.local_query_execution_stats
.current_query_stats
.lock()
.unwrap()
.get(canister_id)
.cloned()
}
/// Set current epoch in query stats collector
///
/// This is used in testing.
pub fn query_stats_set_epoch_for_testing(&mut self, epoch: QueryStatsEpoch) {
self.local_query_execution_stats.set_epoch(epoch);
}
}
fn route_bitcoin_message(
network: BitcoinNetwork,
network_topology: &NetworkTopology,
) -> Result<CanisterId, UserError> {
let canister_id = match network {
// Route to the bitcoin canister if it exists, otherwise return the error.
BitcoinNetwork::Testnet
| BitcoinNetwork::testnet
| BitcoinNetwork::Regtest
| BitcoinNetwork::regtest => {
network_topology
.bitcoin_testnet_canister_id
.ok_or(UserError::new(
ErrorCode::CanisterNotHostedBySubnet,
"Bitcoin testnet canister is not installed.".to_string(),
))?
}
BitcoinNetwork::Mainnet | BitcoinNetwork::mainnet => network_topology
.bitcoin_mainnet_canister_id
.ok_or(UserError::new(
ErrorCode::CanisterNotHostedBySubnet,
"Bitcoin mainnet canister is not installed.".to_string(),
))?,
};
Ok(canister_id)
}
impl QueryHandler for InternalHttpQueryHandler {
type State = ReplicatedState;
fn query(
&self,
mut query: UserQuery,
state: Labeled<Arc<ReplicatedState>>,
data_certificate: Vec<u8>,
) -> Result<WasmResult, UserError> {
let measurement_scope = MeasurementScope::root(&self.metrics.query);
// Update the query receiver if the query is for the management canister.
if query.receiver == CanisterId::ic_00() {
let network = match QueryMethod::from_str(query.method_name.as_str()) {
Ok(QueryMethod::BitcoinGetUtxosQuery) => {
BitcoinGetUtxosArgs::decode(&query.method_payload)?.network
}
Ok(QueryMethod::BitcoinGetBalanceQuery) => {
BitcoinGetBalanceArgs::decode(&query.method_payload)?.network
}
Ok(QueryMethod::FetchCanisterLogs) => {
return match self.config.fetch_canister_logs {
FlagStatus::Enabled => fetch_canister_logs(
query.source.get(),
state.get_ref(),
FetchCanisterLogsRequest::decode(&query.method_payload)?,
),
FlagStatus::Disabled => Err(UserError::new(
ErrorCode::CanisterContractViolation,
format!(
"{} API is not enabled on this subnet",
QueryMethod::FetchCanisterLogs
),
)),
}
}
Err(_) => {
return Err(UserError::new(
ErrorCode::CanisterMethodNotFound,
format!("Query method {} not found.", query.method_name),
));
}
};
query.receiver =
route_bitcoin_message(network, &state.get_ref().metadata.network_topology)?;
}
// Check the query cache first (if the query caching is enabled).
// If a valid cache entry found, the result will be immediately returned.
// Otherwise, the key and the env will be kept for the `push` below.
let (cache_entry_key, cache_entry_env) = if self.config.query_caching == FlagStatus::Enabled
{
let key = query_cache::EntryKey::from(&query);
let env = query_cache::EntryEnv::try_from((&key, state.get_ref().as_ref()))?;
if let Some(result) = self.query_cache.get_valid_result(&key, &env) {
return result;
}
(Some(key), Some(env))
} else {
(None, None)
};
// Letting the canister grow arbitrarily when executing the
// query is fine as we do not persist state modifications.
let subnet_available_memory = subnet_memory_capacity(&self.config);
let max_canister_memory_size = self.config.max_canister_memory_size;
let mut context = query_context::QueryContext::new(
&self.log,
self.hypervisor.as_ref(),
self.own_subnet_type,
state,
data_certificate,
subnet_available_memory,
max_canister_memory_size,
self.max_instructions_per_query,
self.config.max_query_call_graph_depth,
self.config.max_query_call_graph_instructions,
self.config.max_query_call_walltime,
self.config.instruction_overhead_per_query_call,
self.config.composite_queries,
query.receiver,
&self.metrics.query_critical_error,
if self.config.query_stats_aggregation == FlagStatus::Enabled {
Some(&self.local_query_execution_stats)
} else {
None
},
);
let result = context.run(
query,
&self.metrics,
Arc::clone(&self.cycles_account_manager),
&measurement_scope,
);
context.observe_system_api_calls(&self.metrics.query_system_api_calls);
context.observe_evaluated_canisters(&self.metrics.query_evaluated_canisters);
// Add the query execution result to the query cache (if the query caching is enabled).
if self.config.query_caching == FlagStatus::Enabled {
if let (Some(key), Some(env)) = (cache_entry_key, cache_entry_env) {
let call_counters = context.system_api_call_counters();
let _evaluated_ids = context.evaluated_canister_ids();
self.query_cache.push(key, env, &result, call_counters);
}
}
result
}
}
fn fetch_canister_logs(
sender: PrincipalId,
state: &ReplicatedState,
args: FetchCanisterLogsRequest,
) -> Result<WasmResult, UserError> {
let canister_id = args.get_canister_id();
let canister = state.canister_state(&canister_id).ok_or_else(|| {
UserError::new(
ErrorCode::CanisterNotFound,
format!("Canister {canister_id} not found"),
)
})?;
match canister.log_visibility() {
LogVisibility::Public => Ok(()),
LogVisibility::Controllers if canister.controllers().contains(&sender) => Ok(()),
LogVisibility::Controllers => Err(UserError::new(
ErrorCode::CanisterRejectedMessage,
format!(
"Caller {} is not allowed to query ic00 method {}",
sender,
QueryMethod::FetchCanisterLogs
),
)),
}?;
// TODO(IC-272): temporarily return empty logs until full implementation is ready.
let response = FetchCanisterLogsResponse {
canister_log_records: Vec::new(),
};
Ok(WasmResult::Reply(Encode!(&response).unwrap()))
}
impl HttpQueryHandler {
pub(crate) fn new_service(
internal: Arc<dyn QueryHandler<State = ReplicatedState>>,
query_scheduler: QueryScheduler,
state_reader: Arc<dyn StateReader<State = ReplicatedState>>,
) -> QueryExecutionService {
BoxCloneService::new(Self {
internal,
state_reader,
query_scheduler,
})
}
}
impl QueryHandler for HttpQueryHandler {
type State = ReplicatedState;
fn query(
&self,
query: UserQuery,
state: Labeled<Arc<Self::State>>,
data_certificate: Vec<u8>,
) -> Result<WasmResult, UserError> {
self.internal.query(query, state, data_certificate)
}
}
impl Service<(UserQuery, Option<CertificateDelegation>)> for HttpQueryHandler {
type Response = QueryExecutionResponse;
type Error = Infallible;
#[allow(clippy::type_complexity)]
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(
&mut self,
(query, certificate_delegation): (UserQuery, Option<CertificateDelegation>),
) -> Self::Future {
let internal = Arc::clone(&self.internal);
let state_reader = Arc::clone(&self.state_reader);
let (tx, rx) = oneshot::channel();
let canister_id = query.receiver;
self.query_scheduler.push(canister_id, move || {
let start = std::time::Instant::now();
if !tx.is_closed() {
// We managed to upgrade the weak pointer, so the query was not cancelled.
// Canceling the query after this point will have no effect: the query will
// be executed anyway. That is fine because the execution will take O(ms).
// Retrieving the state must be done here in the query handler, and should be immediately used.
// Otherwise, retrieving the state in the Query service in `http_endpoints` can lead to queries being queued up,
// with a reference to older states which can cause out-of-memory crashes.
let result = match get_latest_certified_state_and_data_certificate(
state_reader,
certificate_delegation,
query.receiver,
) {
Some((state, cert)) => {
let time = state.get_ref().metadata.batch_time;
let result = internal.query(query, state, cert);
let response = match result {
Ok(res) => match res {
WasmResult::Reply(vec) => HttpQueryResponse::Replied {
reply: HttpQueryResponseReply { arg: Blob(vec) },
},
WasmResult::Reject(message) => HttpQueryResponse::Rejected {
error_code: ErrorCode::CanisterRejectedMessage.to_string(),
reject_code: RejectCode::CanisterReject as u64,
reject_message: message,
},
},
Err(user_error) => HttpQueryResponse::Rejected {
error_code: user_error.code().to_string(),
reject_code: user_error.reject_code() as u64,
reject_message: user_error.to_string(),
},
};
Ok((response, time))
}
None => Err(QueryExecutionError::CertifiedStateUnavailable),
};
let _ = tx.send(Ok(result));
}
start.elapsed()
});
Box::pin(async move {
rx.await
.expect("The sender was dropped before sending the message.")
})
}
}