Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB - Respect lineage #422

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions sources/dynamodb/offsets/offsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ type OffsetStorage struct {
ttlMap *ttlmap.TTLMap
}

func shardSeenKey(shardId string) string {
return fmt.Sprintf("seen#shardId#%s", shardId)
}

func (o *OffsetStorage) SetShardSeen(shardID string) {
o.ttlMap.Set(ttlmap.SetArgs{
Key: shardSeenKey(shardID),
Value: true,
DoNotFlushToDisk: true,
}, ShardExpirationAndBuffer)
}

func (o *OffsetStorage) GetShardSeen(shardID string) bool {
_, isOk := o.ttlMap.Get(shardSeenKey(shardID))
return isOk
}

func shardProcessingKey(shardId string) string {
return fmt.Sprintf("processing#shardId#%s", shardId)
}
Expand Down
14 changes: 12 additions & 2 deletions sources/dynamodb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ func (s *StreamStore) ListenToChannel(ctx context.Context, writer writers.Writer
}

func (s *StreamStore) processShard(ctx context.Context, shard *dynamodbstreams.Shard, writer writers.Writer) {
var attempts int

// Is there another go-routine processing this shard?
if s.storage.GetShardProcessing(*shard.ShardId) {
return
}

if parentID := shard.ParentShardId; parentID != nil {
// Have we seen the parent? If so, let's wait for processing to finish
// If we haven't seen the parent, then we can assume this is the parent and we don't need to wait.
if s.storage.GetShardSeen(*parentID) && !s.storage.GetShardProcessed(*parentID) {
slog.Info("Parent shard is being processed, let's sleep 3s and retry", slog.String("shardId", *shard.ShardId), slog.String("parentShardId", *parentID))
time.Sleep(3 * time.Second)
s.processShard(ctx, shard, writer)
return
}
}

// If no one is processing it, let's mark it as being processed.
s.storage.SetShardProcessing(*shard.ShardId)
if s.storage.GetShardProcessed(*shard.ShardId) {
Expand Down Expand Up @@ -103,6 +112,7 @@ func (s *StreamStore) processShard(ctx context.Context, shard *dynamodbstreams.S
logger.Panic("Failed to publish messages, exiting...", slog.Any("err", err))
}

var attempts int
if len(getRecordsOutput.Records) > 0 {
attempts = 0
lastRecord := getRecordsOutput.Records[len(getRecordsOutput.Records)-1]
Expand Down
5 changes: 5 additions & 0 deletions sources/dynamodb/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (s *StreamStore) scanForNewShards() error {
return fmt.Errorf("failed to describe stream: %w", err)
}

// We need two loops because we need to mark all the shards as "SEEN" before we process.
for _, shard := range result.StreamDescription.Shards {
s.storage.SetShardSeen(*shard.ShardId)
}

for _, shard := range result.StreamDescription.Shards {
s.shardChan <- shard
}
Expand Down