Skip to content

Commit 40d235c

Browse files
authored
refactor: create an interface for PluginService (#479)
1 parent d398e31 commit 40d235c

48 files changed

Lines changed: 190 additions & 91 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/lib/aws_client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { PluginServiceManagerContainer } from "./plugin_service_manager_container";
18-
import { PluginService } from "./plugin_service";
18+
import { PluginService, PluginServiceImpl } from "./plugin_service";
1919
import { DatabaseDialect, DatabaseType } from "./database_dialect/database_dialect";
2020
import { ConnectionUrlParser } from "./utils/connection_url_parser";
2121
import { HostListProvider } from "./host_list_provider/host_list_provider";
@@ -34,6 +34,7 @@ import { ConfigurationProfile } from "./profile/configuration_profile";
3434
import { AwsWrapperError } from "./utils/errors";
3535
import { Messages } from "./utils/messages";
3636
import { TransactionIsolationLevel } from "./utils/transaction_isolation_level";
37+
import { HostListProviderService } from "./host_list_provider_service";
3738

3839
const { EventEmitter } = pkgStream;
3940

@@ -98,7 +99,7 @@ export abstract class AwsClient extends EventEmitter {
9899

99100
this.telemetryFactory = new DefaultTelemetryFactory(this.properties);
100101
const container = new PluginServiceManagerContainer();
101-
this.pluginService = new PluginService(
102+
this.pluginService = new PluginServiceImpl(
102103
container,
103104
this,
104105
dbType,
@@ -123,12 +124,12 @@ export abstract class AwsClient extends EventEmitter {
123124
await this.setup();
124125
const hostListProvider: HostListProvider = this.pluginService
125126
.getDialect()
126-
.getHostListProvider(this.properties, this.properties.get("host"), this.pluginService);
127+
.getHostListProvider(this.properties, this.properties.get("host"), <HostListProviderService>(<unknown>this.pluginService));
127128
this.pluginService.setHostListProvider(hostListProvider);
128129
await this.pluginService.refreshHostList();
129130
const initialHostInfo = this.pluginService.getInitialConnectionHostInfo();
130131
if (initialHostInfo != null) {
131-
await this.pluginManager.initHostProvider(initialHostInfo, this.properties, this.pluginService);
132+
await this.pluginManager.initHostProvider(initialHostInfo, this.properties, <HostListProviderService>(<unknown>this.pluginService));
132133
await this.pluginService.refreshHostList();
133134
}
134135
}

common/lib/host_list_provider_service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
import { HostListProvider } from "./host_list_provider/host_list_provider";
17+
import { BlockingHostListProvider, HostListProvider } from "./host_list_provider/host_list_provider";
1818
import { HostInfo } from "./host_info";
1919
import { AwsClient } from "./aws_client";
2020
import { DatabaseDialect } from "./database_dialect/database_dialect";
@@ -53,4 +53,6 @@ export interface HostListProviderService {
5353
getTelemetryFactory(): TelemetryFactory;
5454

5555
setAllowedAndBlockedHosts(allowedAndBlockedHosts: AllowedAndBlockedHosts): void;
56+
57+
isBlockingHostListProvider(arg: any): arg is BlockingHostListProvider;
5658
}

common/lib/plugin_service.ts

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,95 @@ import { TelemetryFactory } from "./utils/telemetry/telemetry_factory";
4545
import { DriverDialect } from "./driver_dialect/driver_dialect";
4646
import { AllowedAndBlockedHosts } from "./AllowedAndBlockedHosts";
4747

48-
export class PluginService implements ErrorHandler, HostListProviderService {
48+
export interface PluginService extends ErrorHandler {
49+
isInTransaction(): boolean;
50+
51+
setInTransaction(inTransaction: boolean): void;
52+
53+
getHostListProvider(): HostListProvider | null;
54+
55+
getInitialConnectionHostInfo(): HostInfo | null;
56+
57+
setHostListProvider(hostListProvider: HostListProvider): void;
58+
59+
setInitialConnectionHostInfo(initialConnectionHostInfo: HostInfo): void;
60+
61+
getHostInfoByStrategy(role: HostRole, strategy: string, hosts?: HostInfo[]): HostInfo | undefined;
62+
63+
getCurrentHostInfo(): HostInfo | null;
64+
65+
setCurrentHostInfo(value: HostInfo): void;
66+
67+
getCurrentClient(): AwsClient;
68+
69+
getConnectionUrlParser(): ConnectionUrlParser;
70+
71+
getProperties(): Map<string, any>;
72+
73+
getDialect(): DatabaseDialect;
74+
75+
getDriverDialect(): DriverDialect;
76+
77+
getHostInfoBuilder(): HostInfoBuilder;
78+
79+
isStaticHostListProvider(): boolean;
80+
81+
acceptsStrategy(role: HostRole, strategy: string): boolean;
82+
83+
forceRefreshHostList(): Promise<void>;
84+
85+
forceRefreshHostList(targetClient: ClientWrapper): Promise<void>;
86+
87+
forceRefreshHostList(targetClient?: ClientWrapper): Promise<void>;
88+
89+
forceMonitoringRefresh(shouldVerifyWriter: boolean, timeoutMs: number): Promise<boolean>;
90+
91+
refreshHostList(): Promise<void>;
92+
93+
refreshHostList(targetClient: ClientWrapper): Promise<void>;
94+
95+
refreshHostList(targetClient?: ClientWrapper): Promise<void>;
96+
97+
getAllHosts(): HostInfo[];
98+
99+
getHosts(): HostInfo[];
100+
101+
setAvailability(hostAliases: Set<string>, availability: HostAvailability): void;
102+
103+
updateConfigWithProperties(props: Map<string, any>): void;
104+
105+
fillAliases(targetClient: ClientWrapper, hostInfo: HostInfo): Promise<void>;
106+
107+
identifyConnection(targetClient: ClientWrapper): Promise<HostInfo | null>;
108+
109+
connect(hostInfo: HostInfo, props: Map<string, any>): Promise<ClientWrapper>;
110+
111+
forceConnect(hostInfo: HostInfo, props: Map<string, any>): Promise<ClientWrapper>;
112+
113+
setCurrentClient(newClient: ClientWrapper, hostInfo: HostInfo): Promise<Set<HostChangeOptions>>;
114+
115+
isClientValid(targetClient: ClientWrapper): Promise<boolean>;
116+
117+
abortCurrentClient(): Promise<void>;
118+
119+
abortTargetClient(targetClient: ClientWrapper | undefined | null): Promise<void>;
120+
121+
getSessionStateService(): SessionStateService;
122+
123+
updateState(sql: string): Promise<void>;
124+
125+
updateInTransaction(sql: string): void;
126+
127+
updateDialect(targetClient: ClientWrapper): Promise<void>;
128+
129+
getHostRole(client: any): Promise<HostRole> | undefined;
130+
131+
getTelemetryFactory(): TelemetryFactory;
132+
133+
setAllowedAndBlockedHosts(allowedAndBlockedHosts: AllowedAndBlockedHosts): void;
134+
}
135+
136+
export class PluginServiceImpl implements PluginService, HostListProviderService {
49137
private static readonly DEFAULT_HOST_AVAILABILITY_CACHE_EXPIRE_NANO = 5 * 60_000_000_000; // 5 minutes
50138
private readonly _currentClient: AwsClient;
51139
private _currentHostInfo?: HostInfo;
@@ -154,6 +242,10 @@ export class PluginService implements ErrorHandler, HostListProviderService {
154242
this._currentHostInfo = value;
155243
}
156244

245+
getProperties(): Map<string, any> {
246+
return this.props;
247+
}
248+
157249
getCurrentClient(): AwsClient {
158250
return this._currentClient;
159251
}
@@ -234,7 +326,7 @@ export class PluginService implements ErrorHandler, HostListProviderService {
234326

235327
private updateHostAvailability(hosts: HostInfo[]) {
236328
hosts.forEach((host) => {
237-
const availability = PluginService.hostAvailabilityExpiringCache.get(host.url);
329+
const availability = PluginServiceImpl.hostAvailabilityExpiringCache.get(host.url);
238330
if (availability != null) {
239331
host.availability = availability;
240332
}
@@ -352,7 +444,7 @@ export class PluginService implements ErrorHandler, HostListProviderService {
352444
const changes = new Map<string, Set<HostChangeOptions>>();
353445
for (const host of hostsToChange) {
354446
const currentAvailability = host.getAvailability();
355-
PluginService.hostAvailabilityExpiringCache.put(host.url, availability, PluginService.DEFAULT_HOST_AVAILABILITY_CACHE_EXPIRE_NANO);
447+
PluginServiceImpl.hostAvailabilityExpiringCache.put(host.url, availability, PluginServiceImpl.DEFAULT_HOST_AVAILABILITY_CACHE_EXPIRE_NANO);
356448
if (currentAvailability !== availability) {
357449
let hostChanges = new Set<HostChangeOptions>();
358450
if (availability === HostAvailability.AVAILABLE) {
@@ -606,6 +698,6 @@ export class PluginService implements ErrorHandler, HostListProviderService {
606698
}
607699

608700
static clearHostAvailabilityCache(): void {
609-
PluginService.hostAvailabilityExpiringCache.clear();
701+
PluginServiceImpl.hostAvailabilityExpiringCache.clear();
610702
}
611703
}

common/lib/plugins/default_plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export class DefaultPlugin extends AbstractConnectionPlugin {
126126
throw new AwsWrapperError(Messages.get("DefaultConnectionPlugin.noHostsAvailable"));
127127
}
128128

129-
return this.connectionProviderManager.getHostInfoByStrategy(hosts, role, strategy, this.pluginService.props);
129+
return this.connectionProviderManager.getHostInfoByStrategy(
130+
hosts,
131+
role,
132+
strategy,
133+
this.pluginService.getProperties());
130134
}
131135
}

common/lib/plugins/failover2/failover2_plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ export class Failover2Plugin extends AbstractConnectionPlugin implements CanRele
494494

495495
async releaseResources(): Promise<void> {
496496
const hostListProvider: HostListProvider = this.pluginService.getHostListProvider();
497-
if (this.pluginService.isBlockingHostListProvider(hostListProvider)) {
497+
if (this.hostListProviderService.isBlockingHostListProvider(hostListProvider)) {
498498
await (hostListProvider as BlockingHostListProvider).clearAll();
499499
}
500500
}

mysql/lib/dialect/aurora_mysql_database_dialect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class AuroraMySQLDatabaseDialect extends MySQLDatabaseDialect implements
4646

4747
getHostListProvider(props: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService): HostListProvider {
4848
if (WrapperProperties.PLUGINS.get(props).includes("failover2")) {
49-
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>hostListProviderService);
49+
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>(<unknown>hostListProviderService));
5050
}
5151
return new RdsHostListProvider(props, originalUrl, hostListProviderService);
5252
}

mysql/lib/dialect/rds_multi_az_mysql_database_dialect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class RdsMultiAZMySQLDatabaseDialect extends MySQLDatabaseDialect impleme
5656

5757
getHostListProvider(props: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService): HostListProvider {
5858
if (WrapperProperties.PLUGINS.get(props).includes("failover2")) {
59-
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>hostListProviderService);
59+
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>(<unknown>hostListProviderService));
6060
}
6161
return new RdsHostListProvider(props, originalUrl, hostListProviderService);
6262
}

pg/lib/dialect/aurora_pg_database_dialect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class AuroraPgDatabaseDialect extends PgDatabaseDialect implements Topolo
4747

4848
getHostListProvider(props: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService): HostListProvider {
4949
if (WrapperProperties.PLUGINS.get(props).includes("failover2")) {
50-
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>hostListProviderService);
50+
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>(<unknown>hostListProviderService));
5151
}
5252
return new RdsHostListProvider(props, originalUrl, hostListProviderService);
5353
}

pg/lib/dialect/rds_multi_az_pg_database_dialect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class RdsMultiAZPgDatabaseDialect extends PgDatabaseDialect implements To
6161

6262
getHostListProvider(props: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService): HostListProvider {
6363
if (WrapperProperties.PLUGINS.get(props).includes("failover2")) {
64-
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>hostListProviderService);
64+
return new MonitoringRdsHostListProvider(props, originalUrl, hostListProviderService, <PluginService>(<unknown>hostListProviderService));
6565
}
6666
return new RdsHostListProvider(props, originalUrl, hostListProviderService);
6767
}

tests/integration/container/tests/connect_execute_time_plugin.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { TestEnvironmentFeatures } from "./utils/test_environment_features";
2323
import { features, instanceCount } from "./config";
2424
import { PluginManager } from "../../../../common/lib";
2525
import { RdsHostListProvider } from "../../../../common/lib/host_list_provider/rds_host_list_provider";
26-
import { PluginService } from "../../../../common/lib/plugin_service";
26+
import { PluginServiceImpl } from "../../../../common/lib/plugin_service";
2727
import { ConnectTimePlugin } from "../../../../common/lib/plugins/connect_time_plugin";
2828
import { ExecuteTimePlugin } from "../../../../common/lib/plugins/execute_time_plugin";
2929
import { getTimeInNanos } from "../../../../common/lib/utils/utils";
@@ -73,7 +73,7 @@ describe("aurora connect and execute time plugin", () => {
7373
await TestEnvironment.verifyAllInstancesUp();
7474

7575
RdsHostListProvider.clearAll();
76-
PluginService.clearHostAvailabilityCache();
76+
PluginServiceImpl.clearHostAvailabilityCache();
7777
}, 1320000);
7878

7979
afterEach(async () => {

0 commit comments

Comments
 (0)