|
| 1 | +//go:build cgo && databricks_kernel |
| 2 | + |
| 3 | +package dbsql |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "strings" |
| 14 | + "sync/atomic" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | + |
| 18 | + dbsqlerr "github.com/databricks/databricks-sql-go/errors" |
| 19 | + "github.com/databricks/databricks-sql-go/internal/backend" |
| 20 | + "github.com/databricks/databricks-sql-go/internal/cli_service" |
| 21 | + "github.com/databricks/databricks-sql-go/internal/client" |
| 22 | + "github.com/databricks/databricks-sql-go/internal/config" |
| 23 | + "github.com/databricks/databricks-sql-go/internal/warehouse_cache" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestKernelCloudFetchDisabledPreservesUnsupportedServerError(t *testing.T) { |
| 28 | + for _, automaticRecovery := range []bool{false, true} { |
| 29 | + for _, rejectSession := range []bool{false, true} { |
| 30 | + t.Run(fmt.Sprintf("recovery=%t/session_rejected=%t", automaticRecovery, rejectSession), func(t *testing.T) { |
| 31 | + testKernelCloudFetchDisabledUnsupportedServer(t, automaticRecovery, rejectSession) |
| 32 | + }) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func testKernelCloudFetchDisabledUnsupportedServer(t *testing.T, automaticRecovery, rejectSession bool) { |
| 38 | + t.Helper() |
| 39 | + defer warehouse_cache.ClearCache() |
| 40 | + var sessions atomic.Int64 |
| 41 | + var executions atomic.Int64 |
| 42 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 43 | + w.Header().Set("Content-Type", "application/json") |
| 44 | + switch { |
| 45 | + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/sessions"): |
| 46 | + sessions.Add(1) |
| 47 | + var body struct { |
| 48 | + SessionConfs map[string]string `json:"session_confs"` |
| 49 | + } |
| 50 | + if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.SessionConfs["can_cloud_download"] != "false" { |
| 51 | + http.Error(w, "missing disabled download capability", http.StatusBadRequest) |
| 52 | + return |
| 53 | + } |
| 54 | + for key := range body.SessionConfs { |
| 55 | + if strings.EqualFold(key, "cloudfetch_enabled") || (strings.EqualFold(key, "can_cloud_download") && key != "can_cloud_download") { |
| 56 | + http.Error(w, "conflicting download parameter", http.StatusBadRequest) |
| 57 | + return |
| 58 | + } |
| 59 | + } |
| 60 | + if rejectSession { |
| 61 | + w.WriteHeader(http.StatusBadRequest) |
| 62 | + _, _ = io.WriteString(w, `{"error_code":"CONFIG_NOT_AVAILABLE","message":"Configuration can_cloud_download is not available. SQLSTATE: 42K0I"}`) |
| 63 | + return |
| 64 | + } |
| 65 | + _, _ = io.WriteString(w, `{"session_id":"test-session"}`) |
| 66 | + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/statements"): |
| 67 | + executions.Add(1) |
| 68 | + _, _ = io.WriteString(w, `{"statement_id":"unsupported-inline","status":{"state":"FAILED","sql_state":"42K0I","error":{"error_code":"CONFIG_NOT_AVAILABLE","message":"Configuration can_cloud_download is not available"}}}`) |
| 69 | + default: |
| 70 | + _, _ = io.WriteString(w, `{}`) |
| 71 | + } |
| 72 | + })) |
| 73 | + defer server.Close() |
| 74 | + dbConnector, err := NewConnector( |
| 75 | + WithServerHostname(server.URL), WithHTTPPath("/sql/1.0/warehouses/test"), |
| 76 | + WithAccessToken("test-token"), WithUseKernel(true), WithCloudFetch(false), |
| 77 | + WithSessionParams(map[string]string{"CAN_CLOUD_DOWNLOAD": "true"}), |
| 78 | + WithSkipTLSHostVerify(), WithRetries(-1, time.Millisecond, time.Millisecond), |
| 79 | + ) |
| 80 | + require.NoError(t, err) |
| 81 | + var thriftAttempts atomic.Int64 |
| 82 | + if automaticRecovery { |
| 83 | + c := dbConnector.(*connector) |
| 84 | + c.cfg.UseKernel = false |
| 85 | + c.thriftBackendFactory = func(context.Context, *config.Config, *http.Client) (backend.Backend, error) { |
| 86 | + thriftAttempts.Add(1) |
| 87 | + sqlState := "KP001" |
| 88 | + return &fakeThriftBackend{openSessionErr: client.CheckOpenSessionStatus(&cli_service.TOpenSessionResp{ |
| 89 | + Status: &cli_service.TStatus{StatusCode: cli_service.TStatusCode_ERROR_STATUS, SqlState: &sqlState}, |
| 90 | + })}, nil |
| 91 | + } |
| 92 | + } |
| 93 | + db := sql.OpenDB(dbConnector) |
| 94 | + defer db.Close() //nolint:errcheck |
| 95 | + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 96 | + defer cancel() |
| 97 | + connection, err := db.Conn(ctx) |
| 98 | + if rejectSession { |
| 99 | + require.Error(t, err) |
| 100 | + require.Contains(t, err.Error(), "CONFIG_NOT_AVAILABLE") |
| 101 | + require.Contains(t, err.Error(), "42K0I") |
| 102 | + require.Equal(t, int64(0), executions.Load()) |
| 103 | + } else { |
| 104 | + require.NoError(t, err) |
| 105 | + defer connection.Close() //nolint:errcheck |
| 106 | + _, err = connection.ExecContext(ctx, "SELECT 1") |
| 107 | + require.Error(t, err) |
| 108 | + var executionError dbsqlerr.DBExecutionError |
| 109 | + require.ErrorAs(t, err, &executionError) |
| 110 | + require.Equal(t, "42K0I", executionError.SqlState()) |
| 111 | + require.Contains(t, err.Error(), "CONFIG_NOT_AVAILABLE") |
| 112 | + require.Equal(t, int64(1), executions.Load(), "unsupported delivery must not fall back") |
| 113 | + } |
| 114 | + require.Equal(t, int64(1), sessions.Load(), "unsupported delivery must not reopen the session") |
| 115 | + if automaticRecovery { |
| 116 | + require.Equal(t, int64(1), thriftAttempts.Load(), "backend recovery must not repeat") |
| 117 | + } |
| 118 | +} |
0 commit comments