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
/
transaction.go
175 lines (140 loc) · 4 KB
/
transaction.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
163
164
165
166
167
168
169
170
171
172
173
174
175
package internal
import (
"context"
"io"
"github.com/go-faster/errors"
"go.ytsaurus.tech/library/go/core/log"
"github.com/go-faster/yt/ypath"
"github.com/go-faster/yt/yson"
"github.com/go-faster/yt/yt"
"github.com/go-faster/yt/yt/internal/smartreader"
)
type TxInterceptor struct {
Encoder
Client Encoder
log log.Structured
pinger *Pinger
}
type TransactionParams interface {
TransactionOptions() **yt.TransactionOptions
}
func NewTx(
ctx context.Context,
e Encoder,
log log.Structured,
stop *StopGroup,
config *yt.Config,
options *yt.StartTxOptions,
) (yt.Tx, error) {
if options == nil {
options = &yt.StartTxOptions{}
}
updatedOptions := *options
txTimeout := yson.Duration(config.GetTxTimeout())
updatedOptions.Timeout = &txTimeout
txID, err := e.StartTx(ctx, &updatedOptions)
if err != nil {
return nil, err
}
tx := &TxInterceptor{
Encoder: e,
Client: e,
log: log,
pinger: NewPinger(ctx, &e, txID, config, stop),
}
tx.Encoder.Invoke = tx.Encoder.Invoke.Wrap(tx.Intercept)
tx.Encoder.InvokeRead = tx.Encoder.InvokeRead.Wrap(tx.Read)
tx.Encoder.InvokeWrite = tx.Encoder.InvokeWrite.Wrap(tx.Write)
tx.Encoder.InvokeReadRow = tx.Encoder.InvokeReadRow.Wrap(tx.ReadRow)
tx.Encoder.InvokeWriteRow = tx.Encoder.InvokeWriteRow.Wrap(tx.WriteRow)
if !stop.TryAdd() {
// In this rare event, leave tx running on the master.
return nil, errors.New("client is stopped")
}
go tx.pinger.Run()
return tx, nil
}
func (t *TxInterceptor) ID() yt.TxID {
return t.pinger.txID
}
func (t *TxInterceptor) Finished() <-chan struct{} {
return t.pinger.finished
}
func (t *TxInterceptor) BeginTx(ctx context.Context, options *yt.StartTxOptions) (tx yt.Tx, err error) {
if err = t.pinger.CheckAlive(); err != nil {
return
}
if options == nil {
options = &yt.StartTxOptions{}
}
if options.TransactionOptions == nil {
options.TransactionOptions = &yt.TransactionOptions{}
}
options.TransactionID = t.ID()
return NewTx(ctx, t.Client, t.log, t.pinger.stop, t.pinger.config, options)
}
func (t *TxInterceptor) Abort() (err error) {
return t.pinger.TryAbort(func() error {
return t.Client.AbortTx(t.pinger.ctx, t.pinger.txID, nil)
})
}
func (t *TxInterceptor) Commit() (err error) {
return t.pinger.TryCommit(func() error {
return t.Client.CommitTx(t.pinger.ctx, t.pinger.txID, nil)
})
}
func (t *TxInterceptor) setTx(call *Call) error {
if err := t.pinger.CheckAlive(); err != nil {
return err
}
params, ok := call.Params.(TransactionParams)
if !ok {
return errors.Errorf("call %s is not transactional", call.Params.HTTPVerb())
}
txOpts := params.TransactionOptions()
*txOpts = &yt.TransactionOptions{TransactionID: t.pinger.txID}
return nil
}
func (t *TxInterceptor) Intercept(ctx context.Context, call *Call, next CallInvoker) (res *CallResult, err error) {
if err = t.setTx(call); err != nil {
return
}
return next(ctx, call)
}
func (t *TxInterceptor) Read(ctx context.Context, call *Call, next ReadInvoker) (r io.ReadCloser, err error) {
if err = t.setTx(call); err != nil {
return
}
return next(ctx, call)
}
func (t *TxInterceptor) Write(ctx context.Context, call *Call, next WriteInvoker) (w io.WriteCloser, err error) {
if err = t.setTx(call); err != nil {
return
}
return next(ctx, call)
}
func (t *TxInterceptor) ReadRow(ctx context.Context, call *Call, next ReadRowInvoker) (r yt.TableReader, err error) {
if err = t.setTx(call); err != nil {
return
}
return next(ctx, call)
}
func (t *TxInterceptor) WriteRow(ctx context.Context, call *Call, next WriteRowInvoker) (w yt.TableWriter, err error) {
if err = t.setTx(call); err != nil {
return
}
return next(ctx, call)
}
func (t *TxInterceptor) ReadTable(
ctx context.Context,
path ypath.YPath,
options *yt.ReadTableOptions,
) (r yt.TableReader, err error) {
if options != nil && options.Smart != nil && *options.Smart && !options.Unordered {
opts := *options
opts.Smart = nil
return smartreader.NewReader(ctx, t, false, t.log, path, &opts)
} else {
return t.Encoder.ReadTable(ctx, path, options)
}
}