2014-07-25 06:03:41 +00:00
|
|
|
package connector
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/hoisie/redis"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// A Redis implementation of the Checkpont interface. This class is used to enable the Pipeline.ProcessShard
|
|
|
|
|
// to checkpoint their progress.
|
|
|
|
|
type RedisCheckpoint struct {
|
|
|
|
|
AppName string
|
2014-11-16 00:15:18 +00:00
|
|
|
StreamName string
|
2014-07-25 06:03:41 +00:00
|
|
|
client redis.Client
|
|
|
|
|
sequenceNumber string
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 22:04:52 +00:00
|
|
|
// Check whether 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).
|
2014-07-25 06:03:41 +00:00
|
|
|
func (c *RedisCheckpoint) CheckpointExists(shardID string) bool {
|
|
|
|
|
val, _ := c.client.Get(c.key(shardID))
|
|
|
|
|
|
|
|
|
|
if val != nil && string(val) != "" {
|
|
|
|
|
c.sequenceNumber = string(val)
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 22:04:52 +00:00
|
|
|
// Get the current checkpoint stored for the specified shard.
|
2014-07-25 06:03:41 +00:00
|
|
|
func (c *RedisCheckpoint) SequenceNumber() string {
|
|
|
|
|
return c.sequenceNumber
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 22:04:52 +00:00
|
|
|
// Record a checkpoint for a shard (e.g. sequence number of last record processed by application).
|
|
|
|
|
// Upon failover, record processing is resumed from this point.
|
2014-07-25 06:03:41 +00:00
|
|
|
func (c *RedisCheckpoint) SetCheckpoint(shardID string, sequenceNumber string) {
|
|
|
|
|
c.client.Set(c.key(shardID), []byte(sequenceNumber))
|
|
|
|
|
c.sequenceNumber = sequenceNumber
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 22:04:52 +00:00
|
|
|
// Generate a unique Redis key for storage of Checkpoint.
|
2014-07-25 06:03:41 +00:00
|
|
|
func (c *RedisCheckpoint) key(shardID string) string {
|
|
|
|
|
return fmt.Sprintf("%v:checkpoint:%v:%v", c.AppName, c.StreamName, shardID)
|
|
|
|
|
}
|