Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion common/lib/database_dialect/database_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/

import { HostListProvider } from "../host_list_provider/host_list_provider";
import { HostListProviderService } from "../host_list_provider_service";
import { ClientWrapper } from "../client_wrapper";
import { FailoverRestriction } from "../plugins/failover/failover_restriction";
import { ErrorHandler } from "../error_handler";
import { TransactionIsolationLevel } from "../utils/transaction_isolation_level";
import { HostRole } from "../host_role";
import { FullServicesContainer } from "../utils/full_services_container";
import { HostInfo } from "../host_info";

export enum DatabaseType {
MYSQL,
Expand All @@ -41,6 +41,14 @@ export interface DatabaseDialect {
getDialectUpdateCandidates(): string[];
getErrorHandler(): ErrorHandler;
getHostRole(targetClient: ClientWrapper): Promise<HostRole>;
/**
* Filters the given hosts down to those reachable from the configured accessible regions.
*
* Optional: dialects that are not region-aware (i.e. non-Global Aurora dialects) may omit this
* method, in which case callers should treat all hosts as available. Implementing this as an
* optional member avoids breaking out-of-tree custom dialects when the method is introduced.
*/
filterAvailableHosts?(hosts: HostInfo[], accessibleRegions: string[]): Promise<HostInfo[]>;
isDialect(targetClient: ClientWrapper): Promise<boolean>;
getHostListProvider(props: Map<string, any>, originalUrl: string, servicesContainer: FullServicesContainer): HostListProvider;
isClientValid(targetClient: ClientWrapper): Promise<boolean>;
Expand Down
4 changes: 4 additions & 0 deletions common/lib/database_dialect/database_dialect_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export class DatabaseDialectManager implements DatabaseDialectProvider {
return this.dialect;
}

isConfirmedDialect(): boolean {
return !this.canUpdate;
}

logCurrentDialect() {
logger.debug(`Current dialect: ${this.dialectCode}, ${this.dialect.getDialectName()}, canUpdate: ${this.canUpdate}`);
}
Expand Down
1 change: 1 addition & 0 deletions common/lib/database_dialect/database_dialect_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ import { ClientWrapper } from "../client_wrapper";
export interface DatabaseDialectProvider {
getDialect(props: Map<string, any>): DatabaseDialect;
getDialectForUpdate(targetClient: ClientWrapper, originalHost: string, newHost: string): Promise<DatabaseDialect>;
isConfirmedDialect(): boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
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 { HostInfo } from "../../host_info";
import { HostRole } from "../../host_role";
import { PluginService } from "../../plugin_service";
import { WrapperProperties } from "../../wrapper_property";
import { ClientWrapper } from "../../client_wrapper";
import { logger } from "../../../logutils";
import { Messages } from "../../utils/messages";
import {
AbstractMonitoringConnectionHandler,
MonitoringConnectionPriority,
parseMonitoringConnectionPriorities
} from "./monitoring_connection_handler";

export class AuroraMonitoringConnectionHandler extends AbstractMonitoringConnectionHandler<MonitoringConnectionPriority> {
private readonly writerPriorityIndex: number;
private readonly readerPriorityIndex: number;

constructor(
pluginService: PluginService,
monitoringProperties: Map<string, any>,
getMonitoringClient: () => ClientWrapper | null,
setMonitoringClient: (client: ClientWrapper | null) => void
) {
const priorities = parseMonitoringConnectionPriorities(WrapperProperties.MONITORING_CONNECTION_PRIORITY.get(monitoringProperties));
super(pluginService, monitoringProperties, priorities, getMonitoringClient, setMonitoringClient);
this.writerPriorityIndex = this.computeIndex(true);
this.readerPriorityIndex = this.computeIndex(false);
logger.debug(Messages.get("AuroraMonitoringConnectionHandler.initialized", this.priorities.join(",")));
}

private computeIndex(isWriter: boolean): number {
for (let i = 0; i < this.priorities.length; i++) {
if (this.isSatisfiedBy(this.priorities[i], isWriter)) {
return i;
}
}
return this.priorities.length;
}

private isSatisfiedBy(priority: MonitoringConnectionPriority, isWriter: boolean): boolean {
switch (priority) {
case MonitoringConnectionPriority.STRICT_WRITER:
return isWriter;
case MonitoringConnectionPriority.STRICT_READER:
return !isWriter;
case MonitoringConnectionPriority.WRITER_OR_READER:
return true;
default:
return false;
}
}

protected getPriorityIndex(_hostInfo: HostInfo, isWriter: boolean): number {
return isWriter ? this.writerPriorityIndex : this.readerPriorityIndex;
}

protected findHostsForPriority(priorityIndex: number, candidates: HostInfo[]): HostInfo[] {
const priority = this.priorities[priorityIndex];
if (!priority) {
return [];
}
switch (priority) {
case MonitoringConnectionPriority.STRICT_WRITER:
return candidates.filter((h) => h.role === HostRole.WRITER);
case MonitoringConnectionPriority.STRICT_READER:
return candidates.filter((h) => h.role === HostRole.READER);
case MonitoringConnectionPriority.WRITER_OR_READER: {
const writers = candidates.filter((h) => h.role === HostRole.WRITER);
return writers.length > 0 ? writers : candidates.filter((h) => h.role === HostRole.READER);
}
default:
return [];
}
}
}
Loading
Loading