-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Implement strongly consistent reads support for Query and GetItem operations #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
acd8a92
Initial plan
Copilot c9937d9
Implement strongly consistent reads for Query and GetItem operations
Copilot cb9764b
Final implementation: strongly consistent reads for Query and GetItem…
Copilot 0c71671
Reorganize consistent read tests: move Query tests to query_test.go a…
Copilot 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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,7 @@ type TransactionAPI interface { | |||||
| } | ||||||
|
|
||||||
| type ReadAPI interface { | ||||||
| GetItem(ctx context.Context, pk, sk Attribute, out interface{}) (error, bool) | ||||||
| GetItem(ctx context.Context, pk, sk Attribute, out interface{}, opts ...GetItemOptions) (error, bool) | ||||||
|
||||||
| GetItem(ctx context.Context, pk, sk Attribute, out interface{}, opts ...GetItemOptions) (error, bool) | |
| GetItem(ctx context.Context, pk, sk Attribute, out interface{}, opts ...GetItemOptions) (bool, error) |
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package dynago | ||
|
|
||
| import ( | ||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
| ) | ||
|
|
||
| // WithConsistentRead enables strongly consistent read for Query operations | ||
| // See documentation https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html | ||
| // Note that the cost for strongly consistent reads are double of eventual consistent reads | ||
| func WithConsistentRead() QueryOptions { | ||
| return func(q *dynamodb.QueryInput) { | ||
| q.ConsistentRead = aws.Bool(true) | ||
| } | ||
| } | ||
|
|
||
| // WithConsistentReadItem enables strongly consistent read for GetItem operations | ||
| // See documentation https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html | ||
| // Note that the cost for strongly consistent reads are double of eventual consistent reads | ||
| func WithConsistentReadItem() GetItemOptions { | ||
| return func(g *dynamodb.GetItemInput) { | ||
| g.ConsistentRead = aws.Bool(true) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "context" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/oolio-group/dynago" | ||
| ) | ||
|
|
||
| func TestGetItemWithConsistentRead(t *testing.T) { | ||
| testCases := []struct { | ||
| title string | ||
| pk dynago.Attribute | ||
| sk dynago.Attribute | ||
| opts []dynago.GetItemOptions | ||
| source *User | ||
| expectedFound bool | ||
| expectConsistentRead bool | ||
| }{ | ||
| { | ||
| title: "get item with consistent read enabled", | ||
| pk: dynago.StringValue("users#consistent_getitem"), | ||
| sk: dynago.StringValue("user#consistent"), | ||
| opts: []dynago.GetItemOptions{dynago.WithConsistentReadItem()}, | ||
| source: &User{ | ||
| Id: "consistent_user", | ||
| Pk: "users#consistent_getitem", | ||
| Sk: "user#consistent", | ||
| }, | ||
| expectedFound: true, | ||
| expectConsistentRead: true, | ||
| }, | ||
| { | ||
| title: "get item without consistent read (default eventual consistency)", | ||
| pk: dynago.StringValue("users#eventual_getitem"), | ||
| sk: dynago.StringValue("user#eventual"), | ||
| opts: []dynago.GetItemOptions{}, | ||
| source: &User{ | ||
| Id: "eventual_user", | ||
| Pk: "users#eventual_getitem", | ||
| Sk: "user#eventual", | ||
| }, | ||
| expectedFound: true, | ||
| expectConsistentRead: false, | ||
| }, | ||
| { | ||
| title: "get item not found with consistent read", | ||
| pk: dynago.StringValue("users#notfound"), | ||
| sk: dynago.StringValue("user#notfound"), | ||
| opts: []dynago.GetItemOptions{dynago.WithConsistentReadItem()}, | ||
| source: nil, | ||
| expectedFound: false, | ||
| expectConsistentRead: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.title, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| table := prepareTable(t) | ||
| ctx := context.TODO() | ||
|
|
||
| // prepare the table, write test sample data if provided | ||
| if tc.source != nil { | ||
| err := table.PutItem(ctx, tc.pk, tc.sk, tc.source) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error setting up test data: %s", err) | ||
| } | ||
| } | ||
|
|
||
| var out User | ||
| err, found := table.GetItem(ctx, tc.pk, tc.sk, &out, tc.opts...) | ||
shidil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error %s", err) | ||
| } | ||
|
|
||
| if found != tc.expectedFound { | ||
| t.Errorf("expected found to be %v; got %v", tc.expectedFound, found) | ||
| } | ||
|
|
||
| if tc.expectedFound && tc.source != nil { | ||
| if !reflect.DeepEqual(*tc.source, out) { | ||
| t.Errorf("expected GetItem to return %v; got %v", *tc.source, out) | ||
| } | ||
| } | ||
|
|
||
| // Note: We can't directly verify that ConsistentRead was set in the actual DynamoDB request | ||
| // because that would require mocking the AWS client. The test verifies that the function | ||
| // can be called without error and returns expected results. | ||
| }) | ||
| } | ||
| } | ||
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
Binary file not shown.
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.
Uh oh!
There was an error while loading. Please reload this page.