forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_kinesis_checkpointer.go
440 lines (391 loc) · 13.4 KB
/
aws_kinesis_checkpointer.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package input
// Inspired by Patrick Robinson https://github.com/patrobinson/gokini
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/dafanshu/benthos/v3/internal/docs"
)
//------------------------------------------------------------------------------
var dynamoDBCheckpointFields = docs.FieldSpecs{
docs.FieldCommon("table", "The name of the table to access."),
docs.FieldCommon("create", "Whether, if the table does not exist, it should be created."),
docs.FieldAdvanced("billing_mode", "When creating the table determines the billing mode.").HasOptions("PROVISIONED", "PAY_PER_REQUEST"),
docs.FieldAdvanced("read_capacity_units", "Set the provisioned read capacity when creating the table with a `billing_mode` of `PROVISIONED`."),
docs.FieldAdvanced("write_capacity_units", "Set the provisioned write capacity when creating the table with a `billing_mode` of `PROVISIONED`."),
}
// DynamoDBCheckpointConfig contains configuration parameters for a DynamoDB
// based checkpoint store for Kinesis.
type DynamoDBCheckpointConfig struct {
Table string `json:"table" yaml:"table"`
Create bool `json:"create" yaml:"create"`
ReadCapacityUnits int64 `json:"read_capacity_units" yaml:"read_capacity_units"`
WriteCapacityUnits int64 `json:"write_capacity_units" yaml:"write_capacity_units"`
BillingMode string `json:"billing_mode" yaml:"billing_mode"`
}
// NewDynamoDBCheckpointConfig returns a DynamoDBCheckpoint config struct with
// default values.
func NewDynamoDBCheckpointConfig() DynamoDBCheckpointConfig {
return DynamoDBCheckpointConfig{
Table: "",
Create: false,
ReadCapacityUnits: 0,
WriteCapacityUnits: 0,
BillingMode: "PAY_PER_REQUEST",
}
}
//------------------------------------------------------------------------------
// Common errors that might occur throughout checkpointing.
var (
ErrLeaseNotAcquired = errors.New("the shard could not be leased due to a collision")
)
// awsKinesisCheckpointer manages the shard checkpointing for a given client
// identifier.
type awsKinesisCheckpointer struct {
conf DynamoDBCheckpointConfig
clientID string
leaseDuration time.Duration
commitPeriod time.Duration
svc dynamodbiface.DynamoDBAPI
}
// newAWSKinesisCheckpointer creates a new DynamoDB checkpointer from an AWS
// session and a configuration struct.
func newAWSKinesisCheckpointer(
session *session.Session,
clientID string,
conf DynamoDBCheckpointConfig,
leaseDuration time.Duration,
commitPeriod time.Duration,
) (*awsKinesisCheckpointer, error) {
c := &awsKinesisCheckpointer{
conf: conf,
leaseDuration: leaseDuration,
commitPeriod: commitPeriod,
svc: dynamodb.New(session),
clientID: clientID,
}
if err := c.ensureTableExists(); err != nil {
return nil, err
}
return c, nil
}
//------------------------------------------------------------------------------
func (k *awsKinesisCheckpointer) ensureTableExists() error {
_, err := k.svc.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(k.conf.Table),
})
if err == nil {
return nil
}
if aerr, ok := err.(awserr.Error); !ok || aerr.Code() != dynamodb.ErrCodeResourceNotFoundException {
return err
}
if !k.conf.Create {
return fmt.Errorf("target table %v does not exist", k.conf.Table)
}
input := &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{AttributeName: aws.String("StreamID"), AttributeType: aws.String("S")},
{AttributeName: aws.String("ShardID"), AttributeType: aws.String("S")},
},
BillingMode: aws.String(k.conf.BillingMode),
KeySchema: []*dynamodb.KeySchemaElement{
{AttributeName: aws.String("StreamID"), KeyType: aws.String("HASH")},
{AttributeName: aws.String("ShardID"), KeyType: aws.String("RANGE")},
},
TableName: aws.String(k.conf.Table),
}
if k.conf.BillingMode == "PROVISIONED" {
input.ProvisionedThroughput = &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(k.conf.ReadCapacityUnits),
WriteCapacityUnits: aws.Int64(k.conf.WriteCapacityUnits),
}
}
if _, err = k.svc.CreateTable(input); err != nil {
return fmt.Errorf("failed to create table: %w", err)
}
return nil
}
// awsKinesisCheckpoint contains details of a shard checkpoint.
type awsKinesisCheckpoint struct {
SequenceNumber string
ClientID *string
LeaseTimeout *time.Time
}
// Both checkpoint and err can be nil when the item does not exist.
func (k *awsKinesisCheckpointer) getCheckpoint(ctx context.Context, streamID, shardID string) (*awsKinesisCheckpoint, error) {
rawItem, err := k.svc.GetItemWithContext(ctx, &dynamodb.GetItemInput{
TableName: aws.String(k.conf.Table),
Key: map[string]*dynamodb.AttributeValue{
"ShardID": {
S: aws.String(shardID),
},
"StreamID": {
S: aws.String(streamID),
},
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == dynamodb.ErrCodeResourceNotFoundException {
return nil, nil
}
}
return nil, err
}
c := awsKinesisCheckpoint{}
if s, ok := rawItem.Item["SequenceNumber"]; ok && s.S != nil {
c.SequenceNumber = *s.S
} else {
return nil, errors.New("sequence ID was not found in checkpoint")
}
if s, ok := rawItem.Item["ClientID"]; ok && s.S != nil {
c.ClientID = s.S
}
if s, ok := rawItem.Item["LeaseTimeout"]; ok && s.S != nil {
timeout, err := time.Parse(time.RFC3339Nano, *s.S)
if err != nil {
return nil, err
}
c.LeaseTimeout = &timeout
}
return &c, nil
}
//------------------------------------------------------------------------------
// awsKinesisClientClaim represents a shard claimed by a client.
type awsKinesisClientClaim struct {
ShardID string
LeaseTimeout time.Time
}
// AllClaims returns a map of client IDs to shards claimed by that client,
// including the lease timeout of the claim.
func (k *awsKinesisCheckpointer) AllClaims(ctx context.Context, streamID string) (map[string][]awsKinesisClientClaim, error) {
clientClaims := make(map[string][]awsKinesisClientClaim)
var scanErr error
if err := k.svc.ScanPagesWithContext(ctx, &dynamodb.ScanInput{
TableName: aws.String(k.conf.Table),
FilterExpression: aws.String("StreamID = :stream_id"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":stream_id": {
S: &streamID,
},
},
}, func(page *dynamodb.ScanOutput, last bool) bool {
for _, i := range page.Items {
var clientID string
if s, ok := i["ClientID"]; ok && s.S != nil {
clientID = *s.S
} else {
continue
}
var claim awsKinesisClientClaim
if s, ok := i["ShardID"]; ok && s.S != nil {
claim.ShardID = *s.S
}
if claim.ShardID == "" {
scanErr = errors.New("failed to extract shard id from claim")
return false
}
if s, ok := i["LeaseTimeout"]; ok && s.S != nil {
if claim.LeaseTimeout, scanErr = time.Parse(time.RFC3339Nano, *s.S); scanErr != nil {
scanErr = fmt.Errorf("failed to parse claim lease: %w", scanErr)
return false
}
}
if claim.LeaseTimeout.IsZero() {
scanErr = errors.New("failed to extract lease timeout from claim")
return false
}
clientClaims[clientID] = append(clientClaims[clientID], claim)
}
return true
}); err != nil {
return nil, err
}
return clientClaims, scanErr
}
// Claim attempts to claim a shard for a particular stream ID. If fromClientID
// is specified the shard is stolen from that particular client, and the
// operation fails if a different client ID has it claimed.
//
// If fromClientID is specified this call will claim the new shard but block
// for a period of time before reacquiring the sequence ID. This allows the
// client we're claiming from to gracefully update the sequence number before
// stopping.
func (k *awsKinesisCheckpointer) Claim(ctx context.Context, streamID, shardID, fromClientID string) (string, error) {
newLeaseTimeoutString := time.Now().Add(k.leaseDuration).Format(time.RFC3339Nano)
var conditionalExpression string
expressionAttributeValues := map[string]*dynamodb.AttributeValue{
":new_client_id": {
S: &k.clientID,
},
":new_lease_timeout": {
S: &newLeaseTimeoutString,
},
}
if len(fromClientID) > 0 {
conditionalExpression = "ClientID = :old_client_id"
expressionAttributeValues[":old_client_id"] = &dynamodb.AttributeValue{
S: &fromClientID,
}
} else {
conditionalExpression = "attribute_not_exists(ClientID)"
}
res, err := k.svc.UpdateItemWithContext(ctx, &dynamodb.UpdateItemInput{
ReturnValues: aws.String("ALL_OLD"),
TableName: aws.String(k.conf.Table),
ConditionExpression: aws.String(conditionalExpression),
UpdateExpression: aws.String("SET ClientID = :new_client_id, LeaseTimeout = :new_lease_timeout"),
ExpressionAttributeValues: expressionAttributeValues,
Key: map[string]*dynamodb.AttributeValue{
"StreamID": {
S: &streamID,
},
"ShardID": {
S: &shardID,
},
},
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
return "", ErrLeaseNotAcquired
}
}
return "", err
}
var startingSequence string
if s, ok := res.Attributes["SequenceNumber"]; ok && s.S != nil {
startingSequence = *s.S
}
var currentLease time.Time
if s, ok := res.Attributes["LeaseTimeout"]; ok && s.S != nil {
currentLease, _ = time.Parse(time.RFC3339Nano, *s.S)
}
// Since we've aggressively stolen a shard then it's pretty much guaranteed
// that the client we're stealing from is still processing. What we do is we
// wait a grace period calculated by how long since the previous checkpoint
// and then reacquire the sequence.
//
// This allows the victim client to update the checkpoint with the final
// sequence as it yields the shard.
if len(fromClientID) > 0 && time.Since(currentLease) < k.leaseDuration {
// Wait for the estimated next checkpoint time plus a grace period of
// one second.
waitFor := k.leaseDuration - time.Since(currentLease) + time.Second
select {
case <-time.After(waitFor):
case <-ctx.Done():
return "", ctx.Err()
}
cp, err := k.getCheckpoint(ctx, streamID, shardID)
if err != nil {
return "", err
}
startingSequence = cp.SequenceNumber
}
return startingSequence, nil
}
// Checkpoint attempts to set a sequence number for a stream shard. Returns a
// boolean indicating whether this shard is still owned by the client.
//
// If the shard has been claimed by a new client the sequence will still be set
// so that the new client can begin with the latest sequence.
//
// If final is true the client ID is removed from the checkpoint, indicating
// that this client is finished with the shard.
func (k *awsKinesisCheckpointer) Checkpoint(ctx context.Context, streamID, shardID, sequenceNumber string, final bool) (bool, error) {
item := map[string]*dynamodb.AttributeValue{
"StreamID": {
S: &streamID,
},
"ShardID": {
S: &shardID,
},
}
if len(sequenceNumber) > 0 {
item["SequenceNumber"] = &dynamodb.AttributeValue{
S: &sequenceNumber,
}
}
if !final {
item["ClientID"] = &dynamodb.AttributeValue{
S: &k.clientID,
}
item["LeaseTimeout"] = &dynamodb.AttributeValue{
S: aws.String(time.Now().Add(k.leaseDuration).Format(time.RFC3339Nano)),
}
}
if _, err := k.svc.PutItem(&dynamodb.PutItemInput{
ConditionExpression: aws.String("ClientID = :client_id"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":client_id": {
S: &k.clientID,
},
},
TableName: aws.String(k.conf.Table),
Item: item,
}); err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
return false, nil
}
}
return false, err
}
return true, nil
}
// Yield updates an existing checkpoint sequence number and no other fields.
// This should be done after a non-final checkpoint indicates that shard has
// been stolen and allows the thief client to start with the latest sequence
// rather than the sequence at the point of the theft.
//
// This call is entirely optional, but the benefit is a reduction in duplicated
// messages during a rebalance of shards.
func (k *awsKinesisCheckpointer) Yield(ctx context.Context, streamID, shardID, sequenceNumber string) error {
if sequenceNumber == "" {
// Nothing to present to the thief
return nil
}
_, err := k.svc.UpdateItemWithContext(ctx, &dynamodb.UpdateItemInput{
TableName: aws.String(k.conf.Table),
Key: map[string]*dynamodb.AttributeValue{
"StreamID": {
S: &streamID,
},
"ShardID": {
S: &shardID,
},
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":new_sequence_number": {
S: &sequenceNumber,
},
},
UpdateExpression: aws.String("SET SequenceNumber = :new_sequence_number"),
})
return err
}
// Delete attempts to delete a checkpoint, this should be called when a shard is
// emptied.
func (k *awsKinesisCheckpointer) Delete(ctx context.Context, streamID, shardID string) error {
_, err := k.svc.DeleteItemWithContext(ctx, &dynamodb.DeleteItemInput{
TableName: aws.String(k.conf.Table),
Key: map[string]*dynamodb.AttributeValue{
"StreamID": {
S: &streamID,
},
"ShardID": {
S: &shardID,
},
},
})
return err
}
//------------------------------------------------------------------------------