-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint.go
331 lines (303 loc) · 10 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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"
)
// Update checkpoint locally, and update checkpoint dynamodb table
func (sync *syncState) updateCheckpoint(key primaryKey,
sequenceNumber string,
shard *dynamodbstreams.Shard) {
timestamp := time.Now().Format(time.RFC3339)
if shard != nil {
sync.updateCheckpointLocal(key, sequenceNumber, shard, timestamp)
sync.updateCheckpointRemote(key, shard.ShardId, timestamp)
}
}
func (sync *syncState) updateCheckpointTimestamp(key primaryKey) {
timestamp := time.Now().Format(time.RFC3339)
sync.updateTimestampLocal(key, timestamp)
sync.updateTimestampRemote(key, timestamp)
}
func (sync *syncState) readCheckpoint() {
logger.WithFields(logging.Fields{
"Table": ddbTable,
"Source Table": sync.tableConfig.SrcTable,
"Destination Table": sync.tableConfig.DstTable,
}).Info("Reading checkpoint table")
input := &dynamodb.GetItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(sync.tableConfig.SrcTable)},
dstTable: {S: aws.String(sync.tableConfig.DstTable)},
},
}
result, err := ddbClient.GetItem(input)
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": sync.tableConfig.SrcTable,
"Destination Table": sync.tableConfig.DstTable,
}).Error("Failed to read from checkpoint table")
}
if result.Item == nil {
return
}
if result.Item[checkpt] != nil {
err = json.Unmarshal(result.Item[checkpt].B, &sync.checkpoint)
if err != nil {
logger.WithFields(logging.Fields{
"Source Table": sync.tableConfig.SrcTable,
"Destination Table": sync.tableConfig.DstTable,
"Error": err,
}).Error("Failed to unmarshal checkpoint")
}
}
if result.Item[expiredShards] != nil {
err = json.Unmarshal(result.Item[expiredShards].B, &sync.expiredShards)
if err != nil {
logger.WithFields(logging.Fields{
"Source Table": sync.tableConfig.SrcTable,
"Destination Table": sync.tableConfig.DstTable,
"Error": err,
}).Error("Failed to unmarshal expired shards")
}
}
sync.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": sync.tableConfig.SrcTable,
"Destination Table": sync.tableConfig.DstTable,
"Error": err,
}).Error("Failed to parse checkpoint timestamp")
}
}
// Given a primaryKey, return whether it exists in the
// checkpoint dynamodb table
func (sync *syncState) isCheckpointFound(key primaryKey) bool {
input := &dynamodb.GetItemInput{
TableName: aws.String(ddbTable),
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(key.sourceTable)},
dstTable: {S: aws.String(key.dstTable)},
},
}
result, err := ddbClient.GetItem(input)
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Error("Failed to read from checkpoint table")
return false
}
if result.Item == nil {
return false
}
logger.WithFields(logging.Fields{
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Debug("Found checkpoint record")
return true
}
// Update the checkpoint for `key's` local state
// sync : timestamp,
//
// checkpoint[`shardId`]: `sequenceNumber`
func (sync *syncState) updateCheckpointLocal(
key primaryKey,
sequenceNumber string,
shard *dynamodbstreams.Shard,
timestamp string) {
sync.timestamp, _ = time.Parse(time.RFC3339, timestamp)
sync.checkpoint[*shard.ShardId] = sequenceNumber
}
// Update the checkpoint for `key` in the checkpoint dynamodb table
func (sync *syncState) updateCheckpointRemote(key primaryKey,
shardId *string,
timestamp string) {
data, _ := json.Marshal(sync.checkpoint)
tableKey := map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(key.sourceTable)},
dstTable: {S: aws.String(key.dstTable)},
}
input := &dynamodb.UpdateItemInput{
TableName: aws.String(ddbTable),
Key: tableKey,
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)},
},
UpdateExpression: aws.String("SET #CP = :val, #TS = :ts"),
}
_, err := ddbClient.UpdateItem(input)
if err != nil {
logger.WithFields(logging.Fields{
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
"Sequence Number": sync.checkpoint[*shardId],
"Timestamp": timestamp,
"Shard Id": *shardId,
"Error": err,
}).Error("Error in updating checkpoint on the global config")
} else {
logger.WithFields(logging.Fields{
"Source Table": key.sourceTable, "Destination Table": key.dstTable,
"Sequence Number": sync.checkpoint[*shardId],
"Timestamp": timestamp,
"Shard Id": *shardId,
}).Debug("Successfully updated global config")
}
}
func (sync *syncState) updateTimestampLocal(key primaryKey, timestamp string) {
var err error
sync.timestamp, err = time.Parse(time.RFC3339, timestamp)
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Error("Failed to update local timestamp")
}
}
func (sync *syncState) updateTimestampRemote(key primaryKey, timestamp string) {
tableKey := map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(key.sourceTable)},
dstTable: {S: aws.String(key.dstTable)},
}
input := &dynamodb.UpdateItemInput{
TableName: aws.String(ddbTable),
Key: tableKey,
ExpressionAttributeNames: map[string]*string{
"#TS": aws.String(timestp),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":ts": {S: aws.String(timestamp)},
},
UpdateExpression: aws.String("SET #TS = :ts"),
}
_, err := ddbClient.UpdateItem(input)
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Error("Failed to update checkpoint timestamp")
} else {
logger.WithFields(logging.Fields{
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Debug("Successfully updated checkpoint timestamp")
}
}
// We might need to drop stale checkpoints and expired shards
// while doing a fresh start
func (sync *syncState) dropCheckpoint(key primaryKey) {
// Check if this item exists in checkpoint table
if !sync.isCheckpointFound(key) {
return
}
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(key.sourceTable)},
dstTable: {S: aws.String(key.dstTable)},
},
TableName: aws.String(ddbTable),
}
_, err := ddbClient.DeleteItem(input)
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Error("Error in dropping checkpoint")
} else {
logger.WithFields(logging.Fields{
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Debug("Dropped checkpoint successfully")
}
}
// Remove checkpoint for <primaryKey, shardId> from the local state[key]
func (sync *syncState) expireCheckpointLocal(key primaryKey, shardId *string) {
// Remove from activeShardProcessors
sync.activeShardLock.Lock()
delete(sync.activeShardProcessors, *shardId)
logger.WithFields(logging.Fields{
"Shard Id": *shardId,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Debug("Deleted shard from active shards")
sync.activeShardLock.Unlock()
// Add to expiredShards
sync.completedShardLock.Lock()
sync.expiredShards[*shardId] = true
logger.WithFields(logging.Fields{
"Shard Id": *shardId,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Debug("Added shard to expired shards")
sync.completedShardLock.Unlock()
// Delete checkpoint for this shard
sync.checkpointLock.Lock()
delete(sync.checkpoint, *shardId)
logger.WithFields(logging.Fields{
"Shard Id": *shardId,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
}).Debug("Marking shard as complete")
sync.checkpointLock.Unlock()
}
// Remove checkpoint for <primaryKey, shardId> from the checkpoint dynamodb table
func (sync *syncState) expireCheckpointRemote(key primaryKey, shardId string) {
data, _ := json.Marshal(sync.expiredShards)
timestamp := sync.timestamp.Format(time.RFC3339)
sync.updateCheckpointRemote(key, &shardId, timestamp)
sync.checkpointLock.Lock()
input := &dynamodb.UpdateItemInput{
Key: map[string]*dynamodb.AttributeValue{
sourceTable: {S: aws.String(key.sourceTable)},
dstTable: {S: aws.String(key.dstTable)},
},
TableName: aws.String(ddbTable),
}
input.SetExpressionAttributeNames(map[string]*string{
"#ExpSh": aws.String(expiredShards),
})
input.SetExpressionAttributeValues(map[string]*dynamodb.AttributeValue{
":val": {B: data},
})
input.SetUpdateExpression("SET #ExpSh = :val")
_, err := ddbClient.UpdateItem(input)
if err != nil {
logger.WithFields(logging.Fields{
"Error": err,
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
"Shard Id": shardId,
}).Error("Error in adding expired shard")
} else {
logger.WithFields(logging.Fields{
"Source Table": key.sourceTable,
"Destination Table": key.dstTable,
"Shard Id": shardId,
}).Debug("Successfully added shard to expired shards")
}
sync.checkpointLock.Unlock()
}