2017-02-08 20:23:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/big"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Clever/amazon-kinesis-client-go/kcl"
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-18 19:19:40 +00:00
|
|
|
type sampleRecordProcessor struct {
|
2017-07-21 01:04:34 +00:00
|
|
|
checkpointer kcl.Checkpointer
|
2017-02-08 20:23:00 +00:00
|
|
|
checkpointRetries int
|
|
|
|
|
checkpointFreq time.Duration
|
2017-08-03 21:22:52 +00:00
|
|
|
largestPair kcl.SequencePair
|
2017-02-08 20:23:00 +00:00
|
|
|
lastCheckpoint time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 19:19:40 +00:00
|
|
|
func newSampleRecordProcessor() *sampleRecordProcessor {
|
|
|
|
|
return &sampleRecordProcessor{
|
2017-02-08 20:23:00 +00:00
|
|
|
checkpointRetries: 5,
|
|
|
|
|
checkpointFreq: 60 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 01:04:34 +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
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 21:22:52 +00:00
|
|
|
func (srp *sampleRecordProcessor) shouldUpdateSequence(pair kcl.SequencePair) bool {
|
|
|
|
|
return srp.largestPair.IsLessThan(pair)
|
2017-02-08 20:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-18 19:19:40 +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
|
|
|
|
|
}
|
2017-08-03 21:22:52 +00:00
|
|
|
pair := kcl.SequencePair{seqNumber, record.SubSequenceNumber}
|
|
|
|
|
if srp.shouldUpdateSequence(pair) {
|
|
|
|
|
srp.largestPair = pair
|
2017-02-08 20:23:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if time.Now().Sub(srp.lastCheckpoint) > srp.checkpointFreq {
|
2017-08-03 21:22:52 +00:00
|
|
|
srp.checkpointer.Checkpoint(srp.largestPair, srp.checkpointRetries)
|
2017-02-08 20:23:00 +00:00
|
|
|
srp.lastCheckpoint = time.Now()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 19:19:40 +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-07-18 19:19:40 +00:00
|
|
|
kclProcess := kcl.New(os.Stdin, os.Stdout, os.Stderr, newSampleRecordProcessor())
|
2017-02-08 20:23:00 +00:00
|
|
|
kclProcess.Run()
|
|
|
|
|
}
|