Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
the MicroVM. The image enables the resume hook. So the worker accepts the
next job as soon as the MicroVM resumes.

A callback named gate holds the second job. The caller completes the callback
with SendDurableExecutionCallbackSuccess to start the second job.
The idle policy sets autoResumeEnabled, so the service resumes the MicroVM
on the next request. A callback named gate holds the second job. The caller
completes the callback with SendDurableExecutionCallbackSuccess to start the
second job.
"""

import os
Expand All @@ -31,6 +33,17 @@
# So a failed caller leaves the MicroVM running for 5 minutes, not for the
# whole execution timeout.
GATE = CallbackConfig(timeout=Duration.from_minutes(5))
# The SDK does not resume a MicroVM. The service resumes a suspended MicroVM on
# the next request, because the idle policy sets autoResumeEnabled.
# maxIdleDurationSeconds is above maximumDurationInSeconds, the 10-minute
# session timeout plus 5 minutes. The service's idle timer counts only inbound
# requests, so a shorter value could suspend a running job.
# suspendedDurationSeconds is the same 15 minutes, longer than the session.
IDLE_POLICY = {
"autoResumeEnabled": True,
"maxIdleDurationSeconds": 3600,
"suspendedDurationSeconds": 900,
}


def _require_microvm_env() -> tuple[str, str]:
Expand Down Expand Up @@ -85,10 +98,9 @@ def write_gate_read(vm: MicrovmSession, session_context: DurableContext) -> Any:
# The callback result carries no data. So the handler ignores it.
session_context.create_callback(name="gate", config=GATE).result()

# The request step calls GetMicrovm, finds the MicroVM SUSPENDED, and
# calls ResumeMicrovm before it sends the job. A suspended MicroVM
# keeps its files. So this job reads the file that the first job
# wrote.
# This job's first request finds the MicroVM SUSPENDED, and the
# service resumes it on a request. A suspended MicroVM keeps its
# files. So this job reads the file that the first job wrote.
read = vm.invoke(
session_context,
"read",
Expand All @@ -112,6 +124,7 @@ def write_gate_read(vm: MicrovmSession, session_context: DurableContext) -> Any:
image_identifier=image_arn,
execution_role_arn=execution_role_arn,
timeout=Duration.from_minutes(10),
idle_policy=IDLE_POLICY,
auto_suspend_on_idle=True,
auto_suspend_idle_time=IDLE_TIME,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

The session does not suspend when idle. The first job sets suspend_after, so
the durable function calls SuspendMicrovm after that job's result arrives.
The function's role needs lambda:SuspendMicrovm on the image. A callback named gate holds
the second job. The caller completes the callback with
SendDurableExecutionCallbackSuccess to start the second job.
The function's role needs lambda:SuspendMicrovm on the image. The idle
policy sets autoResumeEnabled, so the service resumes the MicroVM on the
next request. A callback named gate holds the second job. The caller
completes the callback with SendDurableExecutionCallbackSuccess to start the
second job.
"""

import os
Expand All @@ -33,9 +35,21 @@
# whole execution timeout.
GATE = CallbackConfig(timeout=Duration.from_minutes(5))

# The SDK does not resume a MicroVM. The service resumes a suspended MicroVM on
# the next request, because the idle policy sets autoResumeEnabled.
# maxIdleDurationSeconds is above maximumDurationInSeconds, the 10-minute
# session timeout plus 5 minutes. The service's idle timer counts only inbound
# requests, so a shorter value could suspend a running job.
# suspendedDurationSeconds is the same 15 minutes, longer than the session.
IDLE_POLICY = {
"autoResumeEnabled": True,
"maxIdleDurationSeconds": 3600,
"suspendedDurationSeconds": 900,
}

# The second job waits for the resume. The worker can refuse it with HTTP 503
# for up to 30 seconds before that, and the request step retries for up to
# 60 seconds. So its heartbeat timeout is longer than both together.
# for up to 30 seconds after the suspend, and the request step retries for up
# to 60 seconds. So its heartbeat timeout is longer than both together.
RESUMED_JOB_HEARTBEAT_TIMEOUT = Duration.from_seconds(120)


Expand Down Expand Up @@ -89,10 +103,10 @@ def write_gate_read(vm: MicrovmSession, session_context: DurableContext) -> Any:
# The callback result carries no data. So the handler ignores it.
session_context.create_callback(name="gate", config=GATE).result()

