forked from rewardStyle/kinetic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckpointer.go
290 lines (251 loc) · 8.94 KB
/
checkpointer.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
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
package kinetic
import (
"container/list"
"context"
"errors"
"strconv"
"sync"
"sync/atomic"
"time"
)
// checkpointElement is the struct used to store checkpointing information in the doubly linked list.
type checkpointElement struct {
seqNum int
done bool
}
// checkpointList is a doubly linked list used to track sequence numbers. Sequence numbers are linked in ascending
// order.
type checkpointList struct {
*list.List
}
// insert is a method used to insert sequence numbers (ordered) into the doubly linked list. insert is optimized to
// insert from the back, so inserting in sequential or ascending order is faster than reverse / descending order.
func (c *checkpointList) insert(seqNum int) error {
value := &checkpointElement{seqNum: seqNum}
for e := c.Back(); e != nil; e = e.Prev() {
element := e.Value.(*checkpointElement)
switch {
case element.seqNum < seqNum:
c.InsertAfter(value, e)
return nil
case element.seqNum == seqNum:
return nil
case element.seqNum > seqNum:
if e.Prev() != nil && e.Prev().Value.(*checkpointElement).seqNum < seqNum {
c.InsertBefore(value, e)
return nil
}
}
}
c.PushFront(value)
return nil
}
// find is a method used to retrieve the element in the doubly linked list for a given sequence number. find is
// optimized for searching oldest numbers first as it traverse the linked list starting from the beginning.
func (c *checkpointList) find(seqNum int) (*list.Element, bool) {
for e := c.Front(); e != nil; e = e.Next() {
element := e.Value.(*checkpointElement)
switch {
case element.seqNum == seqNum:
return e, true
case element.seqNum < seqNum:
continue
default:
return nil, false
}
}
return nil, false
}
// checkpointOptions is a struct containing all of the configurable options for a checkpoint object.
type checkpointOptions struct {
autoCheckpointCount int // count of newly inserted messages before triggering an automatic checkpoint call
autoCheckpointFreq time.Duration // frequency with which to automatically call checkpoint
checkpointFn func(checkpoint string) error // callback function to call on a sequence number when a checkpoint is discovered with the checkpoint call
countCheckFreq time.Duration // frequency with which to check the insert count
}
// defaultCheckpointOptions is a function that returns a pointer to a checkpointOptions object with default values.
func defaultCheckpointOptions() *checkpointOptions {
return &checkpointOptions{
autoCheckpointCount: 10000,
autoCheckpointFreq: time.Minute,
checkpointFn: func(string) error { return nil },
countCheckFreq: time.Second,
}
}
// checkpointOptionsFn is a function signature used to define function options for configuring all of the
// configurable options of a checkpoint object.
type checkpointOptionsFn func(*checkpointer) error
// checkpointAutoCheckpointCount is a functional option method for configuring the checkpoint's auto checkpoint count.
func checkpointAutoCheckpointCount(count int) checkpointOptionsFn {
return func(o *checkpointer) error {
o.autoCheckpointCount = count
return nil
}
}
// checkpointAutoCheckpointFreq is a functional option method for configuring the checkpoint's auto checkpoint
// frequency.
func checkpointAutoCheckpointFreq(freq time.Duration) checkpointOptionsFn {
return func(o *checkpointer) error {
o.autoCheckpointFreq = freq
return nil
}
}
// checkpointCheckpointFn is a functional option method for configuring the checkpoint's checkpoint callback function.
func checkpointCheckpointFn(fn func(string) error) checkpointOptionsFn {
return func(o *checkpointer) error {
o.checkpointFn = fn
return nil
}
}
// checkpointCountCheckFreq is a functional option method for configuring the checkpoint's count check frequency.
func checkpointCountCheckFreq(freq time.Duration) checkpointOptionsFn {
return func(o *checkpointer) error {
o.countCheckFreq = freq
return nil
}
}
// checkpoint is a data structure that is used as a bookkeeping system to track the state of data processing
// for records pulled off of the consumer's message channel. The check pointing system uses a binary search
// tree keyed off of the Kinesis sequence number of the record. The sequence numbers should be inserted into the
// binary search tree using the insert() function after messages are pulled off of the consumer's message
// channel and should be marked done using the markDone() function after data processing is completed. The
// checkpoint() can be called periodically which may trigger a checkpoint call to KCL if the oldest sequence
// numbers have been marked complete. Call startup() to enable automatic checkpointing and expiration.
type checkpointer struct {
*checkpointOptions // contains all of the configuration settings for the checkpoint object
list *checkpointList
listMu sync.Mutex
counter uint64 // counter to track the number of messages inserted since the last checkpoint
checkpointCh chan empty // channel with which to communicate / coordinate checkpointing
startupOnce sync.Once // used to ensure that the startup function is called once
shutdownOnce sync.Once // used to ensure that the shutdown function is called once
}
// newCheckpoint instantiates a new checkpoint object with default configuration settings unless the function option
// methods are provided to change the default values.
func newCheckpointer(optionFns ...checkpointOptionsFn) *checkpointer {
checkpointer := &checkpointer{
checkpointOptions: defaultCheckpointOptions(),
list: &checkpointList{list.New()},
}
for _, optionFn := range optionFns {
optionFn(checkpointer)
}
return checkpointer
}
// startup is a method used to enable automatic checkpointing.
func (c *checkpointer) startup(ctx context.Context) {
c.startupOnce.Do(func() {
defer func() {
c.shutdownOnce = sync.Once{}
}()
c.checkpointCh = make(chan empty)
go func() {
defer c.shutdown()
autoCheckpointTimer := time.NewTimer(c.autoCheckpointFreq)
counterCheckTicker := time.NewTicker(c.countCheckFreq)
for {
wait:
for atomic.LoadUint64(&c.counter) < uint64(c.autoCheckpointCount) {
select {
case <-ctx.Done():
autoCheckpointTimer.Stop()
counterCheckTicker.Stop()
return
case <-c.checkpointCh:
break wait
case <-autoCheckpointTimer.C:
break wait
case <-counterCheckTicker.C:
continue
}
}
// call check to obtain the checkpoint
if cp, found := c.check(); found {
c.checkpointFn(cp)
}
// reset counter and timer
atomic.StoreUint64(&c.counter, 0)
autoCheckpointTimer.Reset(c.autoCheckpointFreq)
}
}()
})
}
// shutdown is a method used to clean up the checkpoint object
func (c *checkpointer) shutdown() {
c.shutdownOnce.Do(func() {
defer func() {
c.startupOnce = sync.Once{}
}()
if c.checkpointCh != nil {
close(c.checkpointCh)
}
})
}
// size safely determines the number of nodes in the checkpointer.
func (c *checkpointer) size() int {
c.listMu.Lock()
defer c.listMu.Unlock()
return c.list.Len()
}
// insert safely inserts a sequence number into the binary search tree.
func (c *checkpointer) insert(seqNumStr string) error {
c.listMu.Lock()
defer c.listMu.Unlock()
seqNum, err := strconv.Atoi(seqNumStr)
if err != nil {
return err
}
if err := c.list.insert(seqNum); err != nil {
return err
}
atomic.AddUint64(&c.counter, 1)
return nil
}
// markDone safely marks the given sequence number as done and attempts to remove it's previous element if the
// previous element is also marked done or attempts to remove itself if it's next element is also marked done
func (c *checkpointer) markDone(seqNumStr string) error {
c.listMu.Lock()
defer c.listMu.Unlock()
seqNum, err := strconv.Atoi(seqNumStr)
if err != nil {
return err
}
e, ok := c.list.find(seqNum)
if !ok {
return errors.New("Sequence number not found")
}
e.Value.(*checkpointElement).done = true
if e.Prev() != nil && e.Prev().Value.(*checkpointElement).done {
c.list.Remove(e.Prev())
}
if e.Next() != nil && e.Next().Value.(*checkpointElement).done {
c.list.Remove(e)
}
return nil
}
// check returns the largest sequence number marked as done where all smaller sequence numbers have
// also been marked as done.
func (c *checkpointer) check() (checkpoint string, found bool) {
c.listMu.Lock()
defer c.listMu.Unlock()
if c.list != nil {
for e := c.list.Front(); e != nil; e = e.Next() {
checkpointElement := e.Value.(*checkpointElement)
if !checkpointElement.done {
break
} else {
checkpoint = strconv.Itoa(checkpointElement.seqNum)
found = true
}
}
}
return checkpoint, found
}
// checkpoint sends a signal to the checkpointCh channel to trigger a call to checkpoint (potentially).
func (c *checkpointer) checkpoint() error {
if c.checkpointCh == nil {
return errors.New("Nil checkpoint channel")
}
c.checkpointCh <- empty{}
return nil
}