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: connection time plugin #17

Merged
merged 15 commits into from
Apr 9, 2024
4 changes: 3 additions & 1 deletion 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 { ConnectTimePluginFactory } from './plugins/connect_time_plugin';

export class PluginFactoryInfo {}

Expand All @@ -33,7 +34,8 @@ export class ConnectionPluginChainBuilder {

static readonly PLUGIN_FACTORIES = new Map<string, FactoryClass>([
["iam", IamAuthenticationPluginFactory],
["failover", FailoverPluginFactory]
["failover", FailoverPluginFactory],
["connectTime", ConnectTimePluginFactory]
]);

getPlugins(pluginService: PluginService, props: Map<string, any>): ConnectionPlugin[] {
Expand Down
69 changes: 69 additions & 0 deletions common/lib/plugins/connect_time_plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 subscribedMethods: Set<string> = new Set<string>(["connect", "forceConnect"]);
private static connectTime: bigint = 0n;

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

public override async connect<T>(hostInfo: HostInfo, props: Map<string, any>, isInitialConnection: boolean, connectFunc: () => Promise<T>): Promise<T> {
const startTime = getTimeInNanos();

const result = await connectFunc();

const elapsedTimeNanos = getTimeInNanos() - startTime;
ConnectTimePlugin.connectTime += elapsedTimeNanos;
logger.debug(Messages.get("ConnectTimePlugin.connectTime", elapsedTimeNanos.toString()));
return result;
}

public override async forceConnect<T>(hostInfo: HostInfo, props: Map<string, any>, isInitialConnection: boolean, forceConnectFunc: () => Promise<T>): Promise<T> {
const startTime = getTimeInNanos();

const result = await forceConnectFunc();

const elapsedTimeNanos = getTimeInNanos() - startTime;
ConnectTimePlugin.connectTime += elapsedTimeNanos;
logger.debug(Messages.get("ConnectTimePlugin.connectTime", elapsedTimeNanos.toString()));
return result;
}

public static resetConnectTime(): void {
this.connectTime = 0n;
}

public static getTotalConnectTime(): bigint {
return this.connectTime;
}
}

export class ConnectTimePluginFactory implements ConnectionPluginFactory {
getInstance(pluginService: PluginService, properties: Map<string, any>): ConnectionPlugin {
return new ConnectTimePlugin();
}
}
3 changes: 2 additions & 1 deletion common/lib/utils/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,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 in %s nanoseconds."
}
2 changes: 1 addition & 1 deletion common/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function logTopology(hosts: HostInfo[], msgPrefix: string) {
}

export function getTimeInNanos() {
return performance.now();
return process.hrtime.bigint();
}

export function maskProperties(props: Map<string, any>) {
Expand Down
Loading