Skip to content

Commit 9926384

Browse files
committed
move SessionStatus from options package to table
1 parent c6ab847 commit 9926384

File tree

11 files changed

+50
-58
lines changed

11 files changed

+50
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
* Marked the truncated result as retryable error
22
* Added closing sessions if node removed from discovery results
3+
* Moved session status type from `table/options` package to `table`
4+
* Changed session status source type from `uint32` to `string` alias
35

46
## v3.37.6
57
* Added to balancer notifying mechanism for listening in table client event about removing some nodes and closing sessions on them

internal/table/client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
2020
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/table"
22-
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
2322
"github.com/ydb-platform/ydb-go-sdk/v3/testutil/timeutil"
2423
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
2524
)
@@ -134,10 +133,10 @@ func (c *Client) updateNodes(ctx context.Context, endpoints []endpoint.Info) {
134133
return nodeIDs[i] >= nodeID
135134
}) == len(nodeIDs) {
136135
for s := range c.nodes[nodeID] {
137-
if c.index[s].idle != nil {
136+
if info, has := c.index[s]; has && info.idle != nil {
138137
c.internalPoolAsyncCloseSession(ctx, s)
139138
} else {
140-
s.SetStatus(options.SessionClosing)
139+
s.SetStatus(table.SessionClosing)
141140
}
142141
}
143142
}
@@ -632,7 +631,7 @@ func (c *Client) Close(ctx context.Context) (err error) {
632631
for e := c.idle.Front(); e != nil; e = e.Next() {
633632
wg.Add(1)
634633
s := e.Value.(*session)
635-
s.SetStatus(options.SessionClosing)
634+
s.SetStatus(table.SessionClosing)
636635
go func() {
637636
defer wg.Done()
638637
c.internalPoolSyncCloseSession(ctx, s)
@@ -806,7 +805,7 @@ func (c *Client) internalPoolNotify(s *session) (notified bool) {
806805
}
807806

808807
func (c *Client) internalPoolAsyncCloseSession(ctx context.Context, s *session) {
809-
s.SetStatus(options.SessionClosing)
808+
s.SetStatus(table.SessionClosing)
810809
c.spawnedGoroutines.Add(1)
811810
go func() {
812811
defer c.spawnedGoroutines.Done()

internal/table/retry_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xrand"
1616
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
1717
"github.com/ydb-platform/ydb-go-sdk/v3/table"
18-
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
1918
"github.com/ydb-platform/ydb-go-sdk/v3/testutil"
2019
)
2120

@@ -154,7 +153,7 @@ func TestRetryerSessionClosing(t *testing.T) {
154153
config.New(),
155154
func(ctx context.Context, s table.Session) error {
156155
sessions = append(sessions, s)
157-
s.(*session).SetStatus(options.SessionClosing)
156+
s.(*session).SetStatus(table.SessionClosing)
158157
return nil
159158
},
160159
table.Options{},

internal/table/session.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ type session struct {
4646
tableService Ydb_Table_V1.TableServiceClient
4747
config config.Config
4848

49-
status options.SessionStatus
50-
nodeID uint32
49+
status table.SessionStatus
50+
statusMtx sync.RWMutex
51+
nodeID uint32
5152

5253
onClose []func(s *session)
5354
closeOnce sync.Once
@@ -72,23 +73,27 @@ func (s *session) NodeID() uint32 {
7273
return uint32(nodeID)
7374
}
7475

75-
func (s *session) Status() string {
76+
func (s *session) Status() table.SessionStatus {
7677
if s == nil {
77-
return ""
78+
return table.SessionStatusUnknown
7879
}
79-
return options.SessionStatus(atomic.LoadUint32((*uint32)(&s.status))).String()
80+
s.statusMtx.RLock()
81+
defer s.statusMtx.RUnlock()
82+
return s.status
8083
}
8184

82-
func (s *session) SetStatus(status options.SessionStatus) {
83-
atomic.StoreUint32((*uint32)(&s.status), uint32(status))
85+
func (s *session) SetStatus(status table.SessionStatus) {
86+
s.statusMtx.Lock()
87+
defer s.statusMtx.Unlock()
88+
s.status = status
8489
}
8590

8691
func (s *session) isClosed() bool {
87-
return options.SessionStatus(atomic.LoadUint32((*uint32)(&s.status))) == options.SessionClosed
92+
return s.Status() == table.SessionClosed
8893
}
8994

9095
func (s *session) isClosing() bool {
91-
return options.SessionStatus(atomic.LoadUint32((*uint32)(&s.status))) == options.SessionClosing
96+
return s.Status() == table.SessionClosing
9297
}
9398

9499
func newSession(ctx context.Context, cc grpc.ClientConnInterface, config config.Config, opts ...sessionBuilderOption) (
@@ -129,7 +134,7 @@ func newSession(ctx context.Context, cc grpc.ClientConnInterface, config config.
129134
id: result.GetSessionId(),
130135
tableService: c,
131136
config: config,
132-
status: options.SessionReady,
137+
status: table.SessionReady,
133138
}
134139

135140
for _, o := range opts {
@@ -160,7 +165,7 @@ func (s *session) Close(ctx context.Context) (err error) {
160165

161166
s.closeOnce.Do(func() {
162167
defer func() {
163-
s.SetStatus(options.SessionClosed)
168+
s.SetStatus(table.SessionClosed)
164169
}()
165170

166171
onDone := trace.TableOnSessionDelete(s.config.Trace(), &ctx, s)
@@ -232,9 +237,9 @@ func (s *session) KeepAlive(ctx context.Context) (err error) {
232237
}
233238
switch result.SessionStatus {
234239
case Ydb_Table.KeepAliveResult_SESSION_STATUS_READY:
235-
s.SetStatus(options.SessionReady)
240+
s.SetStatus(table.SessionReady)
236241
case Ydb_Table.KeepAliveResult_SESSION_STATUS_BUSY:
237-
s.SetStatus(options.SessionBusy)
242+
s.SetStatus(table.SessionBusy)
238243
}
239244
return nil
240245
}
@@ -447,7 +452,7 @@ func (s *session) checkError(err error) {
447452
return
448453
}
449454
if m := retry.Check(err); m.MustDeleteSession() {
450-
s.SetStatus(options.SessionClosing)
455+
s.SetStatus(table.SessionClosing)
451456
}
452457
}
453458

internal/table/session_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ func TestSessionKeepAlive(t *testing.T) {
6565
if err != nil {
6666
t.Fatal(err)
6767
}
68-
if s.Status() != options.SessionReady.String() {
69-
t.Fatalf("Result %v differ from, expectd %v", s.Status(), options.SessionReady.String())
68+
if s.Status() != table.SessionReady {
69+
t.Fatalf("Result %v differ from, expectd %v", s.Status(), table.SessionReady)
7070
}
7171

7272
status, e = Ydb_Table.KeepAliveResult_SESSION_STATUS_BUSY, nil
7373
err = s.KeepAlive(ctx)
7474
if err != nil {
7575
t.Fatal(err)
7676
}
77-
if s.Status() != options.SessionBusy.String() {
78-
t.Fatalf("Result %v differ from, expectd %v", s.Status(), options.SessionBusy.String())
77+
if s.Status() != table.SessionBusy {
78+
t.Fatalf("Result %v differ from, expectd %v", s.Status(), table.SessionBusy)
7979
}
8080
}
8181

internal/table/trailer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"google.golang.org/grpc/metadata"
66

77
"github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
8-
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/table"
99
)
1010

1111
type trailer struct {
@@ -29,7 +29,7 @@ func checkHintSessionClose(md metadata.MD) bool {
2929
func (t *trailer) processHints() {
3030
switch {
3131
case checkHintSessionClose(t.md):
32-
t.s.SetStatus(options.SessionClosing)
32+
t.s.SetStatus(table.SessionClosing)
3333
default:
3434
// pass
3535
}

internal/xsql/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (c *conn) isClosed() bool {
105105
if atomic.LoadUint32(&c.closed) == 1 {
106106
return true
107107
}
108-
if c.session.Status() != options.SessionReady.String() {
108+
if c.session.Status() != table.SessionReady {
109109
c.setClosed()
110110
return true
111111
}

table/options/models.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package options
22

33
import (
44
"bytes"
5+
"fmt"
56
"time"
67

78
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
@@ -12,33 +13,6 @@ import (
1213
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1314
)
1415

15-
type SessionStatus uint32
16-
17-
const (
18-
SessionStatusUnknown SessionStatus = iota
19-
SessionReady
20-
SessionBusy
21-
SessionClosing
22-
SessionClosed
23-
24-
statusUnknown = "unknown"
25-
)
26-
27-
func (s SessionStatus) String() string {
28-
switch s {
29-
case SessionReady:
30-
return "ready"
31-
case SessionBusy:
32-
return "busy"
33-
case SessionClosing:
34-
return "closing"
35-
case SessionClosed:
36-
return "closed"
37-
default:
38-
return statusUnknown
39-
}
40-
}
41-
4216
type Column struct {
4317
Name string
4418
Type types.Type
@@ -165,7 +139,7 @@ func (c ColumnFamilyCompression) String() string {
165139
case ColumnFamilyCompressionLZ4:
166140
return "lz4"
167141
default:
168-
return statusUnknown
142+
return fmt.Sprintf("unknown_column_family_compression_%d", c)
169143
}
170144
}
171145

table/table.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@ type Client interface {
6565
DoTx(ctx context.Context, op TxOperation, opts ...Option) error
6666
}
6767

68+
type SessionStatus = string
69+
70+
const (
71+
SessionStatusUnknown = SessionStatus("unknown")
72+
SessionReady = SessionStatus("ready")
73+
SessionBusy = SessionStatus("busy")
74+
SessionClosing = SessionStatus("closing")
75+
SessionClosed = SessionStatus("closed")
76+
)
77+
6878
type SessionInfo interface {
6979
ID() string
70-
Status() string
80+
Status() SessionStatus
7181
}
7282

7383
type Session interface {

table/table_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func testTable(t testing.TB) {
245245
if atomic.LoadUint32(&shutdowned) == 0 {
246246
return
247247
}
248-
if info.Session.Status() != options.SessionClosing.String() {
248+
if info.Session.Status() != table.SessionClosing {
249249
return
250250
}
251251
sessionsMtx.Lock()

0 commit comments

Comments
 (0)