Repository navigation
ate-env-api: wait for a suspended environment to wake instead of failing its first call #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Tomer Glottmann (tomergee)
merged 3 commits into
agent-substrate:main
from
vicentefb:fix/wake-retry
Oct 16, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ee8eabe
ate-env-api: probe the guest and retry on Unavailable until a suspend…
vicentefb 3df2928
ate-env-api: cap the wake wait at --wake-running-budget once the acto…
vicentefb 4685b2b
ate-env-api: start the running budget at the first attempt that sees …
vicentefb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package apiservice | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
| "strings" | ||
| "time" | ||
|
|
||
| ateenvv1alpha "github.com/agent-substrate/env/proto/ateenv/v1alpha" | ||
| "github.com/agent-substrate/substrate/pkg/proto/ateapipb" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // DefaultWakeBudget bounds how long a guest call waits for an environment | ||
| // that the control plane reports as suspended or in transition (resuming, | ||
| // pausing, suspending) to wake before the error reaches the caller. The first | ||
| // request that reaches the router is what resumes a suspended actor, and the | ||
| // router holds that request only for its own parking budget, a few seconds. A | ||
| // wake that has to pull the environment's image onto the node or read its | ||
| // snapshot across a region takes longer than that: 18 s for a cold image pull | ||
| // and 7 s for a 1 GiB snapshot read across regions, measured on GKE. | ||
| const DefaultWakeBudget = 2 * time.Minute | ||
|
|
||
| // DefaultWakeRunningBudget bounds the wait when the control plane already | ||
| // reports the environment RUNNING but the router still answers Unavailable. | ||
| // That has two causes: the guest is not yet listening after a restore, which | ||
| // resolves in seconds, or the path to the worker is down, which does not. | ||
| // A short budget covers the first without turning the second into a | ||
| // two-minute hang per call. | ||
| const DefaultWakeRunningBudget = 20 * time.Second | ||
|
|
||
| const ( | ||
| wakeRetryInitialBackoff = 200 * time.Millisecond | ||
| wakeRetryMaxBackoff = 2 * time.Second | ||
| ) | ||
|
|
||
| // ensureAwake sends a trivial request to the environment's guest over conn | ||
| // and repeats it while the router answers Unavailable, which is what an | ||
| // expired parking budget and a guest that is not yet listening both look | ||
| // like. It returns when the guest answers anything else, when the actor has | ||
| // crashed, when ctx ends, or when a budget is spent: WakeBudget bounds the | ||
| // whole wait, and WakeRunningBudget bounds the part of it that starts with | ||
| // the first attempt that sees the actor RUNNING, so a long suspended phase | ||
| // does not eat the time the guest needs to start listening after the state | ||
| // flips. Because the probe is the first request through the router, it is | ||
| // also what wakes the actor; the calls that follow on conn find it up. The | ||
| // state is read once per attempt, and the backoff caps attempts at roughly | ||
| // one per two seconds, so a full two-minute wait costs about sixty | ||
| // control-plane reads. | ||
| func (s *Server) ensureAwake(ctx context.Context, conn *grpc.ClientConn, atespace, id string) error { | ||
| budget := s.WakeBudget | ||
| if budget <= 0 { | ||
| budget = DefaultWakeBudget | ||
| } | ||
| runningBudget := s.WakeRunningBudget | ||
| if runningBudget <= 0 { | ||
| runningBudget = DefaultWakeRunningBudget | ||
| } | ||
| start := time.Now() | ||
| var runningSince time.Time // first attempt that saw the actor RUNNING | ||
| probe := ateenvv1alpha.NewProcessServiceClient(conn) | ||
| backoff := wakeRetryInitialBackoff | ||
| for attempt := 1; ; attempt++ { | ||
| _, err := probe.GetProcess(ctx, &ateenvv1alpha.GetProcessRequest{ProcessId: "ate-env-wake-probe"}) | ||
| if status.Code(err) != codes.Unavailable { | ||
| // Anything else, NotFound for the probe id included, came from | ||
| // the guest itself. | ||
| return nil | ||
| } | ||
| state, serr := s.actorState(ctx, atespace, id) | ||
| if serr != nil { | ||
| return serr | ||
| } | ||
| if state == ateapipb.ActorState_ACTOR_STATE_CRASHED { | ||
| return status.Errorf(codes.FailedPrecondition, "environment %s/%s crashed while waking: %v", atespace, id, err) | ||
| } | ||
| allowed, since, which := budget, start, "wake budget" | ||
| if state == ateapipb.ActorState_ACTOR_STATE_RUNNING { | ||
| if runningSince.IsZero() { | ||
| runningSince = time.Now() | ||
| } | ||
| allowed, since, which = runningBudget, runningSince, "running budget" | ||
| } else { | ||
| runningSince = time.Time{} | ||
| } | ||
| remaining := allowed - time.Since(since) | ||
| if overall := budget - time.Since(start); overall < remaining { | ||
| remaining, allowed, which = overall, budget, "wake budget" | ||
| } | ||
| if remaining <= 0 { | ||
| return status.Errorf(codes.Unavailable, "environment %s/%s did not answer within the %s of %s while %s (%d attempts): %v", | ||
| atespace, id, which, allowed, shortState(state), attempt, err) | ||
| } | ||
| if attempt == 1 { | ||
| log.Printf("waiting for environment %s/%s to wake (state %s, %s %s)", atespace, id, shortState(state), which, allowed) | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| return status.FromContextError(ctx.Err()).Err() | ||
| case <-time.After(min(backoff, remaining)): | ||
| } | ||
| backoff = min(backoff*2, wakeRetryMaxBackoff) | ||
| } | ||
| } | ||
|
|
||
| // actorState reads the actor's state from the control plane. A deleted | ||
| // environment surfaces here as NotFound (through toGRPCError), which is the | ||
| // right answer to a caller still holding its handle. | ||
| func (s *Server) actorState(ctx context.Context, atespace, id string) (ateapipb.ActorState, error) { | ||
| actor, err := s.client.Get(ctx, atespace, id) | ||
| if err != nil { | ||
| return ateapipb.ActorState_ACTOR_STATE_UNSPECIFIED, toGRPCError(err) | ||
| } | ||
| return actor.GetStatus().GetState(), nil | ||
| } | ||
|
|
||
| func shortState(st ateapipb.ActorState) string { | ||
| return strings.TrimPrefix(st.String(), "ACTOR_STATE_") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nits] internal/apiservice/wakeretry.go:90: Return immediately when state is ACTOR_STATE_DELETING so guest calls do not retry against a deleting environment.