-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathxacheck.go
More file actions
387 lines (332 loc) · 9.68 KB
/
xacheck.go
File metadata and controls
387 lines (332 loc) · 9.68 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package backend
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"sync"
"time"
"config"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/xlog"
)
const (
xacheckJSONFile = "xacheck.json"
xacheckTimesOutJSONFile = "xacheck_timesout.json"
)
const (
txnXACommitErrStateCommit = "commit"
txnXACommitErrStateRollback = "rollback"
)
// XaCommitErr tuple.
type XaCommitErr struct {
Time string `json:"time"`
Xaid string `json:"xaid"`
State string `json:"state"`
Times int `json:"times"`
}
// XaCommitErrs tuple
type XaCommitErrs struct {
Logs []*XaCommitErr `json:"xacommit-errs"`
}
// XaCheck tuple.
type XaCheck struct {
log *xlog.Log
dir string
times int
scatter *Scatter
retrys map[string]*XaCommitErr
done chan bool
ticker *time.Ticker
wg sync.WaitGroup
mu sync.RWMutex
}
// NewXaCheck creates the XaCheck tuple.
func NewXaCheck(scatter *Scatter, conf *config.ScatterConfig) *XaCheck {
return &XaCheck{
log: scatter.log,
dir: conf.XaCheckDir,
times: conf.XaCheckRetrys,
scatter: scatter,
retrys: make(map[string]*XaCommitErr),
done: make(chan bool),
ticker: time.NewTicker(time.Duration(time.Second * time.Duration(conf.XaCheckInterval))),
}
}
// Init used to init xa check goroutine.
func (xc *XaCheck) Init() error {
log := xc.log
// If the xc.dir is already a directory, MkdirAll does nothing
// if the dir is one file, return err
if err := os.MkdirAll(xc.dir, 0744); err != nil {
return err
}
if err := xc.LoadXaCommitErrLogs(); err != nil {
return err
}
xc.wg.Add(1)
go func(dc *XaCheck) {
defer dc.wg.Done()
dc.xaCommitCheck()
}(xc)
log.Info("xacheck.init.done")
return nil
}
func writeAppendFile(file string, data []byte) error {
flag := os.O_RDWR | os.O_APPEND
if _, err := os.Stat(file); os.IsNotExist(err) {
flag |= os.O_CREATE
}
f, err := os.OpenFile(file, flag, 0644)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
n, err := f.Write(data)
if err != nil {
return errors.WithStack(err)
}
if n != len(data) {
return errors.WithStack(io.ErrShortWrite)
}
return f.Sync()
}
func (xc *XaCheck) addXaCommitErrLog(new *XaCommitErr) error {
log := xc.log
log.Info("xc.addXaCommitErrLog.add:+%v", new)
if _, ok := xc.retrys[new.Xaid]; ok {
log.Error("xacheck.addXACommitErrLog.xaid[%v].duplicate", new)
return errors.Errorf("xacheck.addXACommitErrLog.xaid[%v].duplicate", new.Xaid)
}
xc.retrys[new.Xaid] = new
return nil
}
// flushXaCommitErrLog is used to write the xaCommitErrlogs to the file.
func (xc *XaCheck) flushXaCommitErrLog() error {
log := xc.log
file := path.Join(xc.dir, xacheckJSONFile)
// stored in the way of the array
var xaCommitErrs XaCommitErrs
for _, v := range xc.retrys {
xaCommitErrs.Logs = append(xaCommitErrs.Logs, v)
}
log.Info("xacheck.flush.to.file[%v].xaCommitErrs:%+v", file, xaCommitErrs)
if err := config.WriteConfig(file, xaCommitErrs); err != nil {
log.Panicf("xacheck.flush.config.to.file[%v].error:%v", file, err)
return err
}
return nil
}
// WriteXaCommitErrLog is used to write the xaCommitErrLog into the xacheck file.
func (xc *XaCheck) WriteXaCommitErrLog(txn *Txn, state string) error {
xaCommitErr := &XaCommitErr{
Time: time.Now().Format("20060102150405"),
Xaid: txn.xid,
State: state,
Times: xc.times,
}
xc.mu.Lock()
defer xc.mu.Unlock()
// add the xaCommitErrLog to xacheck
if err := xc.addXaCommitErrLog(xaCommitErr); err != nil {
return errors.WithStack(err)
}
// TODO if the Radon crash at the moment.
// flush the mem to the file
if err := xc.flushXaCommitErrLog(); err != nil {
return errors.WithStack(err)
}
return nil
}
// commitRetryBackends
// 1st. xa recover to
// 1. check all backends are ready, or else return err
// 2. check the xid is valid to some backends which need the 2nd command, and set needCommitBackends
// 2nd. xa commit/rollback 'xaid' to the Backends which need commit
func (xc *XaCheck) commitRetryBackends(retry *XaCommitErr, scatter *Scatter) (bool, error) {
backends := scatter.AllBackends()
log := xc.log
// if the backend is empty, output error log.
if len(backends) == 0 {
log.Error("xacheck.commitRetryBackends.backend.empty.")
return false, errors.New("xacheck.backend.empty")
}
txn, err := scatter.CreateTransaction()
if err != nil {
log.Error("xacheck.commitRetryBackends.create.transaction.error:[%v]", err)
return false, err
}
defer txn.Finish()
query := fmt.Sprintf("xa %s '%s' ", retry.State, retry.Xaid)
// the 1st stage: xa recover
xaRecoverQuery := "xa recover"
var needCommitBackends []string
// if one backend return the err, the backend may not be ready, will return,
// because all backends are ready,the needCommitBackends is valuable, or else it is misleading.
for _, backend := range backends {
result, err := txn.ExecuteOnThisBackend(backend, xaRecoverQuery)
if err != nil {
log.Warning("xacheck.commitRetryBackends.xa.recover.error:[%v]", err)
return false, err
}
if result != nil && result.RowsAffected > 0 && len(result.Fields) == 4 {
for _, row := range result.Rows {
// just find the xaid in the row from the cmd of 'xa recover'
valStr := string(row[3].Raw())
if strings.EqualFold(valStr, retry.Xaid) {
log.Info("xacheck.commitRetryBackends.recover.query[%v].needCommitBackend[%v]", query, backend)
needCommitBackends = append(needCommitBackends, backend)
}
}
}
}
if len(needCommitBackends) == 0 {
log.Info("xacheck.commitRetryBackends.recover.query[%v].find.no.Backends.need.retry.[%v]", query, retry.Times)
}
// the 2nd stage: xa commit/rollback '$xid'
count := 0
for _, backend := range needCommitBackends {
_, err = txn.ExecuteOnThisBackend(backend, query)
if err == nil {
log.Info("xacheck.commitRetryBackends.query[%v].success.backend[%v]", query, backend)
count++
} else {
log.Warning("xacheck.commitRetryBackends.query[%v].backend[%v].error[%T]:%+v", query, backend, err, err)
}
}
// if there are XA need to retry, and the count committed is equal to the needCommitBackends, return true.
if count > 0 && count == len(needCommitBackends) {
return true, nil
}
if retry.Times <= 0 {
log.Warning("xacheck.commitRetryBackends.query[%v].retry.times.out[%v]", query, retry.Times)
data, err := json.Marshal(retry)
if err != nil {
log.Error("xacheck.Marshal.error:[%v]", err)
return false, err
}
file := path.Join(xc.dir, xacheckTimesOutJSONFile)
if err := writeAppendFile(file, data); err != nil {
log.Panicf("xacheck.flush.config.to.file[%v].error:%v", file, err)
return false, err
}
return true, nil
} else {
retry.Times--
}
return false, nil
}
// the retrys map maybe subtract the retry which is committed when all backends are ready
// or has retried for the number of times but unsuccessfully.
func (xc *XaCheck) xaCommitsRetryMain() error {
log := xc.log
retrys := xc.retrys
if xc.getRetrysLen() > 0 {
log.Info("xacheck.commit.retry %v.", retrys)
}
for _, retry := range retrys {
committedOrTimesout, err := xc.commitRetryBackends(retry, xc.scatter)
if err != nil {
log.Warning("xacheck.commits.retry failed.")
return err
}
if committedOrTimesout {
// every retry is committed, update the mem and flush to the file
delete(xc.retrys, retry.Xaid)
if err := xc.flushXaCommitErrLog(); err != nil {
return errors.WithStack(err)
}
}
}
return nil
}
func (xc *XaCheck) xaCommitsRetry() error {
xc.mu.Lock()
defer xc.mu.Unlock()
// xaCommitsRetryMain
if err := xc.xaCommitsRetryMain(); err != nil {
return err
}
return nil
}
func (xc *XaCheck) xaCommitCheck() {
defer xc.ticker.Stop()
for {
select {
case <-xc.ticker.C:
xc.xaCommitsRetry()
case <-xc.done:
return
}
}
}
// ReadXaCommitErrLogs is used to read the Xaredologs config from the data.
func (xc *XaCheck) ReadXaCommitErrLogs(data string) (*XaCommitErrs, error) {
s := &XaCommitErrs{}
if err := json.Unmarshal([]byte(data), s); err != nil {
return nil, errors.WithStack(err)
}
return s, nil
}
// LoadXaCommitErrLogs is used to load all XaCommitErr from metadir/xacheck.json file.
func (xc *XaCheck) LoadXaCommitErrLogs() error {
log := xc.log
metadir := xc.dir
file := path.Join(metadir, xacheckJSONFile)
if _, err := os.Stat(file); os.IsNotExist(err) {
// not Creating it if the xacheck log doesn't exist.
// to avoid creating the empty file xaredolog.json about "xaredologs": null
// the xaredolog.json will be created when the xaredolog are generated.
return nil
}
data, err := ioutil.ReadFile(file)
if err != nil {
log.Error("xacheck.LoadXaCommitErrLogs.readfile[%v].error:%v", file, err)
return err
}
retrys, err := xc.ReadXaCommitErrLogs(string(data))
if err != nil {
log.Error("xacheck.LoadXaCommitErrLogs.readfile.to.xacheck[%v].error:%v", file, err)
return err
}
for _, new := range retrys.Logs {
if err := xc.addXaCommitErrLog(new); err != nil {
return err
}
log.Info("xacheck.load.xaid:%+v", new.Xaid)
}
return nil
}
// Close is used to close the xacheck goroutine
func (xc *XaCheck) Close() {
close(xc.done)
xc.wg.Wait()
}
// GetXaCheckFile get the XaCheck log file
func (xc *XaCheck) GetXaCheckFile() string {
return path.Join(xc.dir, xacheckJSONFile)
}
// RemoveXaCommitErrLogs is only used to test to avoid the noise,
// XaCommitErrLogs can not be removed in the production environment, it is so important.
func (xc *XaCheck) RemoveXaCommitErrLogs() error {
return os.RemoveAll(xc.dir)
}
func (xc *XaCheck) getRetrysLen() int {
return len(xc.retrys)
}
// GetRetrysLen return the retrys num
func (xc *XaCheck) GetRetrysLen() int {
xc.mu.Lock()
defer xc.mu.Unlock()
return len(xc.retrys)
}