Skip to content

Commit 9d289d5

Browse files
authored
Merge pull request #1608 from ydb-platform/1607-bad-session-on-error
1607 bad session on error
2 parents 14fe684 + ebc6748 commit 9d289d5

File tree

6 files changed

+58
-19
lines changed

6 files changed

+58
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed drop session from pool unnecessary in query service
2+
13
## v3.96.0
24
* Supported of list, set and struct for unmarshall using `sugar.Unmarshall...`
35

internal/query/client.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ func do(
212212

213213
err := op(ctx, s)
214214
if err != nil {
215-
s.SetStatus(session.StatusError)
216-
217215
return xerrors.WithStackTrace(err)
218216
}
219217

@@ -276,10 +274,6 @@ func doTx(
276274

277275
defer func() {
278276
_ = tx.Rollback(ctx)
279-
280-
if opErr != nil {
281-
s.SetStatus(session.StatusError)
282-
}
283277
}()
284278

285279
err = op(ctx, tx)
@@ -539,7 +533,7 @@ func CreateSession(ctx context.Context, c *Client) (*Session, error) {
539533
return nil, xerrors.WithStackTrace(err)
540534
}
541535

542-
s.laztTx = c.config.LazyTx()
536+
s.lazyTx = c.config.LazyTx()
543537

544538
return s, nil
545539
})
@@ -590,7 +584,7 @@ func New(ctx context.Context, cc grpc.ClientConnInterface, cfg *config.Config) *
590584
return nil, xerrors.WithStackTrace(err)
591585
}
592586

593-
s.laztTx = cfg.LazyTx()
587+
s.lazyTx = cfg.LazyTx()
594588

595589
return s, nil
596590
}),

internal/query/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ func newTestSessionWithClient(id string, client Ydb_Query_V1.QueryServiceClient,
15801580
Core: &sessionControllerMock{id: id},
15811581
client: client,
15821582
trace: &trace.Query{},
1583-
laztTx: lazyTx,
1583+
lazyTx: lazyTx,
15841584
}
15851585
}
15861586

internal/query/session.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
7+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
78

89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
@@ -23,7 +24,7 @@ type (
2324

2425
client Ydb_Query_V1.QueryServiceClient
2526
trace *trace.Query
26-
laztTx bool
27+
lazyTx bool
2728
}
2829
)
2930

