Skip to content

Commit 13e35a0

Browse files
authored
Merge pull request #351 from ydb-platform/tx-ctx
* Added some description to error message from table pool get
2 parents e276c39 + df0002f commit 13e35a0

File tree

22 files changed

+743
-279
lines changed

22 files changed

+743
-279
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Added some description to error message from table pool get
2+
* Moved implementation `sugar.GenerateDeclareSection` to `internal/table`
3+
* Added transaction trace callbacks and internal logging with them
4+
* Stored context from `BeginTx` to `internal/xsql` transaction
5+
* Added automatically generated declare section to query text in `database/sql` usage
6+
* Removed supports `sql.LevelSerializable`
7+
* Added `retry.Do` helper for retry custom lambda with `database/sql` without transactions
8+
* Removed `retry.WithTxOptions` option (only default isolation supports)
9+
110
## v3.34.1
211
* Changed `database/sql` driver `prepare` behaviour to `nop` with proxing call to conn exec/query with keep-in-cache flag
312
* Added metadata to `trace.Driver.OnInvoke` and `trace.Driver.OnNewStream` done events

internal/table/client.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,9 @@ func (c *Client) internalPoolGet(ctx context.Context, opts ...getOption) (s *ses
323323
}
324324

325325
var (
326-
i = 0
327-
o = getOptions{t: c.config.Trace()}
326+
start = time.Now()
327+
i = 0
328+
o = getOptions{t: c.config.Trace()}
328329
)
329330
for _, opt := range opts {
330331
opt(&o)
@@ -375,11 +376,24 @@ func (c *Client) internalPoolGet(ctx context.Context, opts ...getOption) (s *ses
375376
}
376377
}
377378
if s == nil && err == nil {
378-
err = xerrors.WithStackTrace(fmt.Errorf("%w: attempts=%d", errNoProgress, i))
379+
err = xerrors.WithStackTrace(errNoProgress)
379380
}
380381
if err != nil {
382+
var (
383+
index int
384+
idle int
385+
createInProgress int
386+
)
387+
c.mu.WithLock(func() {
388+
index = len(c.index)
389+
idle = c.idle.Len()
390+
createInProgress = c.createInProgress
391+
})
381392
return s, xerrors.WithStackTrace(
382-
fmt.Errorf("%w: attempts=%d", err, i),
393+
fmt.Errorf("failed to get session from pool ("+
394+
"attempts: %d, latency: %v, stats: {index: %d, idle: %d, create_in_progress: %d}"+
395+
"): %w", i, time.Since(start), index, idle, createInProgress, err,
396+
),
383397
)
384398
}
385399
return s, nil

internal/table/params.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package table
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"sort"
7+
8+
"github.com/ydb-platform/ydb-go-sdk/v3/table"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
10+
)
11+
12+
// GenerateDeclareSection generates DECLARE section text in YQL query by params
13+
//
14+
// Warning: This is an experimental feature and could change at any time
15+
func GenerateDeclareSection(params *table.QueryParameters) string {
16+
var (
17+
buf bytes.Buffer
18+
names []string
19+
declares = make(map[string]string, len(params.Params()))
20+
)
21+
params.Each(func(name string, v types.Value) {
22+
names = append(names, name)
23+
declares[name] = fmt.Sprintf(
24+
"DECLARE %s AS %s;\n",
25+
name,
26+
v.Type().String(),
27+
)
28+
})
29+
sort.Strings(names)
30+
for _, name := range names {
31+
buf.WriteString(declares[name])
32+
}
33+
return buf.String()
34+
}

internal/table/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ func (s *session) Execute(
666666
optsResult.QueryCachePolicy.GetKeepInCache(),
667667
)
668668
defer func() {
669-
onDone(txr, true, r, err)
669+
onDone(txr, false, r, err)
670670
}()
671671

672672
request, result, err := s.executeDataQuery(ctx, tx, q, params, opts...)

internal/table/transaction.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ func (tx *transaction) Execute(
4646
query string, params *table.QueryParameters,
4747
opts ...options.ExecuteDataQueryOption,
4848
) (r result.Result, err error) {
49+
q := new(dataQuery)
50+
q.initFromText(query)
51+
52+
if params == nil {
53+
params = table.NewQueryParameters()
54+
}
55+
var optsResult options.ExecuteDataQueryDesc
56+
for _, f := range opts {
57+
f(&optsResult)
58+
}
59+
onDone := trace.TableOnSessionTransactionExecute(
60+
tx.s.config.Trace(),
61+
&ctx,
62+
tx.s,
63+
tx,
64+
q,
65+
params,
66+
optsResult.QueryCachePolicy.GetKeepInCache(),
67+
)
68+
defer func() {
69+
onDone(r, err)
70+
}()
4971
_, r, err = tx.s.Execute(ctx, tx.txc(), query, params, opts...)
5072
return
5173
}
@@ -56,6 +78,23 @@ func (tx *transaction) ExecuteStatement(
5678
stmt table.Statement, params *table.QueryParameters,
5779
opts ...options.ExecuteDataQueryOption,
5880
) (r result.Result, err error) {
81+
if params == nil {
82+
params = table.NewQueryParameters()
83+
}
84+
var optsResult options.ExecuteDataQueryDesc
85+
for _, f := range opts {
86+
f(&optsResult)
87+
}
88+
onDone := trace.TableOnSessionTransactionExecuteStatement(
89+
tx.s.config.Trace(),
90+
&ctx,
91+
tx.s,
92+
tx,
93+
params,
94+
)
95+
defer func() {
96+
onDone(r, err)
97+
}()
5998
_, r, err = stmt.Execute(ctx, tx.txc(), params, opts...)
6099
return
61100
}

internal/xsql/badconn/badconn_go1.18.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type badConnError struct {
1515
}
1616

1717
func (e badConnError) Error() string {
18-
return "ydbsql: bad connection: " + e.err.Error()
18+
return e.err.Error()
1919
}
2020

2121
func (e badConnError) Is(err error) bool {

0 commit comments

Comments
 (0)