This repository was archived by the owner on Mar 28, 2023. It is now read-only.
forked from harlow/kinesis-consumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_checkpoint.go
66 lines (51 loc) · 1.8 KB
/
redis_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
package connector
import (
"fmt"
"sync"
"github.com/apex/log"
"github.com/go-redis/redis"
)
// RedisCheckpoint implements the Checkpont interface.
// Used to enable the Pipeline.ProcessShard to checkpoint it's progress
// while reading records from Kinesis stream.
type RedisCheckpoint struct {
AppName string
StreamName string
client *redis.Client
sequenceMutex sync.RWMutex
sequenceNumbers map[string]string
}
// CheckpointExists determines if a checkpoint for a particular Shard exists.
// Typically used to determine whether we should start processing the shard with
// TRIM_HORIZON or AFTER_SEQUENCE_NUMBER (if checkpoint exists).
func (c *RedisCheckpoint) CheckpointExists(shardID string) bool {
val, _ := c.client.Get(c.key(shardID)).Result()
if val != "" {
c.sequenceMutex.Lock()
c.sequenceNumbers[shardID] = val
c.sequenceMutex.Unlock()
return true
}
return false
}
// SequenceNumber returns the current checkpoint stored for the specified shard.
func (c *RedisCheckpoint) SequenceNumber(shardID string) string {
c.sequenceMutex.RLock()
defer c.sequenceMutex.RUnlock()
return c.sequenceNumbers[shardID]
}
// SetCheckpoint stores a checkpoint for a shard (e.g. sequence number of last record processed by application).
// Upon failover, record processing is resumed from this point.
func (c *RedisCheckpoint) SetCheckpoint(shardID string, sequenceNumber string) {
err := c.client.Set(c.key(shardID), sequenceNumber, 0).Err()
if err != nil {
log.WithError(err).Error("redis checkpoint set")
}
c.sequenceMutex.Lock()
c.sequenceNumbers[shardID] = sequenceNumber
c.sequenceMutex.Unlock()
}
// key generates a unique Redis key for storage of Checkpoint.
func (c *RedisCheckpoint) key(shardID string) string {
return fmt.Sprintf("%v:checkpoint:%v:%v", c.AppName, c.StreamName, shardID)
}