-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint.go
271 lines (251 loc) · 8.53 KB
/
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"encoding/json"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodbstreams"
logging "github.com/sirupsen/logrus"
)
const (
sourceTable = "source"
dstTable = "destination"
timestp = "Timestamp"
checkpt = "checkpoint"
expiredShards = "ExpiredShards"
)
func (ss *syncState) readCheckpoint() {
logger.WithFields(logging.Fields{
"Table": ddbTable,
"Source Table": ss.tableConfig.SrcTable,
"Destination Table": ss.tableConfig.DstTable,
}).Info("Reading checkpoint table")
result, err := ddbClient.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(ss.tableConfig.SrcTable)},
dstTable: {S: aws.String(ss.tableConfig.DstTable)},
},
})
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": ss.tableConfig.SrcTable,
"Destination Table": ss.tableConfig.DstTable,
}).Error("Failed to read from checkpoint table")
}
if result == nil || result.Item == nil {
return
}
if result.Item[checkpt] != nil {
err = json.Unmarshal(result.Item[checkpt].B, &ss.checkpoint)
if err != nil {
logger.WithFields(logging.Fields{
"Source Table": ss.tableConfig.SrcTable,
"Destination Table": ss.tableConfig.DstTable,
"Error": err,
}).Error("Failed to unmarshal checkpoint")
}
}
if result.Item[expiredShards] != nil {
err = json.Unmarshal(result.Item[expiredShards].B, &ss.expiredShards)
if err != nil {
logger.WithFields(logging.Fields{
"Source Table": ss.tableConfig.SrcTable,
"Destination Table": ss.tableConfig.DstTable,
"Error": err,
}).Error("Failed to unmarshal expired shards")
}
}
ss.timestamp, err = time.Parse(time.RFC3339, *result.Item[timestp].S)
if err != nil {
logger.WithFields(logging.Fields{
"Checkpoint timestamp": *result.Item[timestp].S,
"Layout Pattern": time.RFC3339,
"Source Table": ss.tableConfig.SrcTable,
"Destination Table": ss.tableConfig.DstTable,
"Error": err,
}).Error("Failed to parse checkpoint timestamp")
}
}
// updateCheckpoint updates the checkpoint info both locally and remotely
func (ss *syncState) updateCheckpoint(sequenceNumber string, shard *dynamodbstreams.Shard) {
if shard != nil {
timestamp := time.Now()
ss.updateCheckpointLocal(sequenceNumber, shard, timestamp)
ss.updateCheckpointRemote(shard.ShardId, timestamp)
}
}
// updateCheckpointLocal updates the checkpoint for `key's` local state sync
func (ss *syncState) updateCheckpointLocal(
sequenceNumber string,
shard *dynamodbstreams.Shard,
timestamp time.Time,
) {
ss.timestamp = timestamp
ss.checkpoint[*shard.ShardId] = sequenceNumber
}
// updateCheckpointRemote updates the checkpoint for `key` in the checkpoint dynamodb table
func (ss *syncState) updateCheckpointRemote(shardId *string, timestamp time.Time) {
data, _ := json.Marshal(ss.checkpoint)
_, err := ddbClient.UpdateItem(&dynamodb.UpdateItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(ss.checkpointPK.sourceTable)},
dstTable: {S: aws.String(ss.checkpointPK.dstTable)},
},
ExpressionAttributeNames: map[string]*string{
"#CP": aws.String(checkpt),
"#TS": aws.String(timestp),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":val": {B: data},
":ts": {S: aws.String(timestamp.Format(time.RFC3339))},
},
UpdateExpression: aws.String("SET #CP = :val, #TS = :ts"),
})
logField := logging.Fields{
"src table": ss.checkpointPK.sourceTable,
"dst table": ss.checkpointPK.dstTable,
"seq number": ss.checkpoint[*shardId],
"timestamp": timestamp,
"shard ID": *shardId,
}
if err != nil {
logField["error"] = err
logger.WithFields(logField).Error("failed to update checkpoint global config")
} else {
logger.WithFields(logField).Debug("successfully updated checkpoint global config")
}
}
func (ss *syncState) updateCheckpointTimestamp() {
timestamp := time.Now()
ss.updateTimestampLocal(timestamp)
ss.updateTimestampRemote(timestamp)
}
func (ss *syncState) updateTimestampLocal(timestamp time.Time) {
ss.timestamp = timestamp
}
func (ss *syncState) updateTimestampRemote(timestamp time.Time) {
_, err := ddbClient.UpdateItem(&dynamodb.UpdateItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(ss.checkpointPK.sourceTable)},
dstTable: {S: aws.String(ss.checkpointPK.dstTable)},
},
ExpressionAttributeNames: map[string]*string{
"#TS": aws.String(timestp),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":ts": {S: aws.String(timestamp.Format(time.RFC3339))},
},
UpdateExpression: aws.String("SET #TS = :ts"),
})
logField := logging.Fields{
"src table": ss.checkpointPK.sourceTable,
"dst table": ss.checkpointPK.dstTable,
}
if err != nil {
logField["error"] = err
logger.WithFields(logField).Error("failed to update checkpoint timestamp")
} else {
logger.WithFields(logField).Debug("successfully updated checkpoint timestamp")
}
}
// Remove checkpoint for <primaryKey, shardId> from the local state[key]
func (ss *syncState) expireCheckpointLocal(shardId *string) {
logField := logging.Fields{
"src table": ss.checkpointPK.sourceTable,
"dst table": ss.checkpointPK.dstTable,
"shard ID": *shardId,
}
// Remove from activeShardProcessors
ss.activeShardLock.Lock()
delete(ss.activeShardProcessors, *shardId)
logger.WithFields(logField).Debug("deleted shard from active shards")
ss.activeShardLock.Unlock()
// Add to expiredShards
ss.completedShardLock.Lock()
ss.expiredShards[*shardId] = true
logger.WithFields(logField).Debug("added shard to expired shards")
ss.completedShardLock.Unlock()
// Delete checkpoint for this shard
ss.checkpointLock.Lock()
delete(ss.checkpoint, *shardId)
logger.WithFields(logField).Debug("marked shard as complete")
ss.checkpointLock.Unlock()
}
// Remove checkpoint for <primaryKey, shardId> from the checkpoint dynamodb table
func (ss *syncState) expireCheckpointRemote(shardId string) {
data, _ := json.Marshal(ss.expiredShards)
ss.updateCheckpointRemote(&shardId, ss.timestamp)
ss.checkpointLock.Lock()
_, err := ddbClient.UpdateItem(&dynamodb.UpdateItemInput{
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(ss.checkpointPK.sourceTable)},
dstTable: {S: aws.String(ss.checkpointPK.dstTable)},
},
TableName: aws.String(ddbTable),
ExpressionAttributeNames: map[string]*string{
"#ExpSh": aws.String(expiredShards),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":val": {B: data},
},
UpdateExpression: aws.String("SET #ExpSh = :val"),
})
ss.checkpointLock.Unlock()
logField := logging.Fields{
"src table": ss.checkpointPK.sourceTable,
"dst table": ss.checkpointPK.dstTable,
"shard ID": shardId,
}
if err != nil {
logField["error"] = err
logger.WithFields(logField).Error("failed to add expired shard")
} else {
logger.WithFields(logField).Debug("successfully added expired shards")
}
}
// isCheckpointFound checks if the checkpointPK exists in the checkpoint dynamodb table
func (ss *syncState) isCheckpointFound() bool {
result, err := ddbClient.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(ss.checkpointPK.sourceTable)},
dstTable: {S: aws.String(ss.checkpointPK.dstTable)},
},
})
if err != nil {
logger.WithFields(logging.Fields{
"src table": ss.checkpointPK.sourceTable,
"dst table": ss.checkpointPK.dstTable,
"error": err,
}).Error("failed to get item from checkpoint table")
return false
}
return result.Item != nil
}
// dropCheckpoint drops stale checkpoints and expired shards for a fresh start
func (ss *syncState) dropCheckpoint() {
if !ss.isCheckpointFound() {
return
}
logField := logging.Fields{
"src Table": ss.checkpointPK.sourceTable,
"dst Table": ss.checkpointPK.dstTable,
}
_, err := ddbClient.DeleteItem(&dynamodb.DeleteItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(ss.checkpointPK.sourceTable)},
dstTable: {S: aws.String(ss.checkpointPK.dstTable)},
},
})
if err != nil {
logField["error"] = err
logger.WithFields(logField).Error("failed to drop checkpoint")
} else {
logger.WithFields(logField).Debug("dropped checkpoint successfully")
}
}