diff --git a/.nextchanges/cli/7752.md b/.nextchanges/cli/7752.md new file mode 100644 index 00000000000..70f317bc683 --- /dev/null +++ b/.nextchanges/cli/7752.md @@ -0,0 +1 @@ +* Stop warning when the host metadata request (`/.well-known/databricks-config`) times out; the CLI already falls back to your config, and the message is still shown with `--debug`. ([#7752](https://github.com/databricks/cli/pull/7752)) diff --git a/libs/hostmetadata/resolver.go b/libs/hostmetadata/resolver.go index 595e37bd226..4ae2ebda8fa 100644 --- a/libs/hostmetadata/resolver.go +++ b/libs/hostmetadata/resolver.go @@ -9,6 +9,8 @@ package hostmetadata import ( "context" "errors" + "net/url" + "strings" "time" "github.com/databricks/cli/libs/cache" @@ -82,12 +84,37 @@ func NewResolver(fetch config.HostMetadataResolver) config.HostMetadataResolver if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { return nil, nil } - // The raw error is env-dependent (DNS vs TLS vs HTTP) and would make - // acceptance goldens brittle, so keep it at Debug; the Warn text is - // stable (host only) for user visibility. - log.Warnf(ctx, "[hostmetadata] failed to fetch host metadata for %s, will skip for %s", host, negativeCacheTTL) + // The Warn text is stable (host only) for user visibility. The raw + // error is env-dependent (DNS vs TLS vs HTTP) and would make + // acceptance goldens brittle, so it stays at Debug. + // A host that accepts the connection but doesn't answer in time is + // still cached, so the next commands don't wait on it again, but the + // timeout says nothing about the host's config and the SDK falls back + // to the user config, so it is logged at Debug too. + if isSDKInactivityTimeout(err) { + log.Debugf(ctx, "[hostmetadata] failed to fetch host metadata for %s, will skip for %s", host, negativeCacheTTL) + } else { + log.Warnf(ctx, "[hostmetadata] failed to fetch host metadata for %s, will skip for %s", host, negativeCacheTTL) + } log.Debugf(ctx, "[hostmetadata] fetch error for %s: %v", host, err) cache.Put(ctx, negative, fp, &negativeSentinel{Error: true}) return nil, nil } } + +// sdkInactivityTimeoutPrefix starts the error the SDK puts in a url.Error when +// a request sees no activity for HTTPTimeout (httpclient/api_client.go). +const sdkInactivityTimeoutPrefix = "request timed out after " + +// isSDKInactivityTimeout reports whether err is the SDK's HTTP inactivity +// timeout. The SDK replaces the url.Error cause with a new unwrapped error, so +// there is no sentinel or type to match and the message text is the only +// signal; TestNewResolver_Timeout_LogsAtDebugAndCachesNegative runs the real +// SDK client and catches a change in that wording. +func isSDKInactivityTimeout(err error) bool { + uerr, ok := errors.AsType[*url.Error](err) + if !ok || uerr.Err == nil { + return false + } + return strings.HasPrefix(uerr.Err.Error(), sdkInactivityTimeoutPrefix) +} diff --git a/libs/hostmetadata/resolver_test.go b/libs/hostmetadata/resolver_test.go index 965f38cd7e1..6693a334b6e 100644 --- a/libs/hostmetadata/resolver_test.go +++ b/libs/hostmetadata/resolver_test.go @@ -1,14 +1,19 @@ package hostmetadata_test import ( + "bytes" "context" "errors" + "log/slog" "net/http" "net/http/httptest" + "net/url" "sync/atomic" "testing" "github.com/databricks/cli/libs/hostmetadata" + "github.com/databricks/cli/libs/log" + "github.com/databricks/cli/libs/log/handler" "github.com/databricks/databricks-sdk-go/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -106,3 +111,65 @@ func TestFactory_EndToEnd_CacheHitSkipsSDKFetch(t *testing.T) { assert.Equal(t, "acct-1", cfg2.AccountID) assert.Equal(t, int32(1), hits.Load(), "second EnsureResolved must not hit the server") } + +// sdkFetch returns the SDK's real well-known fetch for host, using the SDK +// HTTP client with a short timeout. +func sdkFetch(t *testing.T, host string) config.HostMetadataResolver { + cfg := &config.Config{ + Host: host, + Token: "x", + Credentials: config.PatCredentials{}, + HTTPTimeoutSeconds: 1, + RetryTimeoutSeconds: 1, + HostMetadataResolver: func(context.Context, string) (*config.HostMetadata, error) { return nil, nil }, + } + require.NoError(t, cfg.EnsureResolved()) + return cfg.DefaultHostMetadataResolver() +} + +func captureLog(t *testing.T) (context.Context, *bytes.Buffer) { + var buf bytes.Buffer + logger := slog.New(handler.NewFriendlyHandler(&buf, &handler.Options{Level: log.LevelDebug})) + return log.NewContext(t.Context(), logger), &buf +} + +func TestNewResolver_Timeout_LogsAtDebugAndCachesNegative(t *testing.T) { + t.Setenv("DATABRICKS_CACHE_DIR", t.TempDir()) + + var hits atomic.Int32 + release := make(chan struct{}) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + <-release + })) + t.Cleanup(server.Close) + t.Cleanup(func() { close(release) }) + + r := hostmetadata.NewResolver(sdkFetch(t, server.URL)) + + ctx, buf := captureLog(t) + m, err := r(ctx, server.URL) + require.NoError(t, err) + assert.Nil(t, m) + assert.Contains(t, buf.String(), "Debug: [hostmetadata] failed to fetch host metadata for "+server.URL) + assert.NotContains(t, buf.String(), "Warn:") + + first := hits.Load() + _, err = r(ctx, server.URL) + require.NoError(t, err) + assert.Equal(t, first, hits.Load(), "a timeout must be negatively cached") +} + +func TestNewResolver_OtherFetchError_LogsWarn(t *testing.T) { + t.Setenv("DATABRICKS_CACHE_DIR", t.TempDir()) + + fetch := func(ctx context.Context, host string) (*config.HostMetadata, error) { + return nil, &url.Error{Op: "Get", URL: host, Err: errors.New("dial tcp: lookup example: no such host")} + } + r := hostmetadata.NewResolver(fetch) + + ctx, buf := captureLog(t) + _, err := r(ctx, "https://example") + require.NoError(t, err) + assert.Contains(t, buf.String(), "Warn: [hostmetadata] failed to fetch host metadata for https://example") +}