Skip to content

Commit b9c18b8

Browse files
authored
refactor: remove suggested cluster id (#612)
1 parent 557a378 commit b9c18b8

5 files changed

Lines changed: 14 additions & 315 deletions

File tree

common/lib/host_list_provider/rds_host_list_provider.ts

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,12 @@ export class RdsHostListProvider implements DynamicHostListProvider {
4444
private initialHostList: HostInfo[];
4545
protected initialHost: HostInfo;
4646
private refreshRateNano: number;
47-
private suggestedClusterIdRefreshRateNano: number = 10 * 60 * 1_000_000_000; // 10 minutes
4847
private hostList?: HostInfo[];
4948
protected readonly connectionUrlParser: ConnectionUrlParser;
5049
protected readonly hostListProviderService: HostListProviderService;
5150

52-
public static readonly suggestedPrimaryClusterIdCache: CacheMap<string, string> = new CacheMap<string, string>();
53-
public static readonly primaryClusterIdCache: CacheMap<string, boolean> = new CacheMap<string, boolean>();
5451
public clusterId: string = Date.now().toString();
5552
public isInitialized: boolean = false;
56-
public isPrimaryClusterId?: boolean;
5753
public clusterInstanceTemplate?: HostInfo;
5854

5955
constructor(properties: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService) {
@@ -87,8 +83,6 @@ export class RdsHostListProvider implements DynamicHostListProvider {
8783
return;
8884
}
8985

90-
this.isPrimaryClusterId = false;
91-
9286
const hostInfoBuilder = this.hostListProviderService.getHostInfoBuilder();
9387

9488
this.clusterInstanceTemplate = hostInfoBuilder
@@ -98,29 +92,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
9892

9993
this.validateHostPatternSetting(this.clusterInstanceTemplate.host);
10094

101-
const clusterIdSetting: string = WrapperProperties.CLUSTER_ID.get(this.properties);
102-
if (clusterIdSetting) {
103-
this.clusterId = clusterIdSetting;
104-
} else if (this.rdsUrlType === RdsUrlType.RDS_PROXY) {
105-
// Each proxy is associated with a single cluster, so it's safe to use RDS Proxy Url as cluster
106-
// identification
107-
this.clusterId = this.initialHost.url;
108-
} else if (this.rdsUrlType.isRds) {
109-
const clusterSuggestedResult: ClusterSuggestedResult | null = this.getSuggestedClusterId(this.initialHost.hostAndPort);
110-
if (clusterSuggestedResult && clusterSuggestedResult.clusterId) {
111-
this.clusterId = clusterSuggestedResult.clusterId;
112-
this.isPrimaryClusterId = clusterSuggestedResult.isPrimaryClusterId;
113-
} else {
114-
const clusterRdsHostUrl: string | null = this.rdsHelper.getRdsClusterHostUrl(this.initialHost.host);
115-
if (clusterRdsHostUrl) {
116-
this.clusterId = this.clusterInstanceTemplate.isPortSpecified()
117-
? `${clusterRdsHostUrl}:${this.clusterInstanceTemplate.port}`
118-
: clusterRdsHostUrl;
119-
this.isPrimaryClusterId = true;
120-
RdsHostListProvider.primaryClusterIdCache.put(this.clusterId, true, this.suggestedClusterIdRefreshRateNano);
121-
}
122-
}
123-
}
95+
this.clusterId = WrapperProperties.CLUSTER_ID.get(this.properties);
12496

12597
this.isInitialized = true;
12698
}
@@ -195,18 +167,11 @@ export class RdsHostListProvider implements DynamicHostListProvider {
195167
throw new AwsWrapperError("no cluster id");
196168
}
197169

198-
const suggestedPrimaryClusterId: string | null = RdsHostListProvider.suggestedPrimaryClusterIdCache.get(this.clusterId);
199-
if (suggestedPrimaryClusterId && this.clusterId !== suggestedPrimaryClusterId) {
200-
this.clusterId = suggestedPrimaryClusterId;
201-
this.isPrimaryClusterId = true;
202-
}
203-
204170
const cachedHosts: HostInfo[] | null = this.getStoredTopology();
205171

206172
// This clusterId is a primary one and is about to create a new entry in the cache.
207173
// When a primary entry is created it needs to be suggested for other (non-primary) entries.
208174
// Remember a flag to do suggestion after cache is updated.
209-
const needToSuggest: boolean = !cachedHosts && this.isPrimaryClusterId === true;
210175
if (!cachedHosts || forceUpdate) {
211176
// need to re-fetch the topology.
212177
if (!targetClient || !(await this.hostListProviderService.isClientValid(targetClient))) {
@@ -216,9 +181,6 @@ export class RdsHostListProvider implements DynamicHostListProvider {
216181
const hosts = await this.queryForTopology(targetClient, this.hostListProviderService.getDialect());
217182
if (hosts && hosts.length > 0) {
218183
this.storageService.set(this.clusterId, new Topology(hosts));
219-
if (needToSuggest) {
220-
this.suggestPrimaryCluster(hosts);
221-
}
222184
return new FetchTopologyResult(false, hosts);
223185
}
224186
}
@@ -230,63 +192,6 @@ export class RdsHostListProvider implements DynamicHostListProvider {
230192
}
231193
}
232194

233-
private getSuggestedClusterId(hostAndPort: string): ClusterSuggestedResult | null {
234-
const cache: ExpirationCache<string, Topology> = this.storageService.getAll(Topology) as ExpirationCache<string, Topology>;
235-
if (!cache) {
236-
return null;
237-
}
238-
for (const [key, hosts] of cache.getEntries()) {
239-
const isPrimaryCluster: boolean = RdsHostListProvider.primaryClusterIdCache.get(key, false, this.suggestedClusterIdRefreshRateNano) ?? false;
240-
if (key === hostAndPort) {
241-
return new ClusterSuggestedResult(hostAndPort, isPrimaryCluster);
242-
}
243-
244-
if (hosts) {
245-
for (const hostInfo of hosts.hosts) {
246-
if (hostInfo.hostAndPort === hostAndPort) {
247-
logger.debug(Messages.get("RdsHostListProvider.suggestedClusterId", key, hostAndPort));
248-
return new ClusterSuggestedResult(key, isPrimaryCluster);
249-
}
250-
}
251-
}
252-
}
253-
return null;
254-
}
255-
256-
suggestPrimaryCluster(primaryClusterHosts: HostInfo[]): void {
257-
if (!primaryClusterHosts) {
258-
return;
259-
}
260-
261-
const primaryClusterHostUrls: Set<string> = new Set<string>();
262-
primaryClusterHosts.forEach((hostInfo) => {
263-
primaryClusterHostUrls.add(hostInfo.url);
264-
});
265-
266-
const cache: ExpirationCache<string, Topology> = this.storageService.getAll(Topology) as ExpirationCache<string, Topology>;
267-
if (!cache) {
268-
return;
269-
}
270-
for (const [clusterId, clusterHosts] of cache.getEntries()) {
271-
const isPrimaryCluster: boolean | null = RdsHostListProvider.primaryClusterIdCache.get(
272-
clusterId,
273-
false,
274-
this.suggestedClusterIdRefreshRateNano
275-
);
276-
const suggestedPrimaryClusterId: string | null = RdsHostListProvider.suggestedPrimaryClusterIdCache.get(clusterId);
277-
if (isPrimaryCluster || suggestedPrimaryClusterId || !clusterHosts) {
278-
continue;
279-
}
280-
281-
for (const clusterHost of clusterHosts.hosts) {
282-
if (primaryClusterHostUrls.has(clusterHost.url)) {
283-
RdsHostListProvider.suggestedPrimaryClusterIdCache.put(clusterId, this.clusterId, this.suggestedClusterIdRefreshRateNano);
284-
break;
285-
}
286-
}
287-
}
288-
}
289-
290195
async queryForTopology(targetClient: ClientWrapper, dialect: DatabaseDialect): Promise<HostInfo[]> {
291196
if (!isDialectTopologyAware(dialect)) {
292197
throw new TypeError(Messages.get("RdsHostListProvider.incorrectDialect"));
@@ -366,8 +271,8 @@ export class RdsHostListProvider implements DynamicHostListProvider {
366271
}
367272

368273
static clearAll(): void {
369-
RdsHostListProvider.primaryClusterIdCache.clear();
370-
RdsHostListProvider.suggestedPrimaryClusterIdCache.clear();
274+
// No-op
275+
// TODO: remove if still not used after full service container refactoring
371276
}
372277

373278
clear(): void {
@@ -420,13 +325,3 @@ export class FetchTopologyResult {
420325
this.isCachedData = isCachedData;
421326
}
422327
}
423-
424-
class ClusterSuggestedResult {
425-
clusterId: string;
426-
isPrimaryClusterId: boolean;
427-
428-
constructor(clusterId: string, isPrimaryClusterId: boolean) {
429-
this.clusterId = clusterId;
430-
this.isPrimaryClusterId = isPrimaryClusterId;
431-
}
432-
}

common/lib/utils/storage/storage_service.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ export interface StorageService {
5656
*/
5757
set<V>(key: unknown, item: V): void;
5858

59-
// TODO: temporary method to return all storage services for a specific item class.
60-
// Should be removed along with the cluster id refactoring.
61-
getAll<V>(itemClass: Constructor<V>): ExpirationCache<unknown, unknown> | null;
62-
6359
/**
6460
* Gets an item stored in the storage service.
6561
*

common/lib/wrapper_property.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ export class WrapperProperties {
234234
"clusterId",
235235
"A unique identifier for the cluster. " +
236236
"Connections with the same cluster id share a cluster topology cache. " +
237-
"If unspecified, a cluster id is automatically created for AWS RDS clusters.",
238-
null
237+
"If unspecified, a cluster id is '1'.",
238+
"1"
239239
);
240240

241241
static readonly CLUSTER_INSTANCE_HOST_PATTERN = new WrapperProperty<string>(

0 commit comments

Comments
 (0)