Skip to content

Commit

Permalink
chore: make AwsClient#releaseResouces static (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophia-bq authored Dec 2, 2024
1 parent 71ed378 commit 9a7bc2f
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 28 deletions.
4 changes: 0 additions & 4 deletions common/lib/aws_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ export abstract class AwsClient extends EventEmitter {
return await this.pluginService.isClientValid(this.targetClient);
}

async releaseResources(): Promise<any> {
await this.pluginManager.releaseResources();
}

getPluginInstance<T>(iface: any): T {
return this.pluginManager.getPluginInstance(iface);
}
Expand Down
13 changes: 9 additions & 4 deletions common/lib/plugin_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class PluginManager {
private static readonly NOTIFY_CONNECTION_CHANGED_METHOD: string = "notifyConnectionChanged";
private static readonly ACCEPTS_STRATEGY_METHOD: string = "acceptsStrategy";
private static readonly GET_HOST_INFO_BY_STRATEGY_METHOD: string = "getHostInfoByStrategy";
private static PLUGINS: Set<ConnectionPlugin> = new Set();
private readonly props: Map<string, any>;
private _plugins: ConnectionPlugin[] = [];
private readonly connectionProviderManager: ConnectionProviderManager;
Expand Down Expand Up @@ -103,6 +104,9 @@ export class PluginManager {
);
}
}
for (const plugin of this._plugins) {
PluginManager.PLUGINS.add(plugin);
}
}

runMethodFuncWithTelemetry<T>(methodFunc: () => Promise<T>, name: string): Promise<T> {
Expand Down Expand Up @@ -302,22 +306,23 @@ export class PluginManager {
throw new AwsWrapperError("The driver does not support the requested host selection strategy: " + strategy);
}

async releaseResources() {
static async releaseResources() {
// This step allows all connection plugins a chance to clean up any dangling resources or
// perform any last tasks before shutting down.

for (const plugin of this._plugins) {
if (this.implementsCanReleaseResources(plugin)) {
for (const plugin of PluginManager.PLUGINS) {
if (PluginManager.implementsCanReleaseResources(plugin)) {
await plugin.releaseResources();
}
}
PluginManager.PLUGINS = new Set();
}

getConnectionProvider(hostInfo: HostInfo | null, props: Map<string, any>): ConnectionProvider {
return this.connectionProviderManager.getConnectionProvider(hostInfo, props);
}

private implementsCanReleaseResources(plugin: any): plugin is CanReleaseResources {
private static implementsCanReleaseResources(plugin: any): plugin is CanReleaseResources {
return plugin.releaseResources !== undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion mysql/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ClientUtils } from "../../common/lib/utils/client_utils";
import { RdsMultiAZMySQLDatabaseDialect } from "./dialect/rds_multi_az_mysql_database_dialect";
import { TelemetryTraceLevel } from "../../common/lib/utils/telemetry/telemetry_trace_level";
import { MySQL2DriverDialect } from "./dialect/mysql2_driver_dialect";
import { PluginManager } from "../../common/lib";

export class AwsMySQLClient extends AwsClient {
private static readonly knownDialectsByCode: Map<string, DatabaseDialect> = new Map([
Expand Down Expand Up @@ -212,7 +213,6 @@ export class AwsMySQLClient extends AwsClient {
},
null
);
await this.releaseResources();
return result;
}

Expand Down
1 change: 0 additions & 1 deletion pg/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export class AwsPGClient extends AwsClient {
},
null
);
await this.releaseResources();
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/container/tests/aurora_failover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { RdsUtils } from "../../../../common/lib/utils/rds_utils";
import { logger } from "../../../../common/logutils";
import { features, instanceCount } from "./config";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { PluginManager } from "../../../../common/lib";

const itIf =
features.includes(TestEnvironmentFeatures.FAILOVER_SUPPORTED) &&
Expand Down Expand Up @@ -94,6 +95,7 @@ describe("aurora failover", () => {
// pass
}
}
await PluginManager.releaseResources();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/container/tests/autoscaling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ConnectionProviderManager } from "../../../../common/lib/connection_pro
import { TestInstanceInfo } from "./utils/test_instance_info";
import { sleep } from "../../../../common/lib/utils/utils";
import { FailoverSuccessError } from "../../../../common/lib/utils/errors";
import { PluginManager } from "../../../../common/lib";

const itIf =
!features.includes(TestEnvironmentFeatures.PERFORMANCE) &&
Expand Down Expand Up @@ -109,6 +110,7 @@ describe("pooled connection autoscaling", () => {
// pass
}
}
await PluginManager.releaseResources();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/container/tests/basic_connectivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DatabaseEngine } from "./utils/database_engine";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features } from "./config";
import { DatabaseEngineDeployment } from "./utils/database_engine_deployment";
import { PluginManager } from "../../../../common/lib";

const itIf =
!features.includes(TestEnvironmentFeatures.PERFORMANCE) && !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) ? it : it.skip;
Expand Down Expand Up @@ -54,6 +55,7 @@ afterEach(async () => {
// pass
}
}
await PluginManager.releaseResources();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/container/tests/iam_authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { IamAuthenticationPlugin } from "../../../../common/lib/authentication/i
import { logger } from "../../../../common/logutils";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features } from "./config";
import { PluginManager } from "../../../../common/lib";

const itIf =
!features.includes(TestEnvironmentFeatures.PERFORMANCE) &&
Expand Down Expand Up @@ -89,6 +90,7 @@ describe("iam authentication", () => {
});

