-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpointer.go
More file actions
178 lines (139 loc) · 4.96 KB
/
checkpointer.go
File metadata and controls
178 lines (139 loc) · 4.96 KB
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
package kinesis2sse
import (
"errors"
"fmt"
"log/slog"
"sync"
"time"
chk "github.com/vmware/vmware-go-kcl-v2/clientlibrary/checkpoint"
par "github.com/vmware/vmware-go-kcl-v2/clientlibrary/partition"
)
// inMemoryCheckpointer is an in-memory implementation of the DynamoDB-based Checkpointer that ships with the KCL. We
// can use an in-memory implementation, because we don't need durability across restarts, and we don't need multiple
// workers. In order to implement this, I started with a copy of the DynamoDB-based Checkpointer and just deleted
// everything we didn't need.
type inMemoryCheckpointer struct {
workerID string
m map[string]marshalledCheckpoint
logger *slog.Logger // required
lock *sync.Mutex
}
type marshalledCheckpoint struct {
sequenceNumber string
leaseTimeout time.Time
parentShardId string
}
func NewInMemoryCheckpointer(workerID string, logger *slog.Logger) chk.Checkpointer {
return &inMemoryCheckpointer{
workerID: workerID,
m: make(map[string]marshalledCheckpoint),
logger: logger,
lock: &sync.Mutex{},
}
}
func (checkpointer *inMemoryCheckpointer) Init() error {
checkpointer.logger.Debug("Init")
return nil
}
func (checkpointer *inMemoryCheckpointer) GetLease(shard *par.ShardStatus, newAssignTo string) error {
checkpointer.logger.Debug(fmt.Sprintf("GetLease: shardID=%q; newAssignTo=%q", shard.ID, newAssignTo))
// NOTE(mroberts): Lease duration can be nearly infinite, since this is just in-memory.
newLeaseTimeout := time.Now().AddDate(1, 0, 0).UTC()
shard.Mux.Lock()
shard.AssignedTo = newAssignTo
shard.LeaseTimeout = newLeaseTimeout
shard.Mux.Unlock()
return nil
}
func (checkpointer *inMemoryCheckpointer) CheckpointSequence(shard *par.ShardStatus) error {
checkpointer.logger.Debug(fmt.Sprintf("CheckpointSequence: shardID=%q", shard.ID))
leaseTimeout := shard.GetLeaseTimeout().UTC()
item := marshalledCheckpoint{
sequenceNumber: shard.GetCheckpoint(),
leaseTimeout: leaseTimeout,
parentShardId: shard.ParentShardId,
}
return checkpointer.saveItem(shard.ID, item)
}
func (checkpointer *inMemoryCheckpointer) FetchCheckpoint(shard *par.ShardStatus) error {
checkpointer.logger.Debug(fmt.Sprintf("FetchCheckpoint: shardID=%q", shard.ID))
item, err := checkpointer.getItem(shard.ID)
if err != nil {
return err
}
shard.SetCheckpoint(item.sequenceNumber)
shard.SetLeaseOwner(checkpointer.workerID)
shard.LeaseTimeout = item.leaseTimeout
return nil
}
func (checkpointer *inMemoryCheckpointer) RemoveLeaseInfo(shardID string) error {
checkpointer.logger.Debug(fmt.Sprintf("RemoveLeaseInfo: shardID=%q", shardID))
checkpointer.removeItem(shardID)
return nil
}
func (checkpointer *inMemoryCheckpointer) RemoveLeaseOwner(shardID string) error {
checkpointer.logger.Debug(fmt.Sprintf("RemoveLeaseOwner: shardID=%q", shardID))
checkpointer.removeItem(shardID)
return nil
}
func (checkpointer *inMemoryCheckpointer) GetLeaseOwner(shardID string) (string, error) {
checkpointer.logger.Debug(fmt.Sprintf("GetLeaseOwner: shardID=%q", shardID))
_, err := checkpointer.getItem(shardID)
if err != nil {
return "", err
}
return checkpointer.workerID, nil
}
func (checkpointer *inMemoryCheckpointer) ListActiveWorkers(shardStatus map[string]*par.ShardStatus) (map[string][]*par.ShardStatus, error) {
checkpointer.logger.Debug("ListActiveWorkers")
workers := map[string][]*par.ShardStatus{}
for _, shard := range shardStatus {
if shard.GetCheckpoint() == chk.ShardEnd {
continue
}
leaseOwner := shard.GetLeaseOwner()
if leaseOwner == "" {
checkpointer.logger.Debug(fmt.Sprintf("Shard Not Assigned Error. ShardID: %s", shard.ID))
return nil, chk.ErrShardNotAssigned
}
if w, ok := workers[leaseOwner]; ok {
workers[leaseOwner] = append(w, shard)
} else {
workers[leaseOwner] = []*par.ShardStatus{shard}
}
}
return workers, nil
}
func (checkpointer *inMemoryCheckpointer) ClaimShard(shard *par.ShardStatus, _ string) error {
checkpointer.logger.Debug(fmt.Sprintf("ClaimShard: shardID=%q", shard.ID))
err := checkpointer.FetchCheckpoint(shard)
if err != nil && !errors.Is(err, chk.ErrSequenceIDNotFound) {
return err
}
item := marshalledCheckpoint{
leaseTimeout: shard.GetLeaseTimeout(),
sequenceNumber: shard.Checkpoint,
parentShardId: shard.ParentShardId,
}
return checkpointer.saveItem(shard.ID, item)
}
func (checkpointer *inMemoryCheckpointer) saveItem(shardID string, item marshalledCheckpoint) error {
checkpointer.lock.Lock()
defer checkpointer.lock.Unlock()
checkpointer.m[shardID] = item
return nil
}
func (checkpointer *inMemoryCheckpointer) getItem(shardID string) (marshalledCheckpoint, error) {
checkpointer.lock.Lock()
defer checkpointer.lock.Unlock()
item, ok := checkpointer.m[shardID]
if !ok {
return marshalledCheckpoint{}, chk.ErrSequenceIDNotFound
}
return item, nil
}
func (checkpointer *inMemoryCheckpointer) removeItem(shardID string) {
checkpointer.lock.Lock()
defer checkpointer.lock.Unlock()
delete(checkpointer.m, shardID)
}