Skip to content
Draft
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
49 changes: 49 additions & 0 deletions handwritten/spanner/src/channel-pool/affinity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*!
* 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 {ChannelEntry} from './types';

/**
* Kind of affinity required by an operation.
*/
export enum AffinityKind {
/** Multi-statement Read/Write transactions require hard stickiness. */
ReadWrite = 0,
/** Read-Only transactions prefer soft stickiness for warmth. */
ReadOnly = 1,
}

/**
* Caller-owned handle managing channel affinity across multi-statement transactions.
*/
export class TransactionAffinity {
pinnedEntry: ChannelEntry | null = null;

constructor(readonly kind: AffinityKind = AffinityKind.ReadWrite) {}

/**
* Resets affinity upon transaction commit, rollback, or close.
*/
reset(): void {
if (this.pinnedEntry && this.kind === AffinityKind.ReadWrite) {
this.pinnedEntry.activeRwTransactions = Math.max(
0,
this.pinnedEntry.activeRwTransactions - 1,
);
}
this.pinnedEntry = null;
}
}
103 changes: 103 additions & 0 deletions handwritten/spanner/src/channel-pool/channel-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*!
* 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 grpc from '@grpc/grpc-js';
import {ChannelPool} from './types';

/**
* Adapter that presents a ChannelPool as a grpc.Channel to the gRPC client runtime.
*/
export class ChannelPoolChannelAdapter implements grpc.ChannelInterface {
constructor(readonly pool: ChannelPool) {}

close(): void {
// No-op: the underlying ChannelPool lifecycle is managed by its owner
// (e.g. Spanner or caller), not by individual client stubs using this adapter.
}

getTarget(): string {
return this.pool.getTarget();
}

getConnectivityState(tryToConnect?: boolean): grpc.connectivityState {
return this.pool.getConnectivityState(tryToConnect);
}

watchConnectivityState(
currentState: grpc.connectivityState,
deadline: Date | number,
callback: (error?: Error) => void,
): void {
this.pool.watchConnectivityState(currentState, deadline, callback);
}

getChannelzRef(): any {

Check warning on line 47 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return null;
}

get channelRefs(): Array<{channel: grpc.Channel}> {
if ('entries' in (this.pool as any)) {

Check warning on line 52 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return (this.pool as any).entries.map((entry: any) => ({

Check warning on line 53 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 53 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
channel: entry.channel,
}));
}
if ('activeEntries' in (this.pool as any)) {

Check warning on line 57 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return (this.pool as any).activeEntries.map((entry: any) => ({

Check warning on line 58 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 58 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
channel: entry.channel,
}));
}
return [];
}

createCall(
method: string,
deadline: any,

Check warning on line 67 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
host: any,

Check warning on line 68 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
parentCall: any,

Check warning on line 69 in handwritten/spanner/src/channel-pool/channel-adapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
propagateFlags: any,
): any {
// Fallback if call is initiated outside of callInvocationTransformer
const lease = this.pool.acquire();
const call = (lease.entry.channel as any).createCall(
method,
deadline,
host,
parentCall,
propagateFlags,
);
let released = false;
const releaseOnce = () => {
if (!released) {
released = true;
lease.release();
}
};
return new grpc.InterceptingCall(call, {
start: (metadata, listener, next) => {
next(metadata, {
onReceiveStatus: (status, nextStatus) => {
releaseOnce();
nextStatus(status);
},
});
},
cancel: next => {
releaseOnce();
next();
},
});
}
Comment thread
olavloite marked this conversation as resolved.
}
Loading
Loading