Skip to content

Commit 605ea1d

Browse files
Preslav LeConvex, Inc.
authored andcommitted
Rename RequestContext to ExecutionContext (#23870)
Rename RequestContext to ExecutionContext. As it became crystal clear recently, the struct makes sense in the context of a function execution and not as request in general. It should be generated as close to execution as possible. I have renamed the struct, file and proto. I kept RequestId in the same file but reexported it as common::RequestId, so it is easier to move to a different file later. GitOrigin-RevId: 67856b99bedaa28b82e8567ad3f2788c749cf915
1 parent 52c5143 commit 605ea1d

File tree

37 files changed

+161
-173
lines changed

37 files changed

+161
-173
lines changed

crates/application/src/application_function_runner/mod.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use authentication::token_to_authorization_header;
1515
use common::{
1616
backoff::Backoff,
1717
errors::JsError,
18+
execution_context::ExecutionContext,
1819
identity::InertIdentity,
1920
knobs::{
2021
APPLICATION_FUNCTION_RUNNER_SEMAPHORE_TIMEOUT,
@@ -33,10 +34,6 @@ use common::{
3334
},
3435
pause::PauseClient,
3536
query_journal::QueryJournal,
36-
request_context::{
37-
RequestContext,
38-
RequestId,
39-
},
4037
runtime::{
4138
Runtime,
4239
RuntimeInstant,
@@ -56,6 +53,7 @@ use common::{
5653
UdfType,
5754
},
5855
value::ConvexArray,
56+
RequestId,
5957
};
6058
use database::{
6159
unauthorized_error,
@@ -258,7 +256,7 @@ impl<RT: Runtime> FunctionRouter<RT> {
258256
path_and_args: ValidatedUdfPathAndArgs,
259257
udf_type: UdfType,
260258
journal: QueryJournal,
261-
context: RequestContext,
259+
context: ExecutionContext,
262260
) -> anyhow::Result<(Transaction<RT>, FunctionOutcome)> {
263261
anyhow::ensure!(udf_type == UdfType::Query || udf_type == UdfType::Mutation);
264262
// All queries and mutations are run in the isolate environment.
@@ -276,7 +274,7 @@ impl<RT: Runtime> FunctionRouter<RT> {
276274
tx: Transaction<RT>,
277275
path_and_args: ValidatedUdfPathAndArgs,
278276
log_line_sender: mpsc::UnboundedSender<LogLine>,
279-
context: RequestContext,
277+
context: ExecutionContext,
280278
) -> anyhow::Result<ActionOutcome> {
281279
let (_, outcome) = self
282280
.function_runner_execute(
@@ -306,7 +304,7 @@ impl<RT: Runtime> FunctionRouter<RT> {
306304
path_and_args: ValidatedUdfPathAndArgs,
307305
udf_type: UdfType,
308306
journal: QueryJournal,
309-
context: RequestContext,
307+
context: ExecutionContext,
310308
log_line_sender: Option<mpsc::UnboundedSender<LogLine>>,
311309
) -> anyhow::Result<(Option<Transaction<RT>>, FunctionOutcome)> {
312310
let in_memory_index_last_modified = self
@@ -601,7 +599,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
601599
self.module_cache.clone(),
602600
)
603601
.await?;
604-
let context = RequestContext::new(request_id, &caller);
602+
let context = ExecutionContext::new(request_id, &caller);
605603
let (mut tx, outcome) = match validate_result {
606604
Ok(path_and_args) => {
607605
self.isolate_functions
@@ -716,7 +714,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
716714
loop {
717715
// Note that we use different context for every mutation attempt.
718716
// This so every JS function run gets a different executionId.
719-
let context = RequestContext::new(request_id.clone(), &caller);
717+
let context = ExecutionContext::new(request_id.clone(), &caller);
720718

721719
let start = self.runtime.monotonic_now();
722720
let mut tx = self
@@ -875,7 +873,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
875873
udf_path: CanonicalizedUdfPath,
876874
arguments: ConvexArray,
877875
allowed_visibility: AllowedVisibility,
878-
context: RequestContext,
876+
context: ExecutionContext,
879877
) -> anyhow::Result<(Transaction<RT>, UdfOutcome)> {
880878
let result = self
881879
.run_mutation_inner(tx, udf_path, arguments, allowed_visibility, context)
@@ -906,7 +904,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
906904
udf_path: CanonicalizedUdfPath,
907905
arguments: ConvexArray,
908906
allowed_visibility: AllowedVisibility,
909-
context: RequestContext,
907+
context: ExecutionContext,
910908
) -> anyhow::Result<(Transaction<RT>, UdfOutcome)> {
911909
if udf_path.is_system() && !(tx.identity().is_admin() || tx.identity().is_system()) {
912910
anyhow::bail!(unauthorized_error("mutation"));
@@ -974,7 +972,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
974972
}))
975973
},
976974
};
977-
let context = RequestContext::new(request_id.clone(), &caller);
975+
let context = ExecutionContext::new(request_id.clone(), &caller);
978976
let name = name.canonicalize();
979977
let usage_tracking = FunctionUsageTracker::new();
980978
let completion = self
@@ -1019,7 +1017,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
10191017
allowed_visibility: AllowedVisibility,
10201018
caller: FunctionCaller,
10211019
usage_tracking: FunctionUsageTracker,
1022-
context: RequestContext,
1020+
context: ExecutionContext,
10231021
) -> anyhow::Result<ActionCompletion> {
10241022
let result = self
10251023
.run_action_inner(
@@ -1060,7 +1058,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
10601058
allowed_visibility: AllowedVisibility,
10611059
caller: FunctionCaller,
10621060
usage_tracking: FunctionUsageTracker,
1063-
context: RequestContext,
1061+
context: ExecutionContext,
10641062
) -> anyhow::Result<ActionCompletion> {
10651063
if name.is_system() && !(identity.is_admin() || identity.is_system()) {
10661064
anyhow::bail!(unauthorized_error("action"));
@@ -1604,7 +1602,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
16041602
start: RT::Instant,
16051603
caller: FunctionCaller,
16061604
e: &anyhow::Error,
1607-
context: RequestContext,
1605+
context: ExecutionContext,
16081606
) -> anyhow::Result<()> {
16091607
// TODO: We currently synthesize a `UdfOutcome` for
16101608
// an internal system error. If we decide we want to keep internal system errors
@@ -1664,7 +1662,7 @@ impl<RT: Runtime> ActionCallbacks for ApplicationFunctionRunner<RT> {
16641662
name: UdfPath,
16651663
args: Vec<JsonValue>,
16661664
block_logging: bool,
1667-
context: RequestContext,
1665+
context: ExecutionContext,
16681666
) -> anyhow::Result<FunctionResult> {
16691667
let ts = self.database.now_ts_for_reads();
16701668
let result = self
@@ -1692,7 +1690,7 @@ impl<RT: Runtime> ActionCallbacks for ApplicationFunctionRunner<RT> {
16921690
name: UdfPath,
16931691
args: Vec<JsonValue>,
16941692
block_logging: bool,
1695-
context: RequestContext,
1693+
context: ExecutionContext,
16961694
) -> anyhow::Result<FunctionResult> {
16971695
let result = self
16981696
.retry_mutation(
@@ -1722,7 +1720,7 @@ impl<RT: Runtime> ActionCallbacks for ApplicationFunctionRunner<RT> {
17221720
name: UdfPath,
17231721
args: Vec<JsonValue>,
17241722
block_logging: bool,
1725-
context: RequestContext,
1723+
context: ExecutionContext,
17261724
) -> anyhow::Result<FunctionResult> {
17271725
let _tx = self.database.begin(identity.clone()).await?;
17281726
let result = self
@@ -1797,7 +1795,7 @@ impl<RT: Runtime> ActionCallbacks for ApplicationFunctionRunner<RT> {
17971795
udf_path: UdfPath,
17981796
udf_args: Vec<JsonValue>,
17991797
scheduled_ts: UnixTimestamp,
1800-
context: RequestContext,
1798+
context: ExecutionContext,
18011799
) -> anyhow::Result<DocumentIdV6> {
18021800
let mut tx = self.database.begin(identity).await?;
18031801
let (udf_path, udf_args) = validate_schedule_args(

crates/application/src/cache/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ use async_broadcast::{
1616
};
1717
use common::{
1818
self,
19+
execution_context::ExecutionContext,
1920
identity::IdentityCacheKey,
2021
knobs::{
2122
DATABASE_UDF_SYSTEM_TIMEOUT,
2223
DATABASE_UDF_USER_TIMEOUT,
2324
UDF_CACHE_MAX_SIZE,
2425
},
2526
query_journal::QueryJournal,
26-
request_context::{
27-
RequestContext,
28-
RequestId,
29-
},
3027
runtime::{
3128
Runtime,
3229
RuntimeInstant,
@@ -40,6 +37,7 @@ use common::{
4037
UdfType,
4138
},
4239
value::ConvexArray,
40+
RequestId,
4341
};
4442
use database::{
4543
Database,
@@ -277,7 +275,7 @@ impl<RT: Runtime> CacheManager<RT> {
277275
journal: journal.unwrap_or_else(QueryJournal::new),
278276
allowed_visibility,
279277
};
280-
let context = RequestContext::new(request_id, &caller);
278+
let context = ExecutionContext::new(request_id, &caller);
281279

282280
let mut num_attempts = 0;
283281
'top: loop {
@@ -403,7 +401,7 @@ impl<RT: Runtime> CacheManager<RT> {
403401
now: RT::Instant,
404402
identity: &Identity,
405403
ts: Timestamp,
406-
context: RequestContext,
404+
context: ExecutionContext,
407405
) -> Option<CacheOp> {
408406
let go = |sender: Option<(Sender<_>, u64)>| {
409407
let (sender, waiting_entry_id) = match sender {
@@ -799,6 +797,6 @@ enum CacheOp {
799797
ts: Timestamp,
800798
journal: QueryJournal,
801799
allowed_visibility: AllowedVisibility,
802-
context: RequestContext,
800+
context: ExecutionContext,
803801
},
804802
}

crates/application/src/cron_jobs/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use common::{
1212
report_error,
1313
JsError,
1414
},
15+
execution_context::ExecutionContext,
1516
identity::InertIdentity,
1617
knobs::{
1718
SCHEDULED_JOB_EXECUTION_PARALLELISM,
@@ -23,10 +24,6 @@ use common::{
2324
Order,
2425
Query,
2526
},
26-
request_context::{
27-
RequestContext,
28-
RequestId,
29-
},
3027
runtime::{
3128
Runtime,
3229
RuntimeInstant,
@@ -37,6 +34,7 @@ use common::{
3734
ModuleEnvironment,
3835
UdfType,
3936
},
37+
RequestId,
4038
};
4139
use database::{
4240
Database,
@@ -346,7 +344,7 @@ impl<RT: Runtime> CronJobExecutor<RT> {
346344
let start = self.rt.monotonic_now();
347345
let identity = tx.inert_identity();
348346
let caller = FunctionCaller::Cron;
349-
let context = RequestContext::new(request_id, &caller);
347+
let context = ExecutionContext::new(request_id, &caller);
350348
let mutation_result = self
351349
.runner
352350
.run_mutation_no_udf_log(
@@ -480,7 +478,7 @@ impl<RT: Runtime> CronJobExecutor<RT> {
480478
.await?;
481479

482480
// Execute the action
483-
let context = RequestContext::new(request_id, &caller);
481+
let context = ExecutionContext::new(request_id, &caller);
484482
let completion = self
485483
.runner
486484
.run_action_no_udf_log(
@@ -547,7 +545,7 @@ impl<RT: Runtime> CronJobExecutor<RT> {
547545
// started with. We generate a new executionId and use it to log the failures. I
548546
// guess the correct behavior here is to store the executionId in the state so
549547
// we can log correctly here.
550-
let context = RequestContext::new(request_id, &caller);
548+
let context = ExecutionContext::new(request_id, &caller);
551549
let mut model = CronModel::new(&mut tx);
552550
model
553551
.insert_cron_job_log(&job, status, log_lines, 0.0)
@@ -626,7 +624,7 @@ impl<RT: Runtime> CronJobExecutor<RT> {
626624
log_lines: CronJobLogLines,
627625
execution_time: f64,
628626
usage_tracker: FunctionUsageTracker,
629-
context: RequestContext,
627+
context: ExecutionContext,
630628
) -> anyhow::Result<()> {
631629
let Some(mut tx) = self
632630
.new_transaction_for_job_state(job_id, expected_state, usage_tracker)
@@ -661,7 +659,7 @@ impl<RT: Runtime> CronJobExecutor<RT> {
661659
job_id: ResolvedDocumentId,
662660
job: &CronJob,
663661
udf_type: UdfType,
664-
context: RequestContext,
662+
context: ExecutionContext,
665663
) -> anyhow::Result<()> {
666664
let now = self.rt.generate_timestamp()?;
667665
let prev_ts = job.next_ts;

crates/application/src/export_worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ use common::{
2727
ResolvedDocument,
2828
},
2929
errors::report_error,
30+
execution_context::ExecutionId,
3031
maybe_val,
3132
query::{
3233
IndexRange,
3334
IndexRangeExpression,
3435
Order,
3536
},
36-
request_context::ExecutionId,
3737
runtime::{
3838
new_rate_limiter,
3939
Runtime,

crates/application/src/function_log.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use common::{
1717
report_error,
1818
JsError,
1919
},
20+
execution_context::ExecutionContext,
2021
identity::InertIdentity,
2122
knobs::MAX_UDF_EXECUTION,
2223
log_lines::LogLines,
@@ -27,7 +28,6 @@ use common::{
2728
LogSender,
2829
LogTopic,
2930
},
30-
request_context::RequestContext,
3131
runtime::{
3232
Runtime,
3333
UnixTimestamp,
@@ -125,7 +125,7 @@ pub struct FunctionExecution {
125125
/// power.
126126
pub identity: InertIdentity,
127127

128-
pub context: RequestContext,
128+
pub context: ExecutionContext,
129129
}
130130

131131
impl HeapSize for FunctionExecution {
@@ -149,7 +149,7 @@ impl FunctionExecution {
149149
caller: FunctionCaller,
150150
udf_server_version: Option<semver::Version>,
151151
identity: InertIdentity,
152-
context: RequestContext,
152+
context: ExecutionContext,
153153
) -> Self {
154154
FunctionExecution {
155155
params: UdfParams::Function {
@@ -334,7 +334,7 @@ pub struct ActionCompletion {
334334
pub execution_time: Duration,
335335
pub environment: ModuleEnvironment,
336336
pub memory_in_mb: u64,
337-
pub context: RequestContext,
337+
pub context: ExecutionContext,
338338
pub unix_timestamp: UnixTimestamp,
339339
pub caller: FunctionCaller,
340340
pub log_lines: LogLines,
@@ -553,7 +553,7 @@ impl<RT: Runtime> FunctionExecutionLog<RT> {
553553
execution_time: Duration,
554554
caller: FunctionCaller,
555555
usage: FunctionUsageTracker,
556-
context: RequestContext,
556+
context: ExecutionContext,
557557
) {
558558
let usage_stats = usage.gather_user_stats();
559559
let aggregated = usage_stats.aggregate();
@@ -607,7 +607,7 @@ impl<RT: Runtime> FunctionExecutionLog<RT> {
607607
caller: FunctionCaller,
608608
exclude_call_from_usage_tracking: bool,
609609
usage: FunctionUsageTracker,
610-
context: RequestContext,
610+
context: ExecutionContext,
611611
) {
612612
let usage_stats = usage.gather_user_stats();
613613
let aggregated = usage_stats.aggregate();
@@ -655,7 +655,7 @@ impl<RT: Runtime> FunctionExecutionLog<RT> {
655655
execution_time: Duration,
656656
caller: FunctionCaller,
657657
usage: FunctionUsageTracker,
658-
context: RequestContext,
658+
context: ExecutionContext,
659659
) {
660660
let usage_stats = usage.gather_user_stats();
661661
let aggregated = usage_stats.aggregate();
@@ -759,7 +759,7 @@ impl<RT: Runtime> FunctionExecutionLog<RT> {
759759
caller: FunctionCaller,
760760
udf_server_version: Option<semver::Version>,
761761
identity: InertIdentity,
762-
context: RequestContext,
762+
context: ExecutionContext,
763763
) {
764764
let execution = FunctionExecution::for_error(
765765
udf_path,
@@ -778,7 +778,7 @@ impl<RT: Runtime> FunctionExecutionLog<RT> {
778778
&self,
779779
identifier: CanonicalizedUdfPath,
780780
unix_timestamp: UnixTimestamp,
781-
context: RequestContext,
781+
context: ExecutionContext,
782782
log_lines: LogLines,
783783
module_environment: ModuleEnvironment,
784784
) {
@@ -800,7 +800,7 @@ impl<RT: Runtime> FunctionExecutionLog<RT> {
800800
&self,
801801
identifier: HttpActionRoute,
802802
unix_timestamp: UnixTimestamp,
803-
context: RequestContext,
803+
context: ExecutionContext,
804804
log_lines: LogLines,
805805
module_environment: ModuleEnvironment,
806806
) {

0 commit comments

Comments
 (0)