@@ -38,6 +39,8 @@ func (s *Session) QueryResultSet(
3839

3940
r, err := execute(ctx, s.ID(), s.client, q, options.ExecuteSettings(opts...), withTrace(s.trace))
4041
if err != nil {
42+
s.setStatusFromError(err)
43+
4144
return nil, xerrors.WithStackTrace(err)
4245
}
4346

@@ -54,6 +57,8 @@ func (s *Session) queryRow(
5457
) (row query.Row, finalErr error) {
5558
r, err := execute(ctx, s.ID(), s.client, q, settings, resultOpts...)
5659
if err != nil {
60+
s.setStatusFromError(err)
61+
5762
return nil, xerrors.WithStackTrace(err)
5863
}
5964

@@ -111,7 +116,7 @@ func (s *Session) Begin(
111116
}
112117
}()
113118

114-
if s.laztTx {
119+
if s.lazyTx {
115120
return &Transaction{
116121
s: s,
117122
txSettings: txSettings,
@@ -120,6 +125,8 @@ func (s *Session) Begin(
120125

121126
txID, err := begin(ctx, s.client, s.ID(), txSettings)
122127
if err != nil {
128+
s.setStatusFromError(err)
129+
123130
return nil, xerrors.WithStackTrace(err)
124131
}
125132

@@ -140,6 +147,8 @@ func (s *Session) Exec(
140147

141148
r, err := execute(ctx, s.ID(), s.client, q, options.ExecuteSettings(opts...), withTrace(s.trace))
142149
if err != nil {
150+
s.setStatusFromError(err)
151+
143152
return xerrors.WithStackTrace(err)
144153
}
145154

@@ -162,8 +171,21 @@ func (s *Session) Query(
162171

163172
r, err := execute(ctx, s.ID(), s.client, q, options.ExecuteSettings(opts...), withTrace(s.trace))
164173
if err != nil {
174+
s.setStatusFromError(err)
175+
165176
return nil, xerrors.WithStackTrace(err)
166177
}
167178

168179
return r, nil
169180
}
181+
182+
func (s *Session) setStatusFromError(err error) {
183+
switch {
184+
case xerrors.IsTransportError(err):
185+
s.SetStatus(session.StatusError)
186+
case xerrors.IsOperationError(err, Ydb.StatusIds_SESSION_BUSY, Ydb.StatusIds_BAD_SESSION):
187+
s.SetStatus(session.StatusError)
188+
case xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION):
189+
s.SetStatus(session.StatusClosed)
190+
}
191+
}

internal/query/transaction.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"fmt"
66

77
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
8-
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
98
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
109

1110
"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
1211
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
14-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/session"
1513
queryTx "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/tx"
1614
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1715
baseTx "github.com/ydb-platform/ydb-go-sdk/v3/internal/tx"
@@ -116,6 +114,8 @@ func (tx *Transaction) QueryResultSet(
116114
}
117115
r, err := execute(ctx, tx.s.ID(), tx.s.client, q, settings, resultOpts...)
118116
if err != nil {
117+
tx.s.setStatusFromError(err)
118+
119119
return nil, xerrors.WithStackTrace(err)
120120
}
121121

@@ -165,6 +165,8 @@ func (tx *Transaction) QueryRow(
165165
}
166166
r, err := execute(ctx, tx.s.ID(), tx.s.client, q, settings, resultOpts...)
167167
if err != nil {
168+
tx.s.setStatusFromError(err)
169+
168170
return nil, xerrors.WithStackTrace(err)
169171
}
170172

@@ -231,6 +233,8 @@ func (tx *Transaction) Exec(ctx context.Context, q string, opts ...options.Execu
231233

232234
r, err := execute(ctx, tx.s.ID(), tx.s.client, q, settings, resultOpts...)
233235
if err != nil {
236+
tx.s.setStatusFromError(err)
237+
234238
return xerrors.WithStackTrace(err)
235239
}
236240

@@ -299,6 +303,8 @@ func (tx *Transaction) Query(ctx context.Context, q string, opts ...options.Exec
299303
}
300304
r, err := execute(ctx, tx.s.ID(), tx.s.client, q, settings, resultOpts...)
301305
if err != nil {
306+
tx.s.setStatusFromError(err)
307+
302308
return nil, xerrors.WithStackTrace(err)
303309
}
304310

@@ -338,9 +344,7 @@ func (tx *Transaction) CommitTx(ctx context.Context) (finalErr error) {
338344

339345
err = commitTx(ctx, tx.s.client, tx.s.ID(), tx.ID())
340346
if err != nil {
341-
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
342-
tx.s.SetStatus(session.StatusClosed)
343-
}
347+
tx.s.setStatusFromError(err)
344348

345349
return xerrors.WithStackTrace(err)
346350
}
@@ -376,9 +380,7 @@ func (tx *Transaction) Rollback(ctx context.Context) (finalErr error) {
376380

377381
err := rollback(ctx, tx.s.client, tx.s.ID(), tx.ID())
378382
if err != nil {
379-
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
380-
tx.s.SetStatus(session.StatusClosed)
381-
}
383+
tx.s.setStatusFromError(err)
382384

383385
return xerrors.WithStackTrace(err)
384386
}

tests/integration/query_regression_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,22 @@ func TestReadTwoPartsIntoMemoryIssue1559(t *testing.T) {
376376
require.Equal(t, targetCount, len(rows))
377377
require.Greater(t, partReaded, 1)
378378
}
379+
380+
// https://github.com/ydb-platform/ydb-go-sdk/issues/1607
381+
func TestCloseSessionOnCustomerErrorsIssue1607(t *testing.T) {
382+
scope := newScope(t)
383+
384+
sessionID1 := ""
385+
_ = scope.Driver().Query().Do(scope.Ctx, func(ctx context.Context, s query.Session) error {
386+
sessionID1 = s.ID()
387+
return errors.New("test")
388+
})
389+
390+
sessionID2 := ""
391+
_ = scope.Driver().Query().Do(scope.Ctx, func(ctx context.Context, s query.Session) error {
392+
sessionID2 = s.ID()
393+
return nil
394+
})
395+
396+
scope.Require.Equal(sessionID1, sessionID2)
397+
}

0 commit comments

Comments
 (0)