Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: execution time plugin #19

Merged
merged 13 commits into from
Apr 9, 2024
Merged
12 changes: 5 additions & 7 deletions common/lib/abstract_connection_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export abstract class AbstractConnectionPlugin implements ConnectionPlugin {
abstract getSubscribedMethods(): Set<string>;

connect<T>(hostInfo: HostInfo, props: Map<string, any>, isInitialConnection: boolean, connectFunc: () => Promise<T>): Promise<T> {
throw new Error("Method not implemented.");
return connectFunc();
}

forceConnect<T>(hostInfo: HostInfo, props: Map<string, any>, isInitialConnection: boolean, forceConnectFunc: () => Promise<T>): Promise<T> {
throw new Error("Method not implemented.");
return forceConnectFunc();
}

execute<T>(methodName: string, methodFunc: () => Promise<T>, methodArgs: any[]): Promise<T> {
Expand All @@ -41,14 +41,12 @@ export abstract class AbstractConnectionPlugin implements ConnectionPlugin {
hostListProviderService: HostListProviderService,
initHostProviderFunc: () => void
): void {
throw new Error("Method not implemented.");
initHostProviderFunc();
}

notifyConnectionChanged(changes: Set<HostChangeOptions>): OldConnectionSuggestionAction {
throw new Error("Method not implemented.");
return OldConnectionSuggestionAction.NO_OPINION;
}

notifyNodeListChanged(changes: Map<string, Set<HostChangeOptions>>): void {
throw new Error("Method not implemented.");
}
notifyNodeListChanged(changes: Map<string, Set<HostChangeOptions>>): void {}
}
2 changes: 2 additions & 0 deletions common/lib/connection_plugin_chain_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { WrapperProperties } from "./wrapper_property";
import { AwsWrapperError } from "./utils/aws_wrapper_error";
import { Messages } from "./utils/messages";
import { DefaultPlugin } from "./plugins/default_plugin";
import { ExecuteTimePluginFactory } from "./plugins/execute_time_plugin";
import { ConnectTimePluginFactory } from "./plugins/connect_time_plugin";
import { AwsSecretsManagerPluginFactory } from "./authentication/aws_secrets_manager_plugin";

Expand All @@ -35,6 +36,7 @@ export class ConnectionPluginChainBuilder {

static readonly PLUGIN_FACTORIES = new Map<string, FactoryClass>([
["iam", IamAuthenticationPluginFactory],
["executeTime", ExecuteTimePluginFactory],
["connectTime", ConnectTimePluginFactory],
["secretsManager", AwsSecretsManagerPluginFactory],
["failover", FailoverPluginFactory]
Expand Down
5 changes: 1 addition & 4 deletions common/lib/plugins/default_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export class DefaultPlugin extends AbstractConnectionPlugin {

override execute<Type>(methodName: string, methodFunc: () => Type): Type {
logger.debug(Messages.get("DefaultPlugin.executingMethod", methodName));
const start = performance.now();
const res = methodFunc();
logger.debug(`Execution time for plugin ${this.id}: ${performance.now() - start} ms`);
return res;
return methodFunc();
}
}
59 changes: 59 additions & 0 deletions common/lib/plugins/execute_time_plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 { logger } from "../../logutils";
import { AbstractConnectionPlugin } from "../abstract_connection_plugin";
import { ConnectionPlugin } from "../connection_plugin";
import { ConnectionPluginFactory } from "../plugin_factory";
import { PluginService } from "../plugin_service";
import { Messages } from "../utils/messages";
import { getTimeInNanos } from "../utils/utils";

export class ExecuteTimePlugin extends AbstractConnectionPlugin {
private static readonly subscribedMethods: Set<string> = new Set<string>(["*"]);
private static executeTime: bigint = 0n;

public override getSubscribedMethods(): Set<string> {
return ExecuteTimePlugin.subscribedMethods;
}

public override async execute<T>(methodName: string, methodFunc: () => Promise<T>, methodArgs: any[]): Promise<T> {
const startTime = getTimeInNanos();

const result = await methodFunc();

const elapsedTimeNanos = getTimeInNanos() - startTime;

// Convert from ns to ms
logger.debug(Messages.get("ExecuteTimePlugin.executeTime", methodName, (elapsedTimeNanos / 1000000n).toString()));
ExecuteTimePlugin.executeTime += elapsedTimeNanos;
return result;
}

public static resetExecuteTime(): void {
ExecuteTimePlugin.executeTime = 0n;
}

public static getTotalExecuteTime(): bigint {
return ExecuteTimePlugin.executeTime;
}
}

export class ExecuteTimePluginFactory implements ConnectionPluginFactory {
getInstance(pluginService: PluginService, properties: Map<string, any>): ConnectionPlugin {
return new ExecuteTimePlugin();
}
}
6 changes: 2 additions & 4 deletions common/lib/plugins/failover/failover_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ClusterAwareReaderFailoverHandler } from "./reader_failover_handler";
import { SubscribedMethodHelper } from "../../utils/subscribed_method_helper";
import { HostChangeOptions } from "../../host_change_options";
import { ClusterAwareWriterFailoverHandler } from "./writer_failover_handler";
import { Messages } from "../../utils/messages";

export class FailoverPlugin extends AbstractConnectionPlugin {
private static readonly subscribedMethods: Set<string> = new Set([
Expand Down Expand Up @@ -97,13 +98,10 @@ export class FailoverPlugin extends AbstractConnectionPlugin {

override async execute<T>(methodName: string, methodFunc: () => Promise<T>): Promise<T> {
try {
const start = performance.now();
if (this.canUpdateTopology(methodName)) {
await this.updateTopology(false);
}
const res = methodFunc();
logger.debug(`Execution time for plugin ${this.id}: ${performance.now() - start} ms`);
return res;
return methodFunc();
} catch (e) {
logger.debug(e);
throw e;
Expand Down
1 change: 1 addition & 0 deletions common/lib/utils/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
"ClusterAwareWriterFailoverHandler.standaloneNode": "[TaskB] Host %s is not yet connected to a cluster. The cluster is still being reconfigured.",
"ClusterAwareWriterFailoverHandler.taskBAttemptConnectionToNewWriter": "[TaskB] Trying to connect to a new writer: '%s'",
"ClusterAwareWriterFailoverHandler.alreadyWriter": "Current reader connection is actually a new writer connection.",
"ExecuteTimePlugin.executeTime": "Executed method '%s' in %s milliseconds.",
"ConnectTimePlugin.connectTime": "Connected to '%s' in %s milliseconds."
}
44 changes: 44 additions & 0 deletions tests/unit/execute_time_plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 { ExecuteTimePlugin } from "aws-wrapper-common-lib/lib/plugins/execute_time_plugin";
import { sleep } from "aws-wrapper-common-lib/lib/utils/utils";

const mockCallable = jest.fn();
const timeToSleepMs = 1000;
const timeToSleepNs = timeToSleepMs * 1000000;

describe("executeTimePluginTest", () => {
it("test_executeTime", async () => {
mockCallable.mockImplementation(async () => {
await sleep(timeToSleepMs);
return null;
});

const plugin = new ExecuteTimePlugin();

await plugin.execute("query", mockCallable, []);

expect(ExecuteTimePlugin.getTotalExecuteTime()).toBeGreaterThan(timeToSleepNs);

await plugin.execute("query", mockCallable, []);

expect(ExecuteTimePlugin.getTotalExecuteTime()).toBeGreaterThan(timeToSleepNs * 2);

ExecuteTimePlugin.resetExecuteTime();
expect(ExecuteTimePlugin.getTotalExecuteTime()).toEqual(0n);
});
});
Loading