-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
252 lines (215 loc) · 6.09 KB
/
syncer.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
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
package syncer
import (
"errors"
"fmt"
"math"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
"github.com/Rayson9418/cdc/common"
. "github.com/Rayson9418/cdc/logger"
"github.com/Rayson9418/cdc/store"
)
const kSyncerPosFmt = "%d:%d:%d"
type DataSyncInterface interface {
store.SyncerPosInterface
// Name define the syncer name
Name() string
// SetPosStore set position store
SetPosStore(store.SyncerPosInterface)
// QueryCount function for total data count retrieval
QueryCount(start, end time.Time) (int64, error)
// QueryData function for data retrieval
QueryData(offset, limit int, start, end time.Time) (interface{}, int64, error)
// Sink function for data synchronization logic
Sink(data interface{}) error
// InitData function for initialization logic before data synchronization logic
InitData() error
// InitialPos for the position during the initial synchronization
InitialPos() (int64, int64, int64)
// NextPos for the position of the next data synchronization
NextPos(start, end, pos int64) (int64, int64, int64)
// Interval for the synchronization interval
Interval() error
}
type DummySyncer struct {
SyncerName string
store.SyncerPosInterface
}
func (s *DummySyncer) Name() string {
return s.SyncerName
}
func (s *DummySyncer) SetPosStore(store store.SyncerPosInterface) {
s.SyncerPosInterface = store
}
func (s *DummySyncer) QueryCount(start, end time.Time) (int64, error) {
return 0, nil
}
func (s *DummySyncer) QueryData(offset, limit int, start, end time.Time) (interface{}, int64, error) {
return 0, 0, nil
}
func (s *DummySyncer) Sink(interface{}) error {
return nil
}
func (s *DummySyncer) InitData() error {
return nil
}
func (s *DummySyncer) InitialPos() (int64, int64, int64) {
return 0, 0, 0
}
func (s *DummySyncer) NextPos(start, end, pos int64) (int64, int64, int64) {
return start, end, pos
}
func (s *DummySyncer) Interval() error {
return nil
}
func StartSyncer(syncers ...DataSyncInterface) error {
errChn := make(chan error, len(syncers))
for _, s := range syncers {
go func(s DataSyncInterface) {
if err := syncAlways(s); err != nil {
errChn <- err
}
}(s)
}
for err := range errChn {
return err
}
return nil
}
func StartSyncerOnTime(syncers ...DataSyncInterface) error {
errChn := make(chan error, len(syncers))
for _, s := range syncers {
go func(s DataSyncInterface) {
if err := syncOnTime(s); err != nil {
errChn <- err
}
}(s)
}
for err := range errChn {
return err
}
return nil
}
// SyncOnce to immediately execute the synchronization logic once
func SyncOnce(s DataSyncInterface) error {
return syncOnce(s)
}
func syncAlways(s DataSyncInterface) error {
for {
if err := syncOnce(s); err != nil {
return err
}
// Wait for the next synchronization cycle
if err := s.Interval(); err != nil {
return err
}
}
}
func syncOnTime(s DataSyncInterface) error {
for {
hour := time.Now().Hour()
if !(hour >= opt.StartHour && hour <= opt.EndHour) {
time.Sleep(time.Duration(opt.Interval) * time.Hour)
continue
}
if err := syncOnce(s); err != nil {
return err
}
// Wait for the next synchronization cycle
if err := s.Interval(); err != nil {
return err
}
}
}
func syncOnce(s DataSyncInterface) error {
Logger.Info("start syncer", zap.String("name", s.Name()))
syncerPos, err := s.Pos()
if err != nil {
if !errors.Is(err, mongo.ErrNoDocuments) {
Logger.Warn("query latest sync time err", zap.Error(err))
return err
}
// Execute initialization function during the initial synchronization
if err = s.InitData(); err != nil {
Logger.Error("init data failed", zap.String("sync_name", s.Name()))
return err
}
// Obtain the position of the first execution
syncerStartTimestamp, syncEndTimestamp, pos := s.InitialPos()
syncerPos = &store.SyncerPos{
SyncStartTimestamp: syncerStartTimestamp,
SyncEndTimestamp: syncEndTimestamp,
Pos: pos,
}
}
if common.IsToday(syncerPos.LastSyncTime) {
Logger.Info("syncer has already completed today, skip!!!",
zap.String("sync_name", s.Name()),
zap.Int64("run_time", syncerPos.LastSyncTime))
return nil
}
return startSyncer(s, syncerPos)
}
func startSyncer(s DataSyncInterface, syncerPos *store.SyncerPos) error {
var (
syncStartTimestamp = syncerPos.SyncStartTimestamp
syncEndTimestamp = syncerPos.SyncEndTimestamp
pos = syncerPos.Pos
)
endDate := time.Unix(syncEndTimestamp, 0)
startDate := time.Unix(syncStartTimestamp, 0)
// Query total count of data
count, err := s.QueryCount(startDate, endDate)
if err != nil {
Logger.Error("query data count err",
zap.String("sync_name", s.Name()),
zap.Error(err))
return err
}
Logger.Info("query data count",
zap.String("sync_name", s.Name()),
zap.Int64("count", count),
zap.Time("start", startDate),
zap.Time("end", endDate))
// Current date has been fully synchronized
if count != 0 && count <= pos {
Logger.Info("last sync complete, start new sync",
zap.Int64("pos", pos),
zap.String("sync_name", s.Name()))
// Set the next synchronization time range after synchronization is complete
syncStartTimestamp, syncEndTimestamp, pos = s.NextPos(syncStartTimestamp, syncEndTimestamp, pos)
count, err = s.QueryCount(startDate, endDate)
if err != nil {
Logger.Error("query data count err",
zap.String("sync_name", s.Name()),
zap.Error(err))
return err
}
}
// Paging query
queryTimes := math.Ceil(float64(count-pos) / float64(opt.BatchLimit))
offset := int(pos)
for i := 0; i < int(queryTimes); i++ {
dataList, nums, err := s.QueryData(offset, opt.BatchLimit, startDate, endDate)
if err != nil {
Logger.Error("query data err", zap.String("sync_name", s.Name()),
zap.Error(err))
return err
}
offset += opt.BatchLimit
if err = s.Sink(dataList); err != nil {
Logger.Error("sink data err",
zap.String("sync_name", s.Name()),
zap.Error(err))
return err
}
pos += nums
err = s.Save(fmt.Sprintf(kSyncerPosFmt, syncStartTimestamp, syncEndTimestamp, pos))
if err != nil {
return err
}
}
Logger.Info("syncer end, wait next time", zap.String("name", s.Name()))
return nil
}