Skip to content

Commit

Permalink
Pair program.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jun 27, 2024
1 parent 9125d4b commit 5926404
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
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
6 changes: 3 additions & 3 deletions sources/dynamodb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (s *StreamStore) processShard(ctx context.Context, shard *dynamodbstreams.S
}

if parentID := shard.ParentShardId; parentID != nil {
// If the parent shard exists, is it still being processed? If so, let's wait a bit and then retry.
// We must process the parent shard first before processing the child shard.
if s.storage.GetShardProcessing(*parentID) && !s.storage.GetShardProcessed(*parentID) {
// 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)
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

0 comments on commit 5926404

Please sign in to comment.