-
Notifications
You must be signed in to change notification settings - Fork 712
perf(spanner): distribute single-use queries across gRPC channel pool #9273
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
olavloite
wants to merge
1
commit into
main
Choose a base branch
from
spanner-distribute-single-use-across-channel-pool
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.
+729
−17
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /*! | ||
| * Copyright 2026 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 {grpc} from 'google-gax'; | ||
| import grpcGcpModule = require('grpc-gcp'); | ||
|
|
||
| const grpcGcp = grpcGcpModule(grpc); | ||
|
|
||
| export interface ChannelFactoryWithWithoutAffinity { | ||
| _withoutAffinity?: unknown; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a delegate for GcpChannelFactory that overrides getAffinityConfig | ||
| * to return undefined. This causes grpc-gcp to skip affinity lookups and select | ||
| * a channel using its native stream load balancer (getActiveStreamsCount). | ||
| */ | ||
| export function createChannelFactoryWithoutAffinity( | ||
| channelFactory: object, | ||
| ): object { | ||
| const channelFactoryWithoutAffinity = Object.create(channelFactory); | ||
| ( | ||
| channelFactoryWithoutAffinity as {getAffinityConfig?: () => undefined} | ||
| ).getAffinityConfig = () => undefined; | ||
| return channelFactoryWithoutAffinity; | ||
| } | ||
|
olavloite marked this conversation as resolved.
olavloite marked this conversation as resolved.
olavloite marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Custom channel factory override that pre-allocates a static delegate | ||
| * without affinity lookup on the factory instance. This avoids any object | ||
| * or closure allocations per request. | ||
| */ | ||
| export function spannerChannelFactoryOverride( | ||
| address: string, | ||
| credentials: grpc.ChannelCredentials, | ||
| options: object, | ||
| ) { | ||
| const channelFactory = grpcGcp.gcpChannelFactoryOverride( | ||
| address, | ||
| credentials, | ||
| options, | ||
| ); | ||
| if (channelFactory) { | ||
| (channelFactory as ChannelFactoryWithWithoutAffinity)._withoutAffinity = | ||
| createChannelFactoryWithoutAffinity(channelFactory); | ||
| } | ||
| return channelFactory; | ||
| } | ||
|
olavloite marked this conversation as resolved.
|
||
|
|
||
| interface SingleUseTransactionArgument { | ||
| transaction?: { | ||
| singleUse?: unknown; | ||
| single_use?: unknown; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Intercepts calls before dispatch. For single-use transactions (e.g. single queries), | ||
| * routes through the pre-allocated delegate to distribute across channels in the pool. | ||
| * For read/write and multi-use transactions, uses the standard channel factory so | ||
| * requests adhere to session-to-channel affinity. | ||
| */ | ||
| export function spannerCallInvocationTransformer<RequestType, ResponseType>( | ||
| callProperties: grpc.CallProperties<RequestType, ResponseType>, | ||
| ): grpc.CallProperties<RequestType, ResponseType> { | ||
| if (!callProperties) { | ||
| return callProperties; | ||
| } | ||
| const argument = callProperties.argument as | ||
| SingleUseTransactionArgument | undefined; | ||
| if (argument?.transaction?.singleUse || argument?.transaction?.single_use) { | ||
| const channelFactory = | ||
| callProperties.channel as ChannelFactoryWithWithoutAffinity; | ||
| if (channelFactory?._withoutAffinity) { | ||
| callProperties.channel = | ||
| channelFactory._withoutAffinity as typeof callProperties.channel; | ||
| } | ||
| } | ||
| return grpcGcp.gcpCallInvocationTransformer(callProperties); | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| /*! | ||
| * Copyright 2026 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 * as assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import * as proxyquire from 'proxyquire'; | ||
| import {grpc} from 'google-gax'; | ||
| import { | ||
| createChannelFactoryWithoutAffinity, | ||
| spannerCallInvocationTransformer, | ||
| spannerChannelFactoryOverride, | ||
| ChannelFactoryWithWithoutAffinity, | ||
| } from '../src/channel-factory'; | ||
|
|
||
| describe('ChannelFactory and Transformer', () => { | ||
| let sandbox: sinon.SinonSandbox; | ||
|
|
||
| beforeEach(() => { | ||
| sandbox = sinon.createSandbox(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sandbox.restore(); | ||
| }); | ||
|
|
||
| describe('createChannelFactoryWithoutAffinity', () => { | ||
| it('should create a prototype delegate that returns undefined for getAffinityConfig', () => { | ||
| const originalFactory: { | ||
| options: {foo: string}; | ||
| getAffinityConfig(path?: string): {command: string} | undefined; | ||
| } = { | ||
| options: {foo: 'bar'}, | ||
| getAffinityConfig() { | ||
| return {command: 'BOUND'}; | ||
| }, | ||
| }; | ||
|
|
||
| const delegate = createChannelFactoryWithoutAffinity( | ||
| originalFactory, | ||
| ) as typeof originalFactory; | ||
|
|
||
| assert.strictEqual(delegate.getAffinityConfig('/test.Method'), undefined); | ||
| assert.strictEqual( | ||
| originalFactory.getAffinityConfig('/test.Method')?.command, | ||
| 'BOUND', | ||
| ); | ||
| assert.strictEqual(delegate.options, originalFactory.options); | ||
| assert.strictEqual(Object.getPrototypeOf(delegate), originalFactory); | ||
| }); | ||
| }); | ||
|
olavloite marked this conversation as resolved.
|
||
|
|
||
| describe('spannerChannelFactoryOverride', () => { | ||
| it('should attach _withoutAffinity delegate to the created channel factory', () => { | ||
| const channelFactory = spannerChannelFactoryOverride( | ||
| 'localhost:443', | ||
| grpc.credentials.createInsecure(), | ||
| {}, | ||
| ) as ChannelFactoryWithWithoutAffinity & { | ||
| getAffinityConfig: (path: string) => unknown; | ||
| }; | ||
|
|
||
| assert.ok(channelFactory); | ||
| assert.ok(channelFactory._withoutAffinity); | ||
| const withoutAffinity = channelFactory._withoutAffinity as { | ||
| getAffinityConfig: (path: string) => unknown; | ||
| }; | ||
| assert.strictEqual(typeof withoutAffinity.getAffinityConfig, 'function'); | ||
| assert.strictEqual( | ||
| withoutAffinity.getAffinityConfig('/test.Method'), | ||
| undefined, | ||
| ); | ||
| }); | ||
|
|
||
| it('should return nullish channel factory when gcpChannelFactoryOverride returns nullish', () => { | ||
| const proxiedModule = proxyquire('../src/channel-factory', { | ||
| 'grpc-gcp': () => ({ | ||
| gcpChannelFactoryOverride: () => null, | ||
| gcpCallInvocationTransformer: (props: unknown) => props, | ||
| }), | ||
| }); | ||
|
|
||
| const channelFactory = proxiedModule.spannerChannelFactoryOverride( | ||
| 'localhost:443', | ||
| grpc.credentials.createInsecure(), | ||
| {}, | ||
| ); | ||
|
|
||
| assert.strictEqual(channelFactory, null); | ||
| }); | ||
| }); | ||
|
|
||
| describe('spannerCallInvocationTransformer', () => { | ||
| function createMockCallProperties(argument?: unknown, channel?: unknown) { | ||
| return { | ||
| argument, | ||
| channel: channel ?? { | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }, | ||
| } as unknown as grpc.CallProperties<unknown, unknown>; | ||
| } | ||
|
|
||
| it('should route through _withoutAffinity for camelCase singleUse transaction', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties( | ||
| {transaction: {singleUse: {readOnly: {}}}}, | ||
| mockChannelFactory, | ||
| ); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual( | ||
| transformed.channel, | ||
| mockChannelFactory._withoutAffinity, | ||
| ); | ||
| }); | ||
|
|
||
| it('should route through _withoutAffinity for snake_case single_use transaction', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties( | ||
| {transaction: {single_use: {read_only: {}}}}, | ||
| mockChannelFactory, | ||
| ); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual( | ||
| transformed.channel, | ||
| mockChannelFactory._withoutAffinity, | ||
| ); | ||
| }); | ||
|
|
||
| it('should not change channel for multi-use transaction with id', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties( | ||
| {transaction: {id: Buffer.from('tx-123')}}, | ||
| mockChannelFactory, | ||
| ); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual(transformed.channel, mockChannelFactory); | ||
| }); | ||
|
|
||
| it('should not change channel for read-write transaction with begin', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties( | ||
| {transaction: {begin: {readWrite: {}}}}, | ||
| mockChannelFactory, | ||
| ); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual(transformed.channel, mockChannelFactory); | ||
| }); | ||
|
|
||
| it('should not change channel when argument is undefined', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties(undefined, mockChannelFactory); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual(transformed.channel, mockChannelFactory); | ||
| }); | ||
|
|
||
| it('should not change channel when argument is null', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties(null, mockChannelFactory); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual(transformed.channel, mockChannelFactory); | ||
| }); | ||
|
|
||
| it('should not change channel when argument has no transaction property', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| _withoutAffinity: {name: 'withoutAffinityChannel'}, | ||
| }; | ||
| const callProps = createMockCallProperties( | ||
| {name: 'some-session'}, | ||
| mockChannelFactory, | ||
| ); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual(transformed.channel, mockChannelFactory); | ||
| }); | ||
|
|
||
| it('should fall back gracefully when _withoutAffinity is not present on channel', () => { | ||
| const mockChannelFactory = { | ||
| name: 'originalChannel', | ||
| }; | ||
| const callProps = createMockCallProperties( | ||
| {transaction: {singleUse: {readOnly: {}}}}, | ||
| mockChannelFactory, | ||
| ); | ||
|
|
||
| const transformed = spannerCallInvocationTransformer(callProps); | ||
| assert.strictEqual(transformed.channel, mockChannelFactory); | ||
| }); | ||
|
|
||
| it('should return callProperties directly when callProperties is undefined or null', () => { | ||
| assert.strictEqual( | ||
| spannerCallInvocationTransformer( | ||
| undefined as unknown as grpc.CallProperties<unknown, unknown>, | ||
| ), | ||
| undefined, | ||
| ); | ||
| assert.strictEqual( | ||
| spannerCallInvocationTransformer( | ||
| null as unknown as grpc.CallProperties<unknown, unknown>, | ||
| ), | ||
| null, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
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.