Skip to content

Commit f0b065f

Browse files
committed
Add waiting logic for host orchestrator settlement to backend for Docker
While interacting with host orchestrator right after creating host API is returned, there is a possibility of getting 503 service temporarily unavailable error because host is created and booted but host orchestrator is not executed / ready yet. Handling logic for waiting host orchestrator readiness is currently in the client logic. Moving this logic into cloud orchestrator API endpoint makes the client simpler and reduces redundant API endpoints. Apply it first for Docker instance manager. Context: b/501288123
1 parent 5443ff1 commit f0b065f

4 files changed

Lines changed: 128 additions & 0 deletions

File tree

pkg/app/instances/docker.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ func (m *DockerInstanceManager) waitCreateHostOperation(host string) (*apiv1.Hos
188188
return nil, fmt.Errorf("failed to inspect docker container: %w", err)
189189
}
190190
if res.State.Running {
191+
client, err := m.GetHostClient("local", host)
192+
if err != nil {
193+
return nil, err
194+
}
195+
// There is a delay between host creation and its readiness, wait for host readiness and return when it is ready.
196+
readinessChecker, ok := client.(HostReadinessChecker)
197+
if !ok {
198+
readinessChecker = &BasicHostReadinessChecker{Client: client}
199+
}
200+
if err := WaitForHostReady(readinessChecker, 2*time.Minute, 5*time.Second); err != nil {
201+
return nil, err
202+
}
191203
return &apiv1.HostInstance{
192204
Name: host,
193205
}, nil

pkg/app/instances/helper.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package instances
16+
17+
import (
18+
"net/http"
19+
"time"
20+
21+
"github.com/google/cloud-android-orchestration/pkg/app/errors"
22+
)
23+
24+
func WaitForHostReady(checker HostReadinessChecker, maxWait time.Duration, retryDelay time.Duration) error {
25+
deadline := time.Now().Add(maxWait)
26+
27+
for time.Now().Before(deadline) {
28+
ready, err := checker.IsHostReady()
29+
if err != nil {
30+
return err
31+
}
32+
if ready {
33+
return nil
34+
}
35+
time.Sleep(retryDelay)
36+
}
37+
return errors.NewServiceUnavailableError("wait for host orchestrator timed out", nil)
38+
}
39+
40+
type BasicHostReadinessChecker struct {
41+
Client HostClient
42+
}
43+
44+
func (c *BasicHostReadinessChecker) IsHostReady() (bool, error) {
45+
status, err := c.Client.Get("/", "", nil)
46+
if err != nil || status == http.StatusBadGateway || status == http.StatusServiceUnavailable {
47+
return false, nil
48+
}
49+
return true, nil
50+
}

pkg/app/instances/helper_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package instances
16+
17+
import (
18+
"testing"
19+
"time"
20+
)
21+
22+
type mockHostReadinessChecker struct {
23+
isHostReadyFunc func() (bool, error)
24+
}
25+
26+
func (c *mockHostReadinessChecker) IsHostReady() (bool, error) {
27+
return c.isHostReadyFunc()
28+
}
29+
30+
func TestWaitForHostReadySucceeds(t *testing.T) {
31+
checker := &mockHostReadinessChecker{
32+
isHostReadyFunc: func() (bool, error) {
33+
return true, nil
34+
},
35+
}
36+
37+
err := WaitForHostReady(checker, 100*time.Millisecond, 1*time.Millisecond)
38+
if err != nil {
39+
t.Errorf("unexpected error: %v", err)
40+
}
41+
}
42+
43+
func TestWaitForHostReadyRetries(t *testing.T) {
44+
calls := 0
45+
checker := &mockHostReadinessChecker{
46+
isHostReadyFunc: func() (bool, error) {
47+
calls++
48+
if calls == 1 {
49+
return false, nil
50+
}
51+
return true, nil
52+
},
53+
}
54+
55+
err := WaitForHostReady(checker, 100*time.Millisecond, 1*time.Millisecond)
56+
if err != nil {
57+
t.Errorf("unexpected error: %v", err)
58+
}
59+
if calls != 2 {
60+
t.Errorf("expected 2 calls, got %d", calls)
61+
}
62+
}

pkg/app/instances/instances.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type Manager interface {
3838
GetHostClient(zone string, host string) (HostClient, error)
3939
}
4040

41+
type HostReadinessChecker interface {
42+
IsHostReady() (bool, error)
43+
}
44+
4145
type HostClient interface {
4246
// Get and Post requests return the HTTP status code or an error.
4347
// The response body is parsed into the res output parameter if provided.

0 commit comments

Comments
 (0)