@google-cloud/spanner 7.16.0 → 7.17.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 +13 -0
- package/README.md +2 -0
- package/build/protos/google/spanner/v1/spanner.proto +22 -0
- package/build/protos/protos.d.ts +13 -1
- package/build/protos/protos.js +47 -1
- package/build/protos/protos.json +14 -0
- package/build/src/common.d.ts +8 -0
- package/build/src/common.js +19 -1
- package/build/src/database.d.ts +7 -1
- package/build/src/database.js +61 -57
- package/build/src/helper.d.ts +45 -0
- package/build/src/helper.js +71 -0
- package/build/src/index.d.ts +1 -1
- package/build/src/index.js +18 -11
- package/build/src/instance-config.d.ts +1 -0
- package/build/src/instance.d.ts +2 -1
- package/build/src/instance.js +12 -13
- package/build/src/instrument.d.ts +3 -0
- package/build/src/instrument.js +5 -0
- package/build/src/multiplexed-session.d.ts +136 -0
- package/build/src/multiplexed-session.js +197 -0
- package/build/src/session-factory.d.ts +101 -0
- package/build/src/session-factory.js +93 -0
- package/build/src/session-pool.d.ts +1 -39
- package/build/src/session-pool.js +6 -58
- package/build/src/session.d.ts +2 -1
- package/build/src/session.js +5 -6
- package/build/src/transaction.d.ts +1 -1
- package/build/src/transaction.js +8 -9
- package/build/src/v1/spanner_client.d.ts +30 -0
- package/build/src/v1/spanner_client.js +10 -0
- package/package.json +2 -1
package/build/src/instrument.js
CHANGED
|
@@ -18,6 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
exports.SPAN_NAMESPACE_PREFIX = exports.TRACER_VERSION = exports.TRACER_NAME = void 0;
|
|
19
19
|
exports.getTracer = getTracer;
|
|
20
20
|
exports.ensureInitialContextManagerSet = ensureInitialContextManagerSet;
|
|
21
|
+
exports.ensureContextPropagation = ensureContextPropagation;
|
|
21
22
|
exports.startTrace = startTrace;
|
|
22
23
|
exports.setSpanError = setSpanError;
|
|
23
24
|
exports.setSpanErrorAndException = setSpanErrorAndException;
|
|
@@ -46,6 +47,7 @@ function getTracer(tracerProvider) {
|
|
|
46
47
|
const SPAN_NAMESPACE_PREFIX = 'CloudSpanner'; // TODO: discuss & standardize this prefix.
|
|
47
48
|
exports.SPAN_NAMESPACE_PREFIX = SPAN_NAMESPACE_PREFIX;
|
|
48
49
|
const { AsyncHooksContextManager, } = require('@opentelemetry/context-async-hooks');
|
|
50
|
+
const { W3CTraceContextPropagator } = require('@opentelemetry/core');
|
|
49
51
|
/*
|
|
50
52
|
* This function ensures that async/await works correctly by
|
|
51
53
|
* checking if context.active() returns an invalid/unset context
|
|
@@ -64,6 +66,9 @@ function ensureInitialContextManagerSet() {
|
|
|
64
66
|
api_1.context.setGlobalContextManager(contextManager);
|
|
65
67
|
}
|
|
66
68
|
}
|
|
69
|
+
function ensureContextPropagation() {
|
|
70
|
+
api_1.propagation.setGlobalPropagator(new W3CTraceContextPropagator());
|
|
71
|
+
}
|
|
67
72
|
/**
|
|
68
73
|
* startTrace begins an active span in the current active context
|
|
69
74
|
* and passes it back to the set callback function. Each span will
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2024 Google LLC. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { EventEmitter } from 'events';
|
|
17
|
+
import { Database } from './database';
|
|
18
|
+
import { Session } from './session';
|
|
19
|
+
import { GetSessionCallback } from './session-factory';
|
|
20
|
+
import { ObservabilityOptions } from './instrument';
|
|
21
|
+
export declare const MUX_SESSION_AVAILABLE = "mux-session-available";
|
|
22
|
+
export declare const MUX_SESSION_CREATE_ERROR = "mux-session-create-error";
|
|
23
|
+
/**
|
|
24
|
+
* Interface for implementing multiplexed session logic, it should extend the
|
|
25
|
+
* {@link https://nodejs.org/api/events.html|EventEmitter} class
|
|
26
|
+
*
|
|
27
|
+
* @interface MultiplexedSessionInterface
|
|
28
|
+
* @extends external:{@link https://nodejs.org/api/events.html|EventEmitter}
|
|
29
|
+
*
|
|
30
|
+
* @constructs MultiplexedSessionInterface
|
|
31
|
+
* @param {Database} database The database to create a multiplexed session for.
|
|
32
|
+
*/
|
|
33
|
+
export interface MultiplexedSessionInterface extends EventEmitter {
|
|
34
|
+
/**
|
|
35
|
+
* When called creates a multiplexed session.
|
|
36
|
+
*
|
|
37
|
+
* @name MultiplexedSessionInterface#createSession
|
|
38
|
+
*/
|
|
39
|
+
createSession(): void;
|
|
40
|
+
/**
|
|
41
|
+
* When called returns a multiplexed session.
|
|
42
|
+
*
|
|
43
|
+
* @name MultiplexedSessionInterface#getSession
|
|
44
|
+
* @param {GetSessionCallback} callback The callback function.
|
|
45
|
+
*/
|
|
46
|
+
getSession(callback: GetSessionCallback): void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Class used to manage connections to Spanner using multiplexed session.
|
|
50
|
+
*
|
|
51
|
+
* **You don't need to use this class directly, connections will be handled for
|
|
52
|
+
* you.**
|
|
53
|
+
*
|
|
54
|
+
* @class
|
|
55
|
+
* @extends {EventEmitter}
|
|
56
|
+
*/
|
|
57
|
+
export declare class MultiplexedSession extends EventEmitter implements MultiplexedSessionInterface {
|
|
58
|
+
database: Database;
|
|
59
|
+
refreshRate: number;
|
|
60
|
+
isMultiplexedEnabled: boolean;
|
|
61
|
+
_multiplexedSession: Session | null;
|
|
62
|
+
_refreshHandle: NodeJS.Timer;
|
|
63
|
+
_observabilityOptions?: ObservabilityOptions;
|
|
64
|
+
constructor(database: Database);
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new multiplexed session and manages its maintenance.
|
|
67
|
+
*
|
|
68
|
+
* This method initiates the session creation process by calling the `_createSession` method, which returns a Promise.
|
|
69
|
+
*/
|
|
70
|
+
createSession(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new multiplexed session.
|
|
73
|
+
*
|
|
74
|
+
* This method sends a request to the database to create a new session with multiplexing enabled.
|
|
75
|
+
* The response from the database would be an array, the first value of the array will be containing the multiplexed session.
|
|
76
|
+
*
|
|
77
|
+
* @returns {Promise<void>} A Promise that resolves when the session has been successfully created and assigned, an event
|
|
78
|
+
* `mux-session-available` will be emitted to signal that the session is ready.
|
|
79
|
+
*
|
|
80
|
+
* In case of error, an error will get emitted along with the error event.
|
|
81
|
+
*
|
|
82
|
+
* @private
|
|
83
|
+
*/
|
|
84
|
+
_createSession(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Maintains the multiplexed session by periodically refreshing it.
|
|
87
|
+
*
|
|
88
|
+
* This method sets up a periodic refresh interval for maintaining the session. The interval duration
|
|
89
|
+
* is determined by the @param refreshRate option, which is provided in days.
|
|
90
|
+
* The default value is 7 days.
|
|
91
|
+
*
|
|
92
|
+
* @throws {Error} If the multiplexed session creation fails in `_createSession`, the error is caught
|
|
93
|
+
* and ignored. This is because the currently active multiplexed session has a 30-day expiry, providing
|
|
94
|
+
* the maintainer with four opportunities (one every 7 days) to refresh the active session.
|
|
95
|
+
*
|
|
96
|
+
* @returns {void} This method does not return any value.
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
_maintain(): void;
|
|
100
|
+
/**
|
|
101
|
+
* Retrieves a session asynchronously and invokes a callback with the session details.
|
|
102
|
+
*
|
|
103
|
+
* @param {GetSessionCallback} callback - The callback to be invoked once the session is acquired or an error occurs.
|
|
104
|
+
*
|
|
105
|
+
* @returns {void} This method does not return any value, as it operates asynchronously and relies on the callback.
|
|
106
|
+
*
|
|
107
|
+
*/
|
|
108
|
+
getSession(callback: GetSessionCallback): void;
|
|
109
|
+
/**
|
|
110
|
+
* Acquires a session asynchronously, and prepares the transaction for the session.
|
|
111
|
+
*
|
|
112
|
+
* Once a session is successfully acquired, it returns the session object (which may be `null` if unsuccessful).
|
|
113
|
+
*
|
|
114
|
+
* @returns {Promise<Session | null>}
|
|
115
|
+
* A Promise that resolves with the acquired session (or `null` if no session is available after retries).
|
|
116
|
+
*
|
|
117
|
+
*/
|
|
118
|
+
_acquire(): Promise<Session | null>;
|
|
119
|
+
/**
|
|
120
|
+
* Attempts to get a session, waiting for it to become available if necessary.
|
|
121
|
+
*
|
|
122
|
+
* Waits for the `MUX_SESSION_AVAILABLE` event or for the `MUX_SESSION_CREATE_ERROR`
|
|
123
|
+
* to be emitted if the multiplexed session is not yet available. The method listens
|
|
124
|
+
* for these events, and once `mux-session-available` is emitted, it resolves and returns
|
|
125
|
+
* the session.
|
|
126
|
+
*
|
|
127
|
+
* In case of an error, the promise will get rejected and the error will get bubble up to the parent method.
|
|
128
|
+
*
|
|
129
|
+
* @returns {Promise<Session | null>} A promise that resolves with the current multiplexed session if available,
|
|
130
|
+
* or `null` if the session is not available.
|
|
131
|
+
*
|
|
132
|
+
* @private
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
_getSession(): Promise<Session | null>;
|
|
136
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2024 Google LLC. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.MultiplexedSession = exports.MUX_SESSION_CREATE_ERROR = exports.MUX_SESSION_AVAILABLE = void 0;
|
|
19
|
+
const events_1 = require("events");
|
|
20
|
+
const instrument_1 = require("./instrument");
|
|
21
|
+
exports.MUX_SESSION_AVAILABLE = 'mux-session-available';
|
|
22
|
+
exports.MUX_SESSION_CREATE_ERROR = 'mux-session-create-error';
|
|
23
|
+
/**
|
|
24
|
+
* Class used to manage connections to Spanner using multiplexed session.
|
|
25
|
+
*
|
|
26
|
+
* **You don't need to use this class directly, connections will be handled for
|
|
27
|
+
* you.**
|
|
28
|
+
*
|
|
29
|
+
* @class
|
|
30
|
+
* @extends {EventEmitter}
|
|
31
|
+
*/
|
|
32
|
+
class MultiplexedSession extends events_1.EventEmitter {
|
|
33
|
+
constructor(database) {
|
|
34
|
+
super();
|
|
35
|
+
this.database = database;
|
|
36
|
+
// default frequency is 7 days
|
|
37
|
+
this.refreshRate = 7;
|
|
38
|
+
this._multiplexedSession = null;
|
|
39
|
+
this._observabilityOptions = database._observabilityOptions;
|
|
40
|
+
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true'
|
|
41
|
+
? (this.isMultiplexedEnabled = true)
|
|
42
|
+
: (this.isMultiplexedEnabled = false);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new multiplexed session and manages its maintenance.
|
|
46
|
+
*
|
|
47
|
+
* This method initiates the session creation process by calling the `_createSession` method, which returns a Promise.
|
|
48
|
+
*/
|
|
49
|
+
createSession() {
|
|
50
|
+
this._createSession()
|
|
51
|
+
.then(() => {
|
|
52
|
+
this._maintain();
|
|
53
|
+
})
|
|
54
|
+
.catch(err => {
|
|
55
|
+
this.emit('error', err);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new multiplexed session.
|
|
60
|
+
*
|
|
61
|
+
* This method sends a request to the database to create a new session with multiplexing enabled.
|
|
62
|
+
* The response from the database would be an array, the first value of the array will be containing the multiplexed session.
|
|
63
|
+
*
|
|
64
|
+
* @returns {Promise<void>} A Promise that resolves when the session has been successfully created and assigned, an event
|
|
65
|
+
* `mux-session-available` will be emitted to signal that the session is ready.
|
|
66
|
+
*
|
|
67
|
+
* In case of error, an error will get emitted along with the error event.
|
|
68
|
+
*
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
async _createSession() {
|
|
72
|
+
const traceConfig = {
|
|
73
|
+
opts: this._observabilityOptions,
|
|
74
|
+
dbName: this.database.formattedName_,
|
|
75
|
+
};
|
|
76
|
+
return (0, instrument_1.startTrace)('MultiplexedSession.createSession', traceConfig, async (span) => {
|
|
77
|
+
span.addEvent('Requesting a multiplexed session');
|
|
78
|
+
try {
|
|
79
|
+
const [createSessionResponse] = await this.database.createSession({
|
|
80
|
+
multiplexed: true,
|
|
81
|
+
});
|
|
82
|
+
this._multiplexedSession = createSessionResponse;
|
|
83
|
+
span.addEvent(`Created multiplexed session ${this._multiplexedSession.id}`);
|
|
84
|
+
this.emit(exports.MUX_SESSION_AVAILABLE);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
(0, instrument_1.setSpanError)(span, e);
|
|
88
|
+
this.emit(exports.MUX_SESSION_CREATE_ERROR, e);
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
span.end();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Maintains the multiplexed session by periodically refreshing it.
|
|
98
|
+
*
|
|
99
|
+
* This method sets up a periodic refresh interval for maintaining the session. The interval duration
|
|
100
|
+
* is determined by the @param refreshRate option, which is provided in days.
|
|
101
|
+
* The default value is 7 days.
|
|
102
|
+
*
|
|
103
|
+
* @throws {Error} If the multiplexed session creation fails in `_createSession`, the error is caught
|
|
104
|
+
* and ignored. This is because the currently active multiplexed session has a 30-day expiry, providing
|
|
105
|
+
* the maintainer with four opportunities (one every 7 days) to refresh the active session.
|
|
106
|
+
*
|
|
107
|
+
* @returns {void} This method does not return any value.
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
110
|
+
_maintain() {
|
|
111
|
+
const refreshRate = this.refreshRate * 24 * 60 * 60000;
|
|
112
|
+
this._refreshHandle = setInterval(async () => {
|
|
113
|
+
try {
|
|
114
|
+
await this._createSession();
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
}, refreshRate);
|
|
120
|
+
this._refreshHandle.unref();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Retrieves a session asynchronously and invokes a callback with the session details.
|
|
124
|
+
*
|
|
125
|
+
* @param {GetSessionCallback} callback - The callback to be invoked once the session is acquired or an error occurs.
|
|
126
|
+
*
|
|
127
|
+
* @returns {void} This method does not return any value, as it operates asynchronously and relies on the callback.
|
|
128
|
+
*
|
|
129
|
+
*/
|
|
130
|
+
getSession(callback) {
|
|
131
|
+
this._acquire().then(session => callback(null, session, session === null || session === void 0 ? void 0 : session.txn), callback);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Acquires a session asynchronously, and prepares the transaction for the session.
|
|
135
|
+
*
|
|
136
|
+
* Once a session is successfully acquired, it returns the session object (which may be `null` if unsuccessful).
|
|
137
|
+
*
|
|
138
|
+
* @returns {Promise<Session | null>}
|
|
139
|
+
* A Promise that resolves with the acquired session (or `null` if no session is available after retries).
|
|
140
|
+
*
|
|
141
|
+
*/
|
|
142
|
+
async _acquire() {
|
|
143
|
+
const session = await this._getSession();
|
|
144
|
+
// Prepare a transaction for a session
|
|
145
|
+
session.txn = session.transaction(session.parent.queryOptions_);
|
|
146
|
+
return session;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Attempts to get a session, waiting for it to become available if necessary.
|
|
150
|
+
*
|
|
151
|
+
* Waits for the `MUX_SESSION_AVAILABLE` event or for the `MUX_SESSION_CREATE_ERROR`
|
|
152
|
+
* to be emitted if the multiplexed session is not yet available. The method listens
|
|
153
|
+
* for these events, and once `mux-session-available` is emitted, it resolves and returns
|
|
154
|
+
* the session.
|
|
155
|
+
*
|
|
156
|
+
* In case of an error, the promise will get rejected and the error will get bubble up to the parent method.
|
|
157
|
+
*
|
|
158
|
+
* @returns {Promise<Session | null>} A promise that resolves with the current multiplexed session if available,
|
|
159
|
+
* or `null` if the session is not available.
|
|
160
|
+
*
|
|
161
|
+
* @private
|
|
162
|
+
*
|
|
163
|
+
*/
|
|
164
|
+
async _getSession() {
|
|
165
|
+
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
166
|
+
// Check if the multiplexed session is already available
|
|
167
|
+
if (this._multiplexedSession !== null) {
|
|
168
|
+
span.addEvent('Cache hit: has usable multiplexed session');
|
|
169
|
+
return this._multiplexedSession;
|
|
170
|
+
}
|
|
171
|
+
// Define event and promises to wait for the session to become available or for the error
|
|
172
|
+
span.addEvent('Waiting for a multiplexed session to become available');
|
|
173
|
+
let removeAvailableListener;
|
|
174
|
+
let removeErrorListener;
|
|
175
|
+
const promises = [
|
|
176
|
+
new Promise((_, reject) => {
|
|
177
|
+
this.once(exports.MUX_SESSION_CREATE_ERROR, reject);
|
|
178
|
+
removeErrorListener = this.removeListener.bind(this, exports.MUX_SESSION_CREATE_ERROR, reject);
|
|
179
|
+
}),
|
|
180
|
+
new Promise(resolve => {
|
|
181
|
+
this.once(exports.MUX_SESSION_AVAILABLE, resolve);
|
|
182
|
+
removeAvailableListener = this.removeListener.bind(this, exports.MUX_SESSION_AVAILABLE, resolve);
|
|
183
|
+
}),
|
|
184
|
+
];
|
|
185
|
+
try {
|
|
186
|
+
await Promise.race(promises);
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
removeAvailableListener();
|
|
190
|
+
removeErrorListener();
|
|
191
|
+
}
|
|
192
|
+
// Return the multiplexed session after it becomes available
|
|
193
|
+
return this._multiplexedSession;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.MultiplexedSession = MultiplexedSession;
|
|
197
|
+
//# sourceMappingURL=multiplexed-session.js.map
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2024 Google LLC. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Database, Session, Transaction } from '.';
|
|
17
|
+
import { MultiplexedSessionInterface } from './multiplexed-session';
|
|
18
|
+
import { SessionPoolInterface, SessionPoolOptions } from './session-pool';
|
|
19
|
+
import { SessionPoolConstructor } from './database';
|
|
20
|
+
declare const common: any;
|
|
21
|
+
/**
|
|
22
|
+
* @callback GetSessionCallback
|
|
23
|
+
* @param {?Error} error Request error, if any.
|
|
24
|
+
* @param {Session} session The read-write session.
|
|
25
|
+
* @param {Transaction} transaction The transaction object.
|
|
26
|
+
*/
|
|
27
|
+
export interface GetSessionCallback {
|
|
28
|
+
(err: Error | null, session?: Session | null, transaction?: Transaction | null): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Interface for implementing session-factory logic.
|
|
32
|
+
*
|
|
33
|
+
* @interface SessionFactoryInterface
|
|
34
|
+
*/
|
|
35
|
+
export interface SessionFactoryInterface {
|
|
36
|
+
/**
|
|
37
|
+
* When called returns a session.
|
|
38
|
+
*
|
|
39
|
+
* @name SessionFactoryInterface#getSession
|
|
40
|
+
* @param {GetSessionCallback} callback The callback function.
|
|
41
|
+
*/
|
|
42
|
+
getSession(callback: GetSessionCallback): void;
|
|
43
|
+
/**
|
|
44
|
+
* When called returns the pool object.
|
|
45
|
+
*
|
|
46
|
+
* @name SessionFactoryInterface#getPool
|
|
47
|
+
*/
|
|
48
|
+
getPool(): SessionPoolInterface;
|
|
49
|
+
/**
|
|
50
|
+
* To be called when releasing a session.
|
|
51
|
+
*
|
|
52
|
+
* @name SessionFactoryInterface#release
|
|
53
|
+
* @param {Session} session The session to be released.
|
|
54
|
+
*/
|
|
55
|
+
release(session: Session): void;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates a SessionFactory object to manage the creation of
|
|
59
|
+
* session-pool and multiplexed session.
|
|
60
|
+
*
|
|
61
|
+
* @class
|
|
62
|
+
*
|
|
63
|
+
* @param {Database} database Database object.
|
|
64
|
+
* @param {String} name Name of the database.
|
|
65
|
+
* @param {SessionPoolOptions|SessionPoolInterface} options Session pool
|
|
66
|
+
* configuration options or custom pool inteface.
|
|
67
|
+
*/
|
|
68
|
+
export declare class SessionFactory extends common.GrpcServiceObject implements SessionFactoryInterface {
|
|
69
|
+
static [x: string]: any;
|
|
70
|
+
multiplexedSession_: MultiplexedSessionInterface;
|
|
71
|
+
pool_: SessionPoolInterface;
|
|
72
|
+
constructor(database: Database, name: String, poolOptions?: SessionPoolConstructor | SessionPoolOptions);
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves a session, either a regular session or a multiplexed session, based on the environment variable configuration.
|
|
75
|
+
*
|
|
76
|
+
* If the environment variable `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS` is set to `true`, the method will attempt to
|
|
77
|
+
* retrieve a multiplexed session. Otherwise, it will retrieve a session from the regular pool.
|
|
78
|
+
*
|
|
79
|
+
* @param {GetSessionCallback} callback The callback function.
|
|
80
|
+
*/
|
|
81
|
+
getSession(callback: GetSessionCallback): void;
|
|
82
|
+
/**
|
|
83
|
+
* Returns the regular session pool object.
|
|
84
|
+
*
|
|
85
|
+
* @returns {SessionPoolInterface} The session pool used by current instance.
|
|
86
|
+
*/
|
|
87
|
+
getPool(): SessionPoolInterface;
|
|
88
|
+
/**
|
|
89
|
+
* Releases a session back to the session pool.
|
|
90
|
+
*
|
|
91
|
+
* This method returns a session to the pool after it is no longer needed.
|
|
92
|
+
* It is a no-op for multiplexed sessions.
|
|
93
|
+
*
|
|
94
|
+
* @param {Session} session - The session to be released. This should be an instance of `Session` that was
|
|
95
|
+
* previously acquired from the session pool.
|
|
96
|
+
*
|
|
97
|
+
* @throws {Error} If the session is invalid or cannot be released.
|
|
98
|
+
*/
|
|
99
|
+
release(session: Session): void;
|
|
100
|
+
}
|
|
101
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2024 Google LLC. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.SessionFactory = void 0;
|
|
19
|
+
const multiplexed_session_1 = require("./multiplexed-session");
|
|
20
|
+
const session_pool_1 = require("./session-pool");
|
|
21
|
+
const common = require('./common-grpc/service-object');
|
|
22
|
+
/**
|
|
23
|
+
* Creates a SessionFactory object to manage the creation of
|
|
24
|
+
* session-pool and multiplexed session.
|
|
25
|
+
*
|
|
26
|
+
* @class
|
|
27
|
+
*
|
|
28
|
+
* @param {Database} database Database object.
|
|
29
|
+
* @param {String} name Name of the database.
|
|
30
|
+
* @param {SessionPoolOptions|SessionPoolInterface} options Session pool
|
|
31
|
+
* configuration options or custom pool inteface.
|
|
32
|
+
*/
|
|
33
|
+
class SessionFactory extends common.GrpcServiceObject {
|
|
34
|
+
constructor(database, name, poolOptions) {
|
|
35
|
+
super({
|
|
36
|
+
parent: database,
|
|
37
|
+
id: name,
|
|
38
|
+
});
|
|
39
|
+
this.pool_ =
|
|
40
|
+
typeof poolOptions === 'function'
|
|
41
|
+
? new poolOptions(database, null)
|
|
42
|
+
: new session_pool_1.SessionPool(database, poolOptions);
|
|
43
|
+
this.pool_.on('error', this.emit.bind(database, 'error'));
|
|
44
|
+
this.pool_.open();
|
|
45
|
+
this.multiplexedSession_ = new multiplexed_session_1.MultiplexedSession(database);
|
|
46
|
+
// Multiplexed sessions should only be created if its enabled.
|
|
47
|
+
if (this.multiplexedSession_.isMultiplexedEnabled) {
|
|
48
|
+
this.multiplexedSession_.on('error', this.emit.bind(database, 'error'));
|
|
49
|
+
this.multiplexedSession_.createSession();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Retrieves a session, either a regular session or a multiplexed session, based on the environment variable configuration.
|
|
54
|
+
*
|
|
55
|
+
* If the environment variable `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS` is set to `true`, the method will attempt to
|
|
56
|
+
* retrieve a multiplexed session. Otherwise, it will retrieve a session from the regular pool.
|
|
57
|
+
*
|
|
58
|
+
* @param {GetSessionCallback} callback The callback function.
|
|
59
|
+
*/
|
|
60
|
+
getSession(callback) {
|
|
61
|
+
const sessionHandler = this.multiplexedSession_
|
|
62
|
+
.isMultiplexedEnabled
|
|
63
|
+
? this.multiplexedSession_
|
|
64
|
+
: this.pool_;
|
|
65
|
+
sessionHandler.getSession((err, session) => callback(err, session));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Returns the regular session pool object.
|
|
69
|
+
*
|
|
70
|
+
* @returns {SessionPoolInterface} The session pool used by current instance.
|
|
71
|
+
*/
|
|
72
|
+
getPool() {
|
|
73
|
+
return this.pool_;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Releases a session back to the session pool.
|
|
77
|
+
*
|
|
78
|
+
* This method returns a session to the pool after it is no longer needed.
|
|
79
|
+
* It is a no-op for multiplexed sessions.
|
|
80
|
+
*
|
|
81
|
+
* @param {Session} session - The session to be released. This should be an instance of `Session` that was
|
|
82
|
+
* previously acquired from the session pool.
|
|
83
|
+
*
|
|
84
|
+
* @throws {Error} If the session is invalid or cannot be released.
|
|
85
|
+
*/
|
|
86
|
+
release(session) {
|
|
87
|
+
if (!this.multiplexedSession_.isMultiplexedEnabled) {
|
|
88
|
+
this.pool_.release(session);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.SessionFactory = SessionFactory;
|
|
93
|
+
//# sourceMappingURL=session-factory.js.map
|
|
@@ -22,6 +22,7 @@ import { NormalCallback } from './common';
|
|
|
22
22
|
import { GoogleError, grpc } from 'google-gax';
|
|
23
23
|
import trace = require('stack-trace');
|
|
24
24
|
import { ObservabilityOptions } from './instrument';
|
|
25
|
+
import { GetSessionCallback } from './session-factory';
|
|
25
26
|
/**
|
|
26
27
|
* @callback SessionPoolCloseCallback
|
|
27
28
|
* @param {?Error} error Closing error, if any.
|
|
@@ -35,15 +36,6 @@ export type GetReadSessionCallback = NormalCallback<Session>;
|
|
|
35
36
|
export interface GetWriteSessionCallback {
|
|
36
37
|
(err: Error | null, session?: Session | null, transaction?: Transaction | null): void;
|
|
37
38
|
}
|
|
38
|
-
/**
|
|
39
|
-
* @callback GetSessionCallback
|
|
40
|
-
* @param {?Error} error Request error, if any.
|
|
41
|
-
* @param {Session} session The read-write session.
|
|
42
|
-
* @param {Transaction} transaction The transaction object.
|
|
43
|
-
*/
|
|
44
|
-
export interface GetSessionCallback {
|
|
45
|
-
(err: Error | null, session?: Session | null, transaction?: Transaction | null): void;
|
|
46
|
-
}
|
|
47
39
|
/**
|
|
48
40
|
* Interface for implementing custom session pooling logic, it should extend the
|
|
49
41
|
* {@link https://nodejs.org/api/events.html|EventEmitter} class and emit any
|
|
@@ -181,36 +173,6 @@ export declare class SessionPoolExhaustedError extends GoogleError {
|
|
|
181
173
|
* @return true if the error is a 'Session not found' error, and otherwise false.
|
|
182
174
|
*/
|
|
183
175
|
export declare function isSessionNotFoundError(error: grpc.ServiceError | undefined): boolean;
|
|
184
|
-
/**
|
|
185
|
-
* Checks whether the given error is a 'Database not found' error.
|
|
186
|
-
* @param {Error} error The error to check.
|
|
187
|
-
* @return {boolean} True if the error is a 'Database not found' error, and otherwise false.
|
|
188
|
-
*/
|
|
189
|
-
export declare function isDatabaseNotFoundError(error: grpc.ServiceError | undefined): boolean;
|
|
190
|
-
/**
|
|
191
|
-
* Checks whether the given error is an 'Instance not found' error.
|
|
192
|
-
* @param {Error} error The error to check.
|
|
193
|
-
* @return {boolean} True if the error is an 'Instance not found' error, and otherwise false.
|
|
194
|
-
*/
|
|
195
|
-
export declare function isInstanceNotFoundError(error: grpc.ServiceError | undefined): boolean;
|
|
196
|
-
/**
|
|
197
|
-
* Checks whether the given error is a 'Create session permission' error.
|
|
198
|
-
* @param {Error} error The error to check.
|
|
199
|
-
* @return {boolean} True if the error is a 'Create session permission' error, and otherwise false.
|
|
200
|
-
*/
|
|
201
|
-
export declare function isCreateSessionPermissionError(error: grpc.ServiceError | undefined): boolean;
|
|
202
|
-
/**
|
|
203
|
-
* Checks whether the given error is a 'Could not load the default credentials' error.
|
|
204
|
-
* @param {Error} error The error to check.
|
|
205
|
-
* @return {boolean} True if the error is a 'Could not load the default credentials' error, and otherwise false.
|
|
206
|
-
*/
|
|
207
|
-
export declare function isDefaultCredentialsNotSetError(error: grpc.ServiceError | undefined): boolean;
|
|
208
|
-
/**
|
|
209
|
-
* Checks whether the given error is an 'Unable to detect a Project Id in the current environment' error.
|
|
210
|
-
* @param {Error} error The error to check.
|
|
211
|
-
* @return {boolean} True if the error is an 'Unable to detect a Project Id in the current environment' error, and otherwise false.
|
|
212
|
-
*/
|
|
213
|
-
export declare function isProjectIdNotSetInEnvironmentError(error: grpc.ServiceError | undefined): boolean;
|
|
214
176
|
interface SessionInventory {
|
|
215
177
|
sessions: Session[];
|
|
216
178
|
borrowed: Set<Session>;
|