Skip to content

Commit b694c4c

Browse files
stevenchen-dbIsaac
andauthored
air: send submit's config validation over the CLI's existing connection (#7210)
## Changes `air run` sends its `config:validate` request through an SDK client, so it reuses the workspace connection the CLI's other calls already opened instead of opening a second TCP and TLS connection. The request is built the same way as before and still gets exactly one attempt. It now also appears in `--debug` logs. One behavior change: the SDK reports a failed response-body read without the HTTP status. A caller-error response (e.g. 401) whose body stalls mid-read now counts as validation being unavailable, so the submission continues. The server validates the run again when it starts. ## Why Since #7368, validation runs alongside the artifact upload and submit waits for both. The extra handshake only delays submit once it makes validation finish after the upload, which happens for clients far from their workspace. Measured on e2-dogfood with emulated extra round trip, time until submit can start (median): | Added round trip | main | this PR | |---|---|---| | +0ms / +50ms | no difference (upload finishes last) | | | +100ms | ~951ms | ~867ms | | +200ms | ~1748ms | ~1479ms | ## Tests - New `TestPreflightValidationReusesSDKConnection`: an SDK call followed by validation opens one connection (2 on main). - `TestPreflightValidationBodyReadFailureOnCallerErrorBlocks` is now `...FailsOpen`, for the behavior change above. - `go test ./cmd/air/` and `go test ./acceptance -run TestAccept/air` pass; lint is clean. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 77950b6 commit b694c4c

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

‎.nextchanges/air/7210.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `air run` reuses its existing workspace connection for config validation instead of opening a second one, which saves a TCP and TLS handshake per submission. ([#7210](https://github.com/databricks/cli/pull/7210))

‎cmd/air/validateconfig.go‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package aircmd
22

33
import (
44
"context"
5-
"crypto/tls"
65
"encoding/json"
76
"errors"
87
"fmt"
@@ -144,15 +143,14 @@ func validateConfigOnce(ctx context.Context, w *databricks.WorkspaceClient, cfg
144143
req.Header.Set("User-Agent", useragent.FromContext(req.Context()))
145144
traceparent.AddTraceparent(req)
146145

147-
transport := clientCfg.Transport
148-
if transport == nil {
149-
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
150-
if clientCfg.InsecureSkipVerify {
151-
defaultTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
152-
}
153-
transport = defaultTransport
154-
}
155-
resp, err := (&http.Client{Transport: transport}).Do(req)
146+
// Send through an SDK client so the request reuses the connection the CLI's
147+
// other calls already opened, instead of a new TCP and TLS handshake. It makes
148+
// one attempt: the SDK retries some connection errors whatever ErrorRetriable
149+
// says, so a minimal retry window stops it after the first.
150+
clientCfg.ErrorRetriable = func(context.Context, error) bool { return false }
151+
clientCfg.TransientErrors = nil
152+
clientCfg.RetryTimeout = time.Nanosecond
153+
resp, err := (&http.Client{Transport: httpclient.NewApiClient(clientCfg)}).Do(req)
156154
if err != nil {
157155
return fmt.Errorf("failed to validate config: %w", err)
158156
}

‎cmd/air/validateconfig_test.go‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"io"
8+
"net"
89
"net/http"
910
"net/http/httptest"
1011
"net/url"
@@ -18,6 +19,7 @@ import (
1819
sdkconfig "github.com/databricks/databricks-sdk-go/config"
1920
"github.com/databricks/databricks-sdk-go/config/credentials"
2021
sdkauth "github.com/databricks/databricks-sdk-go/config/experimental/auth"
22+
"github.com/databricks/databricks-sdk-go/service/iam"
2123
"github.com/stretchr/testify/assert"
2224
"github.com/stretchr/testify/require"
2325
"golang.org/x/oauth2"
@@ -183,7 +185,9 @@ func TestPreflightValidationFailsOpenOnInvalidSuccessResponse(t *testing.T) {
183185
}
184186
}
185187

186-
func TestPreflightValidationBodyReadFailureOnCallerErrorBlocks(t *testing.T) {
188+
// The SDK reports a failed body read without the response status, so even a
189+
// caller-error response counts as validation being unavailable.
190+
func TestPreflightValidationBodyReadFailureOnCallerErrorFailsOpen(t *testing.T) {
187191
w := validationTestWorkspaceClientWithTransport(t, func(req *http.Request) (*http.Response, error) {
188192
body := io.NopCloser(strings.NewReader(`{}`))
189193
status := http.StatusOK
@@ -201,11 +205,30 @@ func TestPreflightValidationBodyReadFailureOnCallerErrorBlocks(t *testing.T) {
201205

202206
ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)
203207
defer cancel()
204-
_, err := preflightValidate(ctx, w, baseRunConfig(), "/Workspace/Users/me/cmd.sh", nil, "token")
205-
require.Error(t, err)
206-
apiErr, ok := errors.AsType[*apierr.APIError](err)
207-
require.True(t, ok)
208-
assert.Equal(t, http.StatusUnauthorized, apiErr.StatusCode)
208+
unavailable, err := preflightValidate(ctx, w, baseRunConfig(), "/Workspace/Users/me/cmd.sh", nil, "token")
209+
require.NoError(t, err)
210+
assert.True(t, unavailable)
211+
}
212+
213+
func TestPreflightValidationReusesSDKConnection(t *testing.T) {
214+
var connections atomic.Int32
215+
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
216+
_, _ = w.Write([]byte(`{}`))
217+
}))
218+
srv.Config.ConnState = func(_ net.Conn, state http.ConnState) {
219+
if state == http.StateNew {
220+
connections.Add(1)
221+
}
222+
}
223+
srv.Start()
224+
t.Cleanup(srv.Close)
225+
w := validationTestWorkspaceClient(t, srv.URL)
226+
227+
_, err := w.CurrentUser.Me(t.Context(), iam.MeRequest{})
228+
require.NoError(t, err)
229+
_, err = preflightValidate(t.Context(), w, baseRunConfig(), "/Workspace/Users/me/cmd.sh", nil, "token")
230+
require.NoError(t, err)
231+
assert.Equal(t, int32(1), connections.Load())
209232
}
210233

211234
func TestPreflightValidationOAuthRefreshUsesDeadline(t *testing.T) {

0 commit comments

Comments
 (0)