Skip to content

Commit 179b6e2

Browse files
gautamg795Convex, Inc.
authored and
Convex, Inc.
committed
correctly account for memory usage in node actions (#36437)
GitOrigin-RevId: 947da5cdf9399a261a5cab5f49a159615ba3051a
1 parent 6134c9c commit 179b6e2

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

crates/node_executor/src/executor.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub trait NodeExecutor: Sync + Send {
138138

139139
pub struct InvokeResponse {
140140
pub response: JsonValue,
141-
pub memory_used_in_mb: u64,
142141
pub aws_request_id: Option<String>,
143142
}
144143

@@ -237,7 +236,6 @@ impl<RT: Runtime> Actions<RT> {
237236
};
238237
let InvokeResponse {
239238
response,
240-
memory_used_in_mb,
241239
aws_request_id,
242240
} = self.executor.invoke(request, log_line_sender).await?;
243241
let execute_result = ExecuteResponse::try_from(response.clone()).map_err(|e| {
@@ -301,7 +299,8 @@ impl<RT: Runtime> Actions<RT> {
301299
Ok(NodeActionOutcome {
302300
result,
303301
syscall_trace,
304-
memory_used_in_mb,
302+
// This shouldn't ever be None, but we'll use the default 512MB as a fallback.
303+
memory_used_in_mb: execute_result.memory_allocated_mb.unwrap_or(512),
305304
})
306305
}
307306

@@ -315,7 +314,6 @@ impl<RT: Runtime> Actions<RT> {
315314
let request = ExecutorRequest::BuildDeps(request);
316315
let InvokeResponse {
317316
response,
318-
memory_used_in_mb: _,
319317
aws_request_id,
320318
} = self.executor.invoke(request, log_line_sender).await?;
321319
let response: BuildDepsResponse =
@@ -394,7 +392,6 @@ impl<RT: Runtime> Actions<RT> {
394392

395393
let InvokeResponse {
396394
response,
397-
memory_used_in_mb: _,
398395
aws_request_id,
399396
} = self.invoke_analyze(request).await?;
400397
let response: AnalyzeResponse = serde_json::from_value(response.clone()).map_err(|e| {
@@ -687,6 +684,7 @@ struct ExecuteResponse {
687684
udf_time: Option<Duration>,
688685
total_executor_time: Option<Duration>,
689686
syscall_trace: SyscallTrace,
687+
memory_allocated_mb: Option<u64>,
690688
}
691689

692690
#[derive(Debug, PartialEq)]
@@ -747,6 +745,7 @@ impl TryFrom<JsonValue> for ExecuteResponse {
747745
udf_time_ms: Option<f64>,
748746
total_executor_time_ms: Option<f64>,
749747
syscall_trace: Option<BTreeMap<String, SyscallStatsJson>>,
748+
memory_allocated_mb: Option<u64>,
750749
},
751750
#[serde(rename_all = "camelCase")]
752751
Error {
@@ -760,6 +759,7 @@ impl TryFrom<JsonValue> for ExecuteResponse {
760759
udf_time_ms: Option<f64>,
761760
total_executor_time_ms: Option<f64>,
762761
syscall_trace: Option<BTreeMap<String, SyscallStatsJson>>,
762+
memory_allocated_mb: Option<u64>,
763763
},
764764
}
765765
let resp_json: ExecuteResponseJson = serde_json::from_value(v)?;
@@ -772,6 +772,7 @@ impl TryFrom<JsonValue> for ExecuteResponse {
772772
udf_time_ms,
773773
total_executor_time_ms,
774774
syscall_trace,
775+
memory_allocated_mb,
775776
} => ExecuteResponse {
776777
result: ExecuteResponseResult::Success { udf_return },
777778
num_invocations: Some(num_invocations),
@@ -785,6 +786,7 @@ impl TryFrom<JsonValue> for ExecuteResponse {
785786
.map(|(k, v)| (k, v.into()))
786787
.collect::<BTreeMap<_, SyscallStats>>()
787788
.into(),
789+
memory_allocated_mb,
788790
},
789791
ExecuteResponseJson::Error {
790792
message,
@@ -797,6 +799,7 @@ impl TryFrom<JsonValue> for ExecuteResponse {
797799
udf_time_ms,
798800
total_executor_time_ms,
799801
syscall_trace,
802+
memory_allocated_mb,
800803
} => ExecuteResponse {
801804
result: ExecuteResponseResult::Error {
802805
message,
@@ -815,6 +818,7 @@ impl TryFrom<JsonValue> for ExecuteResponse {
815818
.map(|(k, v)| (k, v.into()))
816819
.collect::<BTreeMap<_, SyscallStats>>()
817820
.into(),
821+
memory_allocated_mb,
818822
},
819823
};
820824
Ok(result)

crates/node_executor/src/local.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ impl LocalNodeExecutor {
194194
_ = timeout_future.fuse() => {
195195
anyhow::Ok(NodeExecutorStreamPart::InvokeComplete(Err(InvokeResponse {
196196
response: EXECUTE_TIMEOUT_RESPONSE_JSON.clone(),
197-
memory_used_in_mb: 512,
198197
aws_request_id: None,
199198
})))
200199
},
@@ -242,7 +241,6 @@ impl NodeExecutor for LocalNodeExecutor {
242241
if e.is_timeout() {
243242
return Ok(InvokeResponse {
244243
response: EXECUTE_TIMEOUT_RESPONSE_JSON.clone(),
245-
memory_used_in_mb: 512,
246244
aws_request_id: None,
247245
});
248246
} else {
@@ -261,7 +259,6 @@ impl NodeExecutor for LocalNodeExecutor {
261259
match result {
262260
Ok(payload) => Ok(InvokeResponse {
263261
response: payload,
264-
memory_used_in_mb: 512,
265262
aws_request_id: None,
266263
}),
267264
Err(e) => Ok(e),

npm-packages/node-executor/src/executor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ export type ExecuteResponse = ExecuteResponseInner & {
166166
totalExecutorTimeMs: number;
167167

168168
syscallTrace: Record<string, SyscallStats>;
169+
170+
// The amount of memory allocated to the executor environment. This is constant for the lifetime of the environment.
171+
memoryAllocatedMb: number;
169172
};
170173

171174
export async function execute(
@@ -216,13 +219,18 @@ export async function execute(
216219
}
217220

218221
const totalExecutorTimeMs = logDurationMs("totalExecutorTime", start);
222+
const memoryAllocatedMb = parseInt(
223+
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE ?? "512",
224+
10,
225+
);
219226

220227
return {
221228
...innerResult,
222229
numInvocations,
223230
downloadTimeMs,
224231
totalExecutorTimeMs,
225232
syscallTrace: syscalls.syscallTrace,
233+
memoryAllocatedMb,
226234
};
227235
}
228236

0 commit comments

Comments
 (0)