Skip to content

Commit db47d1e

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 db47d1e

6 files changed

Lines changed: 162 additions & 0 deletions

File tree

pkg/app/app_test.go

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

110+
110111
func (hc *testHostClient) GetReverseProxy() *httputil.ReverseProxy {
111112
return httputil.NewSingleHostReverseProxy(hc.url)
112113
}

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.(ReadinessChecker)
197+
if !ok {
198+
readinessChecker = &defaultReadinessChecker{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(client ReadinessChecker, maxWait time.Duration, retryDelay time.Duration) error {
25+
deadline := time.Now().Add(maxWait)
26+
27+
for time.Now().Before(deadline) {
28+
ready, err := client.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 defaultReadinessChecker struct {
41+
client HostClient
42+
}
43+
44+
func (c *defaultReadinessChecker) 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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
37+
func (c *mockHostClient) GetReverseProxy() *httputil.ReverseProxy {
38+
return nil
39+
}
40+
41+
func TestWaitForHostReadySucceeds(t *testing.T) {
42+
client := &mockHostClient{
43+
getFunc: func(path, query string, res *HostResponse) (int, error) {
44+
return http.StatusOK, nil
45+
},
46+
}
47+
48+
err := WaitForHostReady(&defaultReadinessChecker{client}, 100*time.Millisecond, 1*time.Millisecond)
49+
if err != nil {
50+
t.Errorf("unexpected error: %v", err)
51+
}
52+
}
53+
54+
func TestWaitForHostReadyRetriesOnBadGateway(t *testing.T) {
55+
calls := 0
56+
client := &mockHostClient{
57+
getFunc: func(path, query string, res *HostResponse) (int, error) {
58+
calls++
59+
if calls == 1 {
60+
return http.StatusBadGateway, nil
61+
}
62+
return http.StatusOK, nil
63+
},
64+
}
65+
66+
err := WaitForHostReady(&defaultReadinessChecker{client}, 100*time.Millisecond, 1*time.Millisecond)
67+
if err != nil {
68+
t.Errorf("unexpected error: %v", err)
69+
}
70+
if calls != 2 {
71+
t.Errorf("expected 2 calls, got %d", calls)
72+
}
73+
}
74+
75+
func TestWaitForHostReadyRetriesOnServiceUnavailable(t *testing.T) {
76+
calls := 0
77+
client := &mockHostClient{
78+
getFunc: func(path, query string, res *HostResponse) (int, error) {
79+
calls++
80+
if calls == 1 {
81+
return http.StatusServiceUnavailable, nil
82+
}
83+
return http.StatusOK, nil
84+
},
85+
}
86+
87+
err := WaitForHostReady(&defaultReadinessChecker{client}, 100*time.Millisecond, 1*time.Millisecond)
88+
if err != nil {
89+
t.Errorf("unexpected error: %v", err)
90+
}
91+
if calls != 2 {
92+
t.Errorf("expected 2 calls, got %d", calls)
93+
}
94+
}

pkg/app/instances/hostclient.go

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

111+
111112
func parseReply(res *http.Response, resObj any, resErr *apiv1.Error) error {
112113
var err error
113114
dec := json.NewDecoder(res.Body)

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 ReadinessChecker 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)