This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablet_tx.go
162 lines (125 loc) · 3.4 KB
/
tablet_tx.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
package rpcclient
import (
"context"
"github.com/golang/protobuf/proto"
"go.ytsaurus.tech/library/go/core/xerrors"
"github.com/go-faster/yt/bus"
"github.com/go-faster/yt/yson"
"github.com/go-faster/yt/yt"
"github.com/go-faster/yt/yt/internal"
)
type TransactionOptions struct {
*yt.TransactionOptions
TxStartTimestamp yt.Timestamp
}
type TransactionalRequest interface {
SetTxOptions(opts *TransactionOptions)
}
type tabletTx struct {
Encoder
txID yt.TxID
txStartTimestamp yt.Timestamp
coordinator string
c *client
ctx context.Context
pinger *internal.Pinger
}
func (c *client) BeginTabletTx(
ctx context.Context,
opts *yt.StartTabletTxOptions,
) (yt.TabletTx, error) {
var tx tabletTx
var err error
tx.coordinator, err = c.pickRPCProxy(ctx)
if err != nil {
return nil, err
}
tx.StartCall = c.StartCall
tx.Invoke = tx.do
tx.InvokeReadRow = tx.doReadRow
tx.c = c
txTimeout := yson.Duration(c.conf.GetTxTimeout())
startOptions := &yt.StartTabletTxOptions{
Type: yt.TxTypeTablet,
Sticky: true,
Timeout: &txTimeout,
}
if opts != nil {
startOptions.Atomicity = opts.Atomicity
}
tx.txID, tx.txStartTimestamp, err = tx.startTabletTx(ctx, startOptions)
tx.ctx = ctx
tx.pinger = internal.NewPinger(ctx, &tx, tx.txID, c.conf, c.stop)
go tx.pinger.Run()
return &tx, err
}
func (tx *tabletTx) do(ctx context.Context, call *Call, rsp proto.Message, opts ...bus.SendOption) (err error) {
call.RequestedProxy = tx.coordinator
switch call.Method {
case MethodStartTransaction:
return tx.c.Invoke(ctx, call, rsp, opts...)
case MethodCommitTransaction:
err = tx.pinger.TryCommit(func() error {
return tx.c.Invoke(ctx, call, rsp, opts...)
})
return
case MethodAbortTransaction:
err = tx.pinger.TryAbort(func() error {
return tx.c.Invoke(ctx, call, rsp, opts...)
})
return
case MethodModifyRows:
if err = tx.pinger.CheckAlive(); err != nil {
return
}
return tx.doWriteRows(ctx, call, rsp, opts...)
default:
if err = tx.pinger.CheckAlive(); err != nil {
return
}
return tx.c.Invoke(ctx, call, rsp, opts...)
}
}
func (tx *tabletTx) setTxID(call *Call) error {
req, ok := call.Req.(TransactionalRequest)
if !ok {
return xerrors.Errorf("call %q is not transactional", call.Method)
}
opts := &TransactionOptions{
TransactionOptions: &yt.TransactionOptions{TransactionID: tx.ID()},
TxStartTimestamp: tx.txStartTimestamp,
}
req.SetTxOptions(opts)
return nil
}
func (tx *tabletTx) doReadRow(ctx context.Context, call *Call, rsp ProtoRowset) (r yt.TableReader, err error) {
if err = tx.pinger.CheckAlive(); err != nil {
return
}
call.RequestedProxy = tx.coordinator
call.DisableRetries = true
if err := tx.setTxID(call); err != nil {
return nil, err
}
return tx.c.InvokeReadRow(ctx, call, rsp)
}
func (tx *tabletTx) doWriteRows(ctx context.Context, call *Call, rsp proto.Message, opts ...bus.SendOption) (err error) {
if err = tx.pinger.CheckAlive(); err != nil {
return
}
call.RequestedProxy = tx.coordinator
call.DisableRetries = true
if err := tx.setTxID(call); err != nil {
return err
}
return tx.c.Invoke(ctx, call, rsp, opts...)
}
func (tx *tabletTx) ID() yt.TxID {
return tx.txID
}
func (tx *tabletTx) Commit() error {
return tx.CommitTx(tx.ctx, tx.txID, &yt.CommitTxOptions{Sticky: true})
}
func (tx *tabletTx) Abort() error {
return tx.AbortTx(tx.ctx, tx.txID, &yt.AbortTxOptions{Sticky: true})
}