@google-cloud/spanner 9.0.0 → 9.1.1
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 +3 -16
- package/build/src/backup.js.map +1 -0
- package/build/src/batch-transaction.js +15 -12
- package/build/src/batch-transaction.js.map +1 -0
- package/build/src/codec.d.ts +6 -0
- package/build/src/codec.js +99 -34
- package/build/src/codec.js.map +1 -0
- package/build/src/common-grpc/service-object.js.map +1 -0
- package/build/src/common-grpc/service.js.map +1 -0
- package/build/src/common.d.ts +14 -0
- package/build/src/common.js +27 -1
- package/build/src/common.js.map +1 -0
- package/build/src/database.d.ts +31 -41
- package/build/src/database.js +122 -313
- package/build/src/database.js.map +1 -0
- package/build/src/helper.d.ts +12 -0
- package/build/src/helper.js +44 -0
- package/build/src/helper.js.map +1 -0
- package/build/src/index.d.ts +33 -8
- package/build/src/index.js +338 -134
- package/build/src/index.js.map +1 -0
- package/build/src/instance-config.js.map +1 -0
- package/build/src/instance.d.ts +15 -2
- package/build/src/instance.js +11 -2
- package/build/src/instance.js.map +1 -0
- package/build/src/instrument.d.ts +12 -1
- package/build/src/instrument.js +13 -0
- package/build/src/instrument.js.map +1 -0
- package/build/src/metrics/constants.d.ts +6 -0
- package/build/src/metrics/constants.js +6 -1
- package/build/src/metrics/constants.js.map +1 -0
- package/build/src/metrics/external-types.d.ts +9 -1
- package/build/src/metrics/external-types.js.map +1 -0
- package/build/src/metrics/interceptor.js +32 -28
- package/build/src/metrics/interceptor.js.map +1 -0
- package/build/src/metrics/metrics-tracer-factory.d.ts +24 -18
- package/build/src/metrics/metrics-tracer-factory.js +94 -77
- package/build/src/metrics/metrics-tracer-factory.js.map +1 -0
- package/build/src/metrics/metrics-tracer.d.ts +19 -6
- package/build/src/metrics/metrics-tracer.js +147 -55
- package/build/src/metrics/metrics-tracer.js.map +1 -0
- package/build/src/metrics/spanner-metrics-exporter.d.ts +49 -1
- package/build/src/metrics/spanner-metrics-exporter.js +207 -4
- package/build/src/metrics/spanner-metrics-exporter.js.map +1 -0
- package/build/src/metrics/transform.d.ts +2 -2
- package/build/src/metrics/transform.js +7 -7
- package/build/src/metrics/transform.js.map +1 -0
- package/build/src/multiplexed-session.d.ts +13 -2
- package/build/src/multiplexed-session.js +20 -4
- package/build/src/multiplexed-session.js.map +1 -0
- package/build/src/partial-result-stream.d.ts +26 -33
- package/build/src/partial-result-stream.js +263 -283
- package/build/src/partial-result-stream.js.map +1 -0
- package/build/src/protos.js.map +1 -0
- package/build/src/request-stream-coordinator.d.ts +88 -0
- package/build/src/request-stream-coordinator.js +229 -0
- package/build/src/request-stream-coordinator.js.map +1 -0
- package/build/src/request_id_header.d.ts +6 -4
- package/build/src/request_id_header.js +24 -24
- package/build/src/request_id_header.js.map +1 -0
- package/build/src/session-factory.d.ts +52 -63
- package/build/src/session-factory.js +41 -92
- package/build/src/session-factory.js.map +1 -0
- package/build/src/session-pool.d.ts +97 -227
- package/build/src/session-pool.js +74 -547
- package/build/src/session-pool.js.map +1 -0
- package/build/src/session.js +1 -1
- package/build/src/session.js.map +1 -0
- package/build/src/table.js.map +1 -0
- package/build/src/transaction-runner.js +1 -12
- package/build/src/transaction-runner.js.map +1 -0
- package/build/src/transaction.d.ts +5 -5
- package/build/src/transaction.js +72 -112
- package/build/src/transaction.js.map +1 -0
- package/build/src/v1/index.js.map +1 -0
- package/package.json +25 -27
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/*!
|
|
3
|
-
* Copyright
|
|
3
|
+
* Copyright 2017 Google Inc. All Rights Reserved.
|
|
4
4
|
*
|
|
5
5
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
6
|
* you may not use this file except in compliance with the License.
|
|
@@ -18,12 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
exports.SessionPool = exports.SessionPoolExhaustedError = exports.SessionLeakError = exports.ReleaseError = void 0;
|
|
19
19
|
exports.isSessionNotFoundError = isSessionNotFoundError;
|
|
20
20
|
const events_1 = require("events");
|
|
21
|
-
const p_queue_1 = require("p-queue");
|
|
22
21
|
const google_gax_1 = require("google-gax");
|
|
23
|
-
const api_1 = require("@opentelemetry/api");
|
|
24
|
-
const trace = require("stack-trace");
|
|
25
|
-
const instrument_1 = require("./instrument");
|
|
26
|
-
const helper_1 = require("./helper");
|
|
27
22
|
const DEFAULTS = {
|
|
28
23
|
acquireTimeout: Infinity,
|
|
29
24
|
concurrency: Infinity,
|
|
@@ -38,6 +33,7 @@ const DEFAULTS = {
|
|
|
38
33
|
/**
|
|
39
34
|
* Error to be thrown when attempting to release unknown resources.
|
|
40
35
|
*
|
|
36
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
41
37
|
* @private
|
|
42
38
|
*/
|
|
43
39
|
class ReleaseError extends google_gax_1.GoogleError {
|
|
@@ -51,11 +47,12 @@ exports.ReleaseError = ReleaseError;
|
|
|
51
47
|
/**
|
|
52
48
|
* Error to be thrown when session leaks are detected.
|
|
53
49
|
*
|
|
50
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
54
51
|
* @private
|
|
55
52
|
*/
|
|
56
53
|
class SessionLeakError extends google_gax_1.GoogleError {
|
|
57
54
|
messages;
|
|
58
|
-
constructor(leaks) {
|
|
55
|
+
constructor(leaks = []) {
|
|
59
56
|
super(`${leaks.length} session leak(s) detected.`);
|
|
60
57
|
// Restore error name that was overwritten by the super constructor call.
|
|
61
58
|
this.name = SessionLeakError.name;
|
|
@@ -65,10 +62,12 @@ class SessionLeakError extends google_gax_1.GoogleError {
|
|
|
65
62
|
exports.SessionLeakError = SessionLeakError;
|
|
66
63
|
/**
|
|
67
64
|
* Error to be thrown when the session pool is exhausted.
|
|
65
|
+
*
|
|
66
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
68
67
|
*/
|
|
69
68
|
class SessionPoolExhaustedError extends google_gax_1.GoogleError {
|
|
70
69
|
messages;
|
|
71
|
-
constructor(leaks) {
|
|
70
|
+
constructor(leaks = []) {
|
|
72
71
|
super("No resources available." /* errors.Exhausted */);
|
|
73
72
|
// Restore error name that was overwritten by the super constructor call.
|
|
74
73
|
this.name = SessionPoolExhaustedError.name;
|
|
@@ -78,6 +77,7 @@ class SessionPoolExhaustedError extends google_gax_1.GoogleError {
|
|
|
78
77
|
exports.SessionPoolExhaustedError = SessionPoolExhaustedError;
|
|
79
78
|
/**
|
|
80
79
|
* Checks whether the given error is a 'Session not found' error.
|
|
80
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
81
81
|
* @param error the error to check
|
|
82
82
|
* @return true if the error is a 'Session not found' error, and otherwise false.
|
|
83
83
|
*/
|
|
@@ -87,6 +87,10 @@ function isSessionNotFoundError(error) {
|
|
|
87
87
|
error.message.includes('Session not found'));
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
|
+
* * @deprecated Session pool is deprecated and will be removed in a future release.
|
|
91
|
+
Multiplexed sessions are now used for all operations by default, eliminating
|
|
92
|
+
the need for session pooling.
|
|
93
|
+
|
|
90
94
|
* Class used to manage connections to Spanner.
|
|
91
95
|
*
|
|
92
96
|
* **You don't need to use this class directly, connections will be handled for
|
|
@@ -99,19 +103,10 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
99
103
|
database;
|
|
100
104
|
isOpen;
|
|
101
105
|
options;
|
|
102
|
-
_acquires;
|
|
103
|
-
_evictHandle;
|
|
104
|
-
_inventory;
|
|
105
|
-
_onClose;
|
|
106
|
-
_pending = 0;
|
|
107
|
-
_waiters = 0;
|
|
108
|
-
_pingHandle;
|
|
109
|
-
_requests;
|
|
110
|
-
_traces;
|
|
111
|
-
_observabilityOptions;
|
|
112
106
|
/**
|
|
113
107
|
* Formats stack trace objects into Node-like stack trace.
|
|
114
108
|
*
|
|
109
|
+
* @deprecated Session pool is deprecated and will be removed in a future release.
|
|
115
110
|
* @param {object[]} trace The trace object.
|
|
116
111
|
* @return {string}
|
|
117
112
|
*/
|
|
@@ -125,12 +120,9 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
125
120
|
});
|
|
126
121
|
return `Session leak detected!\n${stack.join('\n')}`;
|
|
127
122
|
}
|
|
128
|
-
/**
|
|
129
|
-
* Total number of available sessions.
|
|
130
|
-
* @type {number}
|
|
131
|
-
*/
|
|
123
|
+
/** @deprecated Use totalWaiters instead. */
|
|
132
124
|
get available() {
|
|
133
|
-
return
|
|
125
|
+
return 0;
|
|
134
126
|
}
|
|
135
127
|
/** @deprecated Starting from v6.5.0 the same session can be reused for
|
|
136
128
|
* different types of transactions.
|
|
@@ -138,26 +130,38 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
138
130
|
get currentWriteFraction() {
|
|
139
131
|
return 0;
|
|
140
132
|
}
|
|
133
|
+
/** @deprecated Starting from v6.5.0 the same session can be reused for
|
|
134
|
+
* different types of transactions.
|
|
135
|
+
*/
|
|
136
|
+
get availableReads() {
|
|
137
|
+
return this.available;
|
|
138
|
+
}
|
|
139
|
+
/** @deprecated Starting from v6.5.0 the same session can be reused for
|
|
140
|
+
* different types of transactions.
|
|
141
|
+
*/
|
|
142
|
+
get availableWrites() {
|
|
143
|
+
return this.available;
|
|
144
|
+
}
|
|
141
145
|
/**
|
|
142
146
|
* Total number of borrowed sessions.
|
|
143
147
|
* @type {number}
|
|
144
148
|
*/
|
|
145
149
|
get borrowed() {
|
|
146
|
-
return
|
|
150
|
+
return 0;
|
|
147
151
|
}
|
|
148
152
|
/**
|
|
149
153
|
* Flag to determine if Pool is full.
|
|
150
154
|
* @type {boolean}
|
|
151
155
|
*/
|
|
152
156
|
get isFull() {
|
|
153
|
-
return
|
|
157
|
+
return false;
|
|
154
158
|
}
|
|
155
159
|
/** @deprecated Use `size()` instead. */
|
|
156
160
|
get reads() {
|
|
157
161
|
return this.size;
|
|
158
162
|
}
|
|
159
163
|
/**
|
|
160
|
-
* Total
|
|
164
|
+
* Total number of sessions in the pool.
|
|
161
165
|
* @type {number}
|
|
162
166
|
*/
|
|
163
167
|
get size() {
|
|
@@ -171,12 +175,20 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
171
175
|
get pendingPrepare() {
|
|
172
176
|
return 0;
|
|
173
177
|
}
|
|
178
|
+
/** @deprecated Use totalPending instead. */
|
|
179
|
+
get numPendingReads() {
|
|
180
|
+
return this.totalPending;
|
|
181
|
+
}
|
|
182
|
+
/** @deprecated Use totalPending instead. */
|
|
183
|
+
get numPendingWrites() {
|
|
184
|
+
return 0;
|
|
185
|
+
}
|
|
174
186
|
/**
|
|
175
|
-
*
|
|
187
|
+
* Total number of sessions currently being created by the pool.
|
|
176
188
|
* @type {number}
|
|
177
189
|
*/
|
|
178
190
|
get totalPending() {
|
|
179
|
-
return
|
|
191
|
+
return 0;
|
|
180
192
|
}
|
|
181
193
|
/** @deprecated Use totalWaiters instead. */
|
|
182
194
|
get numReadWaiters() {
|
|
@@ -184,14 +196,14 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
184
196
|
}
|
|
185
197
|
/** @deprecated Use totalWaiters instead. */
|
|
186
198
|
get numWriteWaiters() {
|
|
187
|
-
return
|
|
199
|
+
return 0;
|
|
188
200
|
}
|
|
189
201
|
/**
|
|
190
|
-
*
|
|
202
|
+
* Total number of waiters for a session from the pool.
|
|
191
203
|
* @type {number}
|
|
192
204
|
*/
|
|
193
205
|
get totalWaiters() {
|
|
194
|
-
return
|
|
206
|
+
return 0;
|
|
195
207
|
}
|
|
196
208
|
/**
|
|
197
209
|
* @constructor
|
|
@@ -209,75 +221,65 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
209
221
|
this.options.min = Math.min(this.options.min, this.options.max);
|
|
210
222
|
this.options.databaseRole = this.options.databaseRole
|
|
211
223
|
? this.options.databaseRole
|
|
212
|
-
: database
|
|
213
|
-
this._inventory = {
|
|
214
|
-
sessions: [],
|
|
215
|
-
borrowed: new Set(),
|
|
216
|
-
};
|
|
217
|
-
this._waiters = 0;
|
|
218
|
-
this._requests = new p_queue_1.default({
|
|
219
|
-
concurrency: this.options.concurrency,
|
|
220
|
-
});
|
|
221
|
-
this._acquires = new p_queue_1.default({
|
|
222
|
-
concurrency: 1,
|
|
223
|
-
});
|
|
224
|
-
this._traces = new Map();
|
|
225
|
-
this._observabilityOptions = database._observabilityOptions;
|
|
224
|
+
: database?.databaseRole;
|
|
226
225
|
}
|
|
227
226
|
/**
|
|
228
|
-
* Closes
|
|
227
|
+
* Closes the pool.
|
|
229
228
|
*
|
|
230
229
|
* @emits SessionPool#close
|
|
231
230
|
* @param {SessionPoolCloseCallback} callback The callback function.
|
|
232
231
|
*/
|
|
233
232
|
close(callback) {
|
|
234
|
-
const sessions = [
|
|
235
|
-
...this._inventory.sessions,
|
|
236
|
-
...this._inventory.borrowed,
|
|
237
|
-
];
|
|
238
233
|
this.isOpen = false;
|
|
239
|
-
this._stopHouseKeeping();
|
|
240
234
|
this.emit('close');
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
.then(() => {
|
|
245
|
-
const leaks = this._getLeaks();
|
|
246
|
-
let error;
|
|
247
|
-
this._inventory.sessions = [];
|
|
248
|
-
this._inventory.borrowed.clear();
|
|
249
|
-
if (leaks.length) {
|
|
250
|
-
error = new SessionLeakError(leaks);
|
|
251
|
-
}
|
|
252
|
-
callback(error);
|
|
253
|
-
})
|
|
254
|
-
.catch(err => callback(err));
|
|
235
|
+
if (callback) {
|
|
236
|
+
callback();
|
|
237
|
+
}
|
|
255
238
|
}
|
|
256
239
|
/**
|
|
257
240
|
* Retrieve a read session.
|
|
258
241
|
*
|
|
259
242
|
* @deprecated Use getSession instead.
|
|
243
|
+
*
|
|
260
244
|
* @param {GetReadSessionCallback} callback The callback function.
|
|
261
245
|
*/
|
|
262
246
|
getReadSession(callback) {
|
|
263
|
-
this.getSession((
|
|
247
|
+
this.getSession((err, session) => {
|
|
248
|
+
callback(err, session);
|
|
249
|
+
});
|
|
264
250
|
}
|
|
265
251
|
/**
|
|
266
252
|
* Retrieve a read/write session.
|
|
267
253
|
*
|
|
268
|
-
* @deprecated
|
|
254
|
+
* @deprecated Use getSession instead.
|
|
255
|
+
*
|
|
269
256
|
* @param {GetWriteSessionCallback} callback The callback function.
|
|
270
257
|
*/
|
|
271
258
|
getWriteSession(callback) {
|
|
272
259
|
this.getSession(callback);
|
|
273
260
|
}
|
|
274
261
|
/**
|
|
275
|
-
* Retrieve a session.
|
|
262
|
+
* Retrieve a session for read or read/write operations.
|
|
263
|
+
*
|
|
264
|
+
* Session pooling has been replaced by multiplexed sessions, so this returns
|
|
265
|
+
* the database's shared multiplexed session, together with a new read/write
|
|
266
|
+
* transaction, instead of a pooled session.
|
|
276
267
|
*
|
|
277
268
|
* @param {GetSessionCallback} callback The callback function.
|
|
278
269
|
*/
|
|
279
270
|
getSession(callback) {
|
|
280
|
-
|
|
271
|
+
const sessionFactory = this.database?.sessionFactory_;
|
|
272
|
+
if (!sessionFactory) {
|
|
273
|
+
process.nextTick(callback, new Error('Session pool is not associated with a database'), null);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
sessionFactory.getSession((err, session) => {
|
|
277
|
+
if (err || !session) {
|
|
278
|
+
callback(err ?? new Error('No session found'), null);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
callback(null, session, session.transaction(this.database.queryOptions_));
|
|
282
|
+
});
|
|
281
283
|
}
|
|
282
284
|
/**
|
|
283
285
|
* Opens the pool, filling it to the configured number of read and write
|
|
@@ -287,496 +289,21 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
287
289
|
* @return {Promise}
|
|
288
290
|
*/
|
|
289
291
|
open() {
|
|
290
|
-
this._onClose = new Promise(resolve => this.once('close', resolve));
|
|
291
|
-
this._startHouseKeeping();
|
|
292
292
|
this.isOpen = true;
|
|
293
293
|
this.emit('open');
|
|
294
|
-
this._fill().catch(err => {
|
|
295
|
-
// Ignore `Database not found` error. This allows a user to call instance.database('db-name')
|
|
296
|
-
// for a database that does not yet exist with SessionPoolOptions.min > 0.
|
|
297
|
-
if ((0, helper_1.isDatabaseNotFoundError)(err) ||
|
|
298
|
-
(0, helper_1.isInstanceNotFoundError)(err) ||
|
|
299
|
-
(0, helper_1.isCreateSessionPermissionError)(err) ||
|
|
300
|
-
(0, helper_1.isDefaultCredentialsNotSetError)(err) ||
|
|
301
|
-
(0, helper_1.isProjectIdNotSetInEnvironmentError)(err)) {
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
this.emit('error', err);
|
|
305
|
-
});
|
|
306
294
|
}
|
|
307
295
|
/**
|
|
308
296
|
* Releases session back into the pool.
|
|
309
297
|
*
|
|
310
298
|
* @throws {Error} For unknown sessions.
|
|
311
299
|
* @emits SessionPool#available
|
|
312
|
-
* @emits SessionPool#
|
|
313
|
-
* @fires SessionPool#session-available
|
|
300
|
+
* @emits SessionPool#session-available
|
|
314
301
|
* @fires @deprecated SessionPool#readonly-available
|
|
315
302
|
* @fires @deprecated SessionPool#readwrite-available
|
|
316
303
|
* @param {Session} session The session to release.
|
|
317
304
|
*/
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
throw new ReleaseError(session);
|
|
321
|
-
}
|
|
322
|
-
delete session.txn;
|
|
323
|
-
session.lastUsed = Date.now();
|
|
324
|
-
if (isSessionNotFoundError(session.lastError)) {
|
|
325
|
-
// Remove the session from the pool. It is not necessary to call _destroy,
|
|
326
|
-
// as the session is already removed from the backend.
|
|
327
|
-
this._inventory.borrowed.delete(session);
|
|
328
|
-
this._traces.delete(session.id);
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
session.lastError = undefined;
|
|
332
|
-
// Delete the trace associated with this session to mark the session as checked
|
|
333
|
-
// back into the pool. This will prevent the session to be marked as leaked if
|
|
334
|
-
// the pool is closed while the session is being prepared.
|
|
335
|
-
this._traces.delete(session.id);
|
|
336
|
-
// Release it into the pool as a session if there are more waiters than
|
|
337
|
-
// there are sessions available. Releasing it will unblock a waiter as soon
|
|
338
|
-
// as possible.
|
|
339
|
-
this._release(session);
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Attempts to borrow a session from the pool.
|
|
343
|
-
*
|
|
344
|
-
* @private
|
|
345
|
-
*
|
|
346
|
-
* @returns {Promise<Session>}
|
|
347
|
-
*/
|
|
348
|
-
async _acquire() {
|
|
349
|
-
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
350
|
-
if (!this.isOpen) {
|
|
351
|
-
span.addEvent('SessionPool is closed');
|
|
352
|
-
throw new google_gax_1.GoogleError("Database is closed." /* errors.Closed */);
|
|
353
|
-
}
|
|
354
|
-
// Get the stacktrace of the caller before we call any async methods, as calling an async method will break the stacktrace.
|
|
355
|
-
const frames = trace.get();
|
|
356
|
-
const startTime = Date.now();
|
|
357
|
-
const timeout = this.options.acquireTimeout;
|
|
358
|
-
// wrapping this logic in a function to call recursively if the session
|
|
359
|
-
// we end up with is already dead
|
|
360
|
-
const getSession = async () => {
|
|
361
|
-
span.addEvent('Acquiring session');
|
|
362
|
-
const elapsed = Date.now() - startTime;
|
|
363
|
-
if (elapsed >= timeout) {
|
|
364
|
-
span.addEvent('Could not acquire session due to an exceeded timeout');
|
|
365
|
-
throw new google_gax_1.GoogleError("Timeout occurred while acquiring session." /* errors.Timeout */);
|
|
366
|
-
}
|
|
367
|
-
const session = await this._getSession(startTime);
|
|
368
|
-
if (this._isValidSession(session)) {
|
|
369
|
-
span.addEvent('Acquired session', {
|
|
370
|
-
'time.elapsed': Date.now() - startTime,
|
|
371
|
-
'session.id': session.id.toString(),
|
|
372
|
-
});
|
|
373
|
-
return session;
|
|
374
|
-
}
|
|
375
|
-
span.addEvent('Could not acquire session because it was invalid. Retrying', {
|
|
376
|
-
'session.id': session.id.toString(),
|
|
377
|
-
});
|
|
378
|
-
this._inventory.borrowed.delete(session);
|
|
379
|
-
return getSession();
|
|
380
|
-
};
|
|
381
|
-
const session = await this._acquires.add(getSession);
|
|
382
|
-
this._prepareTransaction(session);
|
|
383
|
-
this._traces.set(session.id, frames);
|
|
384
|
-
return session;
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Moves a session into the borrowed group.
|
|
388
|
-
*
|
|
389
|
-
* @private
|
|
390
|
-
*
|
|
391
|
-
* @param {Session} session The session object.
|
|
392
|
-
*/
|
|
393
|
-
_borrow(session) {
|
|
394
|
-
const index = this._inventory.sessions.indexOf(session);
|
|
395
|
-
this._inventory.borrowed.add(session);
|
|
396
|
-
this._inventory.sessions.splice(index, 1);
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Borrows the first session from the inventory.
|
|
400
|
-
*
|
|
401
|
-
* @private
|
|
402
|
-
*
|
|
403
|
-
* @return {Session}
|
|
404
|
-
*/
|
|
405
|
-
_borrowFrom() {
|
|
406
|
-
const session = this._inventory.sessions.pop();
|
|
407
|
-
this._inventory.borrowed.add(session);
|
|
408
|
-
return session;
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Grabs the next available session.
|
|
412
|
-
*
|
|
413
|
-
* @private
|
|
414
|
-
*
|
|
415
|
-
* @returns {Promise<Session>}
|
|
416
|
-
*/
|
|
417
|
-
_borrowNextAvailableSession() {
|
|
418
|
-
return this._borrowFrom();
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Attempts to create a single session.
|
|
422
|
-
*
|
|
423
|
-
* @private
|
|
424
|
-
*
|
|
425
|
-
* @returns {Promise}
|
|
426
|
-
*/
|
|
427
|
-
_createSession() {
|
|
428
|
-
return this._createSessions(1);
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Batch creates sessions.
|
|
432
|
-
*
|
|
433
|
-
* @private
|
|
434
|
-
*
|
|
435
|
-
* @param {number} [amount] Config specifying how many sessions to create.
|
|
436
|
-
* @returns {Promise}
|
|
437
|
-
* @emits SessionPool#createError
|
|
438
|
-
*/
|
|
439
|
-
async _createSessions(amount) {
|
|
440
|
-
const labels = this.options.labels;
|
|
441
|
-
const databaseRole = this.options.databaseRole;
|
|
442
|
-
if (amount <= 0) {
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
this._pending += amount;
|
|
446
|
-
let nReturned = 0;
|
|
447
|
-
const nRequested = amount;
|
|
448
|
-
// TODO: Inlining this code for now and later on shall go
|
|
449
|
-
// extract _traceConfig to the constructor when we have plenty of time.
|
|
450
|
-
const traceConfig = {
|
|
451
|
-
opts: this._observabilityOptions,
|
|
452
|
-
dbName: this.database.formattedName_,
|
|
453
|
-
};
|
|
454
|
-
return api_1.context.with(api_1.ROOT_CONTEXT, () => {
|
|
455
|
-
return (0, instrument_1.startTrace)('SessionPool.createSessions', traceConfig, async (span) => {
|
|
456
|
-
span.addEvent(`Requesting ${amount} sessions`);
|
|
457
|
-
// while we can request as many sessions be created as we want, the backend
|
|
458
|
-
// will return at most 100 at a time, hence the need for a while loop.
|
|
459
|
-
while (amount > 0) {
|
|
460
|
-
let sessions = null;
|
|
461
|
-
span.addEvent(`Creating ${amount} sessions`);
|
|
462
|
-
try {
|
|
463
|
-
[sessions] = await this.database.batchCreateSessions({
|
|
464
|
-
count: amount,
|
|
465
|
-
labels: labels,
|
|
466
|
-
databaseRole: databaseRole,
|
|
467
|
-
});
|
|
468
|
-
amount -= sessions.length;
|
|
469
|
-
nReturned += sessions.length;
|
|
470
|
-
}
|
|
471
|
-
catch (e) {
|
|
472
|
-
this._pending -= amount;
|
|
473
|
-
this.emit('createError', e);
|
|
474
|
-
span.addEvent(`Requested for ${nRequested} sessions returned ${nReturned}`);
|
|
475
|
-
(0, instrument_1.setSpanErrorAndException)(span, e);
|
|
476
|
-
span.end();
|
|
477
|
-
throw e;
|
|
478
|
-
}
|
|
479
|
-
sessions.forEach((session) => {
|
|
480
|
-
setImmediate(() => {
|
|
481
|
-
this._inventory.borrowed.add(session);
|
|
482
|
-
this._pending -= 1;
|
|
483
|
-
this.release(session);
|
|
484
|
-
});
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
span.addEvent(`Requested for ${nRequested} sessions returned ${nReturned}`);
|
|
488
|
-
span.end();
|
|
489
|
-
});
|
|
490
|
-
});
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Attempts to delete a session, optionally creating a new one of the same
|
|
494
|
-
* type if the pool is still open and we're under the configured min value.
|
|
495
|
-
*
|
|
496
|
-
* @private
|
|
497
|
-
*
|
|
498
|
-
* @fires SessionPool#error
|
|
499
|
-
* @param {Session} session The session to delete.
|
|
500
|
-
* @returns {Promise}
|
|
501
|
-
*/
|
|
502
|
-
async _destroy(session) {
|
|
503
|
-
try {
|
|
504
|
-
await this._requests.add(() => session.delete());
|
|
505
|
-
}
|
|
506
|
-
catch (e) {
|
|
507
|
-
this.emit('error', e);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
/**
|
|
511
|
-
* Deletes idle sessions that exceed the maxIdle configuration.
|
|
512
|
-
*
|
|
513
|
-
* @private
|
|
514
|
-
*/
|
|
515
|
-
_evictIdleSessions() {
|
|
516
|
-
const { maxIdle, min } = this.options;
|
|
517
|
-
const size = this.size;
|
|
518
|
-
const idle = this._getIdleSessions();
|
|
519
|
-
let count = idle.length;
|
|
520
|
-
let evicted = 0;
|
|
521
|
-
while (count-- > maxIdle && size - evicted++ > min) {
|
|
522
|
-
const session = idle.pop();
|
|
523
|
-
if (!session) {
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
const index = this._inventory.sessions.indexOf(session);
|
|
527
|
-
this._inventory.sessions.splice(index, 1);
|
|
528
|
-
void this._destroy(session);
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* Fills the pool with the minimum number of sessions.
|
|
533
|
-
*
|
|
534
|
-
* @return {Promise}
|
|
535
|
-
*/
|
|
536
|
-
async _fill() {
|
|
537
|
-
const needed = this.options.min - this.size;
|
|
538
|
-
if (needed <= 0) {
|
|
539
|
-
return;
|
|
540
|
-
}
|
|
541
|
-
await this._createSessions(needed);
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* Retrieves a list of all the idle sessions.
|
|
545
|
-
*
|
|
546
|
-
* @private
|
|
547
|
-
*
|
|
548
|
-
* @returns {Session[]}
|
|
549
|
-
*/
|
|
550
|
-
_getIdleSessions() {
|
|
551
|
-
const idlesAfter = this.options.idlesAfter * 60000;
|
|
552
|
-
const sessions = this._inventory.sessions;
|
|
553
|
-
return sessions.filter(session => {
|
|
554
|
-
return Date.now() - session.lastUsed >= idlesAfter;
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
/**
|
|
558
|
-
* Returns stack traces for sessions that have not been released.
|
|
559
|
-
*
|
|
560
|
-
* @return {string[]}
|
|
561
|
-
*/
|
|
562
|
-
_getLeaks() {
|
|
563
|
-
return [...this._traces.values()].map(SessionPool.formatTrace);
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Returns true if the pool has a usable session.
|
|
567
|
-
* @private
|
|
568
|
-
*/
|
|
569
|
-
_hasSessionUsableFor() {
|
|
570
|
-
return this._inventory.sessions.length > 0;
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Attempts to get a session.
|
|
574
|
-
*
|
|
575
|
-
* @private
|
|
576
|
-
*
|
|
577
|
-
* @param {number} startTime Timestamp to use when determining timeouts.
|
|
578
|
-
* @returns {Promise<Session>}
|
|
579
|
-
*/
|
|
580
|
-
async _getSession(startTime) {
|
|
581
|
-
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
582
|
-
if (this._hasSessionUsableFor()) {
|
|
583
|
-
span.addEvent('Cache hit: has usable session');
|
|
584
|
-
return this._borrowNextAvailableSession();
|
|
585
|
-
}
|
|
586
|
-
if (this.isFull && this.options.fail) {
|
|
587
|
-
span.addEvent('Session pool is full and failFast=true');
|
|
588
|
-
throw new SessionPoolExhaustedError(this._getLeaks());
|
|
589
|
-
}
|
|
590
|
-
let removeOnceCloseListener;
|
|
591
|
-
let removeListener;
|
|
592
|
-
// Wait for a session to become available.
|
|
593
|
-
span.addEvent('Waiting for a session to become available');
|
|
594
|
-
const availableEvent = 'session-available';
|
|
595
|
-
const promises = [
|
|
596
|
-
new Promise((_, reject) => {
|
|
597
|
-
const onceCloseListener = () => reject(new google_gax_1.GoogleError("Database is closed." /* errors.Closed */));
|
|
598
|
-
this.once('close', onceCloseListener);
|
|
599
|
-
removeOnceCloseListener = this.removeListener.bind(this, 'close', onceCloseListener);
|
|
600
|
-
}),
|
|
601
|
-
new Promise(resolve => {
|
|
602
|
-
this.once(availableEvent, resolve);
|
|
603
|
-
removeListener = this.removeListener.bind(this, availableEvent, resolve);
|
|
604
|
-
}),
|
|
605
|
-
];
|
|
606
|
-
const timeout = this.options.acquireTimeout;
|
|
607
|
-
let removeTimeoutListener = () => { };
|
|
608
|
-
if (!(0, helper_1.isInfinite)(timeout)) {
|
|
609
|
-
const elapsed = Date.now() - startTime;
|
|
610
|
-
const remaining = timeout - elapsed;
|
|
611
|
-
promises.push(new Promise((_, reject) => {
|
|
612
|
-
const error = new Error("Timeout occurred while acquiring session." /* errors.Timeout */);
|
|
613
|
-
const timeoutFunction = setTimeout(reject.bind(null, error), remaining);
|
|
614
|
-
removeTimeoutListener = () => clearTimeout(timeoutFunction);
|
|
615
|
-
}));
|
|
616
|
-
}
|
|
617
|
-
// Only create a new session if there are more waiters than sessions already
|
|
618
|
-
// being created. The current requester will be waiter number _numWaiters+1.
|
|
619
|
-
if (!this.isFull && this.totalPending <= this.totalWaiters) {
|
|
620
|
-
let amount = this.options.incStep
|
|
621
|
-
? this.options.incStep
|
|
622
|
-
: DEFAULTS.incStep;
|
|
623
|
-
// Create additional sessions if the configured minimum has not been reached.
|
|
624
|
-
const min = this.options.min ? this.options.min : 0;
|
|
625
|
-
if (this.size + this.totalPending + amount < min) {
|
|
626
|
-
amount = min - this.size - this.totalPending;
|
|
627
|
-
}
|
|
628
|
-
// Make sure we don't create more sessions than the pool should have.
|
|
629
|
-
if (amount + this.size > this.options.max) {
|
|
630
|
-
amount = this.options.max - this.size;
|
|
631
|
-
}
|
|
632
|
-
if (amount > 0) {
|
|
633
|
-
this._pending += amount;
|
|
634
|
-
promises.push(new Promise((_, reject) => {
|
|
635
|
-
this._pending -= amount;
|
|
636
|
-
this._createSessions(amount).catch(reject);
|
|
637
|
-
}));
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
let removeErrorListener;
|
|
641
|
-
promises.push(new Promise((_, reject) => {
|
|
642
|
-
this.once('createError', reject);
|
|
643
|
-
removeErrorListener = this.removeListener.bind(this, 'createError', reject);
|
|
644
|
-
}));
|
|
645
|
-
try {
|
|
646
|
-
this._waiters++;
|
|
647
|
-
await Promise.race(promises);
|
|
648
|
-
}
|
|
649
|
-
finally {
|
|
650
|
-
this._waiters--;
|
|
651
|
-
removeOnceCloseListener();
|
|
652
|
-
removeListener();
|
|
653
|
-
removeErrorListener();
|
|
654
|
-
removeTimeoutListener();
|
|
655
|
-
}
|
|
656
|
-
return this._borrowNextAvailableSession();
|
|
657
|
-
}
|
|
658
|
-
/**
|
|
659
|
-
* Checks to see whether or not session is expired.
|
|
660
|
-
*
|
|
661
|
-
* @param {Session} session The session to check.
|
|
662
|
-
* @returns {boolean}
|
|
663
|
-
*/
|
|
664
|
-
_isValidSession(session) {
|
|
665
|
-
// unpinged sessions only stay good for 1 hour
|
|
666
|
-
const MAX_DURATION = 60000 * 60;
|
|
667
|
-
return Date.now() - session.lastUsed < MAX_DURATION;
|
|
668
|
-
}
|
|
669
|
-
/**
|
|
670
|
-
* Pings an individual session.
|
|
671
|
-
*
|
|
672
|
-
* @private
|
|
673
|
-
*
|
|
674
|
-
* @param {Session} session The session to ping.
|
|
675
|
-
* @returns {Promise}
|
|
676
|
-
*/
|
|
677
|
-
async _ping(session) {
|
|
678
|
-
// NOTE: Please do not trace Ping as it gets quite spammy
|
|
679
|
-
// with many root spans polluting the main span.
|
|
680
|
-
// Please see https://github.com/googleapis/google-cloud-go/issues/1691
|
|
681
|
-
this._borrow(session);
|
|
682
|
-
if (!this._isValidSession(session)) {
|
|
683
|
-
this._inventory.borrowed.delete(session);
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
try {
|
|
687
|
-
await session.keepAlive();
|
|
688
|
-
this.release(session);
|
|
689
|
-
}
|
|
690
|
-
catch (e) {
|
|
691
|
-
this._inventory.borrowed.delete(session);
|
|
692
|
-
await this._destroy(session);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
/**
|
|
696
|
-
* Makes a keep alive request to all the idle sessions.
|
|
697
|
-
*
|
|
698
|
-
* @private
|
|
699
|
-
*
|
|
700
|
-
* @returns {Promise}
|
|
701
|
-
*/
|
|
702
|
-
async _pingIdleSessions() {
|
|
703
|
-
const sessions = this._getIdleSessions();
|
|
704
|
-
const pings = sessions.map(session => this._ping(session));
|
|
705
|
-
await Promise.all(pings);
|
|
706
|
-
try {
|
|
707
|
-
await this._fill();
|
|
708
|
-
}
|
|
709
|
-
catch (error) {
|
|
710
|
-
// Ignore `Database not found` error. This allows a user to call instance.database('db-name')
|
|
711
|
-
// for a database that does not yet exist with SessionPoolOptions.min > 0.
|
|
712
|
-
const err = error;
|
|
713
|
-
if ((0, helper_1.isDatabaseNotFoundError)(err) ||
|
|
714
|
-
(0, helper_1.isInstanceNotFoundError)(err) ||
|
|
715
|
-
(0, helper_1.isCreateSessionPermissionError)(err) ||
|
|
716
|
-
(0, helper_1.isDefaultCredentialsNotSetError)(err) ||
|
|
717
|
-
(0, helper_1.isProjectIdNotSetInEnvironmentError)(err)) {
|
|
718
|
-
return;
|
|
719
|
-
}
|
|
720
|
-
this.emit('error', err);
|
|
721
|
-
}
|
|
722
|
-
return;
|
|
723
|
-
}
|
|
724
|
-
/**
|
|
725
|
-
* Creates a transaction for a session.
|
|
726
|
-
*
|
|
727
|
-
* @private
|
|
728
|
-
*
|
|
729
|
-
* @param {Session} session The session object.
|
|
730
|
-
* @param {object} options The transaction options.
|
|
731
|
-
*/
|
|
732
|
-
_prepareTransaction(session) {
|
|
733
|
-
const transaction = session.transaction(session.parent.queryOptions_);
|
|
734
|
-
session.txn = transaction;
|
|
735
|
-
}
|
|
736
|
-
/**
|
|
737
|
-
* Releases a session back into the pool.
|
|
738
|
-
*
|
|
739
|
-
* @private
|
|
740
|
-
*
|
|
741
|
-
* @fires SessionPool#available
|
|
742
|
-
* @fires SessionPool#session-available
|
|
743
|
-
* @fires @deprecated SessionPool#readonly-available
|
|
744
|
-
* @fires @deprecated SessionPool#readwrite-available
|
|
745
|
-
* @param {Session} session The session object.
|
|
746
|
-
*/
|
|
747
|
-
_release(session) {
|
|
748
|
-
this._inventory.sessions.push(session);
|
|
749
|
-
this._inventory.borrowed.delete(session);
|
|
750
|
-
this._traces.delete(session.id);
|
|
751
|
-
this.emit('available');
|
|
752
|
-
this.emit('session-available');
|
|
753
|
-
this.emit('readonly-available');
|
|
754
|
-
this.emit('readwrite-available');
|
|
755
|
-
}
|
|
756
|
-
/**
|
|
757
|
-
* Starts housekeeping (pinging/evicting) of idle sessions.
|
|
758
|
-
*
|
|
759
|
-
* @private
|
|
760
|
-
*/
|
|
761
|
-
_startHouseKeeping() {
|
|
762
|
-
const evictRate = this.options.idlesAfter * 60000;
|
|
763
|
-
this._evictHandle = api_1.context.with(api_1.ROOT_CONTEXT, () => setInterval(() => this._evictIdleSessions(), evictRate));
|
|
764
|
-
this._evictHandle.unref();
|
|
765
|
-
const pingRate = this.options.keepAlive * 60000;
|
|
766
|
-
this._pingHandle = api_1.context.with(api_1.ROOT_CONTEXT, () => setInterval(() => this._pingIdleSessions(), pingRate));
|
|
767
|
-
this._pingHandle.unref();
|
|
768
|
-
}
|
|
769
|
-
/**
|
|
770
|
-
* Stops housekeeping.
|
|
771
|
-
*
|
|
772
|
-
* @private
|
|
773
|
-
*/
|
|
774
|
-
_stopHouseKeeping() {
|
|
775
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
776
|
-
clearInterval(this._pingHandle);
|
|
777
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
778
|
-
clearInterval(this._evictHandle);
|
|
779
|
-
}
|
|
305
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
306
|
+
release(session) { }
|
|
780
307
|
}
|
|
781
308
|
exports.SessionPool = SessionPool;
|
|
782
309
|
//# sourceMappingURL=session-pool.js.map
|