Skip to content

Commit

Permalink
throw failover success
Browse files Browse the repository at this point in the history
  • Loading branch information
joyc-bq committed Jan 7, 2025
1 parent 999c404 commit 9a226f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
19 changes: 5 additions & 14 deletions common/lib/plugins/failover/failover_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,7 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
} else {
await this.failoverReader(failedHost);
}

if (this._isInTransaction || this.pluginService.isInTransaction()) {
// "Transaction resolution unknown. Please re-configure session state if required and try
// restarting transaction."
logger.debug(Messages.get("Failover.transactionResolutionUnknownError"));
throw new TransactionResolutionUnknownError(Messages.get("Failover.transactionResolutionUnknownError"));
} else {
// "The active SQL connection has changed due to a connection failure. Please re-configure
// session state if required."
throw new FailoverSuccessError(Messages.get("Failover.connectionChangedError"));
}
this.throwFailoverSuccessError();
}

async failoverReader(failedHostInfo: HostInfo) {
Expand Down Expand Up @@ -393,7 +383,7 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
await this.pluginService.setCurrentClient(result.client, result.newHost);
this.pluginService.getCurrentHostInfo()?.removeAlias(Array.from(oldAliases));
await this.updateTopology(true);
await this.throwFailoverSuccessError();
this.failoverWriterSuccessCounter.inc();
} catch (error: any) {
this.failoverReaderFailedCounter.inc();
throw error;
Expand All @@ -406,7 +396,7 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
}
}

async throwFailoverSuccessError() {
throwFailoverSuccessError() {
if (this._isInTransaction || this.pluginService.isInTransaction()) {
this.pluginService.setInTransaction(false);

Expand Down Expand Up @@ -455,7 +445,8 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
await this.pluginService.setCurrentClient(result.client, writerHostInfo);
logger.debug(Messages.get("Failover.establishedConnection", this.pluginService.getCurrentHostInfo()?.host ?? ""));
await this.pluginService.refreshHostList();
await this.throwFailoverSuccessError();
this.throwFailoverSuccessError();
this.failoverWriterSuccessCounter.inc();
} catch (error: any) {
this.failoverWriterFailedCounter.inc();
throw error;
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/failover_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ import { AwsMySQLClient } from "../../mysql/lib";
import { anything, instance, mock, reset, resetCalls, spy, verify, when } from "ts-mockito";
import { Messages } from "../../common/lib/utils/messages";
import { HostChangeOptions } from "../../common/lib/host_change_options";
import { ClientWrapper } from "../../common/lib/client_wrapper";
import { NullTelemetryFactory } from "../../common/lib/utils/telemetry/null_telemetry_factory";
import { MySQLClientWrapper } from "../../common/lib/mysql_client_wrapper";
import { DriverDialect } from "../../common/lib/driver_dialect/driver_dialect";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";

const builder = new HostInfoBuilder({ hostAvailabilityStrategy: new SimpleHostAvailabilityStrategy() });

Expand Down Expand Up @@ -196,7 +197,7 @@ describe("reader failover handler", () => {
const hostInfo = builder.withHost("hostA").build();
const hosts = [hostInfo];

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "aslias2"]));
when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockHostInfo.getRawAvailability()).thenReturn(HostAvailability.AVAILABLE);
when(mockPluginService.getHosts()).thenReturn(hosts);
when(await mockPluginService.getHostRole(anything())).thenReturn(HostRole.WRITER);
Expand Down Expand Up @@ -231,7 +232,7 @@ describe("reader failover handler", () => {
const hosts = [hostInfo];
const test = new AwsWrapperError("test");

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "aslias2"]));
when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockHostInfo.getRawAvailability()).thenReturn(HostAvailability.AVAILABLE);
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockReaderResult.exception).thenReturn(test);
Expand All @@ -255,7 +256,7 @@ describe("reader failover handler", () => {
const hosts = [hostInfo];
const test = new AwsWrapperError("test");

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "aslias2"]));
when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockWriterResult.exception).thenReturn(test);
when(mockWriterFailoverHandler.failover(anything())).thenResolve(instance(mockWriterResult));
Expand All @@ -276,7 +277,7 @@ describe("reader failover handler", () => {
const hostInfo = builder.withHost("hostA").build();
const hosts = [hostInfo];

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "aslias2"]));
when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockWriterResult.isConnected).thenReturn(false);
when(mockWriterFailoverHandler.failover(anything())).thenResolve(instance(mockWriterResult));
Expand Down Expand Up @@ -306,7 +307,7 @@ describe("reader failover handler", () => {
const hostInfo = builder.withHost("hostA").build();
const hosts = [hostInfo];

when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "aslias2"]));
when(mockHostInfo.allAliases).thenReturn(new Set<string>(["alias1", "alias2"]));
when(mockPluginService.getHosts()).thenReturn(hosts);
when(mockWriterResult.isConnected).thenReturn(false);
when(mockWriterResult.topology).thenReturn(hosts);
Expand Down

0 comments on commit 9a226f7

Please sign in to comment.