-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdatabase_dialect_manager.ts
More file actions
211 lines (177 loc) · 8.19 KB
/
database_dialect_manager.ts
File metadata and controls
211 lines (177 loc) · 8.19 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
/*
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 { DatabaseDialectProvider } from "./database_dialect_provider";
import { DatabaseDialect, DatabaseType } from "./database_dialect";
import { DatabaseDialectCodes } from "./database_dialect_codes";
import { WrapperProperties } from "../wrapper_property";
import { AwsWrapperError } from "../utils/errors";
import { Messages } from "../utils/messages";
import { RdsUtils } from "../utils/rds_utils";
import { logger } from "../../logutils";
import { CacheMap } from "../utils/cache_map";
import { ClientWrapper } from "../client_wrapper";
import { RdsUrlType } from "../utils/rds_url_type";
export class DatabaseDialectManager implements DatabaseDialectProvider {
/**
* In order to simplify dialect detection, there's an internal host-to-dialect cache.
* The cache contains host endpoints and identified dialect. Cache expiration time in
* milliseconds is defined by the variable below.
*/
private static readonly ENDPOINT_CACHE_EXPIRATION_MS = 86_400_000_000_000; // 24 hours
protected static readonly knownEndpointDialects: CacheMap<string, string> = new CacheMap();
protected readonly knownDialectsByCode: Map<string, DatabaseDialect>;
protected readonly customDialect: DatabaseDialect | null;
protected readonly rdsHelper: RdsUtils = new RdsUtils();
protected readonly dbType: DatabaseType;
protected canUpdate: boolean = false;
protected dialect: DatabaseDialect;
protected dialectCode: string = "";
constructor(knownDialectsByCode: any, dbType: DatabaseType, props: Map<string, any>) {
this.knownDialectsByCode = knownDialectsByCode;
this.dbType = dbType;
const dialectSetting = WrapperProperties.CUSTOM_DATABASE_DIALECT.get(props);
if (dialectSetting && !this.isDatabaseDialect(dialectSetting)) {
throw new AwsWrapperError(Messages.get("DatabaseDialectManager.wrongCustomDialect"));
}
this.customDialect = dialectSetting;
this.dialect = this.getDialect(props);
}
static resetEndpointCache() {
DatabaseDialectManager.knownEndpointDialects.clear();
}
protected isDatabaseDialect(arg: any): arg is DatabaseDialect {
return arg.getDialectName !== undefined;
}
getDialect(props: Map<string, any>): DatabaseDialect {
if (this.dialect) {
return this.dialect;
}
this.canUpdate = false;
if (this.customDialect) {
this.dialectCode = DatabaseDialectCodes.CUSTOM;
this.dialect = this.customDialect;
this.logCurrentDialect();
return this.dialect;
}
const userDialectSetting = WrapperProperties.DIALECT.get(props);
const host = props.get(WrapperProperties.HOST.name);
const dialectCode = userDialectSetting ?? DatabaseDialectManager.knownEndpointDialects.get(host);
if (dialectCode) {
const userDialect = this.knownDialectsByCode.get(dialectCode);
if (userDialect) {
this.dialectCode = dialectCode;
this.dialect = userDialect;
this.logCurrentDialect();
return userDialect;
}
throw new AwsWrapperError(Messages.get("DatabaseDialectManager.unknownDialectCode", dialectCode));
}
if (this.dbType === DatabaseType.MYSQL) {
const type = this.rdsHelper.identifyRdsType(host);
if (type == RdsUrlType.RDS_GLOBAL_WRITER_CLUSTER) {
this.canUpdate = false;
this.dialectCode = DatabaseDialectCodes.GLOBAL_AURORA_MYSQL;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.GLOBAL_AURORA_MYSQL);
this.logCurrentDialect();
return this.dialect;
}
if (type.isRdsCluster) {
this.canUpdate = true;
this.dialectCode = DatabaseDialectCodes.AURORA_MYSQL;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.AURORA_MYSQL);
this.logCurrentDialect();
return this.dialect;
}
if (type.isRds) {
this.canUpdate = true;
this.dialectCode = DatabaseDialectCodes.RDS_MYSQL;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.RDS_MYSQL);
this.logCurrentDialect();
return this.dialect;
}
this.canUpdate = true;
this.dialectCode = DatabaseDialectCodes.MYSQL;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.MYSQL);
this.logCurrentDialect();
return this.dialect;
}
if (this.dbType === DatabaseType.POSTGRES) {
const type = this.rdsHelper.identifyRdsType(host);
if (type === RdsUrlType.RDS_AURORA_LIMITLESS_DB_SHARD_GROUP) {
this.canUpdate = false;
this.dialectCode = DatabaseDialectCodes.AURORA_PG;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.AURORA_PG);
this.logCurrentDialect();
return this.dialect;
}
if (type == RdsUrlType.RDS_GLOBAL_WRITER_CLUSTER) {
this.canUpdate = false;
this.dialectCode = DatabaseDialectCodes.GLOBAL_AURORA_PG;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.GLOBAL_AURORA_PG);
this.logCurrentDialect();
return this.dialect;
}
if (type.isRdsCluster) {
this.canUpdate = true;
this.dialectCode = DatabaseDialectCodes.AURORA_PG;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.AURORA_PG);
this.logCurrentDialect();
return this.dialect;
}
if (type.isRds) {
this.canUpdate = true;
this.dialectCode = DatabaseDialectCodes.RDS_PG;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.RDS_PG);
this.logCurrentDialect();
return this.dialect;
}
this.canUpdate = true;
this.dialectCode = DatabaseDialectCodes.PG;
this.dialect = <DatabaseDialect>this.knownDialectsByCode.get(DatabaseDialectCodes.PG);
this.logCurrentDialect();
return this.dialect;
}
throw new AwsWrapperError(Messages.get("DatabaseDialectManager.getDialectError"));
}
async getDialectForUpdate(targetClient: ClientWrapper, originalHost: string, newHost: string): Promise<DatabaseDialect> {
if (!this.canUpdate) {
return this.dialect;
}
const dialectCandidates = this.dialect.getDialectUpdateCandidates();
if (dialectCandidates.length > 0) {
for (const dialectCandidateCode of dialectCandidates) {
const dialectCandidate = this.knownDialectsByCode.get(dialectCandidateCode);
if (!dialectCandidate) {
throw new AwsWrapperError(Messages.get("DatabaseDialectManager.unknownDialectCode", dialectCandidateCode));
}
const isDialect = await dialectCandidate.isDialect(targetClient);
if (isDialect) {
this.canUpdate = false;
this.dialectCode = dialectCandidateCode;
this.dialect = dialectCandidate;
DatabaseDialectManager.knownEndpointDialects.put(originalHost, dialectCandidateCode, DatabaseDialectManager.ENDPOINT_CACHE_EXPIRATION_MS);
DatabaseDialectManager.knownEndpointDialects.put(newHost, dialectCandidateCode, DatabaseDialectManager.ENDPOINT_CACHE_EXPIRATION_MS);
this.logCurrentDialect();
return this.dialect;
}
}
}
DatabaseDialectManager.knownEndpointDialects.put(originalHost, this.dialectCode, DatabaseDialectManager.ENDPOINT_CACHE_EXPIRATION_MS);
DatabaseDialectManager.knownEndpointDialects.put(newHost, this.dialectCode, DatabaseDialectManager.ENDPOINT_CACHE_EXPIRATION_MS);
this.logCurrentDialect();
return this.dialect;
}
logCurrentDialect() {
logger.debug(`Current dialect: ${this.dialectCode}, ${this.dialect.getDialectName()}, canUpdate: ${this.canUpdate}`);
}
}