Skip to content

Commit

Permalink
feat: read-write splitting plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
joyc-bq committed May 24, 2024
1 parent e22b5dd commit 55aa2e4
Show file tree
Hide file tree
Showing 13 changed files with 1,284 additions and 17,159 deletions.
3 changes: 3 additions & 0 deletions common/lib/connection_plugin_chain_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ExecuteTimePluginFactory } from "./plugins/execute_time_plugin";
import { ConnectTimePluginFactory } from "./plugins/connect_time_plugin";
import { AwsSecretsManagerPluginFactory } from "./authentication/aws_secrets_manager_plugin";
import { ConnectionProvider } from "./connection_provider";
import { ReadWriteSplittingPluginFactory } from "./plugins/read_write_splitting";
import { StaleDnsPluginFactory } from "./plugins/stale_dns/stale_dns_plugin";

export class PluginFactoryInfo {}
Expand All @@ -42,6 +43,8 @@ export class ConnectionPluginChainBuilder {
["connectTime", ConnectTimePluginFactory],
["secretsManager", AwsSecretsManagerPluginFactory],
["failover", FailoverPluginFactory],
["readWriteSplitting", ReadWriteSplittingPluginFactory],
["failover", FailoverPluginFactory],
["staleDns", StaleDnsPluginFactory]
]);

Expand Down
2 changes: 1 addition & 1 deletion common/lib/driver_connection_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class DriverConnectionProvider implements ConnectionProvider {
getHostInfoByStrategy(hosts: HostInfo[], role: HostRole, strategy: string, props?: Map<string, any>): HostInfo {
const acceptedStrategy = DriverConnectionProvider.acceptedStrategies.get(strategy);
if (!acceptedStrategy) {
throw new AwsWrapperError(Messages.get("ConnectionProvider.unsupportedHostInfoSelectorStrategy", strategy, "DriverConnectionProvider"));
throw new AwsWrapperError(Messages.get("ConnectionProvider.unsupportedHostSelectorStrategy", strategy, "DriverConnectionProvider")); // TODO
}
return acceptedStrategy.getHost(hosts, role, props);
}
Expand Down
6 changes: 5 additions & 1 deletion common/lib/host_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class HostInfo {
private readonly _host: string;
private readonly _port: number;
private _availability: HostAvailability;
private readonly _role: HostRole;
private _role: HostRole;
protected aliases: Set<string> = new Set<string>();
private _allAliases: Set<string> = new Set<string>();
private readonly _weight: number; // Greater or equal 0. Lesser the weight, the healthier node.
Expand Down Expand Up @@ -129,6 +129,10 @@ export class HostInfo {
return this._role;
}

set role(value: HostRole) {
this._role = value;
}

get allAliases(): Set<string> {
return this._allAliases;
}
Expand Down
8 changes: 8 additions & 0 deletions common/lib/plugin_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { HostChangeOptions } from "./host_change_options";
import { OldConnectionSuggestionAction } from "./old_connection_suggestion_action";
import { HostRole } from "./host_role";
import { ConnectionProvider } from "./connection_provider";
import { ConnectionProviderManager } from "./connection_provider_manager";
import { chain } from "lodash";

type PluginFunc<T> = (plugin: ConnectionPlugin, targetFunc: () => Promise<T>) => Promise<T>;

Expand Down Expand Up @@ -73,6 +75,7 @@ export class PluginManager {
private readonly _plugins: ConnectionPlugin[] = [];
private pluginServiceManagerContainer: PluginServiceManagerContainer;
private props: Map<string, any>;
private readonly _defaultConnProvider: ConnectionProvider;

constructor(
pluginServiceManagerContainer: PluginServiceManagerContainer,
Expand All @@ -83,6 +86,7 @@ export class PluginManager {
this.pluginServiceManagerContainer = pluginServiceManagerContainer;
this.pluginServiceManagerContainer.pluginManager = this;
this.props = props;
this._defaultConnProvider = defaultConnProvider;
if (this.pluginServiceManagerContainer.pluginService != null) {
this._plugins = new ConnectionPluginChainBuilder().getPlugins(
this.pluginServiceManagerContainer.pluginService,
Expand Down Expand Up @@ -253,4 +257,8 @@ export class PluginManager {

throw new AwsWrapperError("The driver does not support the requested host selection strategy: " + strategy);
}

getDefaultConnProvider(): ConnectionProvider {
return this._defaultConnProvider;
}
}
13 changes: 13 additions & 0 deletions common/lib/plugin_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ export class PluginService implements ErrorHandler, HostListProviderService {
return false;
}

acceptsStrategy(role: HostRole, strategy: string): boolean {
return this.pluginServiceManagerContainer.pluginManager?.acceptsStrategy(role, strategy) ?? false;
}

async forceRefreshHostList(): Promise<void>;
async forceRefreshHostList(targetClient?: any): Promise<void>;
async forceRefreshHostList(targetClient?: any): Promise<void> {
Expand Down Expand Up @@ -248,6 +252,15 @@ export class PluginService implements ErrorHandler, HostListProviderService {
throw new AwsWrapperError("AwsClient is missing create target client function."); // This should not be reached
}

getHostInfoByStrategy(role: HostRole, strategy: string): HostInfo | undefined {
const pluginManager = this.pluginServiceManagerContainer.pluginManager;
return pluginManager?.getHostInfoByStrategy(role, strategy);
}

async getHostRole(client: AwsClient): Promise<HostRole> {
return (await this._hostListProvider?.getHostRole(client, this.dialect)) ?? HostRole.UNKNOWN;
}

connect<T>(hostInfo: HostInfo, props: Map<string, any>, connectFunc: () => Promise<T>) {
if (connectFunc) {
return this.pluginServiceManagerContainer.pluginManager?.connect(hostInfo, props, false, connectFunc);
Expand Down
Loading

0 comments on commit 55aa2e4

Please sign in to comment.