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
124 lines (96 loc) · 2.67 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
package httpclient
import (
"context"
"github.com/go-faster/yt/yson"
"github.com/go-faster/yt/yt"
"github.com/go-faster/yt/yt/internal"
)
type tabletTx struct {
internal.Encoder
txID yt.TxID
coordinator string
c *httpClient
ctx context.Context
pinger *internal.Pinger
}
func (c *httpClient) BeginTabletTx(ctx context.Context, options *yt.StartTabletTxOptions) (yt.TabletTx, error) {
var tx tabletTx
var err error
tx.coordinator, err = c.pickHeavyProxy(ctx)
if err != nil {
return nil, err
}
tx.StartCall = c.StartCall
tx.Invoke = tx.do
tx.InvokeReadRow = tx.doReadRow
tx.InvokeWriteRow = tx.doWriteRow
tx.c = c
txTimeout := yson.Duration(c.config.GetTxTimeout())
startOptions := &yt.StartTabletTxOptions{
Type: yt.TxTypeTablet,
Sticky: true,
Timeout: &txTimeout,
}
if options != nil {
startOptions.Atomicity = options.Atomicity
}
tx.txID, err = tx.StartTabletTx(ctx, startOptions)
tx.ctx = ctx
tx.pinger = internal.NewPinger(ctx, &tx, tx.txID, c.config, c.stop)
go tx.pinger.Run()
return &tx, err
}
func (tx *tabletTx) setTx(call *internal.Call) {
call.DisableRetries = true
txOpts := call.Params.(internal.TransactionParams).TransactionOptions()
*txOpts = &yt.TransactionOptions{TransactionID: tx.txID}
}
func (tx *tabletTx) do(ctx context.Context, call *internal.Call) (res *internal.CallResult, err error) {
call.RequestedProxy = tx.coordinator
switch call.Params.HTTPVerb() {
case internal.VerbStartTransaction:
return tx.c.Invoke(ctx, call)
case internal.VerbCommitTransaction:
err = tx.pinger.TryCommit(func() error {
res, err = tx.c.Invoke(ctx, call)
return err
})
return
case internal.VerbAbortTransaction:
err = tx.pinger.TryAbort(func() error {
res, err = tx.c.Invoke(ctx, call)
return err
})
return
default:
if err = tx.pinger.CheckAlive(); err != nil {
return
}
return tx.c.Invoke(ctx, call)
}
}
func (tx *tabletTx) doReadRow(ctx context.Context, call *internal.Call) (r yt.TableReader, err error) {
if err = tx.pinger.CheckAlive(); err != nil {
return
}
call.RequestedProxy = tx.coordinator
tx.setTx(call)
return tx.c.InvokeReadRow(ctx, call)
}
func (tx *tabletTx) doWriteRow(ctx context.Context, call *internal.Call) (r yt.TableWriter, err error) {
if err = tx.pinger.CheckAlive(); err != nil {
return
}
call.RequestedProxy = tx.coordinator
tx.setTx(call)
return tx.c.InvokeWriteRow(ctx, call)
}
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})
}