-
Notifications
You must be signed in to change notification settings - Fork 218
/
info.go
94 lines (82 loc) · 1.74 KB
/
info.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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package binlog
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"config"
"xbase/sync2"
"github.com/xelabs/go-mysqlstack/xlog"
)
type info struct {
Binlog string `json:"binlog"`
Timestamp int64 `json:"gtid"`
}
// Info tuple.
type Info struct {
log *xlog.Log
file *os.File
binDir string
infoFile string
currTs sync2.AtomicInt64
currBin sync2.AtomicString
}
// NewInfo returns info tuple.
func NewInfo(log *xlog.Log, conf *config.BinlogConfig, fileName string) *Info {
return &Info{
log: log,
binDir: conf.LogDir,
infoFile: filepath.Join(conf.LogDir, fileName),
}
}
// Init used to init the relay.
func (inf *Info) Init() error {
f, err := os.OpenFile(inf.infoFile, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
inf.file = f
return nil
}
// Sync used to sync the ts to the relay file.
func (inf *Info) Sync(binlog string, ts int64) error {
info := &info{Binlog: binlog, Timestamp: ts}
jsons, err := info.MarshalJSON()
if err != nil {
return err
}
inf.file.Truncate(0)
_, err = inf.file.WriteAt(jsons, 0)
inf.currTs.Set(ts)
inf.currBin.Set(binlog)
return err
}
// ReadTs used to get the ts from the relay file.
func (inf *Info) ReadTs() (int64, error) {
info := &info{}
buf, err := ioutil.ReadFile(inf.infoFile)
if err != nil {
return 0, err
}
if len(buf) > 0 {
err = json.Unmarshal(buf, info)
if err != nil {
return 0, err
}
}
return info.Timestamp, nil
}
// Close used to close the file of relay.
func (inf *Info) Close() {
log := inf.log
inf.file.Sync()
inf.file.Close()
log.Info("info.close.last[binlog:%v, ts:%v]", inf.currBin.Get(), inf.currTs.Get())
}