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

chore(storage): add idempotency header #7602

Merged
merged 4 commits into from
Mar 21, 2023
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
5 changes: 5 additions & 0 deletions storage/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ func setRetryHeaderHTTP(req interface{ Header() http.Header }) func(string, int)
return
}
header := req.Header()
// TODO(b/274504690): Consider dropping gccl-invocation-id key since it
// duplicates the X-Goog-Gcs-Idempotency-Token header (added in v1.31.0).
invocationHeader := fmt.Sprintf("gccl-invocation-id/%v gccl-attempt-count/%v", invocationID, attempts)
xGoogHeader := strings.Join([]string{invocationHeader, xGoogDefaultHeader}, " ")
header.Set("x-goog-api-client", xGoogHeader)
// Also use the invocationID for the idempotency token header, which will
// enable idempotent retries for more operations.
header.Set("x-goog-gcs-idempotency-token", invocationID)
}
}

Expand Down
23 changes: 14 additions & 9 deletions storage/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ func TestInvoke(t *testing.T) {
t.Run(test.desc, func(s *testing.T) {
counter := 0
req := &fakeApiaryRequest{header: http.Header{}}
var initialHeader string
var initialClientHeader, initialIdempotencyHeader string
call := func() error {
if counter == 0 {
initialHeader = req.Header()["X-Goog-Api-Client"][0]
initialClientHeader = req.Header()["X-Goog-Api-Client"][0]
initialIdempotencyHeader = req.Header()["X-Goog-Gcs-Idempotency-Token"][0]
}
counter++
if counter <= test.count {
Expand All @@ -186,22 +187,26 @@ func TestInvoke(t *testing.T) {
} else if !test.expectFinalErr && got != test.initialErr {
s.Errorf("got %v, want %v", got, test.initialErr)
}
gotHeader := req.Header()["X-Goog-Api-Client"][0]
gotClientHeader := req.Header()["X-Goog-Api-Client"][0]
gotIdempotencyHeader := req.Header()["X-Goog-Gcs-Idempotency-Token"][0]
wantAttempts := 1 + test.count
if !test.expectFinalErr {
wantAttempts = 1
}
wantHeader := strings.ReplaceAll(initialHeader, "gccl-attempt-count/1", fmt.Sprintf("gccl-attempt-count/%v", wantAttempts))
if gotHeader != wantHeader {
t.Errorf("case %q, retry header:\ngot %v\nwant %v", test.desc, gotHeader, wantHeader)
wantClientHeader := strings.ReplaceAll(initialClientHeader, "gccl-attempt-count/1", fmt.Sprintf("gccl-attempt-count/%v", wantAttempts))
if gotClientHeader != wantClientHeader {
t.Errorf("case %q, retry header:\ngot %v\nwant %v", test.desc, gotClientHeader, wantClientHeader)
}
wantHeaderFormat := "gccl-invocation-id/.{36} gccl-attempt-count/[0-9]+ gl-go/.* gccl/"
match, err := regexp.MatchString(wantHeaderFormat, gotHeader)
wantClientHeaderFormat := "gccl-invocation-id/.{36} gccl-attempt-count/[0-9]+ gl-go/.* gccl/"
match, err := regexp.MatchString(wantClientHeaderFormat, gotClientHeader)
if err != nil {
s.Fatalf("compiling regexp: %v", err)
}
if !match {
s.Errorf("X-Goog-Api-Client header has wrong format\ngot %v\nwant regex matching %v", gotHeader, wantHeaderFormat)
s.Errorf("X-Goog-Api-Client header has wrong format\ngot %v\nwant regex matching %v", gotClientHeader, wantClientHeaderFormat)
}
if gotIdempotencyHeader != initialIdempotencyHeader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider using cmp.Diff and reporting the diff in the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into this, but unfortunately it doesn't work because x-goog-api-client also contains library & Go version information. We search for a substring regex for the added components rather than looking for an exact match for the whole thing. I guess we could reconstruct those components as well but it will make the test brittle to non-breaking changes in apiary, which I'd rather not do.

t.Errorf("case %q, idempotency header:\ngot %v\nwant %v", test.desc, gotIdempotencyHeader, initialIdempotencyHeader)
}
})
}
Expand Down