@google-cloud/spanner 8.13.0 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -16
- package/build/src/batch-transaction.js +15 -12
- package/build/src/codec.d.ts +18 -30
- package/build/src/codec.js +197 -79
- package/build/src/common.d.ts +14 -0
- package/build/src/common.js +28 -3
- package/build/src/database.d.ts +31 -41
- package/build/src/database.js +147 -315
- package/build/src/helper.d.ts +12 -0
- package/build/src/helper.js +44 -0
- package/build/src/index.d.ts +24 -8
- package/build/src/index.js +303 -147
- package/build/src/instance.d.ts +15 -2
- package/build/src/instance.js +11 -2
- package/build/src/instrument.d.ts +12 -1
- package/build/src/instrument.js +13 -0
- package/build/src/metrics/constants.d.ts +13 -6
- package/build/src/metrics/constants.js +24 -14
- package/build/src/metrics/external-types.d.ts +14 -0
- package/build/src/metrics/interceptor.js +39 -44
- package/build/src/metrics/metrics-tracer-factory.d.ts +25 -14
- package/build/src/metrics/metrics-tracer-factory.js +119 -78
- package/build/src/metrics/metrics-tracer.d.ts +19 -6
- package/build/src/metrics/metrics-tracer.js +147 -55
- package/build/src/metrics/spanner-metrics-exporter.js +4 -0
- package/build/src/metrics/transform.d.ts +3 -4
- package/build/src/metrics/transform.js +16 -10
- package/build/src/multiplexed-session.d.ts +26 -2
- package/build/src/multiplexed-session.js +39 -11
- package/build/src/partial-result-stream.d.ts +26 -33
- package/build/src/partial-result-stream.js +263 -283
- package/build/src/request-stream-coordinator.d.ts +88 -0
- package/build/src/request-stream-coordinator.js +229 -0
- package/build/src/request_id_header.d.ts +6 -4
- package/build/src/request_id_header.js +24 -24
- package/build/src/session-factory.d.ts +62 -60
- package/build/src/session-factory.js +46 -86
- package/build/src/session-pool.d.ts +97 -227
- package/build/src/session-pool.js +74 -547
- package/build/src/session.js +1 -1
- package/build/src/transaction-runner.js +1 -12
- package/build/src/transaction.d.ts +21 -5
- package/build/src/transaction.js +107 -136
- package/package.json +28 -37
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2026 Google LLC. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Transform } from 'stream';
|
|
17
|
+
import { CancellableStream } from 'google-gax';
|
|
18
|
+
import { MetricsTracer } from './metrics/metrics-tracer';
|
|
19
|
+
/**
|
|
20
|
+
* Coordinates lifecycle events, cancellation, and metric collection
|
|
21
|
+
* between the user-facing readable/transform stream and the underlying
|
|
22
|
+
* GAX/gRPC call stream.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RequestStreamCoordinator {
|
|
25
|
+
private readonly stream;
|
|
26
|
+
private readonly metricsTracer;
|
|
27
|
+
private callStream;
|
|
28
|
+
private statusReceived;
|
|
29
|
+
private streamFinished;
|
|
30
|
+
private operationCompleted;
|
|
31
|
+
private requestStarted;
|
|
32
|
+
private streamClosed;
|
|
33
|
+
constructor(stream: Transform, metricsTracer: MetricsTracer | null);
|
|
34
|
+
/**
|
|
35
|
+
* Sets up destruction hooks and lifecycle listeners on the user-facing stream.
|
|
36
|
+
*/
|
|
37
|
+
setup(): void;
|
|
38
|
+
/**
|
|
39
|
+
* Cancels the active gRPC call stream if it has not already completed.
|
|
40
|
+
*
|
|
41
|
+
* Note: In `@grpc/grpc-js`, calling `.cancel()` after a stream has already
|
|
42
|
+
* finished or drained with `Status.OK` will overwrite the status to `CANCELLED`.
|
|
43
|
+
* We guard against this by checking `streamFinished` and `statusReceived`.
|
|
44
|
+
*/
|
|
45
|
+
cancelCallStream(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Invokes requestFn to initialize the callStream, or skips invocation
|
|
48
|
+
* if the user-facing stream was already destroyed before preparation finished.
|
|
49
|
+
*
|
|
50
|
+
* @param requestFn The function that instantiates the GAX call stream.
|
|
51
|
+
*/
|
|
52
|
+
attachRequestFn(requestFn: () => CancellableStream): void;
|
|
53
|
+
/**
|
|
54
|
+
* Attaches the underlying gRPC call stream once it is prepared by GAPIC.
|
|
55
|
+
*
|
|
56
|
+
* @param callStream The cancellable stream returned by GAX.
|
|
57
|
+
*/
|
|
58
|
+
attachCallStream(callStream: CancellableStream): void;
|
|
59
|
+
/**
|
|
60
|
+
* Handles errors encountered during GAPIC request preparation.
|
|
61
|
+
*
|
|
62
|
+
* @param error The error from prepareGapicRequest_.
|
|
63
|
+
*/
|
|
64
|
+
handleRequestError(error: Error): void;
|
|
65
|
+
/**
|
|
66
|
+
* Handles stream finish when all data has been written to the transform stream.
|
|
67
|
+
*/
|
|
68
|
+
handleStreamFinish(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Marks that the GAPIC request preparation has been initiated (e.g. upon 'reading').
|
|
71
|
+
*/
|
|
72
|
+
startRequest(): void;
|
|
73
|
+
/**
|
|
74
|
+
* Handles stream close on the user-facing stream. Defers metric completion
|
|
75
|
+
* if the gRPC call is still in flight, ensuring attempt status is recorded
|
|
76
|
+
* by the interceptor before operation completion is finalized.
|
|
77
|
+
*/
|
|
78
|
+
handleStreamClose(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Completes the OpenTelemetry operation metric. Idempotent.
|
|
81
|
+
*/
|
|
82
|
+
completeOperation(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Waits for the gRPC call to deliver its terminal status or close event before
|
|
85
|
+
* finalizing operation metrics.
|
|
86
|
+
*/
|
|
87
|
+
private waitForStatusOrClose;
|
|
88
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2026 Google LLC. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.RequestStreamCoordinator = void 0;
|
|
19
|
+
/**
|
|
20
|
+
* Coordinates lifecycle events, cancellation, and metric collection
|
|
21
|
+
* between the user-facing readable/transform stream and the underlying
|
|
22
|
+
* GAX/gRPC call stream.
|
|
23
|
+
*/
|
|
24
|
+
class RequestStreamCoordinator {
|
|
25
|
+
stream;
|
|
26
|
+
metricsTracer;
|
|
27
|
+
callStream = null;
|
|
28
|
+
statusReceived = false;
|
|
29
|
+
streamFinished = false;
|
|
30
|
+
operationCompleted = false;
|
|
31
|
+
requestStarted = false;
|
|
32
|
+
streamClosed = false;
|
|
33
|
+
constructor(stream, metricsTracer) {
|
|
34
|
+
this.stream = stream;
|
|
35
|
+
this.metricsTracer = metricsTracer;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Sets up destruction hooks and lifecycle listeners on the user-facing stream.
|
|
39
|
+
*/
|
|
40
|
+
setup() {
|
|
41
|
+
// Intercept stream destruction to handle cancellation and metric completion.
|
|
42
|
+
// We override _destroy rather than solely relying on the 'close' event because
|
|
43
|
+
// internal callers (such as transaction retry logic in transaction.ts) or users
|
|
44
|
+
// may call stream.removeAllListeners() before calling stream.destroy().
|
|
45
|
+
const originalDestroy = this.stream._destroy;
|
|
46
|
+
this.stream._destroy = (error, callback) => {
|
|
47
|
+
// Immediately cancel the underlying gRPC call if still active.
|
|
48
|
+
this.cancelCallStream();
|
|
49
|
+
// Ensure stream closure and metric finalization are triggered upon destruction,
|
|
50
|
+
// even if removeAllListeners() previously removed the 'close' listener.
|
|
51
|
+
const handleClose = () => {
|
|
52
|
+
this.handleStreamClose();
|
|
53
|
+
};
|
|
54
|
+
if (typeof originalDestroy === 'function') {
|
|
55
|
+
originalDestroy.call(this.stream, error, (destroyError) => {
|
|
56
|
+
handleClose();
|
|
57
|
+
if (typeof callback === 'function') {
|
|
58
|
+
callback(destroyError);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
handleClose();
|
|
64
|
+
if (typeof callback === 'function') {
|
|
65
|
+
callback(error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
// When the stream finishes writing normally, mark it as finished so that subsequent
|
|
70
|
+
// teardown or destroy calls do not trigger cancelCallStream().
|
|
71
|
+
this.stream.on('finish', () => this.handleStreamFinish());
|
|
72
|
+
// Fallback for streams that close without an explicit destroy() invocation.
|
|
73
|
+
// handleStreamClose() is guarded by streamClosed to ensure idempotency.
|
|
74
|
+
this.stream.on('close', () => this.handleStreamClose());
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Cancels the active gRPC call stream if it has not already completed.
|
|
78
|
+
*
|
|
79
|
+
* Note: In `@grpc/grpc-js`, calling `.cancel()` after a stream has already
|
|
80
|
+
* finished or drained with `Status.OK` will overwrite the status to `CANCELLED`.
|
|
81
|
+
* We guard against this by checking `streamFinished` and `statusReceived`.
|
|
82
|
+
*/
|
|
83
|
+
cancelCallStream() {
|
|
84
|
+
// Guard against overwriting Status.OK:
|
|
85
|
+
// In @grpc/grpc-js, calling .cancel() on a stream that has already received
|
|
86
|
+
// its terminal status or completed normally will overwrite the status to CANCELLED.
|
|
87
|
+
// This guard preserves OK status for normally drained streams as well as queries
|
|
88
|
+
// using the PartialResultSet.last optimization (where the user stream completes early
|
|
89
|
+
// while remaining trailers drain in the background).
|
|
90
|
+
if (this.streamFinished || this.statusReceived) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (this.callStream) {
|
|
94
|
+
// In google-gax, callStream is a StreamProxy wrapping the underlying gRPC call.
|
|
95
|
+
// Calling .destroy() on StreamProxy alone does not cancel the active gRPC call;
|
|
96
|
+
// .cancel() must be invoked explicitly to abort the RPC on the client.
|
|
97
|
+
if (typeof this.callStream.cancel === 'function') {
|
|
98
|
+
this.callStream.cancel();
|
|
99
|
+
}
|
|
100
|
+
// Clean up proxy and stream resources once cancellation has been requested.
|
|
101
|
+
const streamWithDestroy = this.callStream;
|
|
102
|
+
if (typeof streamWithDestroy.destroy === 'function' &&
|
|
103
|
+
!streamWithDestroy.destroyed) {
|
|
104
|
+
streamWithDestroy.destroy();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Invokes requestFn to initialize the callStream, or skips invocation
|
|
110
|
+
* if the user-facing stream was already destroyed before preparation finished.
|
|
111
|
+
*
|
|
112
|
+
* @param requestFn The function that instantiates the GAX call stream.
|
|
113
|
+
*/
|
|
114
|
+
attachRequestFn(requestFn) {
|
|
115
|
+
if (this.stream.destroyed || this.streamClosed) {
|
|
116
|
+
this.completeOperation();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const callStream = requestFn();
|
|
121
|
+
if (callStream) {
|
|
122
|
+
this.attachCallStream(callStream);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
this.handleRequestError(new Error('Failed to initialize request stream.'));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
this.handleRequestError(error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Attaches the underlying gRPC call stream once it is prepared by GAPIC.
|
|
134
|
+
*
|
|
135
|
+
* @param callStream The cancellable stream returned by GAX.
|
|
136
|
+
*/
|
|
137
|
+
attachCallStream(callStream) {
|
|
138
|
+
this.callStream = callStream;
|
|
139
|
+
callStream.once('status', () => {
|
|
140
|
+
this.statusReceived = true;
|
|
141
|
+
});
|
|
142
|
+
callStream
|
|
143
|
+
.on('error', (error) => {
|
|
144
|
+
if (!this.stream.destroyed && !this.streamClosed) {
|
|
145
|
+
this.stream.destroy(error);
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
.pipe(this.stream);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Handles errors encountered during GAPIC request preparation.
|
|
152
|
+
*
|
|
153
|
+
* @param error The error from prepareGapicRequest_.
|
|
154
|
+
*/
|
|
155
|
+
handleRequestError(error) {
|
|
156
|
+
this.stream.destroy(error);
|
|
157
|
+
this.completeOperation();
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Handles stream finish when all data has been written to the transform stream.
|
|
161
|
+
*/
|
|
162
|
+
handleStreamFinish() {
|
|
163
|
+
this.streamFinished = true;
|
|
164
|
+
this.stream.destroy();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Marks that the GAPIC request preparation has been initiated (e.g. upon 'reading').
|
|
168
|
+
*/
|
|
169
|
+
startRequest() {
|
|
170
|
+
this.requestStarted = true;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Handles stream close on the user-facing stream. Defers metric completion
|
|
174
|
+
* if the gRPC call is still in flight, ensuring attempt status is recorded
|
|
175
|
+
* by the interceptor before operation completion is finalized.
|
|
176
|
+
*/
|
|
177
|
+
handleStreamClose() {
|
|
178
|
+
if (this.streamClosed) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.streamClosed = true;
|
|
182
|
+
if (this.callStream) {
|
|
183
|
+
if (!this.statusReceived) {
|
|
184
|
+
this.waitForStatusOrClose(this.callStream);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
this.completeOperation();
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (!this.requestStarted) {
|
|
192
|
+
this.completeOperation();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Completes the OpenTelemetry operation metric. Idempotent.
|
|
197
|
+
*/
|
|
198
|
+
completeOperation() {
|
|
199
|
+
if (this.operationCompleted) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
this.operationCompleted = true;
|
|
203
|
+
this.metricsTracer?.recordOperationCompletion();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Waits for the gRPC call to deliver its terminal status or close event before
|
|
207
|
+
* finalizing operation metrics.
|
|
208
|
+
*/
|
|
209
|
+
waitForStatusOrClose(callStream) {
|
|
210
|
+
callStream.once('status', () => {
|
|
211
|
+
this.statusReceived = true;
|
|
212
|
+
this.completeOperation();
|
|
213
|
+
});
|
|
214
|
+
callStream.once('close', () => {
|
|
215
|
+
// In @grpc/grpc-js, cancellation may emit 'close' before the status callback
|
|
216
|
+
// runs on next tick. Defer via setImmediate to allow onReceiveStatus to execute first.
|
|
217
|
+
setImmediate(() => {
|
|
218
|
+
this.completeOperation();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
callStream.once('error', () => {
|
|
222
|
+
setImmediate(() => {
|
|
223
|
+
this.completeOperation();
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.RequestStreamCoordinator = RequestStreamCoordinator;
|
|
229
|
+
//# sourceMappingURL=request-stream-coordinator.js.map
|
|
@@ -15,19 +15,21 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import * as grpc from '@grpc/grpc-js';
|
|
17
17
|
declare const randIdForProcess: string;
|
|
18
|
+
declare const PROCESS_PREFIX: string;
|
|
18
19
|
declare const X_GOOG_SPANNER_REQUEST_ID_HEADER = "x-goog-spanner-request-id";
|
|
19
20
|
declare class AtomicCounter {
|
|
20
|
-
private
|
|
21
|
+
private _value;
|
|
21
22
|
constructor(initialValue?: number);
|
|
22
|
-
increment(
|
|
23
|
+
increment(amount?: number): number;
|
|
23
24
|
value(): number;
|
|
24
25
|
toString(): string;
|
|
25
26
|
reset(): void;
|
|
26
27
|
}
|
|
28
|
+
declare function getRequestIdPrefix(clientId?: number, channelId?: number): string;
|
|
27
29
|
declare function craftRequestId(nthClientId: number, channelId: number, nthRequest: number, attempt: number): string;
|
|
28
30
|
export declare function resetNthClientId(): void;
|
|
29
31
|
declare function nextSpannerClientId(): number;
|
|
30
|
-
declare function newAtomicCounter(
|
|
32
|
+
declare function newAtomicCounter(initialValue?: number): AtomicCounter;
|
|
31
33
|
declare function injectRequestIDIntoError(config: any, err: Error): void;
|
|
32
34
|
declare function injectRequestIDIntoHeaders(headers: {
|
|
33
35
|
[k: string]: string;
|
|
@@ -41,4 +43,4 @@ export interface RequestIDError extends grpc.ServiceError {
|
|
|
41
43
|
declare const X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR = "x_goog_spanner_request_id";
|
|
42
44
|
declare function attributeXGoogSpannerRequestIdToActiveSpan(config: any): void;
|
|
43
45
|
declare const X_GOOG_REQ_ID_REGEX: RegExp;
|
|
44
|
-
export { AtomicCounter, X_GOOG_REQ_ID_REGEX, X_GOOG_SPANNER_REQUEST_ID_HEADER, X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, attributeXGoogSpannerRequestIdToActiveSpan, craftRequestId, injectRequestIDIntoError, injectRequestIDIntoHeaders, nextNthRequest, nextSpannerClientId, newAtomicCounter, randIdForProcess, };
|
|
46
|
+
export { AtomicCounter, PROCESS_PREFIX, X_GOOG_REQ_ID_REGEX, X_GOOG_SPANNER_REQUEST_ID_HEADER, X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, attributeXGoogSpannerRequestIdToActiveSpan, craftRequestId, getRequestIdPrefix, injectRequestIDIntoError, injectRequestIDIntoHeaders, nextNthRequest, nextSpannerClientId, newAtomicCounter, randIdForProcess, };
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.randIdForProcess = exports.X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR = exports.X_GOOG_SPANNER_REQUEST_ID_HEADER = exports.X_GOOG_REQ_ID_REGEX = exports.AtomicCounter = void 0;
|
|
18
|
+
exports.randIdForProcess = exports.X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR = exports.X_GOOG_SPANNER_REQUEST_ID_HEADER = exports.X_GOOG_REQ_ID_REGEX = exports.PROCESS_PREFIX = exports.AtomicCounter = void 0;
|
|
19
19
|
exports.resetNthClientId = resetNthClientId;
|
|
20
20
|
exports.attributeXGoogSpannerRequestIdToActiveSpan = attributeXGoogSpannerRequestIdToActiveSpan;
|
|
21
21
|
exports.craftRequestId = craftRequestId;
|
|
22
|
+
exports.getRequestIdPrefix = getRequestIdPrefix;
|
|
22
23
|
exports.injectRequestIDIntoError = injectRequestIDIntoError;
|
|
23
24
|
exports.injectRequestIDIntoHeaders = injectRequestIDIntoHeaders;
|
|
24
25
|
exports.nextNthRequest = nextNthRequest;
|
|
@@ -33,36 +34,34 @@ const randIdForProcess = (0, crypto_1.randomBytes)(8)
|
|
|
33
34
|
exports.randIdForProcess = randIdForProcess;
|
|
34
35
|
const REQUEST_HEADER_VERSION = 1;
|
|
35
36
|
const PROCESS_PREFIX = `${REQUEST_HEADER_VERSION}.${randIdForProcess}.`;
|
|
37
|
+
exports.PROCESS_PREFIX = PROCESS_PREFIX;
|
|
36
38
|
const X_GOOG_SPANNER_REQUEST_ID_HEADER = 'x-goog-spanner-request-id';
|
|
37
39
|
exports.X_GOOG_SPANNER_REQUEST_ID_HEADER = X_GOOG_SPANNER_REQUEST_ID_HEADER;
|
|
38
40
|
class AtomicCounter {
|
|
39
|
-
|
|
41
|
+
_value;
|
|
40
42
|
constructor(initialValue) {
|
|
41
|
-
this.
|
|
42
|
-
if (initialValue) {
|
|
43
|
-
this.increment(initialValue);
|
|
44
|
-
}
|
|
43
|
+
this._value = (initialValue ?? 0) >>> 0;
|
|
45
44
|
}
|
|
46
|
-
increment(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
Atomics.add(this.backingBuffer, 0, n);
|
|
51
|
-
return this.value();
|
|
45
|
+
increment(amount) {
|
|
46
|
+
const step = amount ?? 1;
|
|
47
|
+
return (this._value = (this._value + step) >>> 0);
|
|
52
48
|
}
|
|
53
49
|
value() {
|
|
54
|
-
return
|
|
50
|
+
return this._value;
|
|
55
51
|
}
|
|
56
52
|
toString() {
|
|
57
|
-
return `${this.
|
|
53
|
+
return `${this._value}`;
|
|
58
54
|
}
|
|
59
55
|
reset() {
|
|
60
|
-
|
|
56
|
+
this._value = 0;
|
|
61
57
|
}
|
|
62
58
|
}
|
|
63
59
|
exports.AtomicCounter = AtomicCounter;
|
|
60
|
+
function getRequestIdPrefix(clientId = 1, channelId = 1) {
|
|
61
|
+
return `${PROCESS_PREFIX}${clientId ?? 1}.${channelId ?? 1}.`;
|
|
62
|
+
}
|
|
64
63
|
function craftRequestId(nthClientId, channelId, nthRequest, attempt) {
|
|
65
|
-
return `${PROCESS_PREFIX}${nthClientId}.${channelId}.${nthRequest}.${attempt}`;
|
|
64
|
+
return `${PROCESS_PREFIX}${nthClientId ?? 1}.${channelId ?? 1}.${nthRequest ?? 1}.${attempt ?? 1}`;
|
|
66
65
|
}
|
|
67
66
|
const nthClientId = new AtomicCounter();
|
|
68
67
|
// Only exported for deterministic testing.
|
|
@@ -75,11 +74,10 @@ function resetNthClientId() {
|
|
|
75
74
|
* with x-goog-spanner-request-id.
|
|
76
75
|
*/
|
|
77
76
|
function nextSpannerClientId() {
|
|
78
|
-
nthClientId.increment(1);
|
|
79
|
-
return nthClientId.value();
|
|
77
|
+
return nthClientId.increment(1);
|
|
80
78
|
}
|
|
81
|
-
function newAtomicCounter(
|
|
82
|
-
return new AtomicCounter(
|
|
79
|
+
function newAtomicCounter(initialValue) {
|
|
80
|
+
return new AtomicCounter(initialValue);
|
|
83
81
|
}
|
|
84
82
|
function extractRequestID(config) {
|
|
85
83
|
if (!config) {
|
|
@@ -107,16 +105,18 @@ function injectRequestIDIntoHeaders(headers, session, nthRequest, attempt) {
|
|
|
107
105
|
return headers;
|
|
108
106
|
}
|
|
109
107
|
const database = session.parent;
|
|
110
|
-
if (
|
|
108
|
+
if (nthRequest === undefined || nthRequest === null) {
|
|
111
109
|
if (!database || typeof database._nextNthRequest !== 'function') {
|
|
112
110
|
return headers;
|
|
113
111
|
}
|
|
114
112
|
nthRequest = database._nextNthRequest();
|
|
115
113
|
}
|
|
116
|
-
const
|
|
117
|
-
const
|
|
114
|
+
const requestCount = nthRequest ?? 1;
|
|
115
|
+
const attemptCount = attempt ?? 1;
|
|
118
116
|
const withReqId = { ...headers };
|
|
119
|
-
withReqId[X_GOOG_SPANNER_REQUEST_ID_HEADER] =
|
|
117
|
+
withReqId[X_GOOG_SPANNER_REQUEST_ID_HEADER] = database?._requestIdPrefix
|
|
118
|
+
? `${database._requestIdPrefix}${requestCount}.${attemptCount}`
|
|
119
|
+
: craftRequestId(database?._clientId ?? database?._nthClientId ?? 1, database?._channelId ?? 1, requestCount, attemptCount);
|
|
120
120
|
return withReqId;
|
|
121
121
|
}
|
|
122
122
|
function nextNthRequest(database) {
|
|
@@ -17,17 +17,16 @@ import { Database, Session, Transaction } from '.';
|
|
|
17
17
|
import { MultiplexedSessionInterface } from './multiplexed-session';
|
|
18
18
|
import { SessionPoolInterface, SessionPoolOptions } from './session-pool';
|
|
19
19
|
import { SessionPoolConstructor } from './database';
|
|
20
|
+
import { ServiceError } from 'google-gax';
|
|
20
21
|
declare const common: any;
|
|
21
22
|
/**
|
|
22
23
|
* @callback GetSessionCallback
|
|
23
24
|
* @param {?Error} error Request error, if any.
|
|
24
25
|
* @param {Session} [session] The session object.
|
|
25
26
|
* @param {Transaction} [transaction] The transaction object, if applicable.
|
|
26
|
-
* Omitted for read-only multiplexed session acquisitions to prevent throwaway
|
|
27
|
-
* allocations. Provided by getSessionForReadWrite or when using regular session pools.
|
|
28
27
|
*/
|
|
29
28
|
export interface GetSessionCallback {
|
|
30
|
-
(err: Error | null, session?: Session | null, transaction?: Transaction | null): void;
|
|
29
|
+
(err: Error | ServiceError | null, session?: Session | null, transaction?: Transaction | null): void;
|
|
31
30
|
}
|
|
32
31
|
/**
|
|
33
32
|
* Interface for implementing session-factory logic.
|
|
@@ -43,133 +42,136 @@ export interface SessionFactoryInterface {
|
|
|
43
42
|
*/
|
|
44
43
|
getSession(callback: GetSessionCallback): void;
|
|
45
44
|
/**
|
|
46
|
-
* When called returns a session
|
|
45
|
+
* When called returns a cached multiplexed session synchronously if available.
|
|
47
46
|
*
|
|
47
|
+
* @name SessionFactoryInterface#getSessionSync
|
|
48
|
+
*/
|
|
49
|
+
getSessionSync(): Session | null;
|
|
50
|
+
/**
|
|
51
|
+
* When called returns a session for partitioned operations.
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Always delegates to {@link SessionFactoryInterface#getSession}.
|
|
48
54
|
* @name SessionFactoryInterface#getSessionForPartitionedOps
|
|
49
55
|
* @param {GetSessionCallback} callback The callback function.
|
|
50
56
|
*/
|
|
51
|
-
getSessionForPartitionedOps(callback: GetSessionCallback): void;
|
|
57
|
+
getSessionForPartitionedOps?(callback: GetSessionCallback): void;
|
|
52
58
|
/**
|
|
53
|
-
* When called returns a session for read
|
|
59
|
+
* When called returns a session for read/write operations.
|
|
54
60
|
*
|
|
61
|
+
* @deprecated Always delegates to {@link SessionFactoryInterface#getSession}.
|
|
55
62
|
* @name SessionFactoryInterface#getSessionForReadWrite
|
|
56
63
|
* @param {GetSessionCallback} callback The callback function.
|
|
57
64
|
*/
|
|
58
|
-
getSessionForReadWrite(callback: GetSessionCallback): void;
|
|
65
|
+
getSessionForReadWrite?(callback: GetSessionCallback): void;
|
|
59
66
|
/**
|
|
60
67
|
* When called returns the pool object.
|
|
61
68
|
*
|
|
69
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
62
70
|
* @name SessionFactoryInterface#getPool
|
|
63
71
|
*/
|
|
64
|
-
getPool(): SessionPoolInterface;
|
|
72
|
+
getPool?(): SessionPoolInterface;
|
|
65
73
|
/**
|
|
66
|
-
*
|
|
74
|
+
* Releases a session back into the pool. No-op for multiplexed sessions.
|
|
67
75
|
*
|
|
76
|
+
* @deprecated Regular session pooling has been removed; this method is a no-op.
|
|
68
77
|
* @name SessionFactoryInterface#release
|
|
69
|
-
* @param {Session} session The session
|
|
78
|
+
* @param {Session} session The session object.
|
|
70
79
|
*/
|
|
71
|
-
release(session: Session): void;
|
|
80
|
+
release?(session: Session): void;
|
|
72
81
|
/**
|
|
73
|
-
*
|
|
82
|
+
* Returns true if multiplexed sessions are enabled.
|
|
74
83
|
*
|
|
84
|
+
* @deprecated Multiplexed sessions are always enabled.
|
|
75
85
|
* @name SessionFactoryInterface#isMultiplexedEnabled
|
|
86
|
+
* @returns {boolean} Always true.
|
|
76
87
|
*/
|
|
77
|
-
isMultiplexedEnabled(): boolean;
|
|
88
|
+
isMultiplexedEnabled?(): boolean;
|
|
78
89
|
/**
|
|
79
|
-
*
|
|
90
|
+
* Returns true if multiplexed sessions are enabled for read/write transactions.
|
|
80
91
|
*
|
|
92
|
+
* @deprecated Multiplexed sessions are always enabled for read/write transactions.
|
|
81
93
|
* @name SessionFactoryInterface#isMultiplexedEnabledForRW
|
|
94
|
+
* @returns {boolean} Always true.
|
|
82
95
|
*/
|
|
83
|
-
isMultiplexedEnabledForRW(): boolean;
|
|
96
|
+
isMultiplexedEnabledForRW?(): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* When called closes the session factory and stops background maintenance.
|
|
99
|
+
*
|
|
100
|
+
* @name SessionFactoryInterface#close
|
|
101
|
+
*/
|
|
102
|
+
close?(): void;
|
|
84
103
|
}
|
|
85
104
|
/**
|
|
86
105
|
* Creates a SessionFactory object to manage the creation of
|
|
87
|
-
*
|
|
106
|
+
* multiplexed session.
|
|
88
107
|
*
|
|
89
108
|
* @class
|
|
90
109
|
*
|
|
91
110
|
* @param {Database} database Database object.
|
|
92
111
|
* @param {String} name Name of the database.
|
|
93
|
-
* @param {SessionPoolOptions|SessionPoolInterface} options Session pool
|
|
94
|
-
* configuration options or custom pool inteface.
|
|
95
112
|
*/
|
|
96
113
|
export declare class SessionFactory extends common.GrpcServiceObject implements SessionFactoryInterface {
|
|
97
|
-
multiplexedSession_: MultiplexedSessionInterface;
|
|
98
114
|
pool_: SessionPoolInterface;
|
|
99
|
-
|
|
100
|
-
isMultiplexedPartitionOps: boolean;
|
|
101
|
-
isMultiplexedRW: boolean;
|
|
115
|
+
multiplexedSession_: MultiplexedSessionInterface;
|
|
102
116
|
constructor(database: Database, name: String, poolOptions?: SessionPoolConstructor | SessionPoolOptions);
|
|
103
117
|
/**
|
|
104
|
-
*
|
|
118
|
+
* Synchronously returns a cached multiplexed session if available,
|
|
119
|
+
* otherwise null.
|
|
105
120
|
*
|
|
106
|
-
*
|
|
107
|
-
|
|
121
|
+
* @returns {Session|null} The cached multiplexed session or null.
|
|
122
|
+
*/
|
|
123
|
+
getSessionSync(): Session | null;
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves a multiplexed session.
|
|
108
126
|
*
|
|
109
127
|
* @param {GetSessionCallback} callback The callback function.
|
|
110
128
|
*/
|
|
111
129
|
getSession(callback: GetSessionCallback): void;
|
|
112
130
|
/**
|
|
113
|
-
* Retrieves a session for partitioned operations
|
|
114
|
-
* based on whether multiplexed sessions are disabled or not.
|
|
115
|
-
*
|
|
116
|
-
* If multiplexed sessions are disabled for partitioned ops this methods delegates the request to `getSession()`, which returns
|
|
117
|
-
* either a multiplexed session or a regular session based on the env configuration.
|
|
118
|
-
*
|
|
119
|
-
* If the multiplexed sessions are disabled, a session will get retrieved from the regular session pool.
|
|
120
|
-
* Otherwise a multiplexed session will be used.
|
|
131
|
+
* Retrieves a session for partitioned operations.
|
|
121
132
|
*
|
|
133
|
+
* @deprecated Use {@link SessionFactory#getSession} instead.
|
|
122
134
|
* @param {GetSessionCallback} callback The callback function.
|
|
123
135
|
*/
|
|
124
136
|
getSessionForPartitionedOps(callback: GetSessionCallback): void;
|
|
125
137
|
/**
|
|
126
|
-
* Retrieves a session for read
|
|
127
|
-
* based on whether multiplexed sessions are enabled.
|
|
128
|
-
*
|
|
129
|
-
* If multiplexed sessions are disabled for read write this methods delegates the request to `getSession()`, which returns
|
|
130
|
-
* either a multiplexed session or a regular session based on the env configuration.
|
|
131
|
-
*
|
|
132
|
-
* If the multiplexed sessions are disabled, a session will get retrieved from the regular session pool.
|
|
133
|
-
* Otherise a multiplexed session will be used.
|
|
138
|
+
* Retrieves a session for read/write operations.
|
|
134
139
|
*
|
|
140
|
+
* @deprecated Use {@link SessionFactory#getSession} instead.
|
|
135
141
|
* @param {GetSessionCallback} callback The callback function.
|
|
136
142
|
*/
|
|
137
143
|
getSessionForReadWrite(callback: GetSessionCallback): void;
|
|
138
144
|
/**
|
|
139
145
|
* Returns the regular session pool object.
|
|
140
146
|
*
|
|
147
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
141
148
|
* @returns {SessionPoolInterface} The session pool used by current instance.
|
|
142
149
|
*/
|
|
143
150
|
getPool(): SessionPoolInterface;
|
|
144
151
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* This methods does not release a multiplexed session.
|
|
152
|
+
* No-op release method retained for backwards compatibility.
|
|
148
153
|
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* @param {Session} session - The session to be released. This should be an instance of `Session` that was
|
|
153
|
-
* previously acquired from the session pool.
|
|
154
|
-
*
|
|
155
|
-
* @throws {Error} If the session is invalid or cannot be released.
|
|
154
|
+
* @deprecated Multiplexed sessions do not require releasing back to a pool.
|
|
155
|
+
* @param {Session} _session The session object.
|
|
156
156
|
*/
|
|
157
|
-
release(
|
|
157
|
+
release(_session: Session): void;
|
|
158
158
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
* This method returns true if multiplexed session is enabled, otherwise returns false
|
|
159
|
+
* Checks whether multiplexed session is enabled.
|
|
162
160
|
*
|
|
163
|
-
* @
|
|
161
|
+
* @deprecated Multiplexed sessions are always enabled.
|
|
162
|
+
* @returns {boolean} Always returns `true`.
|
|
164
163
|
*/
|
|
165
164
|
isMultiplexedEnabled(): boolean;
|
|
166
165
|
/**
|
|
167
|
-
*
|
|
166
|
+
* Checks whether multiplexed session is enabled for read/write transactions.
|
|
168
167
|
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* @returns {boolean}
|
|
168
|
+
* @deprecated Multiplexed sessions are always enabled for read/write transactions.
|
|
169
|
+
* @returns {boolean} Always returns `true`.
|
|
172
170
|
*/
|
|
173
171
|
isMultiplexedEnabledForRW(): boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Closes the session factory and stops background maintenance.
|
|
174
|
+
*/
|
|
175
|
+
close(): void;
|
|
174
176
|
}
|
|
175
177
|
export {};
|