Skip to content

Commit

Permalink
Merge pull request #1550 from Phala-Network/query-timeout
Browse files Browse the repository at this point in the history
Configurable query timeout
  • Loading branch information
kvinwang authored Mar 13, 2024
2 parents 1dd8911 + ca2e29c commit 6f7c432
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
2 changes: 2 additions & 0 deletions crates/phactory/api/proto/pruntime_rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ message PhactoryInfo {
repeated string supported_attestation_methods = 28;
// The number of live sidevm instances.
uint32 live_sidevm_instances = 29;
// The timeout for contract query in seconds.
uint32 query_timeout = 30;
}

// Basic information for the initialized runtime
Expand Down
3 changes: 3 additions & 0 deletions crates/phactory/api/src/ecall_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub struct InitArgs {

/// The max retry times of getting the attestation report.
pub ra_max_retries: u32,

/// The timeout of a single contract query.
pub query_timeout: u64,
}

pub use phala_git_revision::git_revision;
19 changes: 16 additions & 3 deletions crates/phactory/src/contracts/pink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ impl RuntimeHandle<'_> {
}

pub(crate) mod context {
use std::time::{Duration, Instant};
use std::{
sync::atomic::{AtomicU64, Ordering},
time::{Duration, Instant},
};

use anyhow::{anyhow, Result};
use phala_types::{wrap_content_to_sign, AttestationProvider, SignedContentType};
Expand All @@ -152,6 +155,8 @@ pub(crate) mod context {

environmental::environmental!(exec_context: trait GetContext);

static MAX_QUERY_TIME: AtomicU64 = AtomicU64::new(10);

pub trait GetContext {
fn chain_storage(&self) -> &ChainStorage;
fn exec_context(&self) -> ExecContext;
Expand Down Expand Up @@ -314,9 +319,17 @@ pub(crate) mod context {
time_remaining().as_millis() as u64
}

pub fn set_query_time_limit(seconds: u64) {
MAX_QUERY_TIME.store(seconds, Ordering::Relaxed);
}

pub fn query_time_limit() -> u64 {
MAX_QUERY_TIME.load(Ordering::Relaxed)
}

pub fn time_remaining() -> Duration {
const MAX_QUERY_TIME: Duration = Duration::from_secs(10);
MAX_QUERY_TIME.saturating_sub(call_elapsed())
let limit = Duration::from_secs(query_time_limit());
limit.saturating_sub(call_elapsed())
}

pub fn sidevm_query(origin: [u8; 32], vmid: [u8; 32], payload: Vec<u8>) -> Result<Vec<u8>> {
Expand Down
5 changes: 3 additions & 2 deletions crates/phactory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,12 @@ impl<Platform: pal::Platform> Phactory<Platform> {
}

self.can_load_chain_state = !system::gk_master_key_exists(&args.sealing_path);
self.args = Arc::new(args);
self.query_scheduler = create_query_scheduler(self.args.cores);
self.query_scheduler = create_query_scheduler(args.cores);
self.set_args(args);
}

pub fn set_args(&mut self, args: InitArgs) {
contracts::pink::context::set_query_time_limit(args.query_timeout);
self.args = Arc::new(args);
if let Some(system) = &mut self.system {
system.sealing_path = self.args.sealing_path.clone();
Expand Down
1 change: 1 addition & 0 deletions crates/phactory/src/prpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl<Platform: pal::Platform + Serialize + DeserializeOwned> Phactory<Platform>
},
supported_attestation_methods: self.platform.supported_attestation_methods(),
live_sidevm_instances: sidevm::vm_count() as u32,
query_timeout: self.args.query_timeout as _,
}
}

Expand Down
14 changes: 2 additions & 12 deletions standalone/pruntime/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions standalone/pruntime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ struct Args {
/// The max retry times of getting the attestation report.
#[arg(long, default_value = "1")]
ra_max_retries: u32,

/// The timeout of a single contract query in seconds. The valid range is 5 to 600.
/// Out of range value will be clamped to the nearest bound.
#[arg(long, default_value = "10")]
query_timeout: u64,
}

impl Args {
Expand Down Expand Up @@ -137,6 +142,7 @@ impl Args {
no_rcu: self.no_rcu,
ra_timeout: self.ra_timeout,
ra_max_retries: self.ra_max_retries,
query_timeout: self.query_timeout.clamp(5, 600),
}
}
}
Expand Down

0 comments on commit 6f7c432

Please sign in to comment.