Skip to content

Commit

Permalink
fix failover update topology
Browse files Browse the repository at this point in the history
  • Loading branch information
joyc-bq committed Jan 7, 2025
1 parent 958cff4 commit 9b5f978
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
14 changes: 2 additions & 12 deletions common/lib/plugins/failover/reader_failover_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { HostInfo } from "../../host_info";
import { PluginService } from "../../plugin_service";
import { ReaderFailoverResult } from "./reader_failover_result";
import { getTimeoutTask, maskProperties, shuffleList, sleep } from "../../utils/utils";
import { getTimeoutTask, logAndThrowError, maskProperties, shuffleList, sleep } from "../../utils/utils";
import { HostRole } from "../../host_role";
import { HostAvailability } from "../../host_availability/host_availability";
import { AwsWrapperError, InternalQueryTimeoutError } from "../../utils/errors";
Expand Down Expand Up @@ -110,9 +110,8 @@ export class ClusterAwareReaderFailoverHandler implements ReaderFailoverHandler
return result; // connection to any host is acceptable
}

// ensure new connection is to a reader host
// Ensure new connection is to a reader host
await this.pluginService.refreshHostList();
const topology = this.pluginService.getHosts();
try {
if ((await this.pluginService.getHostRole(result.client)) !== HostRole.READER) {
return result;
Expand Down Expand Up @@ -316,15 +315,6 @@ class ConnectionAttemptTask {
try {
this.targetClient = await this.pluginService.forceConnect(this.newHost, copy);

// Ensure that new connection is a connection to a reader host
try {
if ((await this.pluginService.getHostRole(this.targetClient)) === HostRole.READER) {
return this.targetClient;
}
} catch (error: any) {
logger.debug(Messages.get("ClusterAwareReaderFailoverHandler.errorGettingHostRole", error.message));
}

this.pluginService.setAvailability(this.newHost.allAliases, HostAvailability.AVAILABLE);
logger.info(Messages.get("ClusterAwareReaderFailoverHandler.successfulReaderConnection", this.newHost.host));
if (this.taskHandler.getSelectedConnectionAttemptTask(this.failoverTaskId) === -1) {
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/reader_failover_handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe("reader failover handler", () => {
const successHostIndex = 3;

when(mockPluginService.getHosts()).thenReturn(hosts);
when(await mockPluginService.getHostRole(anything())).thenReturn(HostRole.READER);

for (let i = 0; i < hosts.length; i++) {
if (i !== successHostIndex) {
when(mockPluginService.forceConnect(hosts[i], anything())).thenThrow(new AwsWrapperError("Rejecting test"));
Expand Down Expand Up @@ -326,6 +328,8 @@ describe("reader failover handler", () => {
const reader = new HostInfo("reader", 1234, HostRole.READER);
const hosts = [writer, reader];
when(mockPluginService.getHosts()).thenReturn(hosts);
when(await mockPluginService.getHostRole(anything())).thenReturn(HostRole.READER);


const mockPluginServiceInstance = instance(mockPluginService);
const target = new ClusterAwareReaderFailoverHandler(
Expand Down

0 comments on commit 9b5f978

Please sign in to comment.