-
Notifications
You must be signed in to change notification settings - Fork 8
added closed shard handling #25
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
Open
farvez10
wants to merge
2
commits into
daangn:main
Choose a base branch
from
farvez10:handling-closed-shards
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -311,11 +311,10 @@ func (k *Kinesumer) listShards(stream string) (Shards, error) { | |
| } | ||
| var shards []*Shard | ||
| for _, shard := range output.Shards { | ||
| // TODO(mingrammer): handle CLOSED shards. | ||
| // Closed shards will be handled while consuming the records. | ||
| if shard.SequenceNumberRange.EndingSequenceNumber == nil { | ||
| shards = append(shards, &Shard{ | ||
| ID: *shard.ShardId, | ||
| Closed: shard.SequenceNumberRange.EndingSequenceNumber != nil, | ||
| ID: *shard.ShardId, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -330,11 +329,10 @@ func (k *Kinesumer) listShards(stream string) (Shards, error) { | |
| return nil, errors.WithStack(err) | ||
| } | ||
| for _, shard := range output.Shards { | ||
| // Skip CLOSED shards. | ||
| // Closed shards will be handled while consuming the records. | ||
| if shard.SequenceNumberRange.EndingSequenceNumber == nil { | ||
| shards = append(shards, &Shard{ | ||
| ID: *shard.ShardId, | ||
| Closed: shard.SequenceNumberRange.EndingSequenceNumber != nil, | ||
| ID: *shard.ShardId, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -345,7 +343,8 @@ func (k *Kinesumer) listShards(stream string) (Shards, error) { | |
|
|
||
| // Consume consumes messages from Kinesis. | ||
| func (k *Kinesumer) Consume( | ||
| streams []string) (<-chan *Record, error) { | ||
| streams []string, | ||
| ) (<-chan *Record, error) { | ||
| k.streams = streams | ||
|
|
||
| ctx := context.Background() | ||
|
|
@@ -635,15 +634,8 @@ func (k *Kinesumer) consumeLoop(stream string, shard *Shard) { | |
| default: | ||
| time.Sleep(k.scanInterval) | ||
| records, closed := k.consumeOnce(stream, shard) | ||
| if closed { | ||
| k.cleanupOffsets(stream, shard) | ||
| return // Close consume loop if shard is CLOSED and has no data. | ||
| } | ||
|
|
||
| n := len(records) | ||
| if n == 0 { | ||
| continue | ||
| } | ||
|
|
||
| for i, record := range records { | ||
| r := &Record{ | ||
|
|
@@ -656,6 +648,13 @@ func (k *Kinesumer) consumeLoop(stream string, shard *Shard) { | |
| if k.autoCommit && i == n-1 { | ||
| k.MarkRecord(r) | ||
| } | ||
|
|
||
| // Closed shard may have remaining data, | ||
| // so clean up is executed, once the records are pushed to the channel. | ||
| if closed { | ||
| k.cleanupOffsets(stream, shard) | ||
| return // Close consume loop if shard is CLOSED and has no data. | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -694,17 +693,24 @@ func (k *Kinesumer) consumeOnce(stream string, shard *Shard) ([]*kinesis.Record, | |
| } | ||
| defer k.nextIters[stream].Store(shard.ID, output.NextShardIterator) // Update iter. | ||
|
|
||
| n := len(output.Records) | ||
| // We no longer care about shards that have no records left and are in the "CLOSED" state. | ||
| if n == 0 { | ||
| return nil, shard.Closed | ||
| } | ||
| shard.Closed = getShardStatus(output) | ||
|
|
||
| // outer function has the for loop that takes care of the empty records case | ||
| // so not needed to check it here. | ||
| return output.Records, shard.Closed | ||
| } | ||
|
|
||
| return output.Records, false | ||
| // getShardStatus returns whether the shard is closed or not. | ||
| func getShardStatus(output *kinesis.GetRecordsOutput) bool { | ||
| // set the shard closed state. | ||
| // If shard has no data, NextShardIterator will be nil. | ||
| // Reference: https://docs.aws.amazon.com/cli/latest/reference/kinesis/get-records.html#output | ||
| return output.NextShardIterator == nil | ||
| } | ||
|
|
||
| func (k *Kinesumer) getNextShardIterator( | ||
| ctx context.Context, stream, shardID string) (*string, error) { | ||
| ctx context.Context, stream, shardID string, | ||
| ) (*string, error) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my "gofmt" tool's change, will change if not needed |
||
| if iter, ok := k.nextIters[stream].Load(shardID); ok { | ||
| return iter.(*string), nil | ||
| } | ||
|
|
@@ -729,7 +735,7 @@ func (k *Kinesumer) getNextShardIterator( | |
| } | ||
|
|
||
| func (k *Kinesumer) commitPeriodically() { | ||
| var checkPointTicker = time.NewTicker(k.commitInterval) | ||
| checkPointTicker := time.NewTicker(k.commitInterval) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my "gofmt" tool's change, will change if not needed |
||
|
|
||
| for { | ||
| select { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my "gofmt" tool's change, will change if not needed