afterEach(async () => {
await PluginManager.releaseResources();
await TestEnvironment.verifyClusterStatus();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/container/tests/performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { features } from "./config";
import { MonitorServiceImpl } from "../../../../common/lib/plugins/efm/monitor_service";
import { PerfStat } from "./utils/perf_stat";
import { PerfTestUtility } from "./utils/perf_util";
import { PluginManager } from "../../../../common/lib";

const itIf =
features.includes(TestEnvironmentFeatures.FAILOVER_SUPPORTED) &&
Expand Down Expand Up @@ -253,6 +254,7 @@ describe("performance", () => {
});

afterEach(async () => {
await PluginManager.releaseResources();
await TestEnvironment.verifyClusterStatus();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { AwsPoolConfig } from "../../../../common/lib/aws_pool_config";
import { ConnectionProviderManager } from "../../../../common/lib/connection_provider_manager";
import { InternalPoolMapping } from "../../../../common/lib/utils/internal_pool_mapping";
import { HostInfo } from "../../../../common/lib/host_info";
import { PluginManager } from "../../../../common/lib";

const itIf =
!features.includes(TestEnvironmentFeatures.PERFORMANCE) &&
Expand Down Expand Up @@ -121,6 +122,7 @@ describe("aurora read write splitting", () => {
// pass
}
}
await PluginManager.releaseResources();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ExecuteTimePlugin } from "../../../../common/lib/plugins/execute_time_p
import { TestDriver } from "./utils/test_driver";
import { ConnectionProviderManager } from "../../../../common/lib/connection_provider_manager";
import { InternalPooledConnectionProvider } from "../../../../common/lib/internal_pooled_connection_provider";
import { PluginManager } from "../../../../common/lib";

const itIf =
features.includes(TestEnvironmentFeatures.FAILOVER_SUPPORTED) &&
Expand All @@ -52,6 +53,7 @@ describe("rwperformance", () => {
});

afterEach(async () => {
await PluginManager.releaseResources();
logger.info(`Test finished: ${expect.getState().currentTestName}`);
}, 1320000);

Expand Down
6 changes: 3 additions & 3 deletions tests/plugin_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ suite(
const pluginManager = getPluginManager(props);
const wrapper = new TestConnectionWrapper(props, pluginManager, instance(mockPluginService));
await pluginManager.init();
await wrapper.releaseResources();
await PluginManager.releaseResources();
}),

add("initAndReleaseWithExecuteTimePlugin", async () => {
const props = getProps("executeTime");
const pluginManager = getPluginManager(props);
const wrapper = new TestConnectionWrapper(props, pluginManager, instance(mockPluginService));
await pluginManager.init();
await wrapper.releaseResources();
await PluginManager.releaseResources();
}),

add("initAndReleaseWithReadWriteSplittingPlugin", async () => {
const props = getProps("readWriteSplitting");
const pluginManager = getPluginManager(props);
const wrapper = new TestConnectionWrapper(props, pluginManager, instance(mockPluginService));
await pluginManager.init();
await wrapper.releaseResources();
await PluginManager.releaseResources();
}),

add("executeStatementBaseline", async () => {
Expand Down
12 changes: 6 additions & 6 deletions tests/plugin_manager_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,37 +350,37 @@ suite(
add("releaseResourcesWithNoPlugins", async () => {
const pluginManagerWithNoPlugins = getPluginManagerWithNoPlugins();
await pluginManagerWithNoPlugins.init();
return async () => await pluginManagerWithNoPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith1Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(1, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith2Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(2, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith5Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(5, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith10Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(10, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add.only("releaseResourcesWithDefaultPlugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init();
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

cycle(),
Expand Down
12 changes: 6 additions & 6 deletions tests/plugin_manager_telemetry_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,37 +410,37 @@ suite(
add("releaseResourcesWithNoPlugins", async () => {
const pluginManagerWithNoPlugins = getPluginManagerWithNoPlugins();
await pluginManagerWithNoPlugins.init();
return async () => await pluginManagerWithNoPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith1Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(1, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith2Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(2, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith5Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(5, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add("releaseResourcesWith10Plugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init(await createPlugins(10, instance(mockPluginService), instance(mockConnectionProvider), propsWithPlugins));
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

add.only("releaseResourcesWithDefaultPlugins", async () => {
const pluginManagerWithPlugins = getPluginManagerWithPlugins();
await pluginManagerWithPlugins.init();
return async () => await pluginManagerWithPlugins.releaseResources();
return async () => await PluginManager.releaseResources();
}),

cycle(),
Expand Down
6 changes: 3 additions & 3 deletions tests/plugin_telemetry_benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ suite(
add("initAndReleaseBaseline", async () => {
const wrapper = new TestConnectionWrapper(props, pluginManager, instance(mockPluginService));
await pluginManager.init();
await wrapper.releaseResources();
await PluginManager.releaseResources();
await wrapper.end();
}),

add("initAndReleaseWithExecuteTimePlugin", async () => {
const wrapper = new TestConnectionWrapper(propsExecute, pluginManagerExecute, instance(mockPluginService));
await pluginManagerExecute.init();
await wrapper.releaseResources();
await PluginManager.releaseResources();
await wrapper.end();
}),

add("initAndReleaseWithReadWriteSplittingPlugin", async () => {
const wrapper = new TestConnectionWrapper(propsReadWrite, pluginManagerReadWrite, instance(mockPluginService));
await pluginManagerReadWrite.init();
await wrapper.releaseResources();
await PluginManager.releaseResources();
await wrapper.end();
}),

Expand Down

0 comments on commit 9a7bc2f

Please sign in to comment.