-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient.ts
More file actions
413 lines (361 loc) · 14.4 KB
/
client.ts
File metadata and controls
413 lines (361 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg";
import { AwsClient } from "../../common/lib/aws_client";
import { PgConnectionUrlParser } from "./pg_connection_url_parser";
import { DatabaseDialect, DatabaseType } from "../../common/lib/database_dialect/database_dialect";
import { DatabaseDialectCodes } from "../../common/lib/database_dialect/database_dialect_codes";
import { RdsPgDatabaseDialect } from "./dialect/rds_pg_database_dialect";
import { PgDatabaseDialect } from "./dialect/pg_database_dialect";
import { AuroraPgDatabaseDialect } from "./dialect/aurora_pg_database_dialect";
import {
AwsPoolConfig,
AwsWrapperError,
ConnectionProvider,
FailoverSuccessError,
HostInfo,
InternalPooledConnectionProvider,
TransactionIsolationLevel,
UndefinedClientError,
UnsupportedMethodError
} from "../../common/lib";
import { Messages } from "../../common/lib/utils/messages";
import { ClientWrapper } from "../../common/lib/client_wrapper";
import { RdsMultiAZClusterPgDatabaseDialect } from "./dialect/rds_multi_az_pg_database_dialect";
import { TelemetryTraceLevel } from "../../common/lib/utils/telemetry/telemetry_trace_level";
import { NodePostgresDriverDialect } from "./dialect/node_postgres_driver_dialect";
import { isDialectTopologyAware } from "../../common/lib/utils/utils";
import { PGClient, PGPoolClient } from "./pg_client";
import { DriverConnectionProvider } from "../../common/lib/driver_connection_provider";
import { AwsClientConfig } from "../../common/lib/wrapper_property";
export interface AwsPgClientConfig extends ClientConfig, AwsClientConfig {}
class BaseAwsPgClient extends AwsClient implements PGClient {
private static readonly knownDialectsByCode: Map<string, DatabaseDialect> = new Map([
[DatabaseDialectCodes.PG, new PgDatabaseDialect()],
[DatabaseDialectCodes.RDS_PG, new RdsPgDatabaseDialect()],
[DatabaseDialectCodes.AURORA_PG, new AuroraPgDatabaseDialect()],
[DatabaseDialectCodes.RDS_MULTI_AZ_PG, new RdsMultiAZClusterPgDatabaseDialect()]
]);
constructor(config: AwsPgClientConfig, connectionProvider?: ConnectionProvider) {
super(
config,
DatabaseType.POSTGRES,
BaseAwsPgClient.knownDialectsByCode,
new PgConnectionUrlParser(),
new NodePostgresDriverDialect(),
connectionProvider ?? new DriverConnectionProvider()
);
}
// AWS Client Implementation
private async queryWithoutUpdate(text: string): Promise<QueryResult> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"query",
async () => {
return this.targetClient?.client.query(text);
},
text
);
}
async setReadOnly(readOnly: boolean): Promise<QueryResult | void> {
this.pluginService.getSessionStateService().setupPristineReadOnly();
const result = await this.queryWithoutUpdate(`SET SESSION CHARACTERISTICS AS TRANSACTION READ ${readOnly ? "ONLY" : "WRITE"}`);
this.pluginService.getSessionStateService().updateReadOnly(readOnly);
return result;
}
isReadOnly(): boolean | undefined {
return this.pluginService.getSessionStateService().getReadOnly();
}
async setAutoCommit(autoCommit: boolean): Promise<QueryResult | void> {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "setAutoCommit"));
}
getAutoCommit(): boolean {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "getAutoCommit"));
}
async setTransactionIsolation(level: TransactionIsolationLevel): Promise<QueryResult | void> {
if (level == this.getTransactionIsolation()) {
return;
}
this.pluginService.getSessionStateService().setupPristineTransactionIsolation();
switch (level) {
case TransactionIsolationLevel.TRANSACTION_READ_UNCOMMITTED:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
break;
case TransactionIsolationLevel.TRANSACTION_READ_COMMITTED:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED");
break;
case TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ");
break;
case TransactionIsolationLevel.TRANSACTION_SERIALIZABLE:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE");
break;
default:
throw new AwsWrapperError(Messages.get("Client.invalidTransactionIsolationLevel", String(level)));
}
this.pluginService.getSessionStateService().setTransactionIsolation(level);
}
getTransactionIsolation(): TransactionIsolationLevel | undefined {
return this.pluginService.getSessionStateService().getTransactionIsolation();
}
async setCatalog(catalog: string): Promise<void> {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "setCatalog"));
}
getCatalog(): string {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "getCatalog"));
}
async setSchema(schema: string): Promise<QueryResult | void> {
if (!schema) {
return;
}
if (schema === this.getSchema()) {
return;
}
this.pluginService.getSessionStateService().setupPristineSchema();
const result = await this.queryWithoutUpdate(`SET search_path TO ${schema};`);
this.pluginService.getSessionStateService().setSchema(schema);
return result;
}
getSchema(): string | undefined {
return this.pluginService.getSessionStateService().getSchema();
}
async end() {
if (!this.isConnected || !this.targetClient) {
// No connections have been initialized.
// This might happen if end is called in a finally block when an error occurred while initializing the first connection.
return;
}
const hostInfo: HostInfo | null = this.pluginService.getCurrentHostInfo();
return await this.pluginManager.execute(
hostInfo,
this.properties,
"end",
() => {
const res = this.targetClient!.end();
this.targetClient = undefined;
this.isConnected = false;
return res;
},
null
);
}
async rollback(): Promise<any> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"rollback",
async () => {
if (this.targetClient) {
this.pluginService.updateInTransaction("rollback");
return await this.targetClient.rollback();
}
return null;
},
null
);
}
// PG Client Implementation
async connect(): Promise<void> {
await this.internalConnect();
const context = this.telemetryFactory.openTelemetryContext("awsClient.connect", TelemetryTraceLevel.TOP_LEVEL);
return await context.start(async () => {
const hostInfo = this.pluginService.getCurrentHostInfo();
if (hostInfo == null) {
throw new AwsWrapperError(Messages.get("HostInfo.noHostParameter"));
}
const result: ClientWrapper = await this.pluginManager.connect(hostInfo, this.properties, true, null);
if (isDialectTopologyAware(this.pluginService.getDialect())) {
try {
const role = await this.pluginService.getHostRole(result);
// The current host role may be incorrect, use the created client to confirm the host role.
if (role !== undefined && role !== result.hostInfo.role) {
result.hostInfo.role = role;
this.pluginService.setCurrentHostInfo(result.hostInfo);
this.pluginService.setInitialConnectionHostInfo(result.hostInfo);
}
} catch (error) {
// Ignore
}
}
await this.pluginService.setCurrentClient(result, result.hostInfo);
await this.internalPostConnect();
});
}
query(text: string): Promise<any>;
query(text: string, values: any[]): Promise<any>;
query<T extends Submittable>(queryStream: T): T;
query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
async query<R extends QueryResultRow = any, I = any[]>(
queryTextOrConfig: string | QueryConfig<I>,
values?: QueryConfigValues<I>
): Promise<QueryResult<R>> {
// config can be a string or a query config object
const host = this.pluginService.getCurrentHostInfo();
const context = this.telemetryFactory.openTelemetryContext("awsClient.query", TelemetryTraceLevel.TOP_LEVEL);
return await context.start(async () => {
return await this.pluginManager.execute(
host,
this.properties,
"query",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
const sql = typeof queryTextOrConfig === "string" ? queryTextOrConfig : queryTextOrConfig.text;
await this.pluginService.updateState(sql);
return values !== undefined ? this.targetClient.query(queryTextOrConfig, values) : this.targetClient.query(queryTextOrConfig);
},
[queryTextOrConfig, values]
);
});
}
copyFrom(queryText: string): Promise<NodeJS.WritableStream> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"copyFrom",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
return await this.targetClient.client.copyFrom(queryText);
},
queryText
);
}
copyTo(queryText: string): Promise<NodeJS.ReadableStream> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"copyTo",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
return await this.targetClient.client.copyTo(queryText);
},
queryText
);
}
escapeIdentifier(str: string): Promise<string> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"escapeIdentifier",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
return await this.targetClient.client.escapeIdentifier(str);
},
str
);
}
escapeLiteral(str: string): Promise<string> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"escapeLiteral",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
return await this.targetClient.client.escapeLiteral(str);
},
str
);
}
prepare(name: string, text: string, nParams?: number): Promise<void> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"prepare",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
return await this.targetClient.client.prepare(name, text, nParams);
},
[name, text, nParams]
);
}
}
export class AwsPGClient extends BaseAwsPgClient {
constructor(config: any) {
super(config, new DriverConnectionProvider());
}
}
class AwsPGPooledConnection extends BaseAwsPgClient {
constructor(config: any, provider: ConnectionProvider) {
super(config, provider);
}
async release(): Promise<void> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"release",
async () => {
if (!this.targetClient) {
throw new UndefinedClientError();
}
this.pluginService.removeErrorListener(this.targetClient);
return await this.targetClient.client.release();
},
null
);
}
}
export type { AwsPGPooledConnection };
export class AwsPgPoolClient implements PGPoolClient {
private readonly connectionProvider: InternalPooledConnectionProvider;
private readonly config;
private readonly poolConfig;
constructor(config: any, poolConfig?: AwsPoolConfig) {
this.connectionProvider = new InternalPooledConnectionProvider(poolConfig);
this.config = config;
this.poolConfig = poolConfig;
}
async connect(): Promise<AwsPGPooledConnection> {
const awsPGPooledConnection: AwsPGPooledConnection = new AwsPGPooledConnection(this.config, this.connectionProvider);
await awsPGPooledConnection.connect();
return awsPGPooledConnection;
}
async end(): Promise<void> {
await this.connectionProvider.releaseResources();
}
query(text: string): Promise<any>;
query(text: string, values: any[]): Promise<any>;
query<T extends Submittable>(queryStream: T): T;
query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
async query<R extends QueryResultRow = any, I = any[]>(
queryTextOrConfig: string | QueryConfig<I>,
values?: QueryConfigValues<I>
): Promise<QueryResult<R>> {
const awsPGPooledConnection: AwsPGPooledConnection = new AwsPGPooledConnection(this.config, this.connectionProvider);
try {
await awsPGPooledConnection.connect();
const res = await awsPGPooledConnection.query(queryTextOrConfig as any, values);
await awsPGPooledConnection.end();
return res;
} catch (error: any) {
if (!(error instanceof FailoverSuccessError)) {
// Release pooled connection.
await awsPGPooledConnection.end();
}
throw error;
}
}
}