# The first job set suspend_after. So the request step calls
# GetMicrovm, finds the MicroVM SUSPENDED, and calls ResumeMicrovm
# before it sends the job. A suspended MicroVM keeps its files. So
# this job reads the file that the first job wrote.
# The first job set suspend_after. So this job's first request finds
# the MicroVM SUSPENDED, and the service resumes it on a request.
# A suspended MicroVM keeps its files. So this job reads the file
# that the first job wrote.
read = vm.invoke(
session_context,
"read",
Expand Down Expand Up @@ -120,6 +134,7 @@ def write_gate_read(vm: MicrovmSession, session_context: DurableContext) -> Any:
image_identifier=image_arn,
execution_role_arn=execution_role_arn,
timeout=Duration.from_minutes(10),
idle_policy=IDLE_POLICY,
),
handler=write_gate_read,
)
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def test_my_example(durable_runner):
- Deploy the MicroVM examples with the SAM template, with `MicrovmImageArn` and `MicrovmExecutionRoleArn` set. `cli.py deploy` sets neither `MICROVM_IMAGE_ARN` nor `MICROVM_EXECUTION_ROLE_ARN`. So the example raises `RuntimeError` when it runs. When `build/` is missing, `cli.py` also builds it without the extras. So the example then fails at import.
- Pass a cloud-test input as an object, such as a `dict`. The cloud runner encodes the input with `json.dumps`. So a string that already holds JSON is encoded again, and the handler receives a `str` instead of a `dict`.
- Start a MicroVM test with `run_async`, and read its result with `wait_for_result`. The synchronous `run` uses botocore's 60-second read timeout, and botocore sends the Invoke again after a read timeout. Each resend starts a new execution, which launches its own MicroVM.
- `test_microvm_session_suspend.py` checks that an idle session MicroVM suspends and resumes. The function's role needs `lambda:GetMicrovm` and `lambda:ResumeMicrovm`, and the MicroVM execution role needs `lambda:SuspendMicrovm` on the image.
- `test_microvm_session_suspend_after.py` checks that the durable function suspends a session MicroVM after a job with `suspend_after=True`, in a session without auto-suspend, and that the next job resumes it. The function's role needs `lambda:GetMicrovm`, `lambda:ResumeMicrovm`, and `lambda:SuspendMicrovm` on the image. The MicroVM execution role needs no suspend permission.
- `test_microvm_session_suspend.py` checks that an idle session MicroVM suspends, and that the service resumes it on the next job's request. The example sets an idle policy with `autoResumeEnabled`. The function's role needs `lambda:GetMicrovm`, and the MicroVM execution role needs `lambda:SuspendMicrovm` on the image.
- `test_microvm_session_suspend_after.py` checks that the durable function suspends a session MicroVM after a job with `suspend_after=True`, in a session without auto-suspend, and that the service resumes it on the next job's request. The example sets an idle policy with `autoResumeEnabled`. The function's role needs `lambda:GetMicrovm` and `lambda:SuspendMicrovm` on the image. The MicroVM execution role needs no suspend permission.

### Cloud-Only Tests
`@pytest.mark.durable_execution(..., cloud_only=True)` marks a test that needs a service the local runner cannot provide, such as a MicroVM. Local mode skips the test with the reason `cloud_only: runs only against the deployed function`. Cloud mode runs it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def test_suspends_the_idle_microvm_and_resumes_it_for_the_next_job(durable_runne
expect_microvm_terminated(microvm_id)

# The tree is the tree of the session example without suspending, with
# the callback named gate in place of that example's wait. The state
# check and the resume run inside each request step. So the tree shows
# them only as a request step that succeeded after the suspend.
# the callback named gate in place of that example's wait. The service
# resumes the MicroVM during the second request step. So the tree shows
# the resume only as a request step that succeeded after the suspend.
assert tree(result.operations) == [
(
"suspend-session",
Expand Down Expand Up @@ -156,14 +156,15 @@ def test_suspends_the_idle_microvm_and_resumes_it_for_the_next_job(durable_runne
)
]

# With auto_suspend_on_idle, the launch records the resume mode. Each
# request step then calls GetMicrovm before it sends the job, and calls
# ResumeMicrovm for a suspended MicroVM. No idle policy is set. So only
# the request step can have called ResumeMicrovm.
# The SDK does not resume a MicroVM, so the launch records no resume
# mode. The idle policy sets autoResumeEnabled, so the service resumed
# the MicroVM on the second job's request.
session = result.get_context("suspend-session")
launch = deserialize_operation_payload(
session.get_step("suspend-session.launch").result
)
assert launch["delivery"] == "http"
assert launch["microvmId"] == microvm_id
assert launch["resumeBeforeDelivery"] is True
assert launch == {
"microvmId": microvm_id,
"endpoint": launch["endpoint"],
"delivery": "http",
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ def test_suspends_the_microvm_after_the_first_job_and_resumes_it_for_the_next(

# 1. The gate starts after the first job ends.
# 2. The second job starts only after the test completes the gate.
# 3. The session does not suspend when idle. So only the first
# job's suspend_after can suspend the MicroVM.
# 3. The session does not suspend when idle, and the idle
# policy's maxIdleDurationSeconds is longer than the session.
# So only the first job's suspend_after can suspend the
# MicroVM.
# 4. So a SUSPENDED state read now lies between the two jobs, and
# comes from suspend_after.
# The poll lasts at most 150 seconds. The gate times out after 5
Expand Down Expand Up @@ -120,9 +122,9 @@ def test_suspends_the_microvm_after_the_first_job_and_resumes_it_for_the_next(

expect_microvm_terminated(microvm_id)

# The tree is the tree of the suspend example. The state check and the
# resume run inside the second request step. So the tree shows them only
# as a request step that succeeded after the suspend.
# The tree is the tree of the suspend example. The service resumes the
# MicroVM during the second request step. So the tree shows the resume
# only as a request step that succeeded after the suspend.
assert tree(result.operations) == [
(
"suspend-after-session",
Expand Down Expand Up @@ -160,13 +162,15 @@ def test_suspends_the_microvm_after_the_first_job_and_resumes_it_for_the_next(
)
]

# The session sets neither auto_suspend_on_idle nor an idle policy. So the
# launch records no resume mode. suspend_after is an option of one job,
# and the launch checkpoint does not record it.
# The SDK does not resume a MicroVM, so the launch records no resume
# mode. suspend_after is an option of one job, and the launch checkpoint
# does not record it.
session = result.get_context("suspend-after-session")
launch = deserialize_operation_payload(
session.get_step("suspend-after-session.launch").result
)
assert launch["delivery"] == "http"
assert launch["microvmId"] == microvm_id
assert launch["resumeBeforeDelivery"] is False
assert launch == {
"microvmId": microvm_id,
"endpoint": launch["endpoint"],
"delivery": "http",
}
Loading
Loading