-
Notifications
You must be signed in to change notification settings - Fork 16
/
checkpoint.go
257 lines (204 loc) · 5.77 KB
/
checkpoint.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
253
254
255
256
257
package stream
import (
"errors"
"sync"
"time"
"github.com/Trendyol/go-dcp/wrapper"
"github.com/Trendyol/go-dcp/config"
"github.com/Trendyol/go-dcp/metadata"
"github.com/Trendyol/go-dcp/couchbase"
"github.com/Trendyol/go-dcp/models"
"github.com/Trendyol/go-dcp/logger"
"github.com/couchbase/gocbcore/v10"
)
const (
CheckpointTypeAuto = "auto"
CheckpointAutoResetTypeLatest = "latest"
)
type Checkpoint interface {
Save()
Load() (*wrapper.ConcurrentSwissMap[uint16, *models.Offset], *wrapper.ConcurrentSwissMap[uint16, bool], bool)
Clear()
StartSchedule()
StopSchedule()
GetMetric() *CheckpointMetric
}
type CheckpointMetric struct {
OffsetWrite int
OffsetWriteLatency int64
}
type checkpoint struct {
stream Stream
client couchbase.Client
metadata metadata.Metadata
config *config.Dcp
saveLock *sync.Mutex
loadLock *sync.Mutex
metric *CheckpointMetric
bucketUUID string
vbIds []uint16
running bool
}
func (s *checkpoint) Save() {
offsets, dirtyOffsets, anyDirtyOffset := s.stream.GetOffsets()
if !anyDirtyOffset {
logger.Log.Trace("no need to save checkpoint")
return
}
s.saveLock.Lock()
defer s.saveLock.Unlock()
checkpointDump := map[uint16]*models.CheckpointDocument{}
offsets.Range(func(vbID uint16, offset *models.Offset) bool {
checkpointDump[vbID] = &models.CheckpointDocument{
Checkpoint: &models.CheckpointDocumentCheckpoint{
VbUUID: uint64(offset.VbUUID),
SeqNo: offset.SeqNo,
Snapshot: &models.CheckpointDocumentSnapshot{
StartSeqNo: offset.StartSeqNo,
EndSeqNo: offset.EndSeqNo,
},
},
BucketUUID: s.bucketUUID,
}
return true
})
dirtyOffsetsDump := map[uint16]bool{}
var dirtyOffsetCount int
dirtyOffsets.Range(func(vbID uint16, dirt bool) bool {
if dirt {
dirtyOffsetCount++
}
dirtyOffsetsDump[vbID] = dirt
return true
})
s.metric.OffsetWrite = dirtyOffsetCount
start := time.Now()
err := s.metadata.Save(checkpointDump, dirtyOffsetsDump, s.bucketUUID)
s.metric.OffsetWriteLatency = time.Since(start).Milliseconds()
if err == nil {
logger.Log.Trace("saved checkpoint")
s.stream.UnmarkDirtyOffsets()
} else {
logger.Log.Error("error while saving checkpoint document: %v", err)
}
}
//nolint:funlen
func (s *checkpoint) Load() (*wrapper.ConcurrentSwissMap[uint16, *models.Offset], *wrapper.ConcurrentSwissMap[uint16, bool], bool) {
s.loadLock.Lock()
defer s.loadLock.Unlock()
dump, exist, err := s.metadata.Load(s.vbIds, s.bucketUUID)
if err == nil {
logger.Log.Debug("loaded checkpoint")
} else {
logger.Log.Error("error while loading checkpoint document, err: %v", err)
panic(err)
}
seqNoMap, err := s.client.GetVBucketSeqNos(false)
if err != nil {
logger.Log.Error("error while getting vBucket seqNos, err: %v", err)
panic(err)
}
offsets := wrapper.CreateConcurrentSwissMap[uint16, *models.Offset](1024)
dirtyOffsets := wrapper.CreateConcurrentSwissMap[uint16, bool](1024)
anyDirtyOffset := false
if !exist && s.config.Checkpoint.AutoReset == CheckpointAutoResetTypeLatest {
logger.Log.Debug("no checkpoint found, auto reset checkpoint to latest")
dump.Range(func(vbID uint16, doc *models.CheckpointDocument) bool {
currentSeqNo, _ := seqNoMap.Load(vbID)
if currentSeqNo != 0 {
dirtyOffsets.Store(vbID, true)
anyDirtyOffset = true
}
failOverLogs, err := s.client.GetFailOverLogs(vbID)
if err != nil {
logger.Log.Error("error while get failOver logs when initialize latest, err: %v", err)
panic(err)
}
offsets.Store(vbID, &models.Offset{
SnapshotMarker: &models.SnapshotMarker{
StartSeqNo: currentSeqNo,
EndSeqNo: currentSeqNo,
},
VbUUID: failOverLogs[0].VbUUID,
SeqNo: currentSeqNo,
})
return true
})
return offsets, dirtyOffsets, anyDirtyOffset
}
dump.Range(func(vbID uint16, doc *models.CheckpointDocument) bool {
latestSeqNo, _ := seqNoMap.Load(vbID)
if doc.Checkpoint.SeqNo > latestSeqNo {
err := errors.New("checkpoint seqNo bigger then vBucket latest seqNo")
logger.Log.Error(
"error while loading checkpoint, vbID: %v, checkpoint seqNo: %v, latest seqNo: %v, err: %v",
vbID, doc.Checkpoint.SeqNo, latestSeqNo, err,
)
panic(err)
}
offsets.Store(vbID, &models.Offset{
SnapshotMarker: &models.SnapshotMarker{
StartSeqNo: doc.Checkpoint.Snapshot.StartSeqNo,
EndSeqNo: doc.Checkpoint.Snapshot.EndSeqNo,
},
VbUUID: gocbcore.VbUUID(doc.Checkpoint.VbUUID),
SeqNo: doc.Checkpoint.SeqNo,
})
return true
})
return offsets, dirtyOffsets, anyDirtyOffset
}
func (s *checkpoint) Clear() {
_ = s.metadata.Clear(s.vbIds)
logger.Log.Debug("cleared checkpoint")
}
func (s *checkpoint) StartSchedule() {
if s.config.Checkpoint.Type != CheckpointTypeAuto {
return
}
go func() {
s.running = true
for s.running {
time.Sleep(s.config.Checkpoint.Interval)
s.Save()
}
}()
logger.Log.Debug("started checkpoint schedule")
}
func (s *checkpoint) StopSchedule() {
if s.config.Checkpoint.Type != CheckpointTypeAuto {
return
}
s.running = false
logger.Log.Debug("stopped checkpoint schedule")
}
func (s *checkpoint) GetMetric() *CheckpointMetric {
return s.metric
}
func getBucketUUID(client couchbase.Client) string {
snapshot, err := client.GetDcpAgentConfigSnapshot()
if err != nil {
logger.Log.Error("error while get config snapshot, err: %v", err)
panic(err)
}
return snapshot.BucketUUID()
}
func NewCheckpoint(
stream Stream,
vbIds []uint16,
client couchbase.Client,
metadata metadata.Metadata,
config *config.Dcp,
) Checkpoint {
return &checkpoint{
client: client,
stream: stream,
vbIds: vbIds,
bucketUUID: getBucketUUID(client),
metadata: metadata,
config: config,
saveLock: &sync.Mutex{},
loadLock: &sync.Mutex{},
metric: &CheckpointMetric{},
}
}