-
Notifications
You must be signed in to change notification settings - Fork 373
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
feat: add x-goog-api-client headers for retry metrics #1920
Changes from 4 commits
6f28703
1ea0eed
d798c02
61a4593
4978466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library'; | |
import {Readable, Writable} from 'stream'; | ||
import retry = require('async-retry'); | ||
import {RetryOptions, PreconditionOptions} from './storage'; | ||
import * as packageJson from '../package.json'; | ||
import * as uuid from 'uuid'; | ||
|
||
const NOT_FOUND_STATUS_CODE = 404; | ||
const TERMINATED_UPLOAD_STATUS_CODE = 410; | ||
|
@@ -262,6 +264,11 @@ export class Upload extends Writable { | |
contentLength: number | '*'; | ||
retryOptions: RetryOptions; | ||
timeOfFirstRequest: number; | ||
private currentInvocationId = { | ||
chunk: uuid.v4(), | ||
uri: uuid.v4(), | ||
offset: uuid.v4(), | ||
}; | ||
private upstreamChunkBuffer: Buffer = Buffer.alloc(0); | ||
private chunkBufferEncoding?: BufferEncoding = undefined; | ||
private numChunksReadInRequest = 0; | ||
|
@@ -530,6 +537,7 @@ export class Upload extends Writable { | |
protected async createURIAsync(): Promise<string> { | ||
const metadata = this.metadata; | ||
|
||
// Check if headers already exist before creating new ones | ||
const reqOpts: GaxiosOptions = { | ||
method: 'POST', | ||
url: [this.baseURI, this.bucket, 'o'].join('/'), | ||
|
@@ -541,7 +549,9 @@ export class Upload extends Writable { | |
this.params | ||
), | ||
data: metadata, | ||
headers: {}, | ||
headers: { | ||
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.uri}`, | ||
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. Just to clarify-- I spoke with @frankyn and he mentioned that each request in a resumable upload should have its own invocation id (unless it is a retry of a request that failed). Is that reflective of what's happening here? (Looks like it is from the test cases, but wanted to double check) 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. Correct, that is what is happening here. The invocation ID is only changed after a successful response. If a failure case is hit the same ID will be used on the retry. Each request (create a URI, send a chunk, etc...) all utilize a new ID. |
||
}, | ||
}; | ||
|
||
if (metadata.contentLength) { | ||
|
@@ -572,6 +582,8 @@ export class Upload extends Writable { | |
async (bail: (err: Error) => void) => { | ||
try { | ||
const res = await this.makeRequest(reqOpts); | ||
// We have successfully got a URI we can now create a new invocation id | ||
this.currentInvocationId.uri = uuid.v4(); | ||
return res.headers.location; | ||
} catch (err) { | ||
const e = err as GaxiosError; | ||
|
@@ -707,20 +719,20 @@ export class Upload extends Writable { | |
}, | ||
}); | ||
|
||
let headers: GaxiosOptions['headers'] = {}; | ||
const headers: GaxiosOptions['headers'] = { | ||
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.chunk}`, | ||
}; | ||
|
||
// If using multiple chunk upload, set appropriate header | ||
if (multiChunkMode && expectedUploadSize) { | ||
// The '-1' is because the ending byte is inclusive in the request. | ||
const endingByte = expectedUploadSize + this.numBytesWritten - 1; | ||
headers = { | ||
'Content-Length': expectedUploadSize, | ||
'Content-Range': `bytes ${this.offset}-${endingByte}/${this.contentLength}`, | ||
}; | ||
headers['Content-Length'] = expectedUploadSize; | ||
headers[ | ||
'Content-Range' | ||
] = `bytes ${this.offset}-${endingByte}/${this.contentLength}`; | ||
} else { | ||
headers = { | ||
'Content-Range': `bytes ${this.offset}-*/${this.contentLength}`, | ||
}; | ||
headers['Content-Range'] = `bytes ${this.offset}-*/${this.contentLength}`; | ||
} | ||
|
||
const reqOpts: GaxiosOptions = { | ||
|
@@ -750,6 +762,9 @@ export class Upload extends Writable { | |
return; | ||
} | ||
|
||
// At this point we can safely create a new id for the chunk | ||
this.currentInvocationId.chunk = uuid.v4(); | ||
|
||
const shouldContinueWithNextMultiChunkRequest = | ||
this.chunkSize && | ||
resp.status === RESUMABLE_INCOMPLETE_STATUS_CODE && | ||
|
@@ -849,10 +864,16 @@ export class Upload extends Writable { | |
const opts: GaxiosOptions = { | ||
method: 'PUT', | ||
url: this.uri!, | ||
headers: {'Content-Length': 0, 'Content-Range': 'bytes */*'}, | ||
headers: { | ||
'Content-Length': 0, | ||
'Content-Range': 'bytes */*', | ||
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.offset}`, | ||
}, | ||
}; | ||
try { | ||
const resp = await this.makeRequest(opts); | ||
// Successfully got the offset we can now create a new offset invocation id | ||
this.currentInvocationId.offset = uuid.v4(); | ||
if (resp.status === RESUMABLE_INCOMPLETE_STATUS_CODE) { | ||
if (resp.headers.range) { | ||
const range = resp.headers.range as string; | ||
|
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.
I didn't catch this line during our pairing, but I believe this may break
package.json#files
for consumers (vianpm i @google-cloud/storage
) asbuild/package.json
wouldn't be included [npm docs].We currently use this pattern for including
package.json
(a little messy):nodejs-storage/src/storage.ts
Lines 617 to 625 in cbe2590
We have a few options:
build/package.json
topackage.json#files
. Drawback: some tooling may believebuild
in itself is a separate package (aspackage.json
) and may misbehave.src/storage
. Drawback: messy-looking.CC: @bcoe - as I'm working on an easy
package.json
import proposal for Node.js.