@google-cloud/spanner 9.0.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 +6 -0
- package/build/src/codec.js +99 -34
- package/build/src/common.d.ts +14 -0
- package/build/src/common.js +27 -1
- package/build/src/database.d.ts +31 -41
- package/build/src/database.js +122 -313
- package/build/src/helper.d.ts +12 -0
- package/build/src/helper.js +44 -0
- package/build/src/index.d.ts +16 -8
- package/build/src/index.js +210 -90
- 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 +6 -0
- package/build/src/metrics/constants.js +6 -1
- package/build/src/metrics/interceptor.js +32 -28
- package/build/src/metrics/metrics-tracer-factory.d.ts +6 -11
- package/build/src/metrics/metrics-tracer-factory.js +62 -64
- package/build/src/metrics/metrics-tracer.d.ts +19 -6
- package/build/src/metrics/metrics-tracer.js +147 -55
- package/build/src/multiplexed-session.d.ts +13 -2
- package/build/src/multiplexed-session.js +20 -4
- 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 +52 -63
- package/build/src/session-factory.js +41 -92
- 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 +5 -5
- package/build/src/transaction.js +72 -112
- package/package.json +9 -10
package/build/src/database.js
CHANGED
|
@@ -27,7 +27,6 @@ const backup_1 = require("./backup");
|
|
|
27
27
|
const batch_transaction_1 = require("./batch-transaction");
|
|
28
28
|
const session_factory_1 = require("./session-factory");
|
|
29
29
|
const session_1 = require("./session");
|
|
30
|
-
const session_pool_1 = require("./session-pool");
|
|
31
30
|
const table_1 = require("./table");
|
|
32
31
|
const transaction_1 = require("./transaction");
|
|
33
32
|
const transaction_runner_1 = require("./transaction-runner");
|
|
@@ -43,10 +42,15 @@ const request_id_header_1 = require("./request_id_header");
|
|
|
43
42
|
* @class
|
|
44
43
|
*
|
|
45
44
|
* @param {string} name Name of the database.
|
|
46
|
-
* @param {SessionPoolOptions|SessionPoolInterface}
|
|
47
|
-
* configuration options
|
|
48
|
-
*
|
|
45
|
+
* @param {SessionPoolOptions|SessionPoolInterface} [poolOptions] Session pool
|
|
46
|
+
* configuration options.
|
|
47
|
+
* **Deprecated**. Session pool options is deprecated and will be removed in a future release.
|
|
48
|
+
* Multiplexed sessions are now used for all operations by default, eliminating
|
|
49
|
+
* the need for session pooling.
|
|
50
|
+
* @param {google.spanner.v1.ExecuteSqlRequest.IQueryOptions} [queryOptions]
|
|
49
51
|
* The default query options to use for queries on the database.
|
|
52
|
+
* @param {string} [databaseRole] The database role to use for sessions on
|
|
53
|
+
* the database.
|
|
50
54
|
*
|
|
51
55
|
* @example
|
|
52
56
|
* ```
|
|
@@ -62,7 +66,6 @@ class Database extends common.GrpcServiceObject {
|
|
|
62
66
|
pool_;
|
|
63
67
|
sessionFactory_;
|
|
64
68
|
queryOptions_;
|
|
65
|
-
isMuxEnabledForRW_;
|
|
66
69
|
commonHeaders_;
|
|
67
70
|
request;
|
|
68
71
|
databaseRole;
|
|
@@ -71,7 +74,26 @@ class Database extends common.GrpcServiceObject {
|
|
|
71
74
|
_observabilityOptions; // TODO: exmaine if we can remove it
|
|
72
75
|
_traceConfig;
|
|
73
76
|
_nthRequest;
|
|
74
|
-
|
|
77
|
+
_clientIdValue = 1;
|
|
78
|
+
get _clientId() {
|
|
79
|
+
return this._clientIdValue;
|
|
80
|
+
}
|
|
81
|
+
set _clientId(value) {
|
|
82
|
+
this._clientIdValue = value ?? 1;
|
|
83
|
+
this._updateRequestIdPrefix();
|
|
84
|
+
}
|
|
85
|
+
_channelIdValue = 1;
|
|
86
|
+
get _channelId() {
|
|
87
|
+
return this._channelIdValue;
|
|
88
|
+
}
|
|
89
|
+
set _channelId(value) {
|
|
90
|
+
this._channelIdValue = value ?? 1;
|
|
91
|
+
this._updateRequestIdPrefix();
|
|
92
|
+
}
|
|
93
|
+
_updateRequestIdPrefix() {
|
|
94
|
+
this._requestIdPrefix = (0, request_id_header_1.getRequestIdPrefix)(this._clientIdValue, this._channelIdValue);
|
|
95
|
+
}
|
|
96
|
+
_requestIdPrefix = (0, request_id_header_1.getRequestIdPrefix)(1, 1);
|
|
75
97
|
constructor(instance, name, poolOptions, queryOptions, databaseRole) {
|
|
76
98
|
const methods = {
|
|
77
99
|
/**
|
|
@@ -124,40 +146,7 @@ class Database extends common.GrpcServiceObject {
|
|
|
124
146
|
id: name,
|
|
125
147
|
methods,
|
|
126
148
|
createMethod: (_, options, callback) => {
|
|
127
|
-
|
|
128
|
-
if (pool._pending > 0) {
|
|
129
|
-
// If there are BatchCreateSessions requests pending, then we should
|
|
130
|
-
// wait until these have finished before we try to create the database.
|
|
131
|
-
// Otherwise the results of these requests might be propagated to
|
|
132
|
-
// client requests that are submitted after the database has been
|
|
133
|
-
// created. If the pending requests have not finished within 10 seconds,
|
|
134
|
-
// they will be ignored and the database creation will proceed.
|
|
135
|
-
let timeout;
|
|
136
|
-
const promises = [
|
|
137
|
-
new Promise(resolve => (timeout = setTimeout(resolve, 10000))),
|
|
138
|
-
new Promise(resolve => {
|
|
139
|
-
pool
|
|
140
|
-
.on('available', () => {
|
|
141
|
-
if (pool._pending === 0) {
|
|
142
|
-
clearTimeout(timeout);
|
|
143
|
-
resolve();
|
|
144
|
-
}
|
|
145
|
-
})
|
|
146
|
-
.on('createError', () => {
|
|
147
|
-
if (pool._pending === 0) {
|
|
148
|
-
clearTimeout(timeout);
|
|
149
|
-
resolve();
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
}),
|
|
153
|
-
];
|
|
154
|
-
Promise.race(promises)
|
|
155
|
-
.then(() => instance.createDatabase(formattedName_, options, callback))
|
|
156
|
-
.catch(() => { });
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
return instance.createDatabase(formattedName_, options, callback);
|
|
160
|
-
}
|
|
149
|
+
return instance.createDatabase(formattedName_, options, callback);
|
|
161
150
|
},
|
|
162
151
|
});
|
|
163
152
|
this.formattedName_ = formattedName_;
|
|
@@ -172,12 +161,11 @@ class Database extends common.GrpcServiceObject {
|
|
|
172
161
|
};
|
|
173
162
|
this.request = instance.request;
|
|
174
163
|
this._nthRequest = (0, request_id_header_1.newAtomicCounter)(0);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
164
|
+
const spanner = instance.parent;
|
|
165
|
+
this._clientId =
|
|
166
|
+
spanner?._nthClientId ??
|
|
167
|
+
instance._nthClientId ??
|
|
168
|
+
1;
|
|
181
169
|
this._observabilityOptions = instance._observabilityOptions;
|
|
182
170
|
this.commonHeaders_ = {
|
|
183
171
|
...instance.commonHeaders_,
|
|
@@ -187,12 +175,6 @@ class Database extends common.GrpcServiceObject {
|
|
|
187
175
|
this.requestStream = instance.requestStream;
|
|
188
176
|
this.sessionFactory_ = new session_factory_1.SessionFactory(this, name, poolOptions);
|
|
189
177
|
this.pool_ = this.sessionFactory_.getPool();
|
|
190
|
-
this.isMuxEnabledForRW_ = this.sessionFactory_.isMultiplexedEnabledForRW();
|
|
191
|
-
const sessionPoolInstance = this.pool_;
|
|
192
|
-
if (sessionPoolInstance) {
|
|
193
|
-
sessionPoolInstance._observabilityOptions =
|
|
194
|
-
instance._observabilityOptions;
|
|
195
|
-
}
|
|
196
178
|
this.queryOptions_ = Object.assign(Object.assign({}, queryOptions), Database.getEnvironmentQueryOptions());
|
|
197
179
|
}
|
|
198
180
|
_nextNthRequest() {
|
|
@@ -240,11 +222,10 @@ class Database extends common.GrpcServiceObject {
|
|
|
240
222
|
sessionTemplate: { labels: labels, creatorRole: databaseRole },
|
|
241
223
|
sessionCount: count,
|
|
242
224
|
};
|
|
243
|
-
const
|
|
225
|
+
const allHeaders = this._metadataWithRequestId(this._nextNthRequest(), 1, this.commonHeaders_);
|
|
244
226
|
if (this._getSpanner().routeToLeaderEnabled) {
|
|
245
|
-
(0, common_1.addLeaderAwareRoutingHeader)(
|
|
227
|
+
(0, common_1.addLeaderAwareRoutingHeader)(allHeaders);
|
|
246
228
|
}
|
|
247
|
-
const allHeaders = this._metadataWithRequestId(this._nextNthRequest(), 1, headers);
|
|
248
229
|
(0, instrument_1.startTrace)('Database.batchCreateSessions', this._traceConfig, span => {
|
|
249
230
|
this.request({
|
|
250
231
|
client: 'SpannerClient',
|
|
@@ -271,14 +252,9 @@ class Database extends common.GrpcServiceObject {
|
|
|
271
252
|
});
|
|
272
253
|
}
|
|
273
254
|
_metadataWithRequestId(nthRequest, attempt, priorMetadata) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const withReqId = {
|
|
278
|
-
...priorMetadata,
|
|
279
|
-
};
|
|
280
|
-
withReqId[request_id_header_1.X_GOOG_SPANNER_REQUEST_ID_HEADER] = (0, request_id_header_1.craftRequestId)(this._clientId || 1, 1, // TODO: Properly infer the channelId
|
|
281
|
-
nthRequest, attempt);
|
|
255
|
+
const withReqId = priorMetadata ? { ...priorMetadata } : {};
|
|
256
|
+
withReqId[request_id_header_1.X_GOOG_SPANNER_REQUEST_ID_HEADER] =
|
|
257
|
+
`${this._requestIdPrefix}${nthRequest ?? 1}.${attempt ?? 1}`;
|
|
282
258
|
return withReqId;
|
|
283
259
|
}
|
|
284
260
|
/**
|
|
@@ -317,10 +293,16 @@ class Database extends common.GrpcServiceObject {
|
|
|
317
293
|
return transaction;
|
|
318
294
|
}
|
|
319
295
|
close(callback) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
296
|
+
this.sessionFactory_?.close?.();
|
|
297
|
+
const databases = this.parent.databases_;
|
|
298
|
+
for (const [key, database] of databases) {
|
|
299
|
+
if (database === this) {
|
|
300
|
+
databases.delete(key);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (callback) {
|
|
304
|
+
process.nextTick(callback);
|
|
305
|
+
}
|
|
324
306
|
}
|
|
325
307
|
createBatchTransaction(optionsOrCallback, cb) {
|
|
326
308
|
const callback = typeof optionsOrCallback === 'function'
|
|
@@ -338,15 +320,9 @@ class Database extends common.GrpcServiceObject {
|
|
|
338
320
|
return;
|
|
339
321
|
}
|
|
340
322
|
const transaction = this.batchTransaction({ session: session }, options);
|
|
341
|
-
this._releaseOnEnd(session, transaction, span);
|
|
342
323
|
transaction.begin((err, resp) => {
|
|
343
324
|
if (err) {
|
|
344
325
|
(0, instrument_1.setSpanError)(span, err);
|
|
345
|
-
if ((0, session_pool_1.isSessionNotFoundError)(err)) {
|
|
346
|
-
span.addEvent('No session available', {
|
|
347
|
-
'session.id': session?.id,
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
326
|
span.end();
|
|
351
327
|
callback(err, null, resp);
|
|
352
328
|
return;
|
|
@@ -413,30 +389,6 @@ class Database extends common.GrpcServiceObject {
|
|
|
413
389
|
callback(null, table, operation, resp);
|
|
414
390
|
});
|
|
415
391
|
}
|
|
416
|
-
/**
|
|
417
|
-
* Decorates transaction so that when end() is called it will return the session
|
|
418
|
-
* back into the pool.
|
|
419
|
-
*
|
|
420
|
-
* @private
|
|
421
|
-
*
|
|
422
|
-
* @param {Session} session The session to release.
|
|
423
|
-
* @param {Transaction} transaction The transaction to observe.
|
|
424
|
-
* @returns {Transaction}
|
|
425
|
-
*/
|
|
426
|
-
_releaseOnEnd(session, transaction, span) {
|
|
427
|
-
transaction.once('end', () => {
|
|
428
|
-
try {
|
|
429
|
-
this.sessionFactory_.release(session);
|
|
430
|
-
}
|
|
431
|
-
catch (e) {
|
|
432
|
-
(0, instrument_1.setSpanErrorAndException)(span, e);
|
|
433
|
-
this.emit('error', e);
|
|
434
|
-
}
|
|
435
|
-
finally {
|
|
436
|
-
span.end();
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
392
|
delete(optionsOrCallback, cb) {
|
|
441
393
|
const gaxOpts = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
|
442
394
|
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
@@ -738,30 +690,16 @@ class Database extends common.GrpcServiceObject {
|
|
|
738
690
|
return;
|
|
739
691
|
}
|
|
740
692
|
const snapshot = session.snapshot(options, this.queryOptions_);
|
|
693
|
+
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
741
694
|
snapshot.begin(err => {
|
|
742
695
|
if (err) {
|
|
743
696
|
(0, instrument_1.setSpanError)(span, err);
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
span.addEvent('No session available', {
|
|
747
|
-
'session.id': session?.id,
|
|
748
|
-
});
|
|
749
|
-
session.lastError = err;
|
|
750
|
-
this.sessionFactory_.release(session);
|
|
751
|
-
span.end();
|
|
752
|
-
this.getSnapshot(options, callback);
|
|
753
|
-
}
|
|
754
|
-
else {
|
|
755
|
-
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
756
|
-
this.sessionFactory_.release(session);
|
|
757
|
-
span.end();
|
|
758
|
-
callback(err);
|
|
759
|
-
}
|
|
697
|
+
span.end();
|
|
698
|
+
callback(err);
|
|
760
699
|
return;
|
|
761
700
|
}
|
|
762
|
-
this._releaseOnEnd(session, snapshot, span);
|
|
763
701
|
span.end();
|
|
764
|
-
callback(
|
|
702
|
+
callback(null, snapshot);
|
|
765
703
|
});
|
|
766
704
|
});
|
|
767
705
|
});
|
|
@@ -777,21 +715,22 @@ class Database extends common.GrpcServiceObject {
|
|
|
777
715
|
...this._traceConfig,
|
|
778
716
|
transactionTag: options.requestOptions?.transactionTag,
|
|
779
717
|
}, span => {
|
|
780
|
-
this.sessionFactory_.
|
|
781
|
-
if (
|
|
782
|
-
if (options.requestOptions) {
|
|
783
|
-
transaction.requestOptions = Object.assign(transaction.requestOptions || {}, options.requestOptions);
|
|
784
|
-
}
|
|
785
|
-
transaction?.setReadWriteTransactionOptions(options);
|
|
786
|
-
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
787
|
-
transaction._observabilityOptions = this._observabilityOptions;
|
|
788
|
-
this._releaseOnEnd(session, transaction, span);
|
|
789
|
-
}
|
|
790
|
-
else {
|
|
718
|
+
this.sessionFactory_.getSession((err, session, transaction) => {
|
|
719
|
+
if (err) {
|
|
791
720
|
(0, instrument_1.setSpanError)(span, err);
|
|
721
|
+
span.end();
|
|
722
|
+
cb(err);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const activeTransaction = transaction ?? session.transaction(this.queryOptions_);
|
|
726
|
+
if (options.requestOptions) {
|
|
727
|
+
activeTransaction.requestOptions = Object.assign(activeTransaction.requestOptions || {}, options.requestOptions);
|
|
792
728
|
}
|
|
729
|
+
activeTransaction?.setReadWriteTransactionOptions(options);
|
|
730
|
+
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
731
|
+
activeTransaction._observabilityOptions = this._observabilityOptions;
|
|
793
732
|
span.end();
|
|
794
|
-
cb(
|
|
733
|
+
cb(null, activeTransaction);
|
|
795
734
|
});
|
|
796
735
|
});
|
|
797
736
|
}
|
|
@@ -853,18 +792,15 @@ class Database extends common.GrpcServiceObject {
|
|
|
853
792
|
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
854
793
|
config.reqOpts.session = session.formattedName_;
|
|
855
794
|
this.request(config, (err, ...args) => {
|
|
856
|
-
sessionFactory_.release(session);
|
|
857
795
|
callback(err, ...args);
|
|
858
796
|
});
|
|
859
797
|
};
|
|
860
|
-
const session = sessionFactory_.
|
|
861
|
-
? sessionFactory_.getSessionSync?.()
|
|
862
|
-
: null;
|
|
798
|
+
const session = sessionFactory_.getSessionSync?.();
|
|
863
799
|
if (session) {
|
|
864
800
|
onSession(null, session);
|
|
865
801
|
return;
|
|
866
802
|
}
|
|
867
|
-
sessionFactory_.
|
|
803
|
+
sessionFactory_.getSession(onSession);
|
|
868
804
|
}
|
|
869
805
|
/**
|
|
870
806
|
* Make an API request as a stream, first assuring an active session is used.
|
|
@@ -879,11 +815,11 @@ class Database extends common.GrpcServiceObject {
|
|
|
879
815
|
const self = this;
|
|
880
816
|
const sessionFactory_ = this.sessionFactory_;
|
|
881
817
|
let requestStream;
|
|
882
|
-
let
|
|
818
|
+
let aborted = false;
|
|
883
819
|
const waitForSessionStream = streamEvents(through.obj());
|
|
884
820
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
885
821
|
waitForSessionStream.abort = () => {
|
|
886
|
-
|
|
822
|
+
aborted = true;
|
|
887
823
|
if (requestStream) {
|
|
888
824
|
requestStream.cancel();
|
|
889
825
|
}
|
|
@@ -891,29 +827,21 @@ class Database extends common.GrpcServiceObject {
|
|
|
891
827
|
function destroyStream(err) {
|
|
892
828
|
waitForSessionStream.destroy(err);
|
|
893
829
|
}
|
|
894
|
-
function releaseSession() {
|
|
895
|
-
if (session) {
|
|
896
|
-
sessionFactory_.release(session);
|
|
897
|
-
session = null;
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
830
|
waitForSessionStream.on('reading', () => {
|
|
901
|
-
sessionFactory_.getSession((err,
|
|
831
|
+
sessionFactory_.getSession((err, session) => {
|
|
832
|
+
if (aborted) {
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
902
835
|
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
903
836
|
if (err) {
|
|
904
837
|
(0, instrument_1.setSpanError)(span, err);
|
|
905
838
|
destroyStream(err);
|
|
906
839
|
return;
|
|
907
840
|
}
|
|
908
|
-
span.addEvent('Using Session', { 'session.id':
|
|
909
|
-
session = session_;
|
|
841
|
+
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
910
842
|
config.reqOpts.session = session.formattedName_;
|
|
911
843
|
requestStream = self.requestStream(config);
|
|
912
|
-
requestStream
|
|
913
|
-
.on('error', releaseSession)
|
|
914
|
-
.on('error', destroyStream)
|
|
915
|
-
.on('end', releaseSession)
|
|
916
|
-
.pipe(waitForSessionStream);
|
|
844
|
+
requestStream.on('error', destroyStream).pipe(waitForSessionStream);
|
|
917
845
|
});
|
|
918
846
|
});
|
|
919
847
|
return waitForSessionStream;
|
|
@@ -956,8 +884,7 @@ class Database extends common.GrpcServiceObject {
|
|
|
956
884
|
const options = typeof optionsOrCallback === 'object'
|
|
957
885
|
? optionsOrCallback
|
|
958
886
|
: {};
|
|
959
|
-
if (this.runStream !== Database.prototype.runStream
|
|
960
|
-
!this.sessionFactory_.isMultiplexedEnabled()) {
|
|
887
|
+
if (this.runStream !== Database.prototype.runStream) {
|
|
961
888
|
this._runLegacy(query, options, callback);
|
|
962
889
|
return;
|
|
963
890
|
}
|
|
@@ -965,8 +892,8 @@ class Database extends common.GrpcServiceObject {
|
|
|
965
892
|
}
|
|
966
893
|
/**
|
|
967
894
|
* Always runs the query through the full streaming pipeline (Database.prototype.runStream).
|
|
968
|
-
* Used when runStream has been overridden on Database
|
|
969
|
-
*
|
|
895
|
+
* Used when runStream has been overridden on Database to preserve backward compatibility
|
|
896
|
+
* with custom stream implementations.
|
|
970
897
|
*
|
|
971
898
|
* @private
|
|
972
899
|
*/
|
|
@@ -975,8 +902,8 @@ class Database extends common.GrpcServiceObject {
|
|
|
975
902
|
let stats;
|
|
976
903
|
let metadata;
|
|
977
904
|
(0, instrument_1.startTrace)('Database.run', {
|
|
978
|
-
...query,
|
|
979
905
|
...this._traceConfig,
|
|
906
|
+
...(0, instrument_1.getQueryTraceConfig)(query),
|
|
980
907
|
}, span => {
|
|
981
908
|
this.runStream(query, options)
|
|
982
909
|
.on('error', err => {
|
|
@@ -1010,8 +937,8 @@ class Database extends common.GrpcServiceObject {
|
|
|
1010
937
|
*/
|
|
1011
938
|
_run(query, options, callback) {
|
|
1012
939
|
const traceConfig = {
|
|
1013
|
-
...query,
|
|
1014
940
|
...this._traceConfig,
|
|
941
|
+
...(0, instrument_1.getQueryTraceConfig)(query),
|
|
1015
942
|
};
|
|
1016
943
|
(0, instrument_1.startTrace)('Database.run', traceConfig, runSpan => {
|
|
1017
944
|
(0, instrument_1.startTrace)('Database.runStream', traceConfig, streamSpan => {
|
|
@@ -1021,7 +948,7 @@ class Database extends common.GrpcServiceObject {
|
|
|
1021
948
|
}
|
|
1022
949
|
/**
|
|
1023
950
|
* Acquires a session and executes the query on a snapshot, managing span
|
|
1024
|
-
* lifetimes
|
|
951
|
+
* lifetimes.
|
|
1025
952
|
*
|
|
1026
953
|
* @private
|
|
1027
954
|
*/
|
|
@@ -1050,7 +977,7 @@ class Database extends common.GrpcServiceObject {
|
|
|
1050
977
|
streamSpan.addEvent('Using Session', { 'session.id': session?.id });
|
|
1051
978
|
try {
|
|
1052
979
|
snapshot = session.snapshot(options, this.queryOptions_);
|
|
1053
|
-
this._runOnSnapshot(snapshot,
|
|
980
|
+
this._runOnSnapshot(snapshot, query, complete);
|
|
1054
981
|
}
|
|
1055
982
|
catch (syncError) {
|
|
1056
983
|
// Defer error delivery via nextTick so callback callers never experience
|
|
@@ -1066,19 +993,11 @@ class Database extends common.GrpcServiceObject {
|
|
|
1066
993
|
this.sessionFactory_.getSession(onSession);
|
|
1067
994
|
}
|
|
1068
995
|
/**
|
|
1069
|
-
* Executes the query on the snapshot
|
|
996
|
+
* Executes the query on the snapshot.
|
|
1070
997
|
*
|
|
1071
998
|
* @private
|
|
1072
999
|
*/
|
|
1073
|
-
_runOnSnapshot(snapshot,
|
|
1074
|
-
snapshot.once('end', () => {
|
|
1075
|
-
try {
|
|
1076
|
-
this.sessionFactory_.release(session);
|
|
1077
|
-
}
|
|
1078
|
-
catch (releaseError) {
|
|
1079
|
-
this.emit('error', releaseError);
|
|
1080
|
-
}
|
|
1081
|
-
});
|
|
1000
|
+
_runOnSnapshot(snapshot, query, callback) {
|
|
1082
1001
|
const snapshotWithRun = snapshot;
|
|
1083
1002
|
try {
|
|
1084
1003
|
if (typeof snapshotWithRun._run === 'function' &&
|
|
@@ -1099,12 +1018,10 @@ class Database extends common.GrpcServiceObject {
|
|
|
1099
1018
|
}
|
|
1100
1019
|
runPartitionedUpdate(query, callback) {
|
|
1101
1020
|
return (0, instrument_1.startTrace)('Database.runPartitionedUpdate', {
|
|
1102
|
-
...query,
|
|
1103
1021
|
...this._traceConfig,
|
|
1104
|
-
|
|
1105
|
-
?.requestTag,
|
|
1022
|
+
...(0, instrument_1.getQueryTraceConfig)(query),
|
|
1106
1023
|
}, span => {
|
|
1107
|
-
this.sessionFactory_.
|
|
1024
|
+
this.sessionFactory_.getSession((err, session) => {
|
|
1108
1025
|
if (err) {
|
|
1109
1026
|
(0, instrument_1.setSpanError)(span, err);
|
|
1110
1027
|
span.end();
|
|
@@ -1128,21 +1045,18 @@ class Database extends common.GrpcServiceObject {
|
|
|
1128
1045
|
}
|
|
1129
1046
|
transaction.begin(err => {
|
|
1130
1047
|
if (err) {
|
|
1131
|
-
this.sessionFactory_.release(session);
|
|
1132
1048
|
callback(err, 0);
|
|
1133
1049
|
return;
|
|
1134
1050
|
}
|
|
1135
1051
|
transaction.runUpdate(query, async (err, updateCount) => {
|
|
1136
1052
|
if (err) {
|
|
1137
1053
|
if (err.code !== google_gax_1.grpc.status.ABORTED) {
|
|
1138
|
-
this.sessionFactory_.release(session);
|
|
1139
1054
|
callback(err, 0);
|
|
1140
1055
|
return;
|
|
1141
1056
|
}
|
|
1142
1057
|
void this._runPartitionedUpdate(session, query, callback);
|
|
1143
1058
|
}
|
|
1144
1059
|
else {
|
|
1145
|
-
this.sessionFactory_.release(session);
|
|
1146
1060
|
callback(null, updateCount);
|
|
1147
1061
|
return;
|
|
1148
1062
|
}
|
|
@@ -1277,9 +1191,8 @@ class Database extends common.GrpcServiceObject {
|
|
|
1277
1191
|
runStream(query, options) {
|
|
1278
1192
|
const proxyStream = through.obj();
|
|
1279
1193
|
return (0, instrument_1.startTrace)('Database.runStream', {
|
|
1280
|
-
...query,
|
|
1281
1194
|
...this._traceConfig,
|
|
1282
|
-
|
|
1195
|
+
...(0, instrument_1.getQueryTraceConfig)(query),
|
|
1283
1196
|
}, span => {
|
|
1284
1197
|
this.sessionFactory_.getSession((err, session) => {
|
|
1285
1198
|
if (err) {
|
|
@@ -1290,42 +1203,15 @@ class Database extends common.GrpcServiceObject {
|
|
|
1290
1203
|
}
|
|
1291
1204
|
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
1292
1205
|
const snapshot = session.snapshot(options, this.queryOptions_);
|
|
1293
|
-
|
|
1294
|
-
let dataReceived = false;
|
|
1295
|
-
let dataStream = snapshot.runStream(query);
|
|
1206
|
+
const dataStream = snapshot.runStream(query);
|
|
1296
1207
|
const endListener = () => {
|
|
1297
1208
|
snapshot.end();
|
|
1298
1209
|
};
|
|
1299
1210
|
dataStream
|
|
1300
|
-
.once('data', () => (dataReceived = true))
|
|
1301
1211
|
.once('error', err => {
|
|
1302
1212
|
(0, instrument_1.setSpanError)(span, err);
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
!this.sessionFactory_.isMultiplexedEnabled()) {
|
|
1306
|
-
// If it is a 'Session not found' error and we have not yet received
|
|
1307
|
-
// any data, we can safely retry the query on a new session.
|
|
1308
|
-
// Register the error on the session so the pool can discard it.
|
|
1309
|
-
if (session) {
|
|
1310
|
-
session.lastError = err;
|
|
1311
|
-
}
|
|
1312
|
-
span.addEvent('No session available', {
|
|
1313
|
-
'session.id': session?.id,
|
|
1314
|
-
});
|
|
1315
|
-
// Remove the current data stream from the end user stream.
|
|
1316
|
-
dataStream.unpipe(proxyStream);
|
|
1317
|
-
dataStream.removeListener('end', endListener);
|
|
1318
|
-
dataStream.end();
|
|
1319
|
-
snapshot.end();
|
|
1320
|
-
span.end();
|
|
1321
|
-
// Create a new data stream and add it to the end user stream.
|
|
1322
|
-
dataStream = this.runStream(query, options);
|
|
1323
|
-
dataStream.pipe(proxyStream);
|
|
1324
|
-
}
|
|
1325
|
-
else {
|
|
1326
|
-
proxyStream.destroy(err);
|
|
1327
|
-
snapshot.end();
|
|
1328
|
-
}
|
|
1213
|
+
proxyStream.destroy(err);
|
|
1214
|
+
snapshot.end();
|
|
1329
1215
|
})
|
|
1330
1216
|
.on('stats', stats => proxyStream.emit('stats', stats))
|
|
1331
1217
|
.on('response', response => proxyStream.emit('response', response))
|
|
@@ -1354,57 +1240,33 @@ class Database extends common.GrpcServiceObject {
|
|
|
1354
1240
|
}, span => this._runTransaction(span, options, runFn));
|
|
1355
1241
|
}
|
|
1356
1242
|
_runTransaction(span, options, runFn) {
|
|
1357
|
-
this.sessionFactory_.
|
|
1243
|
+
this.sessionFactory_.getSession((err, session, transaction) => {
|
|
1358
1244
|
if (err) {
|
|
1359
1245
|
(0, instrument_1.setSpanError)(span, err);
|
|
1360
|
-
if ((0, session_pool_1.isSessionNotFoundError)(err)) {
|
|
1361
|
-
span.addEvent('No session available', {
|
|
1362
|
-
'session.id': session?.id,
|
|
1363
|
-
});
|
|
1364
|
-
span.end();
|
|
1365
|
-
this.runTransaction(options, runFn);
|
|
1366
|
-
return;
|
|
1367
|
-
}
|
|
1368
1246
|
span.end();
|
|
1369
1247
|
runFn(err);
|
|
1370
1248
|
return;
|
|
1371
1249
|
}
|
|
1372
|
-
|
|
1250
|
+
span.addEvent('Using Session', { 'session.id': session?.id });
|
|
1251
|
+
const activeTransaction = transaction ?? session.transaction(this.queryOptions_);
|
|
1252
|
+
this._executeTransactionRunner(session, activeTransaction, span, options, runFn);
|
|
1373
1253
|
});
|
|
1374
1254
|
}
|
|
1375
1255
|
_executeTransactionRunner(session, transaction, span, options, runFn) {
|
|
1376
1256
|
transaction._observabilityOptions = this._observabilityOptions;
|
|
1377
1257
|
transaction.requestOptions = Object.assign(transaction.requestOptions || {}, options.requestOptions);
|
|
1378
1258
|
transaction.setReadWriteTransactionOptions(options);
|
|
1379
|
-
const release = () => {
|
|
1380
|
-
this.sessionFactory_.release(session);
|
|
1381
|
-
span.end();
|
|
1382
|
-
};
|
|
1383
1259
|
const runner = new transaction_runner_1.TransactionRunner(session, transaction, runFn, options);
|
|
1384
1260
|
runner
|
|
1385
1261
|
.run()
|
|
1386
1262
|
.then(() => {
|
|
1387
|
-
|
|
1388
|
-
return null;
|
|
1389
|
-
}, err => {
|
|
1390
|
-
(0, instrument_1.setSpanError)(span, err);
|
|
1391
|
-
if ((0, session_pool_1.isSessionNotFoundError)(err)) {
|
|
1392
|
-
span.addEvent('No session available', {
|
|
1393
|
-
'session.id': session.id,
|
|
1394
|
-
});
|
|
1395
|
-
release();
|
|
1396
|
-
this.runTransaction(options, runFn);
|
|
1397
|
-
}
|
|
1398
|
-
else {
|
|
1399
|
-
setImmediate(runFn, err);
|
|
1400
|
-
release();
|
|
1401
|
-
}
|
|
1263
|
+
span.end();
|
|
1402
1264
|
return null;
|
|
1403
1265
|
})
|
|
1404
1266
|
.catch(err => {
|
|
1405
|
-
(0, instrument_1.
|
|
1267
|
+
(0, instrument_1.setSpanError)(span, err);
|
|
1268
|
+
setImmediate(runFn, err);
|
|
1406
1269
|
span.end();
|
|
1407
|
-
this.emit('error', err);
|
|
1408
1270
|
});
|
|
1409
1271
|
}
|
|
1410
1272
|
/**
|
|
@@ -1432,9 +1294,8 @@ class Database extends common.GrpcServiceObject {
|
|
|
1432
1294
|
*
|
|
1433
1295
|
* **NOTE:** In the event that you encounter an error while reading/writing,
|
|
1434
1296
|
* if you decide to forgo calling {@link Transaction#commit} or
|
|
1435
|
-
* {@link Transaction#rollback}, then you
|
|
1436
|
-
* {@link Transaction#end} to
|
|
1437
|
-
* **Failure to do could result in a Session leak.**
|
|
1297
|
+
* {@link Transaction#rollback}, then you should call
|
|
1298
|
+
* {@link Transaction#end} to end the transaction.
|
|
1438
1299
|
*
|
|
1439
1300
|
* For a more complete listing of functionality available to a Transaction, see
|
|
1440
1301
|
* the {@link Transaction} API documentation. For a general overview of
|
|
@@ -1474,45 +1335,28 @@ class Database extends common.GrpcServiceObject {
|
|
|
1474
1335
|
? optionsOrRunFn
|
|
1475
1336
|
: {};
|
|
1476
1337
|
let sessionId = '';
|
|
1477
|
-
const getSession = this.sessionFactory_.
|
|
1338
|
+
const getSession = this.sessionFactory_.getSession.bind(this.sessionFactory_);
|
|
1478
1339
|
return (0, instrument_1.startTrace)('Database.runTransactionAsync', {
|
|
1479
1340
|
...this._traceConfig,
|
|
1480
1341
|
transactionTag: options?.requestOptions?.transactionTag,
|
|
1481
1342
|
}, async (span) => {
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
}
|
|
1500
|
-
finally {
|
|
1501
|
-
span.end();
|
|
1502
|
-
this.sessionFactory_.release(session);
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
catch (e) {
|
|
1506
|
-
if ((0, session_pool_1.isSessionNotFoundError)(e)) {
|
|
1507
|
-
span.addEvent('No session available', {
|
|
1508
|
-
'session.id': sessionId,
|
|
1509
|
-
});
|
|
1510
|
-
}
|
|
1511
|
-
else {
|
|
1512
|
-
span.end();
|
|
1513
|
-
throw e;
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
1343
|
+
try {
|
|
1344
|
+
const session = this.sessionFactory_.getSessionSync?.() ??
|
|
1345
|
+
(await (0, promisify_1.promisify)(getSession)())[0];
|
|
1346
|
+
const activeTransaction = session.transaction(this.queryOptions_);
|
|
1347
|
+
activeTransaction.requestOptions = Object.assign(activeTransaction.requestOptions || {}, options.requestOptions);
|
|
1348
|
+
activeTransaction.setReadWriteTransactionOptions(options);
|
|
1349
|
+
sessionId = session?.id;
|
|
1350
|
+
span.addEvent('Using Session', { 'session.id': sessionId });
|
|
1351
|
+
const runner = new transaction_runner_1.AsyncTransactionRunner(session, activeTransaction, runFn, options);
|
|
1352
|
+
return await runner.run();
|
|
1353
|
+
}
|
|
1354
|
+
catch (e) {
|
|
1355
|
+
(0, instrument_1.setSpanErrorAndException)(span, e);
|
|
1356
|
+
throw e;
|
|
1357
|
+
}
|
|
1358
|
+
finally {
|
|
1359
|
+
span.end();
|
|
1516
1360
|
}
|
|
1517
1361
|
});
|
|
1518
1362
|
}
|
|
@@ -1580,7 +1424,7 @@ class Database extends common.GrpcServiceObject {
|
|
|
1580
1424
|
...this._traceConfig,
|
|
1581
1425
|
transactionTag: options?.requestOptions?.transactionTag,
|
|
1582
1426
|
}, span => {
|
|
1583
|
-
this.sessionFactory_.
|
|
1427
|
+
this.sessionFactory_.getSession((err, session) => {
|
|
1584
1428
|
if (err) {
|
|
1585
1429
|
proxyStream.destroy(err);
|
|
1586
1430
|
(0, instrument_1.setSpanError)(span, err);
|
|
@@ -1595,8 +1439,7 @@ class Database extends common.GrpcServiceObject {
|
|
|
1595
1439
|
requestOptions: options?.requestOptions,
|
|
1596
1440
|
excludeTxnFromChangeStream: options?.excludeTxnFromChangeStreams,
|
|
1597
1441
|
});
|
|
1598
|
-
|
|
1599
|
-
let dataStream = this.requestStream({
|
|
1442
|
+
const dataStream = this.requestStream({
|
|
1600
1443
|
client: 'SpannerClient',
|
|
1601
1444
|
method: 'batchWrite',
|
|
1602
1445
|
reqOpts,
|
|
@@ -1604,36 +1447,13 @@ class Database extends common.GrpcServiceObject {
|
|
|
1604
1447
|
headers: this.commonHeaders_,
|
|
1605
1448
|
});
|
|
1606
1449
|
dataStream
|
|
1607
|
-
.once('data', () => (dataReceived = true))
|
|
1608
1450
|
.once('error', err => {
|
|
1609
1451
|
(0, instrument_1.setSpanError)(span, err);
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
// If there's a 'Session not found' error and we have not yet received
|
|
1613
|
-
// any data, we can safely retry the writes on a new session.
|
|
1614
|
-
// Register the error on the session so the pool can discard it.
|
|
1615
|
-
if (session) {
|
|
1616
|
-
session.lastError = err;
|
|
1617
|
-
}
|
|
1618
|
-
span.addEvent('No session available', {
|
|
1619
|
-
'session.id': session?.id,
|
|
1620
|
-
});
|
|
1621
|
-
// Remove the current data stream from the end user stream.
|
|
1622
|
-
dataStream.unpipe(proxyStream);
|
|
1623
|
-
dataStream.end();
|
|
1624
|
-
span.end();
|
|
1625
|
-
// Create a new stream and add it to the end user stream.
|
|
1626
|
-
dataStream = this.batchWriteAtLeastOnce(mutationGroups, options);
|
|
1627
|
-
dataStream.pipe(proxyStream);
|
|
1628
|
-
}
|
|
1629
|
-
else {
|
|
1630
|
-
span.end();
|
|
1631
|
-
proxyStream.destroy(err);
|
|
1632
|
-
}
|
|
1452
|
+
span.end();
|
|
1453
|
+
proxyStream.destroy(err);
|
|
1633
1454
|
})
|
|
1634
1455
|
.once('end', () => {
|
|
1635
1456
|
span.end();
|
|
1636
|
-
this.sessionFactory_.release(session);
|
|
1637
1457
|
})
|
|
1638
1458
|
.pipe(proxyStream);
|
|
1639
1459
|
});
|
|
@@ -1649,16 +1469,6 @@ class Database extends common.GrpcServiceObject {
|
|
|
1649
1469
|
: {};
|
|
1650
1470
|
return (0, instrument_1.startTrace)('Database.writeAtLeastOnce', this._traceConfig, span => {
|
|
1651
1471
|
this.sessionFactory_.getSession((err, session, transaction) => {
|
|
1652
|
-
if (err &&
|
|
1653
|
-
(0, session_pool_1.isSessionNotFoundError)(err) &&
|
|
1654
|
-
!this.sessionFactory_.isMultiplexedEnabled()) {
|
|
1655
|
-
span.addEvent('No session available', {
|
|
1656
|
-
'session.id': session?.id,
|
|
1657
|
-
});
|
|
1658
|
-
span.end();
|
|
1659
|
-
this.writeAtLeastOnce(mutations, options, cb);
|
|
1660
|
-
return;
|
|
1661
|
-
}
|
|
1662
1472
|
if (err) {
|
|
1663
1473
|
(0, instrument_1.setSpanError)(span, err);
|
|
1664
1474
|
span.end();
|
|
@@ -1674,7 +1484,6 @@ class Database extends common.GrpcServiceObject {
|
|
|
1674
1484
|
cb(error);
|
|
1675
1485
|
return;
|
|
1676
1486
|
}
|
|
1677
|
-
this._releaseOnEnd(session, activeTransaction, span);
|
|
1678
1487
|
try {
|
|
1679
1488
|
activeTransaction.setReadWriteTransactionOptions(options);
|
|
1680
1489
|
activeTransaction.setQueuedMutations(mutations.proto());
|