diff --git a/common/lib/connection_plugin_chain_builder.ts b/common/lib/connection_plugin_chain_builder.ts index d590a798..b54f4a12 100644 --- a/common/lib/connection_plugin_chain_builder.ts +++ b/common/lib/connection_plugin_chain_builder.ts @@ -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 { ConnectTimePluginFactory } from "./plugins/connect_time_plugin"; import { AwsSecretsManagerPluginFactory } from "./authentication/aws_secrets_manager_plugin"; export class PluginFactoryInfo {} @@ -34,6 +35,7 @@ export class ConnectionPluginChainBuilder { static readonly PLUGIN_FACTORIES = new Map([ ["iam", IamAuthenticationPluginFactory], + ["connectTime", ConnectTimePluginFactory], ["secretsManager", AwsSecretsManagerPluginFactory], ["failover", FailoverPluginFactory] ]); diff --git a/common/lib/plugins/connect_time_plugin.ts b/common/lib/plugins/connect_time_plugin.ts new file mode 100644 index 00000000..9ba0a5d3 --- /dev/null +++ b/common/lib/plugins/connect_time_plugin.ts @@ -0,0 +1,79 @@ +/* + 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 { AbstractConnectionPlugin } from "../abstract_connection_plugin"; +import { logger } from "../../logutils"; +import { HostInfo } from "../host_info"; +import { getTimeInNanos } from "../utils/utils"; +import { Messages } from "../utils/messages"; +import { ConnectionPluginFactory } from "../plugin_factory"; +import { ConnectionPlugin } from "../connection_plugin"; +import { PluginService } from "../plugin_service"; + +export class ConnectTimePlugin extends AbstractConnectionPlugin { + private static readonly subscribedMethods: Set = new Set(["connect", "forceConnect"]); + private static connectTime: bigint = 0n; + + public override getSubscribedMethods(): Set { + return ConnectTimePlugin.subscribedMethods; + } + + public override async connect( + hostInfo: HostInfo, + props: Map, + isInitialConnection: boolean, + connectFunc: () => Promise + ): Promise { + const startTime = getTimeInNanos(); + + const result = await connectFunc(); + + const elapsedTimeNanos = getTimeInNanos() - startTime; + ConnectTimePlugin.connectTime += elapsedTimeNanos; + logger.debug(Messages.get("ConnectTimePlugin.connectTime", hostInfo.host, (elapsedTimeNanos / 1000000n).toString())); + return result; + } + + public override async forceConnect( + hostInfo: HostInfo, + props: Map, + isInitialConnection: boolean, + forceConnectFunc: () => Promise + ): Promise { + const startTime = getTimeInNanos(); + + const result = await forceConnectFunc(); + + const elapsedTimeNanos = getTimeInNanos() - startTime; + ConnectTimePlugin.connectTime += elapsedTimeNanos; + logger.debug(Messages.get("ConnectTimePlugin.connectTime", hostInfo.host, (elapsedTimeNanos / 1000000n).toString())); + return result; + } + + public static resetConnectTime(): void { + ConnectTimePlugin.connectTime = 0n; + } + + public static getTotalConnectTime(): bigint { + return ConnectTimePlugin.connectTime; + } +} + +export class ConnectTimePluginFactory implements ConnectionPluginFactory { + getInstance(pluginService: PluginService, properties: Map): ConnectionPlugin { + return new ConnectTimePlugin(); + } +} diff --git a/common/lib/utils/locales/en.json b/common/lib/utils/locales/en.json index d5a4205e..a830a130 100644 --- a/common/lib/utils/locales/en.json +++ b/common/lib/utils/locales/en.json @@ -37,5 +37,6 @@ "ClusterAwareWriterFailoverHandler.taskBFailedToConnectToAnyReader": "[TaskB] Failed to connect to any reader.", "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." + "ClusterAwareWriterFailoverHandler.alreadyWriter": "Current reader connection is actually a new writer connection.", + "ConnectTimePlugin.connectTime": "Connected to '%s' in %s milliseconds." }