@google-cloud/spanner 6.4.0 → 6.6.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/CHANGELOG.md +26 -0
- package/README.md +4 -0
- package/build/src/backup.d.ts +12 -12
- package/build/src/codec.d.ts +1 -1
- package/build/src/common.d.ts +4 -4
- package/build/src/database.d.ts +210 -30
- package/build/src/database.js +89 -31
- package/build/src/index.d.ts +16 -15
- package/build/src/index.js +33 -27
- package/build/src/instance-config.d.ts +10 -10
- package/build/src/instance.d.ts +30 -30
- package/build/src/partial-result-stream.d.ts +1 -1
- package/build/src/session-pool.d.ts +65 -81
- package/build/src/session-pool.js +129 -252
- package/build/src/session.d.ts +16 -9
- package/build/src/session.js +5 -0
- package/build/src/table.d.ts +20 -20
- package/build/src/transaction-runner.d.ts +1 -1
- package/build/src/transaction-runner.js +4 -1
- package/build/src/transaction.d.ts +34 -11
- package/build/src/transaction.js +102 -13
- package/package.json +4 -4
|
@@ -31,8 +31,8 @@ const DEFAULTS = {
|
|
|
31
31
|
max: 100,
|
|
32
32
|
maxIdle: 1,
|
|
33
33
|
min: 25,
|
|
34
|
-
writes: 0,
|
|
35
34
|
incStep: 25,
|
|
35
|
+
databaseRole: null,
|
|
36
36
|
};
|
|
37
37
|
/**
|
|
38
38
|
* Error to be thrown when attempting to release unknown resources.
|
|
@@ -146,44 +146,6 @@ exports.isProjectIdNotSetInEnvironmentError = isProjectIdNotSetInEnvironmentErro
|
|
|
146
146
|
* @extends {EventEmitter}
|
|
147
147
|
*/
|
|
148
148
|
class SessionPool extends events_1.EventEmitter {
|
|
149
|
-
/**
|
|
150
|
-
* @constructor
|
|
151
|
-
* @param {Database} database The DB instance.
|
|
152
|
-
* @param {SessionPoolOptions} [options] Configuration options.
|
|
153
|
-
*/
|
|
154
|
-
constructor(database, options) {
|
|
155
|
-
super();
|
|
156
|
-
this._pending = 0;
|
|
157
|
-
this._pendingPrepare = 0;
|
|
158
|
-
this._numInProcessPrepare = 0;
|
|
159
|
-
if (options && options.min && options.max && options.min > options.max) {
|
|
160
|
-
throw new TypeError('Min sessions may not be greater than max sessions.');
|
|
161
|
-
}
|
|
162
|
-
this.isOpen = false;
|
|
163
|
-
this.database = database;
|
|
164
|
-
this.options = Object.assign({}, DEFAULTS, options);
|
|
165
|
-
this.options.min = Math.min(this.options.min, this.options.max);
|
|
166
|
-
const { writes } = this.options;
|
|
167
|
-
if (writes < 0 || writes > 1) {
|
|
168
|
-
throw new TypeError('Write percentage should be represented as a float between 0.0 and 1.0.');
|
|
169
|
-
}
|
|
170
|
-
this._inventory = {
|
|
171
|
-
["readonly" /* types.ReadOnly */]: [],
|
|
172
|
-
["readwrite" /* types.ReadWrite */]: [],
|
|
173
|
-
borrowed: new Set(),
|
|
174
|
-
};
|
|
175
|
-
this._waiters = {
|
|
176
|
-
["readonly" /* types.ReadOnly */]: 0,
|
|
177
|
-
["readwrite" /* types.ReadWrite */]: 0,
|
|
178
|
-
};
|
|
179
|
-
this._requests = new p_queue_1.default({
|
|
180
|
-
concurrency: this.options.concurrency,
|
|
181
|
-
});
|
|
182
|
-
this._acquires = new p_queue_1.default({
|
|
183
|
-
concurrency: 1,
|
|
184
|
-
});
|
|
185
|
-
this._traces = new Map();
|
|
186
|
-
}
|
|
187
149
|
/**
|
|
188
150
|
* Formats stack trace objects into Node-like stack trace.
|
|
189
151
|
*
|
|
@@ -205,24 +167,13 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
205
167
|
* @type {number}
|
|
206
168
|
*/
|
|
207
169
|
get available() {
|
|
208
|
-
|
|
209
|
-
const writes = this._inventory["readwrite" /* types.ReadWrite */];
|
|
210
|
-
return reads.length + writes.length;
|
|
170
|
+
return this._inventory.sessions.length;
|
|
211
171
|
}
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
* @type {number}
|
|
172
|
+
/** @deprecated Starting from v6.5.0 the same session can be reused for
|
|
173
|
+
* different types of transactions.
|
|
215
174
|
*/
|
|
216
175
|
get currentWriteFraction() {
|
|
217
|
-
|
|
218
|
-
// There are no sessions in the pool. Define the current write fraction as
|
|
219
|
-
// 0.5. That means that if the user has configured a write fraction >= 0.5
|
|
220
|
-
// the first session to be created will be a write session, while it will
|
|
221
|
-
// otherwise be a read-only session.
|
|
222
|
-
return 0.5;
|
|
223
|
-
}
|
|
224
|
-
const writes = this._inventory["readwrite" /* types.ReadWrite */].length + this.pendingPrepare;
|
|
225
|
-
return writes / (this.available + this.pendingPrepare);
|
|
176
|
+
return 0;
|
|
226
177
|
}
|
|
227
178
|
/**
|
|
228
179
|
* Total number of borrowed sessions.
|
|
@@ -238,14 +189,9 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
238
189
|
get isFull() {
|
|
239
190
|
return this.size >= this.options.max;
|
|
240
191
|
}
|
|
241
|
-
/**
|
|
242
|
-
* Total number of read sessions.
|
|
243
|
-
* @type {number}
|
|
244
|
-
*/
|
|
192
|
+
/** @deprecated Use `size()` instead. */
|
|
245
193
|
get reads() {
|
|
246
|
-
|
|
247
|
-
const borrowed = [...this._inventory.borrowed].filter(session => session.type === "readonly" /* types.ReadOnly */).length;
|
|
248
|
-
return available + borrowed;
|
|
194
|
+
return this.size;
|
|
249
195
|
}
|
|
250
196
|
/**
|
|
251
197
|
* Total size of pool.
|
|
@@ -254,52 +200,67 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
254
200
|
get size() {
|
|
255
201
|
return this.available + this.borrowed;
|
|
256
202
|
}
|
|
257
|
-
/**
|
|
258
|
-
* Total number of write sessions.
|
|
259
|
-
* @type {number}
|
|
260
|
-
*/
|
|
203
|
+
/** @deprecated Use `size()` instead. */
|
|
261
204
|
get writes() {
|
|
262
|
-
|
|
263
|
-
const borrowed = [...this._inventory.borrowed].filter(session => session.type === "readwrite" /* types.ReadWrite */).length;
|
|
264
|
-
return available + borrowed;
|
|
205
|
+
return this.size;
|
|
265
206
|
}
|
|
266
|
-
/**
|
|
267
|
-
* Number of sessions currently being prepared for a read/write transaction
|
|
268
|
-
* before being released into the pool. This number does not include the
|
|
269
|
-
* number of sessions being prepared for a read/write transaction that have
|
|
270
|
-
* already been checked out of the pool.
|
|
271
|
-
* @type {number}
|
|
272
|
-
*/
|
|
207
|
+
/** @deprecated Starting v6.5.0 the pending prepare state is obsolete. */
|
|
273
208
|
get pendingPrepare() {
|
|
274
|
-
return
|
|
209
|
+
return 0;
|
|
275
210
|
}
|
|
276
211
|
/**
|
|
277
212
|
* Number of sessions being created or prepared for a read/write transaction.
|
|
278
213
|
* @type {number}
|
|
279
214
|
*/
|
|
280
215
|
get totalPending() {
|
|
281
|
-
return this._pending
|
|
216
|
+
return this._pending;
|
|
282
217
|
}
|
|
283
|
-
/**
|
|
284
|
-
* Current number of waiters for a read-only session.
|
|
285
|
-
* @type {number}
|
|
286
|
-
*/
|
|
218
|
+
/** @deprecated Use totalWaiters instead. */
|
|
287
219
|
get numReadWaiters() {
|
|
288
|
-
return this.
|
|
220
|
+
return this.totalWaiters;
|
|
289
221
|
}
|
|
290
|
-
/**
|
|
291
|
-
* Current number of waiters for a read/write session.
|
|
292
|
-
* @type {number}
|
|
293
|
-
*/
|
|
222
|
+
/** @deprecated Use totalWaiters instead. */
|
|
294
223
|
get numWriteWaiters() {
|
|
295
|
-
return this.
|
|
224
|
+
return this.totalWaiters;
|
|
296
225
|
}
|
|
297
226
|
/**
|
|
298
227
|
* Sum of read and write waiters.
|
|
299
228
|
* @type {number}
|
|
300
229
|
*/
|
|
301
230
|
get totalWaiters() {
|
|
302
|
-
return this.
|
|
231
|
+
return this._waiters;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* @constructor
|
|
235
|
+
* @param {Database} database The DB instance.
|
|
236
|
+
* @param {SessionPoolOptions} [options] Configuration options.
|
|
237
|
+
*/
|
|
238
|
+
constructor(database, options) {
|
|
239
|
+
super();
|
|
240
|
+
this._pending = 0;
|
|
241
|
+
this._waiters = 0;
|
|
242
|
+
if (options && options.min && options.max && options.min > options.max) {
|
|
243
|
+
throw new TypeError('Min sessions may not be greater than max sessions.');
|
|
244
|
+
}
|
|
245
|
+
this.isOpen = false;
|
|
246
|
+
this.database = database;
|
|
247
|
+
this.options = Object.assign({}, DEFAULTS, options);
|
|
248
|
+
this.options.min = Math.min(this.options.min, this.options.max);
|
|
249
|
+
this.options.databaseRole = this.options.databaseRole
|
|
250
|
+
? this.options.databaseRole
|
|
251
|
+
: database.databaseRole;
|
|
252
|
+
this._inventory = {
|
|
253
|
+
sessions: [],
|
|
254
|
+
borrowed: new Set(),
|
|
255
|
+
};
|
|
256
|
+
this._waiters = 0;
|
|
257
|
+
this._requests = new p_queue_1.default({
|
|
258
|
+
concurrency: this.options.concurrency,
|
|
259
|
+
});
|
|
260
|
+
this._acquires = new p_queue_1.default({
|
|
261
|
+
concurrency: 1,
|
|
262
|
+
});
|
|
263
|
+
this._traces = new Map();
|
|
303
264
|
}
|
|
304
265
|
/**
|
|
305
266
|
* Closes and the pool.
|
|
@@ -309,8 +270,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
309
270
|
*/
|
|
310
271
|
close(callback) {
|
|
311
272
|
const sessions = [
|
|
312
|
-
...this._inventory
|
|
313
|
-
...this._inventory["readwrite" /* types.ReadWrite */],
|
|
273
|
+
...this._inventory.sessions,
|
|
314
274
|
...this._inventory.borrowed,
|
|
315
275
|
];
|
|
316
276
|
this.isOpen = false;
|
|
@@ -320,8 +280,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
320
280
|
this._requests.onIdle().then(() => {
|
|
321
281
|
const leaks = this._getLeaks();
|
|
322
282
|
let error;
|
|
323
|
-
this._inventory
|
|
324
|
-
this._inventory["readwrite" /* types.ReadWrite */] = [];
|
|
283
|
+
this._inventory.sessions = [];
|
|
325
284
|
this._inventory.borrowed.clear();
|
|
326
285
|
if (leaks.length) {
|
|
327
286
|
error = new SessionLeakError(leaks);
|
|
@@ -332,18 +291,28 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
332
291
|
/**
|
|
333
292
|
* Retrieve a read session.
|
|
334
293
|
*
|
|
294
|
+
* @deprecated Use getSession instead.
|
|
335
295
|
* @param {GetReadSessionCallback} callback The callback function.
|
|
336
296
|
*/
|
|
337
297
|
getReadSession(callback) {
|
|
338
|
-
this.
|
|
298
|
+
this.getSession((error, session) => callback(error, session));
|
|
339
299
|
}
|
|
340
300
|
/**
|
|
341
301
|
* Retrieve a read/write session.
|
|
342
302
|
*
|
|
303
|
+
* @deprecated use getSession instead.
|
|
343
304
|
* @param {GetWriteSessionCallback} callback The callback function.
|
|
344
305
|
*/
|
|
345
306
|
getWriteSession(callback) {
|
|
346
|
-
this.
|
|
307
|
+
this.getSession(callback);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Retrieve a session.
|
|
311
|
+
*
|
|
312
|
+
* @param {GetSessionCallback} callback The callback function.
|
|
313
|
+
*/
|
|
314
|
+
getSession(callback) {
|
|
315
|
+
this._acquire().then(session => callback(null, session, session.txn), callback);
|
|
347
316
|
}
|
|
348
317
|
/**
|
|
349
318
|
* Opens the pool, filling it to the configured number of read and write
|
|
@@ -371,14 +340,14 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
371
340
|
});
|
|
372
341
|
}
|
|
373
342
|
/**
|
|
374
|
-
* Releases session back into the pool.
|
|
375
|
-
* will also prepare a new transaction before releasing it.
|
|
343
|
+
* Releases session back into the pool.
|
|
376
344
|
*
|
|
377
345
|
* @throws {Error} For unknown sessions.
|
|
378
346
|
* @emits SessionPool#available
|
|
379
347
|
* @emits SessionPool#error
|
|
380
|
-
* @
|
|
381
|
-
* @
|
|
348
|
+
* @fires SessionPool#session-available
|
|
349
|
+
* @fires @deprecated SessionPool#readonly-available
|
|
350
|
+
* @fires @deprecated SessionPool#readwrite-available
|
|
382
351
|
* @param {Session} session The session to release.
|
|
383
352
|
*/
|
|
384
353
|
release(session) {
|
|
@@ -395,45 +364,23 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
395
364
|
return;
|
|
396
365
|
}
|
|
397
366
|
session.lastError = undefined;
|
|
398
|
-
// Release it into the pool as a read-only session in the following cases:
|
|
399
|
-
// 1. There are more waiters than there are sessions available. Releasing it
|
|
400
|
-
// into the pool will ensure that a waiter will be unblocked as soon as
|
|
401
|
-
// possible.
|
|
402
|
-
// 2. The user has not set a write fraction, but this session has been used
|
|
403
|
-
// as a read/write session. This is an indication that the application
|
|
404
|
-
// needs read/write sessions, and the pool should try to keep that number
|
|
405
|
-
// of read/write sessions dynamically.
|
|
406
|
-
// 3. The user has set a write fraction and that fraction has been reached.
|
|
407
|
-
const shouldBeWrite = (session.type === "readwrite" /* types.ReadWrite */ && this.options.writes === 0) ||
|
|
408
|
-
(this.options.writes > 0 &&
|
|
409
|
-
this.currentWriteFraction < this.options.writes);
|
|
410
|
-
if (this.totalWaiters > this.available || !shouldBeWrite) {
|
|
411
|
-
session.type = "readonly" /* types.ReadOnly */;
|
|
412
|
-
this._release(session);
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
367
|
// Delete the trace associated with this session to mark the session as checked
|
|
416
368
|
// back into the pool. This will prevent the session to be marked as leaked if
|
|
417
369
|
// the pool is closed while the session is being prepared.
|
|
418
370
|
this._traces.delete(session.id);
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
.then(() => {
|
|
424
|
-
this._pendingPrepare--;
|
|
425
|
-
this._release(session);
|
|
426
|
-
});
|
|
371
|
+
// Release it into the pool as a session if there are more waiters than
|
|
372
|
+
// there are sessions available. Releasing it will unblock a waiter as soon
|
|
373
|
+
// as possible.
|
|
374
|
+
this._release(session);
|
|
427
375
|
}
|
|
428
376
|
/**
|
|
429
377
|
* Attempts to borrow a session from the pool.
|
|
430
378
|
*
|
|
431
379
|
* @private
|
|
432
380
|
*
|
|
433
|
-
* @param {string} type The desired type to borrow.
|
|
434
381
|
* @returns {Promise<Session>}
|
|
435
382
|
*/
|
|
436
|
-
async _acquire(
|
|
383
|
+
async _acquire() {
|
|
437
384
|
if (!this.isOpen) {
|
|
438
385
|
throw new google_gax_1.GoogleError("Database is closed." /* errors.Closed */);
|
|
439
386
|
}
|
|
@@ -448,7 +395,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
448
395
|
if (elapsed >= timeout) {
|
|
449
396
|
throw new google_gax_1.GoogleError("Timeout occurred while acquiring session." /* errors.Timeout */);
|
|
450
397
|
}
|
|
451
|
-
const session = await this._getSession(
|
|
398
|
+
const session = await this._getSession(startTime);
|
|
452
399
|
if (this._isValidSession(session)) {
|
|
453
400
|
return session;
|
|
454
401
|
}
|
|
@@ -456,26 +403,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
456
403
|
return getSession();
|
|
457
404
|
};
|
|
458
405
|
const session = await this._acquires.add(getSession);
|
|
459
|
-
|
|
460
|
-
this._numInProcessPrepare++;
|
|
461
|
-
try {
|
|
462
|
-
await this._prepareTransaction(session);
|
|
463
|
-
}
|
|
464
|
-
catch (e) {
|
|
465
|
-
if (isSessionNotFoundError(e)) {
|
|
466
|
-
this._inventory.borrowed.delete(session);
|
|
467
|
-
}
|
|
468
|
-
else {
|
|
469
|
-
this._release(session);
|
|
470
|
-
}
|
|
471
|
-
throw e;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
// Mark the session as the type that was requested. This ensures that the
|
|
475
|
-
// fraction of read/write sessions in the pool is kept aligned with the
|
|
476
|
-
// actual need if the user has not specified a write fraction in the session
|
|
477
|
-
// pool options.
|
|
478
|
-
session.type = type;
|
|
406
|
+
this._prepareTransaction(session);
|
|
479
407
|
this._traces.set(session.id, frames);
|
|
480
408
|
return session;
|
|
481
409
|
}
|
|
@@ -487,22 +415,19 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
487
415
|
* @param {Session} session The session object.
|
|
488
416
|
*/
|
|
489
417
|
_borrow(session) {
|
|
490
|
-
const
|
|
491
|
-
const index = this._inventory[type].indexOf(session);
|
|
418
|
+
const index = this._inventory.sessions.indexOf(session);
|
|
492
419
|
this._inventory.borrowed.add(session);
|
|
493
|
-
this._inventory
|
|
420
|
+
this._inventory.sessions.splice(index, 1);
|
|
494
421
|
}
|
|
495
422
|
/**
|
|
496
|
-
* Borrows the first session from
|
|
497
|
-
* actually contains a session of the desired type.
|
|
423
|
+
* Borrows the first session from the inventory.
|
|
498
424
|
*
|
|
499
425
|
* @private
|
|
500
426
|
*
|
|
501
|
-
* @param {string} type The desired session type.
|
|
502
427
|
* @return {Session}
|
|
503
428
|
*/
|
|
504
|
-
_borrowFrom(
|
|
505
|
-
const session = this._inventory
|
|
429
|
+
_borrowFrom() {
|
|
430
|
+
const session = this._inventory.sessions.pop();
|
|
506
431
|
this._inventory.borrowed.add(session);
|
|
507
432
|
return session;
|
|
508
433
|
}
|
|
@@ -511,67 +436,55 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
511
436
|
*
|
|
512
437
|
* @private
|
|
513
438
|
*
|
|
514
|
-
* @param {string} type The desired session type.
|
|
515
439
|
* @returns {Promise<Session>}
|
|
516
440
|
*/
|
|
517
|
-
_borrowNextAvailableSession(
|
|
518
|
-
|
|
519
|
-
if (type === "readonly" /* types.ReadOnly */ && hasReads) {
|
|
520
|
-
return this._borrowFrom("readonly" /* types.ReadOnly */);
|
|
521
|
-
}
|
|
522
|
-
const hasWrites = !!this._inventory["readwrite" /* types.ReadWrite */].length;
|
|
523
|
-
if (hasWrites) {
|
|
524
|
-
return this._borrowFrom("readwrite" /* types.ReadWrite */);
|
|
525
|
-
}
|
|
526
|
-
return this._borrowFrom("readonly" /* types.ReadOnly */);
|
|
441
|
+
_borrowNextAvailableSession() {
|
|
442
|
+
return this._borrowFrom();
|
|
527
443
|
}
|
|
528
444
|
/**
|
|
529
|
-
* Attempts to create a single session
|
|
445
|
+
* Attempts to create a single session.
|
|
530
446
|
*
|
|
531
447
|
* @private
|
|
532
448
|
*
|
|
533
|
-
* @param {string} type The desired type to create.
|
|
534
449
|
* @returns {Promise}
|
|
535
450
|
*/
|
|
536
|
-
_createSession(
|
|
537
|
-
|
|
538
|
-
const options = { [kind]: 1 };
|
|
539
|
-
return this._createSessions(options);
|
|
451
|
+
_createSession() {
|
|
452
|
+
return this._createSessions(1);
|
|
540
453
|
}
|
|
541
454
|
/**
|
|
542
|
-
* Batch creates sessions
|
|
455
|
+
* Batch creates sessions.
|
|
543
456
|
*
|
|
544
457
|
* @private
|
|
545
458
|
*
|
|
546
|
-
* @param {
|
|
459
|
+
* @param {number} [amount] Config specifying how many sessions to create.
|
|
547
460
|
* @returns {Promise}
|
|
548
461
|
* @emits SessionPool#createError
|
|
549
462
|
*/
|
|
550
|
-
async _createSessions(
|
|
463
|
+
async _createSessions(amount) {
|
|
551
464
|
const labels = this.options.labels;
|
|
552
|
-
|
|
553
|
-
if (
|
|
465
|
+
const databaseRole = this.options.databaseRole;
|
|
466
|
+
if (amount <= 0) {
|
|
554
467
|
return;
|
|
555
468
|
}
|
|
556
|
-
this._pending +=
|
|
469
|
+
this._pending += amount;
|
|
557
470
|
// while we can request as many sessions be created as we want, the backend
|
|
558
471
|
// will return at most 100 at a time, hence the need for a while loop.
|
|
559
|
-
while (
|
|
472
|
+
while (amount > 0) {
|
|
560
473
|
let sessions = null;
|
|
561
474
|
try {
|
|
562
475
|
[sessions] = await this.database.batchCreateSessions({
|
|
563
|
-
count:
|
|
564
|
-
labels,
|
|
476
|
+
count: amount,
|
|
477
|
+
labels: labels,
|
|
478
|
+
databaseRole: databaseRole,
|
|
565
479
|
});
|
|
566
|
-
|
|
480
|
+
amount -= sessions.length;
|
|
567
481
|
}
|
|
568
482
|
catch (e) {
|
|
569
|
-
this._pending -=
|
|
483
|
+
this._pending -= amount;
|
|
570
484
|
this.emit('createError', e);
|
|
571
485
|
throw e;
|
|
572
486
|
}
|
|
573
487
|
sessions.forEach((session) => {
|
|
574
|
-
session.type = writes-- > 0 ? "readwrite" /* types.ReadWrite */ : "readonly" /* types.ReadOnly */;
|
|
575
488
|
setImmediate(() => {
|
|
576
489
|
this._inventory.borrowed.add(session);
|
|
577
490
|
this._pending -= 1;
|
|
@@ -614,9 +527,8 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
614
527
|
if (!session) {
|
|
615
528
|
continue;
|
|
616
529
|
}
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
this._inventory[type].splice(index, 1);
|
|
530
|
+
const index = this._inventory.sessions.indexOf(session);
|
|
531
|
+
this._inventory.sessions.splice(index, 1);
|
|
620
532
|
this._destroy(session);
|
|
621
533
|
}
|
|
622
534
|
}
|
|
@@ -631,7 +543,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
631
543
|
return;
|
|
632
544
|
}
|
|
633
545
|
try {
|
|
634
|
-
await this._createSessions(
|
|
546
|
+
await this._createSessions(needed);
|
|
635
547
|
}
|
|
636
548
|
catch (e) {
|
|
637
549
|
this.emit('error', e);
|
|
@@ -646,10 +558,7 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
646
558
|
*/
|
|
647
559
|
_getIdleSessions() {
|
|
648
560
|
const idlesAfter = this.options.idlesAfter * 60000;
|
|
649
|
-
const sessions =
|
|
650
|
-
...this._inventory["readonly" /* types.ReadOnly */],
|
|
651
|
-
...this._inventory["readwrite" /* types.ReadWrite */],
|
|
652
|
-
];
|
|
561
|
+
const sessions = this._inventory.sessions;
|
|
653
562
|
return sessions.filter(session => {
|
|
654
563
|
return Date.now() - session.lastUsed >= idlesAfter;
|
|
655
564
|
});
|
|
@@ -663,48 +572,31 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
663
572
|
return [...this._traces.values()].map(SessionPool.formatTrace);
|
|
664
573
|
}
|
|
665
574
|
/**
|
|
666
|
-
* Returns true if the pool has a
|
|
667
|
-
* type, i.e. if a read-only session is requested, it returns true if the
|
|
668
|
-
* pool has a read-only or a read/write session. If a read/write session is
|
|
669
|
-
* requested, the method only returns true if the pool has a read/write
|
|
670
|
-
* session available.
|
|
671
|
-
* @param type The type of session.
|
|
575
|
+
* Returns true if the pool has a usable session.
|
|
672
576
|
* @private
|
|
673
577
|
*/
|
|
674
|
-
_hasSessionUsableFor(
|
|
675
|
-
return
|
|
676
|
-
this._inventory["readwrite" /* types.ReadWrite */].length > 0);
|
|
578
|
+
_hasSessionUsableFor() {
|
|
579
|
+
return this._inventory.sessions.length > 0;
|
|
677
580
|
}
|
|
678
581
|
/**
|
|
679
|
-
* Attempts to get a session
|
|
680
|
-
* may try to use a different type.
|
|
582
|
+
* Attempts to get a session.
|
|
681
583
|
*
|
|
682
584
|
* @private
|
|
683
585
|
*
|
|
684
|
-
* @param {string} type The desired session type.
|
|
685
586
|
* @param {number} startTime Timestamp to use when determining timeouts.
|
|
686
587
|
* @returns {Promise<Session>}
|
|
687
588
|
*/
|
|
688
|
-
async _getSession(
|
|
689
|
-
if (this._hasSessionUsableFor(
|
|
690
|
-
return this._borrowNextAvailableSession(
|
|
691
|
-
}
|
|
692
|
-
// If a read/write session is requested and the pool has a read-only session
|
|
693
|
-
// available, we should return that session unless there is a session
|
|
694
|
-
// currently being prepared for read/write that is not already claimed by
|
|
695
|
-
// another requester.
|
|
696
|
-
if (type === "readwrite" /* types.ReadWrite */ &&
|
|
697
|
-
this._hasSessionUsableFor("readonly" /* types.ReadOnly */) &&
|
|
698
|
-
this.numWriteWaiters >= this.pendingPrepare) {
|
|
699
|
-
return this._borrowNextAvailableSession(type);
|
|
589
|
+
async _getSession(startTime) {
|
|
590
|
+
if (this._hasSessionUsableFor()) {
|
|
591
|
+
return this._borrowNextAvailableSession();
|
|
700
592
|
}
|
|
701
593
|
if (this.isFull && this.options.fail) {
|
|
702
594
|
throw new SessionPoolExhaustedError(this._getLeaks());
|
|
703
595
|
}
|
|
704
596
|
let removeOnceCloseListener;
|
|
705
597
|
let removeListener;
|
|
706
|
-
// Wait for
|
|
707
|
-
const availableEvent =
|
|
598
|
+
// Wait for a session to become available.
|
|
599
|
+
const availableEvent = 'session-available';
|
|
708
600
|
const promises = [
|
|
709
601
|
new Promise((_, reject) => {
|
|
710
602
|
const onceCloseListener = () => reject(new google_gax_1.GoogleError("Database is closed." /* errors.Closed */));
|
|
@@ -730,23 +622,23 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
730
622
|
// Only create a new session if there are more waiters than sessions already
|
|
731
623
|
// being created. The current requester will be waiter number _numWaiters+1.
|
|
732
624
|
if (!this.isFull && this.totalPending <= this.totalWaiters) {
|
|
733
|
-
let
|
|
625
|
+
let amount = this.options.incStep
|
|
734
626
|
? this.options.incStep
|
|
735
627
|
: DEFAULTS.incStep;
|
|
736
628
|
// Create additional sessions if the configured minimum has not been reached.
|
|
737
629
|
const min = this.options.min ? this.options.min : 0;
|
|
738
|
-
if (this.size + this.totalPending +
|
|
739
|
-
|
|
630
|
+
if (this.size + this.totalPending + amount < min) {
|
|
631
|
+
amount = min - this.size - this.totalPending;
|
|
740
632
|
}
|
|
741
633
|
// Make sure we don't create more sessions than the pool should have.
|
|
742
|
-
if (
|
|
743
|
-
|
|
634
|
+
if (amount + this.size > this.options.max) {
|
|
635
|
+
amount = this.options.max - this.size;
|
|
744
636
|
}
|
|
745
|
-
if (
|
|
746
|
-
this._pending +=
|
|
637
|
+
if (amount > 0) {
|
|
638
|
+
this._pending += amount;
|
|
747
639
|
promises.push(new Promise((_, reject) => {
|
|
748
|
-
this._pending -=
|
|
749
|
-
this._createSessions(
|
|
640
|
+
this._pending -= amount;
|
|
641
|
+
this._createSessions(amount).catch(reject);
|
|
750
642
|
}));
|
|
751
643
|
}
|
|
752
644
|
}
|
|
@@ -756,17 +648,17 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
756
648
|
removeErrorListener = this.removeListener.bind(this, 'createError', reject);
|
|
757
649
|
}));
|
|
758
650
|
try {
|
|
759
|
-
this._waiters
|
|
651
|
+
this._waiters++;
|
|
760
652
|
await Promise.race(promises);
|
|
761
653
|
}
|
|
762
654
|
finally {
|
|
763
|
-
this._waiters
|
|
655
|
+
this._waiters--;
|
|
764
656
|
removeOnceCloseListener();
|
|
765
657
|
removeListener();
|
|
766
658
|
removeErrorListener();
|
|
767
659
|
removeTimeoutListener();
|
|
768
660
|
}
|
|
769
|
-
return this._borrowNextAvailableSession(
|
|
661
|
+
return this._borrowNextAvailableSession();
|
|
770
662
|
}
|
|
771
663
|
/**
|
|
772
664
|
* Checks to see whether or not session is expired.
|
|
@@ -822,11 +714,9 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
822
714
|
*
|
|
823
715
|
* @param {Session} session The session object.
|
|
824
716
|
* @param {object} options The transaction options.
|
|
825
|
-
* @returns {Promise}
|
|
826
717
|
*/
|
|
827
|
-
|
|
718
|
+
_prepareTransaction(session) {
|
|
828
719
|
const transaction = session.transaction(session.parent.queryOptions_);
|
|
829
|
-
await transaction.begin();
|
|
830
720
|
session.txn = transaction;
|
|
831
721
|
}
|
|
832
722
|
/**
|
|
@@ -835,32 +725,19 @@ class SessionPool extends events_1.EventEmitter {
|
|
|
835
725
|
* @private
|
|
836
726
|
*
|
|
837
727
|
* @fires SessionPool#available
|
|
838
|
-
* @fires SessionPool#
|
|
839
|
-
* @fires SessionPool#
|
|
728
|
+
* @fires SessionPool#session-available
|
|
729
|
+
* @fires @deprecated SessionPool#readonly-available
|
|
730
|
+
* @fires @deprecated SessionPool#readwrite-available
|
|
840
731
|
* @param {Session} session The session object.
|
|
841
732
|
*/
|
|
842
733
|
_release(session) {
|
|
843
|
-
|
|
844
|
-
this._inventory[type].push(session);
|
|
734
|
+
this._inventory.sessions.push(session);
|
|
845
735
|
this._inventory.borrowed.delete(session);
|
|
846
736
|
this._traces.delete(session.id);
|
|
847
737
|
this.emit('available');
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
!this.numReadWaiters &&
|
|
852
|
-
this.numWriteWaiters) {
|
|
853
|
-
emitType = "readwrite" /* types.ReadWrite */;
|
|
854
|
-
}
|
|
855
|
-
else if (type === "readwrite" /* types.ReadWrite */ &&
|
|
856
|
-
!this.numWriteWaiters &&
|
|
857
|
-
this.numReadWaiters) {
|
|
858
|
-
emitType = "readonly" /* types.ReadOnly */;
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
emitType = type;
|
|
862
|
-
}
|
|
863
|
-
this.emit(emitType + '-available');
|
|
738
|
+
this.emit('session-available');
|
|
739
|
+
this.emit('readonly-available');
|
|
740
|
+
this.emit('readwrite-available');
|
|
864
741
|
}
|
|
865
742
|
/**
|
|
866
743
|
* Starts housekeeping (pinging/evicting) of idle sessions.
|