forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
392 lines (349 loc) · 13.8 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package dbsql
import (
"context"
"database/sql/driver"
"time"
"github.com/databricks/databricks-sql-go/driverctx"
"github.com/databricks/databricks-sql-go/internal/cli_service"
"github.com/databricks/databricks-sql-go/internal/client"
"github.com/databricks/databricks-sql-go/internal/config"
dbsqlerr "github.com/databricks/databricks-sql-go/internal/err"
"github.com/databricks/databricks-sql-go/internal/rows"
"github.com/databricks/databricks-sql-go/internal/sentinel"
"github.com/databricks/databricks-sql-go/logger"
"github.com/pkg/errors"
)
type conn struct {
id string
cfg *config.Config
client cli_service.TCLIService
session *cli_service.TOpenSessionResp
}
// Prepare prepares a statement with the query bound to this connection.
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return &stmt{conn: c, query: query}, nil
}
// PrepareContext prepares a statement with the query bound to this connection.
// Currently, PrepareContext does not use context and is functionally equivalent to Prepare.
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return &stmt{conn: c, query: query}, nil
}
// Close closes the session.
// sql package maintains a free pool of connections and only calls Close when there's a surplus of idle connections.
func (c *conn) Close() error {
log := logger.WithContext(c.id, "", "")
ctx := driverctx.NewContextWithConnId(context.Background(), c.id)
_, err := c.client.CloseSession(ctx, &cli_service.TCloseSessionReq{
SessionHandle: c.session.SessionHandle,
})
if err != nil {
log.Err(err).Msg("databricks: failed to close connection")
return dbsqlerr.WrapErr(err, "failed to close connection")
}
return nil
}
// Not supported in Databricks.
func (c *conn) Begin() (driver.Tx, error) {
return nil, errors.New(dbsqlerr.ErrTransactionsNotSupported)
}
// Not supported in Databricks.
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return nil, errors.New(dbsqlerr.ErrTransactionsNotSupported)
}
// Ping attempts to verify that the server is accessible.
// Returns ErrBadConn if ping fails and consequently DB.Ping will remove the conn from the pool.
func (c *conn) Ping(ctx context.Context) error {
log := logger.AddContext(logger.Ctx(ctx), c.id, driverctx.CorrelationIdFromContext(ctx), "")
ctx = driverctx.NewContextWithConnId(ctx, c.id)
ctx1, cancel := context.WithTimeout(ctx, c.cfg.PingTimeout)
defer cancel()
_, err := c.QueryContext(ctx1, "select 1", nil)
if err != nil {
log.Err(err).Msg("databricks: failed to ping")
return driver.ErrBadConn
}
return nil
}
// ResetSession is called prior to executing a query on the connection.
// The session with this driver does not have any important state to reset before re-use.
func (c *conn) ResetSession(ctx context.Context) error {
return nil
}
// IsValid signals whether a connection is valid or if it should be discarded.
func (c *conn) IsValid() bool {
return c.session.GetStatus().StatusCode == cli_service.TStatusCode_SUCCESS_STATUS
}
// ExecContext executes a query that doesn't return rows, such
// as an INSERT or UPDATE.
//
// ExecContext honors the context timeout and return when it is canceled.
// Statement ExecContext is the same as connection ExecContext
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, "")
msg, start := logger.Track("ExecContext")
defer log.Duration(msg, start)
ctx = driverctx.NewContextWithConnId(ctx, c.id)
if len(args) > 0 {
q, err := SubstituteArgs(query, args)
if err != nil {
return nil, err
}
query = q
}
exStmtResp, opStatusResp, err := c.runQuery(ctx, query, args)
if exStmtResp != nil && exStmtResp.OperationHandle != nil {
// we have an operation id so update the logger
log = logger.AddContext(logger.Ctx(ctx), c.id, corrId, client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID))
// since we have an operation handle we can close the operation if necessary
alreadyClosed := exStmtResp.DirectResults != nil && exStmtResp.DirectResults.CloseOperation != nil
newCtx := driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), c.id), corrId)
if !alreadyClosed && (opStatusResp == nil || opStatusResp.GetOperationState() != cli_service.TOperationState_CLOSED_STATE) {
_, err1 := c.client.CloseOperation(newCtx, &cli_service.TCloseOperationReq{
OperationHandle: exStmtResp.OperationHandle,
})
if err1 != nil {
log.Err(err1).Msg("databricks: failed to close operation after executing statement")
}
}
}
if err != nil {
log.Err(err).Msgf("databricks: failed to execute query: query %s", query)
return nil, dbsqlerr.WrapErrf(err, "failed to execute query")
}
res := result{AffectedRows: opStatusResp.GetNumModifiedRows()}
return &res, nil
}
// QueryContext executes a query that may return rows, such as a
// SELECT.
//
// QueryContext honors the context timeout and return when it is canceled.
// Statement QueryContext is the same as connection QueryContext
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, "")
msg, start := log.Track("QueryContext")
ctx = driverctx.NewContextWithConnId(ctx, c.id)
if len(args) > 0 {
q, err := SubstituteArgs(query, args)
if err != nil {
return nil, err
}
query = q
}
// first we try to get the results synchronously.
// at any point in time that the context is done we must cancel and return
exStmtResp, _, err := c.runQuery(ctx, query, args)
if exStmtResp != nil && exStmtResp.OperationHandle != nil {
log = logger.AddContext(logger.Ctx(ctx), c.id, driverctx.CorrelationIdFromContext(ctx), client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID))
}
defer log.Duration(msg, start)
if err != nil {
log.Err(err).Msg("databricks: failed to run query") // To log query we need to redact credentials
return nil, dbsqlerr.WrapErrf(err, "failed to run query")
}
// hold on to the operation handle
opHandle := exStmtResp.OperationHandle
rows, err := rows.NewRows(c.id, corrId, opHandle, c.client, c.cfg, exStmtResp.DirectResults)
return rows, err
}
func (c *conn) runQuery(ctx context.Context, query string, args []driver.NamedValue) (*cli_service.TExecuteStatementResp, *cli_service.TGetOperationStatusResp, error) {
log := logger.AddContext(logger.Ctx(ctx), c.id, driverctx.CorrelationIdFromContext(ctx), "")
// first we try to get the results synchronously.
// at any point in time that the context is done we must cancel and return
exStmtResp, err := c.executeStatement(ctx, query, args)
if err != nil {
return exStmtResp, nil, err
}
opHandle := exStmtResp.OperationHandle
if opHandle != nil && opHandle.OperationId != nil {
log = logger.AddContext(logger.Ctx(ctx),
c.id,
driverctx.CorrelationIdFromContext(ctx), client.SprintGuid(opHandle.OperationId.GUID),
)
}
if exStmtResp.DirectResults != nil {
opStatus := exStmtResp.DirectResults.GetOperationStatus()
switch opStatus.GetOperationState() {
// terminal states
// good
case cli_service.TOperationState_FINISHED_STATE:
return exStmtResp, opStatus, nil
// bad
case cli_service.TOperationState_CANCELED_STATE,
cli_service.TOperationState_CLOSED_STATE,
cli_service.TOperationState_ERROR_STATE,
cli_service.TOperationState_TIMEDOUT_STATE:
logBadQueryState(log, opStatus)
return exStmtResp, opStatus, errors.New(opStatus.GetDisplayMessage())
// live states
case cli_service.TOperationState_INITIALIZED_STATE,
cli_service.TOperationState_PENDING_STATE,
cli_service.TOperationState_RUNNING_STATE:
statusResp, err := c.pollOperation(ctx, opHandle)
if err != nil {
return exStmtResp, statusResp, err
}
switch statusResp.GetOperationState() {
// terminal states
// good
case cli_service.TOperationState_FINISHED_STATE:
return exStmtResp, statusResp, nil
// bad
case cli_service.TOperationState_CANCELED_STATE,
cli_service.TOperationState_CLOSED_STATE,
cli_service.TOperationState_ERROR_STATE,
cli_service.TOperationState_TIMEDOUT_STATE:
logBadQueryState(log, statusResp)
return exStmtResp, statusResp, errors.New(statusResp.GetDisplayMessage())
// live states
default:
logBadQueryState(log, statusResp)
return exStmtResp, statusResp, errors.New("invalid operation state. This should not have happened")
}
// weird states
default:
logBadQueryState(log, opStatus)
return exStmtResp, opStatus, errors.New("invalid operation state. This should not have happened")
}
} else {
statusResp, err := c.pollOperation(ctx, opHandle)
if err != nil {
return exStmtResp, statusResp, err
}
switch statusResp.GetOperationState() {
// terminal states
// good
case cli_service.TOperationState_FINISHED_STATE:
return exStmtResp, statusResp, nil
// bad
case cli_service.TOperationState_CANCELED_STATE,
cli_service.TOperationState_CLOSED_STATE,
cli_service.TOperationState_ERROR_STATE,
cli_service.TOperationState_TIMEDOUT_STATE:
logBadQueryState(log, statusResp)
return exStmtResp, statusResp, errors.New(statusResp.GetDisplayMessage())
// live states
default:
logBadQueryState(log, statusResp)
return exStmtResp, statusResp, errors.New("invalid operation state. This should not have happened")
}
}
}
func logBadQueryState(log *logger.DBSQLLogger, opStatus *cli_service.TGetOperationStatusResp) {
log.Error().Msgf("databricks: query state: %s", opStatus.GetOperationState())
log.Error().Msg(opStatus.GetErrorMessage())
}
func (c *conn) executeStatement(ctx context.Context, query string, args []driver.NamedValue) (*cli_service.TExecuteStatementResp, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, "")
req := cli_service.TExecuteStatementReq{
SessionHandle: c.session.SessionHandle,
Statement: query,
RunAsync: c.cfg.RunAsync,
QueryTimeout: int64(c.cfg.QueryTimeout / time.Second),
GetDirectResults: &cli_service.TSparkGetDirectResults{
MaxRows: int64(c.cfg.MaxRows),
},
}
if c.cfg.UseArrowBatches {
req.CanReadArrowResult_ = &c.cfg.UseArrowBatches
req.UseArrowNativeTypes = &cli_service.TSparkArrowTypes{
DecimalAsArrow: &c.cfg.UseArrowNativeDecimal,
TimestampAsArrow: &c.cfg.UseArrowNativeTimestamp,
ComplexTypesAsArrow: &c.cfg.UseArrowNativeComplexTypes,
IntervalTypesAsArrow: &c.cfg.UseArrowNativeIntervalTypes,
}
}
ctx = driverctx.NewContextWithConnId(ctx, c.id)
resp, err := c.client.ExecuteStatement(ctx, &req)
var shouldCancel = func(resp *cli_service.TExecuteStatementResp) bool {
if resp == nil {
return false
}
hasHandle := resp.OperationHandle != nil
isOpen := resp.DirectResults != nil && resp.DirectResults.CloseOperation == nil
return hasHandle && isOpen
}
select {
default:
case <-ctx.Done():
newCtx := driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), c.id), corrId)
// in case context is done, we need to cancel the operation if necessary
if err == nil && shouldCancel(resp) {
log.Debug().Msg("databricks: canceling query")
_, err1 := c.client.CancelOperation(newCtx, &cli_service.TCancelOperationReq{
OperationHandle: resp.GetOperationHandle(),
})
if err1 != nil {
log.Err(err).Msgf("databricks: cancel failed")
} else {
log.Debug().Msgf("databricks: cancel success")
}
} else {
log.Debug().Msg("databricks: query did not need cancellation")
}
return nil, ctx.Err()
}
return resp, err
}
func (c *conn) pollOperation(ctx context.Context, opHandle *cli_service.TOperationHandle) (*cli_service.TGetOperationStatusResp, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, client.SprintGuid(opHandle.OperationId.GUID))
var statusResp *cli_service.TGetOperationStatusResp
ctx = driverctx.NewContextWithConnId(ctx, c.id)
newCtx := driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), c.id), corrId)
pollSentinel := sentinel.Sentinel{
OnDoneFn: func(statusResp any) (any, error) {
return statusResp, nil
},
StatusFn: func() (sentinel.Done, any, error) {
var err error
log.Debug().Msg("databricks: polling status")
statusResp, err = c.client.GetOperationStatus(newCtx, &cli_service.TGetOperationStatusReq{
OperationHandle: opHandle,
})
if statusResp != nil && statusResp.OperationState != nil {
log.Debug().Msgf("databricks: status %s", statusResp.GetOperationState().String())
}
return func() bool {
if err != nil {
return true
}
switch statusResp.GetOperationState() {
case cli_service.TOperationState_INITIALIZED_STATE,
cli_service.TOperationState_PENDING_STATE,
cli_service.TOperationState_RUNNING_STATE:
return false
default:
log.Debug().Msg("databricks: polling done")
return true
}
}, statusResp, err
},
OnCancelFn: func() (any, error) {
log.Debug().Msg("databricks: canceling query")
ret, err := c.client.CancelOperation(newCtx, &cli_service.TCancelOperationReq{
OperationHandle: opHandle,
})
return ret, err
},
}
_, resp, err := pollSentinel.Watch(ctx, c.cfg.PollInterval, 0)
if err != nil {
return nil, dbsqlerr.WrapErr(err, "failed to poll query state")
}
statusResp, ok := resp.(*cli_service.TGetOperationStatusResp)
if !ok {
return nil, errors.New("could not read query status")
}
return statusResp, nil
}
var _ driver.Conn = (*conn)(nil)
var _ driver.Pinger = (*conn)(nil)
var _ driver.SessionResetter = (*conn)(nil)
var _ driver.Validator = (*conn)(nil)
var _ driver.ExecerContext = (*conn)(nil)
var _ driver.QueryerContext = (*conn)(nil)
var _ driver.ConnPrepareContext = (*conn)(nil)
var _ driver.ConnBeginTx = (*conn)(nil)