-
Notifications
You must be signed in to change notification settings - Fork 111
feat(x-goog-spanner-request-id): add bases #2211
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
olavloite
merged 8 commits into
googleapis:main
from
odeke-em:x-goog-spanner-request-id-bases
Feb 9, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7a4a01
feat(x-goog-spanner-request-id): add bases
odeke-em cc6fc4a
Fix lint
odeke-em aa68686
Address review comments
odeke-em b277b36
Address more review feedback
odeke-em 444a6f5
fix: format process-id as a 32-bit hex number
olavloite 92eef36
chore: fix formatting issue
olavloite 99ce0d8
fix: left-pad process-id
olavloite 3df1d41
fix: no of elements after process-id should be 4
olavloite 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 |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ system-test/*key.json | |
| .DS_Store | ||
| package-lock.json | ||
| __pycache__ | ||
| .idea | ||
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,200 @@ | ||
| /** | ||
| * Copyright 2025 Google LLC. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import {randomBytes} from 'crypto'; | ||
| // eslint-disable-next-line n/no-extraneous-import | ||
| import * as grpc from '@grpc/grpc-js'; | ||
| const randIdForProcess = randomBytes(8) | ||
| .readUint32LE(0) | ||
| .toString(16) | ||
| .padStart(8, '0'); | ||
| const X_GOOG_SPANNER_REQUEST_ID_HEADER = 'x-goog-spanner-request-id'; | ||
|
|
||
| class AtomicCounter { | ||
| private readonly backingBuffer: Uint32Array; | ||
|
|
||
| constructor(initialValue?: number) { | ||
| this.backingBuffer = new Uint32Array( | ||
| new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT) | ||
| ); | ||
| if (initialValue) { | ||
| this.increment(initialValue); | ||
| } | ||
| } | ||
|
|
||
| public increment(n?: number): number { | ||
| if (!n) { | ||
| n = 1; | ||
| } | ||
| Atomics.add(this.backingBuffer, 0, n); | ||
| return this.value(); | ||
| } | ||
|
|
||
| public value(): number { | ||
| return Atomics.load(this.backingBuffer, 0); | ||
| } | ||
|
|
||
| public toString(): string { | ||
| return `${this.value()}`; | ||
| } | ||
|
|
||
| public reset() { | ||
| Atomics.store(this.backingBuffer, 0, 0); | ||
| } | ||
| } | ||
|
|
||
| const REQUEST_HEADER_VERSION = 1; | ||
|
|
||
| function craftRequestId( | ||
| nthClientId: number, | ||
| channelId: number, | ||
| nthRequest: number, | ||
| attempt: number | ||
| ) { | ||
| return `${REQUEST_HEADER_VERSION}.${randIdForProcess}.${nthClientId}.${channelId}.${nthRequest}.${attempt}`; | ||
| } | ||
|
|
||
| const nthClientId = new AtomicCounter(); | ||
|
|
||
| // Only exported for deterministic testing. | ||
| export function resetNthClientId() { | ||
| nthClientId.reset(); | ||
| } | ||
|
|
||
| /* | ||
| * nextSpannerClientId increments the internal | ||
| * counter for created SpannerClients, for use | ||
| * with x-goog-spanner-request-id. | ||
| */ | ||
| function nextSpannerClientId(): number { | ||
| nthClientId.increment(1); | ||
| return nthClientId.value(); | ||
| } | ||
|
|
||
| function newAtomicCounter(n?: number): AtomicCounter { | ||
| return new AtomicCounter(n); | ||
| } | ||
|
|
||
| interface withHeaders { | ||
| headers: {[k: string]: string}; | ||
| } | ||
|
|
||
| function extractRequestID(config: any): string { | ||
| if (!config) { | ||
| return ''; | ||
| } | ||
|
|
||
| const hdrs = config as withHeaders; | ||
| if (hdrs && hdrs.headers) { | ||
| return hdrs.headers[X_GOOG_SPANNER_REQUEST_ID_HEADER]; | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| function injectRequestIDIntoError(config: any, err: Error) { | ||
| if (!err) { | ||
| return; | ||
| } | ||
|
|
||
| // Inject that RequestID into the actual | ||
| // error object regardless of the type. | ||
| const requestID = extractRequestID(config); | ||
| if (requestID) { | ||
| Object.assign(err, {requestID: requestID}); | ||
| } | ||
| } | ||
|
|
||
| interface withNextNthRequest { | ||
| _nextNthRequest: Function; | ||
| } | ||
|
|
||
| interface withMetadataWithRequestId { | ||
| _nthClientId: number; | ||
| _channelId: number; | ||
| } | ||
|
|
||
| function injectRequestIDIntoHeaders( | ||
| headers: {[k: string]: string}, | ||
| session: any, | ||
olavloite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| nthRequest?: number, | ||
| attempt?: number | ||
| ) { | ||
| if (!session) { | ||
| return headers; | ||
| } | ||
|
|
||
| if (!nthRequest) { | ||
| const database = session.parent as withNextNthRequest; | ||
| if (!(database && typeof database._nextNthRequest === 'function')) { | ||
| return headers; | ||
| } | ||
| nthRequest = database._nextNthRequest(); | ||
| } | ||
|
|
||
| attempt = attempt || 1; | ||
| return _metadataWithRequestId(session, nthRequest!, attempt, headers); | ||
| } | ||
|
|
||
| function _metadataWithRequestId( | ||
| session: any, | ||
| nthRequest: number, | ||
| attempt: number, | ||
| priorMetadata?: {[k: string]: string} | ||
| ): {[k: string]: string} { | ||
| if (!priorMetadata) { | ||
| priorMetadata = {}; | ||
| } | ||
| const withReqId = { | ||
| ...priorMetadata, | ||
| }; | ||
| const database = session.parent as withMetadataWithRequestId; | ||
| let clientId = 1; | ||
| let channelId = 1; | ||
| if (database) { | ||
| clientId = database._nthClientId || 1; | ||
| channelId = database._channelId || 1; | ||
| } | ||
| withReqId[X_GOOG_SPANNER_REQUEST_ID_HEADER] = craftRequestId( | ||
| clientId, | ||
| channelId, | ||
| nthRequest, | ||
| attempt | ||
| ); | ||
| return withReqId; | ||
| } | ||
|
|
||
| function nextNthRequest(database): number { | ||
| if (!(database && typeof database._nextNthRequest === 'function')) { | ||
| return 1; | ||
| } | ||
| return database._nextNthRequest(); | ||
| } | ||
|
|
||
| export interface RequestIDError extends grpc.ServiceError { | ||
| requestID: string; | ||
| } | ||
|
|
||
| export { | ||
| AtomicCounter, | ||
| X_GOOG_SPANNER_REQUEST_ID_HEADER, | ||
| craftRequestId, | ||
| injectRequestIDIntoError, | ||
| injectRequestIDIntoHeaders, | ||
| nextNthRequest, | ||
| nextSpannerClientId, | ||
| newAtomicCounter, | ||
| randIdForProcess, | ||
| }; | ||
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,171 @@ | ||
| /** | ||
| * Copyright 2025 Google LLC. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* eslint-disable prefer-rest-params */ | ||
| import * as assert from 'assert'; | ||
| import { | ||
| RequestIDError, | ||
| X_GOOG_SPANNER_REQUEST_ID_HEADER, | ||
| craftRequestId, | ||
| injectRequestIDIntoError, | ||
| injectRequestIDIntoHeaders, | ||
| newAtomicCounter, | ||
| nextNthRequest, | ||
| randIdForProcess, | ||
| } from '../src/request_id_header'; | ||
|
|
||
| describe('RequestId', () => { | ||
| describe('AtomicCounter', () => { | ||
| it('Constructor with initialValue', done => { | ||
| const ac0 = newAtomicCounter(); | ||
| assert.deepStrictEqual(ac0.value(), 0); | ||
| assert.deepStrictEqual( | ||
| ac0.increment(2), | ||
| 2, | ||
| 'increment should return the added value' | ||
| ); | ||
| assert.deepStrictEqual( | ||
| ac0.value(), | ||
| 2, | ||
| 'increment should have modified the value' | ||
| ); | ||
|
|
||
| const ac1 = newAtomicCounter(1); | ||
| assert.deepStrictEqual(ac1.value(), 1); | ||
| assert.deepStrictEqual( | ||
| ac1.increment(1 << 27), | ||
| (1 << 27) + 1, | ||
| 'increment should return the added value' | ||
| ); | ||
| assert.deepStrictEqual( | ||
| ac1.value(), | ||
| (1 << 27) + 1, | ||
| 'increment should have modified the value' | ||
| ); | ||
| done(); | ||
| }); | ||
|
|
||
| it('reset', done => { | ||
| const ac0 = newAtomicCounter(1); | ||
| ac0.increment(); | ||
| assert.strictEqual(ac0.value(), 2); | ||
| ac0.reset(); | ||
| assert.strictEqual(ac0.value(), 0); | ||
| done(); | ||
| }); | ||
|
|
||
| it('toString', done => { | ||
| const ac0 = newAtomicCounter(1); | ||
| ac0.increment(); | ||
| assert.strictEqual(ac0.value(), 2); | ||
| assert.strictEqual(ac0.toString(), '2'); | ||
| assert.strictEqual(`${ac0}`, '2'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('craftRequestId', () => { | ||
| it('has a 32-bit hex-formatted process-id', done => { | ||
| assert.match( | ||
| randIdForProcess, | ||
| /^[0-9A-Fa-f]{8}$/, | ||
| `process-id should be a 32-bit hexadecimal number, but was ${randIdForProcess}` | ||
| ); | ||
| done(); | ||
| }); | ||
|
|
||
| it('with attempts', done => { | ||
| assert.strictEqual( | ||
| craftRequestId(1, 2, 3, 4), | ||
| `1.${randIdForProcess}.1.2.3.4` | ||
| ); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('injectRequestIDIntoError', () => { | ||
| it('with non-null error', done => { | ||
| const err: Error = new Error('this one'); | ||
| const config = {headers: {}}; | ||
| config.headers[X_GOOG_SPANNER_REQUEST_ID_HEADER] = '1.2.3.4.5.6'; | ||
| injectRequestIDIntoError(config, err); | ||
| assert.strictEqual((err as RequestIDError).requestID, '1.2.3.4.5.6'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('injectRequestIDIntoHeaders', () => { | ||
| it('with null session', done => { | ||
| const hdrs = {}; | ||
| injectRequestIDIntoHeaders(hdrs, null, 2, 1); | ||
| done(); | ||
| }); | ||
|
|
||
| it('with nthRequest explicitly passed in', done => { | ||
| const session = { | ||
| parent: { | ||
| _nextNthRequest: () => { | ||
| return 5; | ||
| }, | ||
| }, | ||
| }; | ||
| const got = injectRequestIDIntoHeaders({}, session, 2, 5); | ||
| const want = { | ||
| 'x-goog-spanner-request-id': `1.${randIdForProcess}.1.1.2.5`, | ||
| }; | ||
| assert.deepStrictEqual(got, want); | ||
| done(); | ||
| }); | ||
|
|
||
| it('infer nthRequest from session', done => { | ||
| const session = { | ||
| parent: { | ||
| _nextNthRequest: () => { | ||
| return 5; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const inputHeaders: {[k: string]: string} = {}; | ||
| const got = injectRequestIDIntoHeaders(inputHeaders, session); | ||
| const want = { | ||
| 'x-goog-spanner-request-id': `1.${randIdForProcess}.1.1.5.1`, | ||
| }; | ||
| assert.deepStrictEqual(got, want); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('nextNthRequest', () => { | ||
| const fauxDatabase = {}; | ||
| assert.deepStrictEqual( | ||
| nextNthRequest(fauxDatabase), | ||
| 1, | ||
| 'Without override, should default to 1' | ||
| ); | ||
|
|
||
| Object.assign(fauxDatabase, { | ||
| _nextNthRequest: () => { | ||
| return 4; | ||
| }, | ||
| }); | ||
| assert.deepStrictEqual( | ||
| nextNthRequest(fauxDatabase), | ||
| 4, | ||
| 'With override should infer value' | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.