2017-02-08 20:23:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/big"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Clever/amazon-kinesis-client-go/kcl"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SampleRecordProcessor struct {
|
2017-05-19 22:01:57 +00:00
|
|
|
checkpointer *kcl.Checkpointer
|
2017-02-08 20:23:00 +00:00
|
|
|
checkpointRetries int
|
|
|
|
|
checkpointFreq time.Duration
|
|
|
|
|
largestSeq *big.Int
|
|
|
|
|
largestSubSeq int
|
|
|
|
|
lastCheckpoint time.Time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New() *SampleRecordProcessor {
|
|
|
|
|
return &SampleRecordProcessor{
|
|
|
|
|
checkpointRetries: 5,
|
|
|
|
|
checkpointFreq: 60 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 22:01:57 +00:00
|
|
|
func (srp *SampleRecordProcessor) Initialize(shardID string, checkpointer *kcl.Checkpointer) error {
|
2017-02-08 20:23:00 +00:00
|
|
|
srp.lastCheckpoint = time.Now()
|
2017-05-19 22:01:57 +00:00
|
|
|
srp.checkpointer = checkpointer
|
2017-02-08 20:23:00 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (srp *SampleRecordProcessor) shouldUpdateSequence(sequenceNumber *big.Int, subSequenceNumber int) bool {
|
|
|
|
|
return srp.largestSeq == nil || sequenceNumber.Cmp(srp.largestSeq) == 1 ||
|
|
|
|
|
(sequenceNumber.Cmp(srp.largestSeq) == 0 && subSequenceNumber > srp.largestSubSeq)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 22:01:57 +00:00
|
|
|
func (srp *SampleRecordProcessor) ProcessRecords(records []kcl.Record) error {
|
2017-02-08 20:23:00 +00:00
|
|
|
for _, record := range records {
|
|
|
|
|
seqNumber := new(big.Int)
|
|
|
|
|
if _, ok := seqNumber.SetString(record.SequenceNumber, 10); !ok {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "could not parse sequence number '%s'\n", record.SequenceNumber)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if srp.shouldUpdateSequence(seqNumber, record.SubSequenceNumber) {
|
|
|
|
|
srp.largestSeq = seqNumber
|
|
|
|
|
srp.largestSubSeq = record.SubSequenceNumber
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if time.Now().Sub(srp.lastCheckpoint) > srp.checkpointFreq {
|
2017-05-18 00:46:29 +00:00
|
|
|
largestSeq := srp.largestSeq.String()
|
2017-05-22 23:05:34 +00:00
|
|
|
srp.checkpointer.CheckpointWithRetry(&largestSeq, &srp.largestSubSeq, srp.checkpointRetries)
|
2017-02-08 20:23:00 +00:00
|
|
|
srp.lastCheckpoint = time.Now()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 22:01:57 +00:00
|
|
|
func (srp *SampleRecordProcessor) Shutdown(reason string) error {
|
2017-02-08 20:23:00 +00:00
|
|
|
if reason == "TERMINATE" {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Was told to terminate, will attempt to checkpoint.\n")
|
2017-05-22 23:06:56 +00:00
|
|
|
srp.checkpointer.Shutdown()
|
2017-02-08 20:23:00 +00:00
|
|
|
} else {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Shutting down due to failover. Will not checkpoint.\n")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
f, err := os.Create("/tmp/kcl_stderr")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
2017-04-05 20:51:44 +00:00
|
|
|
kclProcess := kcl.New(os.Stdin, os.Stdout, os.Stderr, New())
|
2017-02-08 20:23:00 +00:00
|
|
|
kclProcess.Run()
|
|
|
|
|
}
|