Skip to content

Commit 9bdd7e9

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 6244a75 commit 9bdd7e9

6 files changed

Lines changed: 158 additions & 0 deletions

File tree

pkg/app/app_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func (hc *testHostClient) Post(path, query string, bodyJSON any, res *instances.
107107
return 200, nil
108108
}
109109

110+
func (hc *testHostClient) PingHO() (int, error) {
111+
return 200, nil
112+
}
113+
110114
func (hc *testHostClient) GetReverseProxy() *httputil.ReverseProxy {
111115
return httputil.NewSingleHostReverseProxy(hc.url)
112116
}

pkg/app/instances/docker.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ 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+
if err := WaitForHostReady(client, 2*time.Minute, 5*time.Second, nil); err != nil {
197+
return nil, err
198+
}
191199
return &apiv1.HostInstance{
192200
Name: host,
193201
}, nil

pkg/app/instances/helper.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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(client HostChecker, maxWait time.Duration, retryDelay time.Duration, check func() error) error {
25+
deadline := time.Now().Add(maxWait)
26+
27+
for time.Now().Before(deadline) {
28+
if check != nil {
29+
if err := check(); err != nil {
30+
return err
31+
}
32+
}
33+
status, err := client.PingHO()
34+
if err == nil && status != http.StatusBadGateway && status != http.StatusServiceUnavailable {
35+
return nil
36+
}
37+
time.Sleep(retryDelay)
38+
}
39+
return errors.NewServiceUnavailableError("wait for host orchestrator timed out", nil)
40+
}

pkg/app/instances/helper_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
"net/http/httputil"
20+
"testing"
21+
"time"
22+
)
23+
24+
type mockHostClient struct {
25+
getFunc func(string, string, *HostResponse) (int, error)
26+
}
27+
28+
func (c *mockHostClient) Get(path, query string, res *HostResponse) (int, error) {
29+
return c.getFunc(path, query, res)
30+
}
31+
32+
func (c *mockHostClient) Post(path, query string, body any, res *HostResponse) (int, error) {
33+
return 0, nil
34+
}
35+
36+
func (c *mockHostClient) PingHO() (int, error) {
37+
return c.Get("/", "", nil)
38+
}
39+
40+
func (c *mockHostClient) GetReverseProxy() *httputil.ReverseProxy {
41+
return nil
42+
}
43+
44+
func TestWaitForHostReadySucceeds(t *testing.T) {
45+
client := &mockHostClient{
46+
getFunc: func(path, query string, res *HostResponse) (int, error) {
47+
return http.StatusOK, nil
48+
},
49+
}
50+
51+
err := WaitForHostReady(client, 100*time.Millisecond, 1*time.Millisecond, nil)
52+
if err != nil {
53+
t.Errorf("unexpected error: %v", err)
54+
}
55+
}
56+
57+
func TestWaitForHostReadyRetriesOnBadGateway(t *testing.T) {
58+
calls := 0
59+
client := &mockHostClient{
60+
getFunc: func(path, query string, res *HostResponse) (int, error) {
61+
calls++
62+
if calls == 1 {
63+
return http.StatusBadGateway, nil
64+
}
65+
return http.StatusOK, nil
66+
},
67+
}
68+
69+
err := WaitForHostReady(client, 100*time.Millisecond, 1*time.Millisecond, nil)
70+
if err != nil {
71+
t.Errorf("unexpected error: %v", err)
72+
}
73+
if calls != 2 {
74+
t.Errorf("expected 2 calls, got %d", calls)
75+
}
76+
}
77+
78+
func TestWaitForHostReadyRetriesOnServiceUnavailable(t *testing.T) {
79+
calls := 0
80+
client := &mockHostClient{
81+
getFunc: func(path, query string, res *HostResponse) (int, error) {
82+
calls++
83+
if calls == 1 {
84+
return http.StatusServiceUnavailable, nil
85+
}
86+
return http.StatusOK, nil
87+
},
88+
}
89+
90+
err := WaitForHostReady(client, 100*time.Millisecond, 1*time.Millisecond, nil)
91+
if err != nil {
92+
t.Errorf("unexpected error: %v", err)
93+
}
94+
if calls != 2 {
95+
t.Errorf("expected 2 calls, got %d", calls)
96+
}
97+
}

pkg/app/instances/hostclient.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func (c *NetHostClient) GetReverseProxy() *httputil.ReverseProxy {
108108
return devProxy
109109
}
110110

111+
func (c *NetHostClient) PingHO() (int, error) {
112+
return c.Get("/", "", nil)
113+
}
114+
111115
func parseReply(res *http.Response, resObj any, resErr *apiv1.Error) error {
112116
var err error
113117
dec := json.NewDecoder(res.Body)

pkg/app/instances/instances.go

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

41+
type HostChecker interface {
42+
PingHO() (int, error)
43+
}
44+
4145
type HostClient interface {
46+
HostChecker
4247
// Get and Post requests return the HTTP status code or an error.
4348
// The response body is parsed into the res output parameter if provided.
4449
Get(URLPath, URLQuery string, res *HostResponse) (int, error)

0 commit comments

Comments
 (0)